mimetypes: init module mimetypes
This commit is contained in:
parent
810b78fc95
commit
03201fd28d
3 changed files with 1249 additions and 1 deletions
2
Makefile
2
Makefile
|
@ -1,7 +1,7 @@
|
||||||
DC = ldc2
|
DC = ldc2
|
||||||
CFLAGS = -Jstatic -Oz
|
CFLAGS = -Jstatic -Oz
|
||||||
|
|
||||||
OBJS = httpd.o
|
OBJS = httpd.o mimetypes.o
|
||||||
|
|
||||||
all: httpd
|
all: httpd
|
||||||
|
|
||||||
|
|
46
mimetypes.d
Normal file
46
mimetypes.d
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
module mimetypes;
|
||||||
|
|
||||||
|
import std.csv;
|
||||||
|
import std.traits;
|
||||||
|
|
||||||
|
string[string] mimeTypes;
|
||||||
|
|
||||||
|
static this()
|
||||||
|
{
|
||||||
|
mimeTypes = mimeMapping!("mimetypes.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
template mimeMapping(string csvFile, T = string[string])
|
||||||
|
if (isAssociativeArray!T)
|
||||||
|
{
|
||||||
|
enum mimeMapping = makeMimeMapping!(import(csvFile), T);
|
||||||
|
}
|
||||||
|
|
||||||
|
private T
|
||||||
|
makeMimeMapping(string csv, T)()
|
||||||
|
if (isAssociativeArray!T)
|
||||||
|
{
|
||||||
|
T result;
|
||||||
|
|
||||||
|
foreach (record; csvReader!T(csv, null)) {
|
||||||
|
result[record["Extension"]] = record["Type"];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
string
|
||||||
|
mimeType(string fileName) @safe
|
||||||
|
{
|
||||||
|
import std.path : extension;
|
||||||
|
import std.uni : toLower;
|
||||||
|
|
||||||
|
string ext;
|
||||||
|
|
||||||
|
ext = fileName.extension();
|
||||||
|
if (!ext)
|
||||||
|
return "application/octet-stream";
|
||||||
|
|
||||||
|
ext = ext[1 .. $].toLower();
|
||||||
|
return mimeTypes[ext];
|
||||||
|
}
|
1202
static/mimetypes.csv
Normal file
1202
static/mimetypes.csv
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue