-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathindex.js
90 lines (83 loc) · 2.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
let datafire = require('datafire');
let nodepath = require('path');
let fs = require('fs');
let CONTENT_TYPES = {
default: 'text/plain',
'.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',
'.woff2': 'application/font-woff2',
}
let ENCODINGS = {
default: 'utf8',
'.woff': 'binary',
'.woff2': 'binary',
}
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 extname = nodepath.extname(filename);
let ctype = input.contentType || CONTENT_TYPES[extname] || CONTENT_TYPES.default;
let encoding = ENCODINGS[extname] || ENCODINGS.default;
fs.readFile(filename, encoding, (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({
encoding,
headers: {'Content-Type': ctype},
body: contents,
}))
})
})
}
})