Skip to content

Commit a9cf12f

Browse files
authored
Make getContentType available for 3rd party usage (#7254)
* Refactored to make getContentType public for 3rd party use. * Added missing "jpeg" extension * Use getContentType() from mime namespace. * Also add .jpeg extension
1 parent 668b33d commit a9cf12f

File tree

4 files changed

+26
-59
lines changed

4 files changed

+26
-59
lines changed

Diff for: libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino

+1-44
Original file line numberDiff line numberDiff line change
@@ -115,49 +115,6 @@ void replyServerError(String msg) {
115115
server.send(500, FPSTR(TEXT_PLAIN), msg + "\r\n");
116116
}
117117

118-
String getContentType(String filename) {
119-
if (filename.endsWith(".htm")) {
120-
return "text/html";
121-
}
122-
if (filename.endsWith(".html")) {
123-
return "text/html";
124-
}
125-
if (filename.endsWith(".css")) {
126-
return "text/css";
127-
}
128-
if (filename.endsWith(".js")) {
129-
return "application/javascript";
130-
}
131-
if (filename.endsWith(".png")) {
132-
return "image/png";
133-
}
134-
if (filename.endsWith(".gif")) {
135-
return "image/gif";
136-
}
137-
if (filename.endsWith(".jpg")) {
138-
return "image/jpeg";
139-
}
140-
if (filename.endsWith(".jpeg")) {
141-
return "image/jpeg";
142-
}
143-
if (filename.endsWith(".ico")) {
144-
return "image/x-icon";
145-
}
146-
if (filename.endsWith(".xml")) {
147-
return "text/xml";
148-
}
149-
if (filename.endsWith(".pdf")) {
150-
return "application/x-pdf";
151-
}
152-
if (filename.endsWith(".zip")) {
153-
return "application/x-zip";
154-
}
155-
if (filename.endsWith(".gz")) {
156-
return "application/x-gzip";
157-
}
158-
return FPSTR(TEXT_PLAIN);
159-
}
160-
161118
#ifdef USE_SPIFFS
162119
/*
163120
Checks filename for character combinations that are not supported by FSBrowser (alhtough valid on SPIFFS).
@@ -304,7 +261,7 @@ bool handleFileRead(String path) {
304261
if (server.hasArg("download")) {
305262
contentType = F("application/octet-stream");
306263
} else {
307-
contentType = getContentType(path);
264+
contentType = mime::getContentType(path);
308265
}
309266

310267
if (!fileSystem->exists(path)) {

Diff for: libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h

+4-14
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
120120
}
121121
DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);
122122

123-
String contentType = getContentType(path);
123+
String contentType = mime::getContentType(path);
124124

125125
// look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for
126126
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
@@ -146,19 +146,9 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
146146
return true;
147147
}
148148

149-
static String getContentType(const String& path) {
150-
char buff[sizeof(mimeTable[0].mimeType)];
151-
// Check all entries but last one for match, return if found
152-
for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) {
153-
strcpy_P(buff, mimeTable[i].endsWith);
154-
if (path.endsWith(buff)) {
155-
strcpy_P(buff, mimeTable[i].mimeType);
156-
return String(buff);
157-
}
158-
}
159-
// Fall-through and just return default type
160-
strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType);
161-
return String(buff);
149+
/* Deprecated version. Please use mime::getContentType instead */
150+
static String getContentType(const String& path) __attribute__((deprecated)) {
151+
return mime::getContentType(path);
162152
}
163153

164154
protected:

Diff for: libraries/ESP8266WebServer/src/detail/mimetable.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "mimetable.h"
22
#include "pgmspace.h"
3+
#include "WString.h"
34

45
namespace mime
56
{
@@ -16,6 +17,7 @@ const Entry mimeTable[maxType] PROGMEM =
1617
{ ".png", "image/png" },
1718
{ ".gif", "image/gif" },
1819
{ ".jpg", "image/jpeg" },
20+
{ ".jpeg", "image/jpeg" },
1921
{ ".ico", "image/x-icon" },
2022
{ ".svg", "image/svg+xml" },
2123
{ ".ttf", "application/x-font-ttf" },
@@ -32,4 +34,19 @@ const Entry mimeTable[maxType] PROGMEM =
3234
{ "", "application/octet-stream" }
3335
};
3436

37+
String getContentType(const String& path) {
38+
char buff[sizeof(mimeTable[0].mimeType)];
39+
// Check all entries but last one for match, return if found
40+
for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) {
41+
strcpy_P(buff, mimeTable[i].endsWith);
42+
if (path.endsWith(buff)) {
43+
strcpy_P(buff, mimeTable[i].mimeType);
44+
return String(buff);
45+
}
46+
}
47+
// Fall-through and just return default type
48+
strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType);
49+
return String(buff);
50+
}
51+
3552
}

Diff for: libraries/ESP8266WebServer/src/detail/mimetable.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef __MIMETABLE_H__
22
#define __MIMETABLE_H__
33

4+
#include "WString.h"
45

56
namespace mime
67
{
@@ -16,6 +17,7 @@ enum type
1617
png,
1718
gif,
1819
jpg,
20+
jpeg,
1921
ico,
2022
svg,
2123
ttf,
@@ -41,7 +43,8 @@ struct Entry
4143

4244

4345
extern const Entry mimeTable[maxType];
44-
}
4546

47+
String getContentType(const String& path);
48+
}
4649

4750
#endif

0 commit comments

Comments
 (0)