-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
101 lines (84 loc) · 3.37 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
91
92
93
94
95
96
97
98
99
100
101
var fs = require('fs-extra');
var http = require('http');
var path = require('path');
var express = require('express');
var MATUProjectBackup = require('./matu-project-backup.js');
var archiver = require('archiver');
var CACHE_DIR = 'electron-cache';
var PLAYER_DIR = 'player';
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
response.render('pages/index');
});
app.get(/\/\d+/, function(request, response) {
var PATH = request.path.replace(/\//, '');
console.log('***********Packaging ' + PATH);
console.log('- versions: ' + JSON.stringify(request.query));
console.log('- downloading project to ZIP');
MATUProjectBackup(PATH, function(err, file) {
function sendError() {
response.status(500);
response.setHeader('Content-type', 'text/html');
response.end('<script>window.parent.photron_error()</script>');
}
if(err) {
console.error(err);
sendError();
return;
}
var archive = archiver('zip', {});
// good practice to catch this error explicitly
archive.on('error', function(err) {
console.error(err);
sendError();
});
response.setHeader('Content-disposition', 'attachment; filename=photron-' + PATH + '.zip');
response.setHeader('Content-type', 'application/zip');
archive.pipe(response);
response.on('finish', function() {
console.log(' - Response sent');
});
// append files
var basedir = 'photron-' + PATH;
if(request.query.win32 == '') {
console.log(' - win32');
archive.directory(CACHE_DIR + '/photron-win32-ia32', basedir + '-win32-ia32');
archive.append(file, { name: basedir + '-win32-ia32/resources/app/project.zip' });
}
if(request.query.win64 == '') {
console.log(' - win64');
archive.directory(CACHE_DIR + '/photron-win32-x64', basedir + '-win32-x64');
archive.append(file, { name: basedir + '-win32-x64/resources/app/project.zip' });
}
if(request.query.linux32 == '') {
console.log(' - linux32');
archive.directory(CACHE_DIR + '/photron-linux-ia32', basedir + '-linux-ia32');
archive.append(file, { name: basedir + '-linux-ia32/resources/app/project.zip' });
}
if(request.query.linux64 == '') {
console.log(' - linux64');
archive.directory(CACHE_DIR + '/photron-linux-x64', basedir + '-linux-x64');
archive.append(file, { name: basedir + '-linux-x64/resources/app/project.zip' });
}
if(request.query.linuxarm == '') {
console.log(' - linuxarm');
archive.directory(CACHE_DIR + '/photron-linux-armv7l', basedir + '-linux-armv7l');
archive.append(file, { name: basedir + '-linux-armv7l/resources/app/project.zip' });
}
if(request.query.mac64 == '') {
console.log(' - mac64');
archive.directory(CACHE_DIR + '/photron-darwin-x64', basedir + '-darwin-x64');
archive.append(file, { name: basedir + '-darwin-x64/photron.app/Contents/Resources/app/project.zip' });
}
// finalize the archive (ie we are done appending files but streams have to finish yet)
archive.finalize();
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});