Skip to content

Commit 78d69eb

Browse files
committed
update file
1 parent 1d6aeda commit 78d69eb

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

src/api.js

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
'use strict';
2-
3-
const fs = require('fs');
4-
var http = require('http');
5-
const path = require('path');
2+
const handler = require('./handler.js');
63

74
module.exports = function (router) {
85

@@ -15,41 +12,7 @@ module.exports = function (router) {
1512
});
1613

1714
router.post('/handler', function (req, res) {
18-
console.log(req.file);
19-
const cmd = req.query.cmd;
20-
const url = req.query.url;
21-
const file = req.file;
22-
if(!cmd) return res.error('cmd');
23-
if(!file && !url) return res.error('file');
24-
const command = cmd.split('/');
25-
if(command[1] && command[1] === 'info') {
26-
const stat = fs.statSync(file);
27-
const total = stat.size;
28-
return res.success({cmd, file, url, total});
29-
}
30-
if(url) {
31-
const filename = Math.random().toString(36).substr(2);
32-
const filepath = path.resolve('/tmp/', filename);
33-
const video = fs.createWriteStream(filepath);
34-
video.on('finish', () => {
35-
video.close(() => {
36-
const stat = fs.statSync(filepath);
37-
const total = stat.size;
38-
res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' });
39-
fs.createReadStream(filepath).pipe(res);
40-
});
41-
});
42-
const request = http.get(url, (response) => {
43-
response.pipe(video);
44-
console.log(url);
45-
});
46-
} else {
47-
const stat = fs.statSync(file);
48-
const total = stat.size;
49-
res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' });
50-
fs.createReadStream(file).pipe(res);
51-
}
52-
15+
handler(req, res);
5316
});
5417

5518
};

src/handler.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const fs = require('fs');
2+
var http = require('http');
3+
const path = require('path');
4+
5+
module.exports = function(req, res) {
6+
const cmd = req.query.cmd;
7+
const url = req.query.url;
8+
const file = req.file;
9+
if(!cmd) return res.error('cmd');
10+
if(!file && !url) return res.error('file');
11+
const command = cmd.split('/');
12+
13+
if(command[1] && command[1] === 'info') {
14+
const stat = fs.statSync(file);
15+
const total = stat.size;
16+
return res.success({cmd, file, url, total});
17+
}
18+
19+
if(url) {
20+
const filename = Math.random().toString(36).substr(2);
21+
const filepath = path.resolve('/tmp/', filename);
22+
const video = fs.createWriteStream(filepath);
23+
video.on('finish', () => {
24+
video.close(() => {
25+
const stat = fs.statSync(filepath);
26+
const total = stat.size;
27+
res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' });
28+
fs.createReadStream(filepath).pipe(res);
29+
});
30+
});
31+
const request = http.get(url, (response) => {
32+
response.pipe(video);
33+
console.log(url);
34+
});
35+
} else {
36+
const stat = fs.statSync(file);
37+
const total = stat.size;
38+
res.writeHead(200, { 'Content-Length': total, 'Content-Type': 'video/mp4' });
39+
fs.createReadStream(file).pipe(res);
40+
}
41+
};

0 commit comments

Comments
 (0)