mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +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:
@ -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:
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user