1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-25 20:02:37 +03:00

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
This commit is contained in:
vdeconinck 2020-04-30 05:36:42 +02:00 committed by GitHub
parent 668b33ddf1
commit a9cf12fcf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 59 deletions

View File

@ -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).
@ -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)) {

View File

@ -120,7 +120,7 @@ public:
}
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...
@ -146,19 +146,9 @@ public:
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:

View File

@ -1,5 +1,6 @@
#include "mimetable.h"
#include "pgmspace.h"
#include "WString.h"
namespace mime
{
@ -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" },
@ -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);
}
}

View File

@ -1,6 +1,7 @@
#ifndef __MIMETABLE_H__
#define __MIMETABLE_H__
#include "WString.h"
namespace mime
{
@ -16,6 +17,7 @@ enum type
png,
gif,
jpg,
jpeg,
ico,
svg,
ttf,
@ -41,7 +43,8 @@ struct Entry
extern const Entry mimeTable[maxType];
String getContentType(const String& path);
}
#endif