Skip to content

Commit

Permalink
Make getContentType available for 3rd party usage (#7254)
Browse files Browse the repository at this point in the history
* Refactored to make getContentType public for 3rd party use.

* Added missing "jpeg" extension

* Use getContentType() from mime namespace.

* Also add .jpeg extension
  • Loading branch information
vdeconinck committed Apr 30, 2020
1 parent 668b33d commit a9cf12f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 59 deletions.
45 changes: 1 addition & 44 deletions libraries/ESP8266WebServer/examples/FSBrowser/FSBrowser.ino
Original file line number Diff line number Diff line change
Expand Up @@ -115,49 +115,6 @@ void replyServerError(String msg) {
server.send(500, FPSTR(TEXT_PLAIN), msg + "\r\n");
}

String getContentType(String filename) {
if (filename.endsWith(".htm")) {
return "text/html";
}
if (filename.endsWith(".html")) {
return "text/html";
}
if (filename.endsWith(".css")) {
return "text/css";
}
if (filename.endsWith(".js")) {
return "application/javascript";
}
if (filename.endsWith(".png")) {
return "image/png";
}
if (filename.endsWith(".gif")) {
return "image/gif";
}
if (filename.endsWith(".jpg")) {
return "image/jpeg";
}
if (filename.endsWith(".jpeg")) {
return "image/jpeg";
}
if (filename.endsWith(".ico")) {
return "image/x-icon";
}
if (filename.endsWith(".xml")) {
return "text/xml";
}
if (filename.endsWith(".pdf")) {
return "application/x-pdf";
}
if (filename.endsWith(".zip")) {
return "application/x-zip";
}
if (filename.endsWith(".gz")) {
return "application/x-gzip";
}
return FPSTR(TEXT_PLAIN);
}

#ifdef USE_SPIFFS
/*
Checks filename for character combinations that are not supported by FSBrowser (alhtough valid on SPIFFS).
Expand Down Expand Up @@ -304,7 +261,7 @@ bool handleFileRead(String path) {
if (server.hasArg("download")) {
contentType = F("application/octet-stream");
} else {
contentType = getContentType(path);
contentType = mime::getContentType(path);
}

if (!fileSystem->exists(path)) {
Expand Down
18 changes: 4 additions & 14 deletions libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
}
DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);

String contentType = getContentType(path);
String contentType = mime::getContentType(path);

// 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
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
Expand All @@ -146,19 +146,9 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
return true;
}

static String getContentType(const String& path) {
char buff[sizeof(mimeTable[0].mimeType)];
// Check all entries but last one for match, return if found
for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) {
strcpy_P(buff, mimeTable[i].endsWith);
if (path.endsWith(buff)) {
strcpy_P(buff, mimeTable[i].mimeType);
return String(buff);
}
}
// Fall-through and just return default type
strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType);
return String(buff);
/* Deprecated version. Please use mime::getContentType instead */
static String getContentType(const String& path) __attribute__((deprecated)) {
return mime::getContentType(path);
}

protected:
Expand Down
17 changes: 17 additions & 0 deletions libraries/ESP8266WebServer/src/detail/mimetable.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "mimetable.h"
#include "pgmspace.h"
#include "WString.h"

namespace mime
{
Expand All @@ -16,6 +17,7 @@ const Entry mimeTable[maxType] PROGMEM =
{ ".png", "image/png" },
{ ".gif", "image/gif" },
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".ico", "image/x-icon" },
{ ".svg", "image/svg+xml" },
{ ".ttf", "application/x-font-ttf" },
Expand All @@ -32,4 +34,19 @@ const Entry mimeTable[maxType] PROGMEM =
{ "", "application/octet-stream" }
};

String getContentType(const String& path) {
char buff[sizeof(mimeTable[0].mimeType)];
// Check all entries but last one for match, return if found
for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) {
strcpy_P(buff, mimeTable[i].endsWith);
if (path.endsWith(buff)) {
strcpy_P(buff, mimeTable[i].mimeType);
return String(buff);
}
}
// Fall-through and just return default type
strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType);
return String(buff);
}

}
5 changes: 4 additions & 1 deletion libraries/ESP8266WebServer/src/detail/mimetable.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __MIMETABLE_H__
#define __MIMETABLE_H__

#include "WString.h"

namespace mime
{
Expand All @@ -16,6 +17,7 @@ enum type
png,
gif,
jpg,
jpeg,
ico,
svg,
ttf,
Expand All @@ -41,7 +43,8 @@ struct Entry


extern const Entry mimeTable[maxType];
}

String getContentType(const String& path);
}

#endif

0 comments on commit a9cf12f

Please sign in to comment.