1
1
const express = require ( 'express' ) ;
2
2
const multer = require ( 'multer' ) ;
3
3
const bencode = require ( 'bencode' ) ;
4
- const cyrpto = require ( 'crypto' ) ;
4
+ const crypto = require ( 'crypto' ) ;
5
5
const db = require ( './../db.js' ) ;
6
- const _ = require ( 'lodash ' ) ;
6
+ const fs = require ( 'fs ' ) ;
7
7
8
8
const app = express . Router ( ) ;
9
9
10
- const storage = multer . diskStorage ( {
10
+ const util = require ( 'util' ) ;
11
+
12
+ /*const storage = multer.diskStorage({
11
13
destination: (req, file, cb) => {
12
- cb ( null , `/home/justin/torrents/${ ( new Date ) . getMonth ( ) } ` ) ;
14
+ const now = new Date();
15
+ const month = now.getMonth() + 1;
16
+ const year = now.getFullYear();
17
+ console.log(util.inspect(file));
18
+
19
+ cb(null, `${process.env.TORRENT_DIR}${year}/${month}/`);
13
20
},
14
21
filename: (req, file, cb) => {
15
- cb ( null , file . hash ) ;
22
+ cb(null, `${ file.hash}.torrent` );
16
23
}
17
- } ) ;
18
-
19
- const fileFilter = ( req , file , cb ) => {
20
- file . hash = hashTorrent ( stripTrackers ( file . buffer ) ) ;
21
- cb ( null , true ) ;
24
+ });*/
25
+
26
+ const saveToDisk = torrent => {
27
+ const now = new Date ( ) ;
28
+ const month = now . getMonth ( ) + 1 ;
29
+ const year = now . getFullYear ( ) ;
30
+ const path = `${ process . env . TORRENT_DIR } ${ year } /${ month } /${ torrent . hash } .torrent` ;
31
+
32
+ return new Promise ( ( resolve , reject ) => {
33
+ fs . writeFile ( path , new Buffer ( torrent . buffer ) , err => {
34
+ if ( err ) {
35
+ return reject ( err ) ;
36
+ }
37
+ torrent . path = path ;
38
+ return resolve ( torrent ) ;
39
+ } ) ;
40
+ } ) ;
22
41
} ;
23
42
24
- const upload = multer ( { storage, fileFilter } ) ;
43
+ const storage = multer . memoryStorage ( ) ;
44
+
45
+ const upload = multer ( { storage } ) ;
25
46
26
47
const stripTrackers = torrentBuffer => {
27
48
const torrent = bencode . decode ( torrentBuffer ) ;
28
- delete torrent . announce ;
49
+ delete torrent [ ' announce' ] ;
29
50
return bencode . encode ( torrent ) ;
30
51
} ;
31
52
32
53
const hashTorrent = torrentBuffer => {
33
54
const hash = crypto . createHash ( 'sha256' ) ;
34
- const code = process . env . COOKIE_SECRET + torrentBuffer . toString ( ) + Math . floor ( ( new Date ) . getTime ( ) / 1000 ) . toString ( ) ;
55
+ const code = process . env . COOKIE_SECRET + torrentBuffer . toString ( 'utf8' ) + Math . floor ( ( new Date ) . getTime ( ) / 1000 ) . toString ( ) ;
35
56
hash . update ( code ) ;
36
57
return hash . digest ( 'hex' ) ;
37
58
} ;
@@ -51,7 +72,7 @@ const verifyAlbum = group => {
51
72
}
52
73
} ) ;
53
74
54
- requiredField . forEach ( field => {
75
+ requiredFields . forEach ( field => {
55
76
if ( ! verifiedGroup . has ( field ) ) {
56
77
return Promise . reject ( new Error ( 'Missing required fields.' ) ) ;
57
78
}
@@ -72,7 +93,7 @@ const createGroup = group => {
72
93
return verifyAlbum ( group ) . then ( verifiedGroup => {
73
94
const keys = verifiedGroup . keys ( ) ;
74
95
const values = verifiedGroup . values ( ) ;
75
- const length = verifiedGroup . size ( ) ;
96
+ const length = verifiedGroup . size ;
76
97
77
98
return db . query ( `insert into tracker.groups (${ keys . toString ( ) } ) values (${ upTo ( length ) } ) return id` , values ) ;
78
99
} ) ;
@@ -82,20 +103,32 @@ const store = (torrent, group) => {
82
103
return db . query ( 'insert into tracker.torrents (hash, path, group) values ($1, $2, $3)' , [ torrent . hash , torrent . path , group ] ) ;
83
104
} ;
84
105
85
- app . post ( '/upload' , upload . fields ( [ { name : 'torrent' , maxCount : 1 } ] ) , ( req , res ) => {
86
- let group = req . body . group ;
87
- if ( typeof ( group ) !== 'Number' ) {
88
- createGroup ( group ) . then ( result => {
89
- group = result . rows [ 0 ] . id ;
90
- } ) . catch ( err => {
91
- res . send ( 400 ) ;
92
- } ) ;
93
- }
94
- store ( req . file , group ) . then ( result => {
95
- const torrentId = result . rows [ 0 ] . id ;
96
- if ( torrentId ) {
97
- res . send ( 200 , { id : torrentId } ) ;
106
+ const torrentUpload = upload . fields ( [ { name : 'torrent' , maxCount : 1 } ] ) ;
107
+
108
+ app . post ( '/upload' , torrentUpload , ( req , res ) => {
109
+ //app.post('/upload', upload.single('torrent'), (req, res) => {
110
+ const torrent = stripTrackers ( req . files . torrent [ 0 ] . buffer ) ;
111
+ const group = JSON . parse ( req . body . group ) ;
112
+
113
+ torrent . hash = hashTorrent ( torrent . buffer ) ;
114
+
115
+ saveToDisk ( torrent ) . then ( torrent => {
116
+ if ( typeof ( group ) !== 'Number' ) {
117
+ return createGroup ( group ) . then ( result => {
118
+ group = result . rows [ 0 ] . id ;
119
+ return group
120
+ } ) . then ( group => {
121
+ return store ( torrent , group ) . then ( result => {
122
+ const torrentId = result . rows [ 0 ] . id ;
123
+ if ( torrentId ) {
124
+ res . send ( 200 , { id : torrentId } ) ;
125
+ }
126
+ } ) ;
127
+ } )
98
128
}
129
+ } ) . catch ( err => {
130
+ console . log ( err ) ;
131
+ res . sendStatus ( 500 ) ;
99
132
} ) ;
100
133
} ) ;
101
134
0 commit comments