forked from C9E4F7/DTube-snap-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snap-processor.js
executable file
·147 lines (101 loc) · 4.29 KB
/
snap-processor.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const http = require('http');
const uuidv4 = require('uuid/v4');
const shell = require('shelljs');
const formidable = require('formidable');
const readChunk = require('read-chunk');
const fileType = require('file-type');
var cmds = require('./snap-processor-cmds.js');
corsvar = process.env.CORSVAR || "https://d.tube"
// variable to assure only one upload request happens
var reqhappened = false;
// generated token
const genToken = uuidv4();
http.createServer(function (req, res) {
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Headers', 'Origin, Authorization, Accept');
res.setHeader('Access-Control-Allow-Headers', 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range');
res.setHeader('Access-Control-Allow-Methods','GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Origin', corsvar);
res.setHeader('Access-Control-Max-Age', '1728000');
res.setHeader('Connection', 'keep-alive');
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
if (req.url == '/getStatus') {
res.statusCode = 200;
res.end('{"version":"0.7.5","currentWaitingInQueue":{"audioCpuToEncode":0,"videoGpuToEncode":0,"audioVideoCpuToEncode":0,"spriteToCreate":0,"ipfsToAdd":0}}');
};
// sending progress to user
if (req.url.match(/\/getProgressByToken.*/)) {
res.end(JSON.stringify(cmds.processResponse));
};
if (req.url == '/uploadImage' && !reqhappened) {
if (req.method === 'OPTIONS'){
res.statusCode = 204;
res.end();
} else {
res.statusCode = 200;
reqhappened = true;
var form = new formidable.IncomingForm();
//Sane Form options
form.maxFields = 1
form.encoding = 'utf-8';
form.maxFileSize = '10240000';
form.parse(req, function (err, fields, files) {
});
// file is moved to upload folder and renamed to uuid
form.on('fileBegin', function (name, file){
file.path = "./upload/" + genToken;
});
form.on('file', function (name, file) {
//frontend needs to know if upload was successful and receive the token
var successResponse = { success: "", token: ""};
var allowedExtensions = ['jpg', 'jpeg', 'bmp', 'png'];
const buffer = readChunk.sync(file.path, 0, fileType.minimumBytes);
var uploadedFileType = fileType(buffer).ext;
console.log(uploadedFileType);
if (!allowedExtensions.includes(uploadedFileType)){
// if not image, success is false, no token, end process
successResponse.success = "false";
res.end(JSON.stringify(successResponse));
process.exit();
} else {
// if file is valid, success is true and provide token
successResponse.success = "true";
successResponse.token = genToken;
res.end(JSON.stringify(successResponse));
// resize the image with shell command
shell.exec(cmds.shell_cmds.createResizeCmd(file.path), function(code, stdout, stderr) {
// if code aint 0 there's an error
if (code) {
console.log(stderr);
process.exit();
}
// upload resized (called source for some reason) image to ipfs
cmds.ipfs_cmds.ipfsUpload("./snap/resizedImg", "ipfsAddSource");
// overlay dtube logo on image with shell command
shell.exec(cmds.shell_cmds.createOverlayCmd("./snap/resizedImg"), function(code, stdout, stderr){
// if code aint 0 there's an error
if (code) {
console.log(stderr);
process.exit();
}
// upload overlayed image to ipfs
cmds.ipfs_cmds.ipfsUpload("./snap/overlayedImg", "ipfsAddOverlay");
// check if finished and exit if done.
cmds.checkIfFinished();
});
});
}
});
form.on('error', function(err) {
console.error('Error', err)
throw err;
process.exit();
});
}
} else {
res.end("There's nothing here for you");
}
}).listen(5000, ()=> {
console.log("listening on port 5000");
});