1 module mime; 2 3 import std.base64; 4 import std.range; 5 import std.string; 6 7 8 string extensionToMimeType(string ext) { 9 switch(ext.toLower()) { 10 case ".jpg": 11 case ".jpeg": 12 return "image/jpeg"; 13 case ".png": 14 return "image/png"; 15 case ".gif": 16 return "image/gif"; 17 case ".tga": 18 return "image/targa"; 19 case ".tif": 20 case ".tiff": 21 return "image/tiff"; 22 default: 23 break; 24 } 25 return null; 26 } 27 28 29 string mimeTypeToExtension(string mime) { 30 switch(mime.toLower()) { 31 case "image/jpg": 32 case "image/jpeg": 33 case "image/pjpeg": 34 return ".jpg"; 35 case "image/x-png": 36 case "image/png": 37 return ".png"; 38 case "image/gif": 39 return ".gif"; 40 case "image/x-targa": 41 case "image/targa": 42 case "image/tga": 43 return ".tga"; 44 case "image/tif": 45 case "image/tiff": 46 case "image/x-tiff": 47 case "image/x-tif": 48 return ".tif"; 49 default: 50 break; 51 } 52 return null; 53 } 54 55 56 string mimeEncode(string input) { 57 Appender!string mime = appender!string; 58 foreach (ref encoded; Base64.encoder(chunks(cast(ubyte[])input, 57))) { 59 mime.put(encoded); 60 mime.put("\r\n"); 61 } 62 return mime.data(); 63 }