'use strict';

let datafire = require('datafire');
let nodepath = require('path');
let fs = require('fs');

let CONTENT_TYPES = {
  '.html': 'text/html',
  '.js': 'application/javascript',
  '.json': 'application/json',
  '.xml': 'application/xml',
  '.zip': 'application/zip',
  '.pdf': 'application/pdf',
  '.sql': 'application/sql',
  '.mpeg': 'audio/mpeg',
  '.css': 'text/css',
  '.csv': 'text/csv',
  '.png': 'image/png',
  '.jpeg': 'image/jpeg',
  '.gif': 'image/gif',
  '.eot': 'application/vnd.ms-fontobject',
  '.otf': 'application/font-sfnt',
  '.svg': 'image/svg+xml',
  '.ttf': 'application/font-sfnt',
  '.woff': 'application/font-woff',
}

let fileserver = module.exports = new datafire.Integration({
  id: 'fileserver',
  title: "File Server",
  description: "Serve static files, such as HTML, CSS, JavaScript, and images",
});

fileserver.addAction('serve', {
  inputs: [{
    title: 'filename',
    type: 'string',
  }, {
    title: 'contentType',
    type: 'string',
    default: '',
  }],

  security: {
    fileserver: {
      fields: {
        baseDirectory: 'The directory to use as the file server\'s root',
      }
    }
  },

  handler: (input, context) => {
    let baseDir = context.accounts.fileserver.baseDirectory;
    if (!baseDir) throw new Error("Must specify context.accounts.fileserver.baseDirectory");
    return new Promise((resolve, reject) => {
      let filename = nodepath.join('/', input.filename);
      filename = nodepath.join(baseDir, filename);
      let ctype = input.contentType;
      if (!ctype) {
        let extname = nodepath.extname(filename);
        ctype = CONTENT_TYPES[extname] || 'text/plain';
      }
      fs.readFile(filename, 'utf8', (err, contents) => {
        if (err) {
          let resp = {statusCode: 500, body: "Error retrieving file " + input.filename};
          if (err.code === 'ENOENT') {
            resp = {statusCode: 404, body: "File not found: " + input.filename};
          } else {
            return reject(new datafire.Response({
              statusCode: 500,
              body: "Error retrieving file " + input.filename,
            }));
          }
          return reject(new datafire.Response(resp));
        }
        resolve(new datafire.Response({
          headers: {'Content-Type': ctype},
          body: contents,
        }))
      })
    })
  }
})

@@ -123,16 +123,41 @@
 PES = %7B%0A
+  default: 'text/plain',%0A
   '.html
@@ -168,24 +168,24 @@
 text/html',%0A
-
   '.js': 'ap
@@ -670,16 +670,137 @@
 -woff',%0A
+  '.woff2': 'application/font-woff2',%0A%7D%0A%0Alet ENCODINGS = %7B%0A  default: 'utf8',%0A  '.woff': 'binary',%0A  '.woff2': 'binary',%0A
 %7D%0A%0Alet f
@@ -1636,67 +1636,8 @@
 e);%0A
-      let ctype = input.contentType;%0A      if (!ctype) %7B%0A  
     
@@ -1690,17 +1690,19 @@
     
- 
+let
  ctype =
  CON
@@ -1697,16 +1697,37 @@
  ctype =
+ input.contentType %7C%7C
  CONTENT
@@ -1749,29 +1749,92 @@
  %7C%7C 
-'text/plain';%0A      %7D
+CONTENT_TYPES.default;%0A      let encoding = ENCODINGS%5Bextname%5D %7C%7C ENCODINGS.default;
 %0A   
@@ -1858,22 +1858,24 @@
 lename, 
-'utf8'
+encoding
 , (err, 
@@ -2365,32 +2365,32 @@
 sp));%0A        %7D%0A
-
         resolve(
@@ -2405,32 +2405,52 @@
 fire.Response(%7B%0A
+          encoding,%0A
           header

add encoding info