When serving output files, intelligently determine the appropriate content-type.

cherry pick 6fa3fda3ed28239cf3ac9720629f9707663aa197 from datajoy.
This commit is contained in:
Shane Kilkelly
2015-09-21 14:04:08 +01:00
parent d83efdbc98
commit 29be2dc700
3 changed files with 79 additions and 5 deletions

View File

@@ -0,0 +1,26 @@
Path = require 'path'
# here we coerce html, css and js to text/plain,
# otherwise choose correct mime type based on file extension,
# falling back to octet-stream
module.exports = ContentTypeMapper =
map: (path) ->
switch Path.extname(path)
when '.txt', '.html', '.js', '.css'
return 'text/plain'
when '.csv'
return 'text/csv'
when '.pdf'
return 'application/pdf'
when '.png'
return 'image/png'
when '.jpg', '.jpeg'
return 'image/jpeg'
when '.tiff'
return 'image/tiff'
when '.gif'
return 'image/gif'
when '.svg'
return 'image/svg+xml'
else
return 'application/octet-stream'