This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.js
62 lines (54 loc) · 1.65 KB
/
post.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
var tools = require('../../server/tools');
var constant = require('../../constants');
var multipart = require('connect-multiparty');
module.exports = [multipart(), function (req, res) {
if (!req.files ||!req.files.file || !req.files.file.name || !req.files.file.path) {
return res.json({error: 'Upload error'});
}
var file = req.files.file;
var sources = [];
handleFile(file.path, file.name, sources)
.then(function () {
return tools.fs.unlink(file.path);
})
.then(function () {
return tools.source.reload();
})
.then(function () {
return tools.source.list();
})
.then(function (sources) {
res.json({
sources: sources.map(function (source) {
return source.toJSON();
})
});
})
.catch(function (err) {
res.send({error: err ? err.message : 'Unknown error'});
});
}];
/**
* Try to identify an archive as a source package
* @param {string} source
* @param {string} filename - Original filename (if different from source)
* @param {array} sources output
* @return {Promise}
*/
function handleFile (source, filename, sources) {
filename = filename || path.basename(source);
if (!tools.compression.hasArchiveExtension(filename)) {
return Promise.resolve(false);
}
return tools.compression.uncompressToTmp(source, ['js', 'json', 'jpg', 'png', 'gif'])
.then(function (result) {
try {
var source = require(result.tmpPath);
if (source.guid) {
return tools.fs.rename(result.tmpPath, constant.SOURCES_PATH + '/' + source.guid);
}
} catch (err) {
return tools.fs.rmTmpDir(result.tmpPath);
}
});
}