This repository was archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
124 lines (110 loc) · 3.97 KB
/
api.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
var crypto = require('crypto');
var fs = require('fs');
var path = require('path');
function hashFile(filePath, cb) {
var fd = fs.createReadStream(filePath);
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');
fd.on('end', function() {
hash.end();
cb(hash.read());
});
fd.pipe(hash);
};
module.exports = function(app, astah, s3, projectDir, exportDir) {
function findFiles(hash, absUrl) {
return fs.readdirSync(path.join(exportDir, hash))
.filter(function(filename) {
return path.extname(filename) !== '.bak';
})
.map(function(filename) {
return {
export_url: absUrl('/projects/' + hash + '?file=' + filename),
filename: filename
};
});
}
function makeAbsoluteUrl(req) {
return function(relativePath) {
return req.protocol + '://' + req.get('host') + relativePath;
};
}
function tryUploadS3(objName, filePath) {
if(s3) {
s3.putFile(filePath, objName, function(err, res) {
if(err) {
console.error(err);
} else {
res.resume();
}
});
}
}
app.post('/projects', function(req, res) {
var absUrl = makeAbsoluteUrl(req);
var projectFile = req.files.project;
hashFile(projectFile.path, function(hash) {
var projectUrl = absUrl('/projects/' + hash);
var projectPath = path.join(projectDir,
hash + '.' + projectFile.extension);
fs.renameSync(projectFile.path, projectPath);
var projName = '/projects/' + hash + '.' + projectFile.extension;
tryUploadS3(projName, projectPath);
astah.exportImage(projectPath, exportDir, 'png').then(function() {
var exports = findFiles(hash, absUrl);
exports.forEach(function(exportedFile) {
var exportName = '/exports/' + hash +
'/' + exportedFile.filename;
var exportPath = path.join(exportDir, hash, exportedFile.filename);
tryUploadS3(exportName, exportPath);
});
res.status(201);
res.location(projectUrl);
return res.format({
text: function() {
res.send(exports.map(function(exportedFile) {
return exportedFile.export_url;
}).join("\n"));
},
json: function() {
res.json({
project_url: projectUrl,
exports: exports
});
}
});
}, function(err) {
res.status(500);
res.send(err);
});
});
});
app.get('/projects/:sha', function(req, res) {
var absUrl = makeAbsoluteUrl(req);
if(req.query.file) {
var exportPath = path.join(exportDir, req.params.sha, req.query.file);
if(fs.existsSync(exportPath)) {
return res.sendFile(exportPath);
} else {
res.status(404),
res.send('Could not find file ' + req.query.file);
}
} else {
var projectUrl = absUrl('/projects/' + req.params.sha);
var exports = findFiles(req.params.sha, absUrl);
res.status(200);
return res.format({
text: function() {
res.send(exports.join("\n"));
},
json: function() {
res.json({
project_url: projectUrl,
exports: exports
});
}
});
}
});
};