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
74module . 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} ;
0 commit comments