@@ -4,7 +4,6 @@ const bencode = require('bencode');
4
4
const crypto = require ( 'crypto' ) ;
5
5
const db = require ( './../db.js' ) ;
6
6
const fs = require ( 'fs' ) ;
7
- const _ = require ( 'lodash' ) ;
8
7
9
8
const app = express . Router ( ) ;
10
9
@@ -25,6 +24,15 @@ const verificationTypes = {
25
24
}
26
25
} ;
27
26
27
+ /**
28
+ * Saves torrent file to disk
29
+ *
30
+ * Saves torrent to the path defined in the .env file.
31
+ * Also adds a path property to the torrent Object
32
+ *
33
+ * @param torrent: torrent Object
34
+ * @return Promise<torrent | error>
35
+ */
28
36
const saveToDisk = torrent => {
29
37
const now = new Date ( ) ;
30
38
const month = now . getMonth ( ) + 1 ;
@@ -46,22 +54,29 @@ const storage = multer.memoryStorage();
46
54
47
55
const upload = multer ( { storage } ) ;
48
56
57
+ /**
58
+ * Processes a torrent
59
+ *
60
+ * Removes trackers from torrent
61
+ * Adds fileList and hash fields to the torrent
62
+ *
63
+ * @param torrent: torrent Object
64
+ * @return Promise<torrent | error>
65
+ */
49
66
const processTorrent = torrent => {
50
67
const decodedTorrent = bencode . decode ( torrent . buffer ) ;
51
- const processedTorrent = _ . cloneDeep ( torrent ) ;
52
-
53
68
delete decodedTorrent [ 'announce' ] ;
54
69
55
- processedTorrent . fileList = [ ] ;
56
- processedTorrent . totalFileSize = decodedTorrent . info . files . reduce ( ( total , file ) => {
57
- processedTorrent . fileList . push ( { fileName : file . path , fileSize : file . length } ) ;
70
+ torrent . fileList = [ ] ;
71
+ torrent . totalFileSize = decodedTorrent . info . files . reduce ( ( total , file ) => {
72
+ torrent . fileList . push ( { fileName : file . path , fileSize : file . length } ) ;
58
73
return total += file . length ;
59
74
} , 0 ) ;
60
75
61
- processedTorrent . buffer = bencode . encode ( decodedTorrent ) ;
62
- processedTorrent . hash = hashTorrent ( processedTorrent . buffer ) ;
76
+ torrent . buffer = bencode . encode ( decodedTorrent ) ;
77
+ torrent . hash = hashTorrent ( torrent . buffer ) ;
63
78
64
- return Promise . resolve ( processedTorrent ) ;
79
+ return Promise . resolve ( torrent ) ;
65
80
} ;
66
81
67
82
const hashTorrent = torrentBuffer => {
@@ -71,6 +86,17 @@ const hashTorrent = torrentBuffer => {
71
86
return hash . digest ( 'hex' ) ;
72
87
} ;
73
88
89
+ /**
90
+ * Verifies an Object
91
+ *
92
+ * Object verified against an element of verificationTypes
93
+ * Removes all extraneous fields on the input Object
94
+ * Throws error if required fields are missing
95
+ *
96
+ * @param input: Object to be verified
97
+ * @param type: Field of the verificationTypes Object
98
+ * @return Promise<Map<torrent> | error>
99
+ */
74
100
const verify = ( input , type ) => {
75
101
const required = type . required ;
76
102
const allowed = type . allowed ;
@@ -103,21 +129,43 @@ const upTo = to => {
103
129
return upToArray . toString ( ) ;
104
130
} ;
105
131
132
+ /**
133
+ * Gets the group_id that the input torrent should use
134
+ *
135
+ * Either uses the user provided group number or creates a new group if the
136
+ * user provides one, or creates a new group from the user provided data
137
+ *
138
+ * @param group: group user input
139
+ * @return Promise<number | error>
140
+ */
106
141
const getGroup = group => {
107
- if ( typeof ( group ) === 'number' ) return Promise . resolve ( group ) ;
142
+ if ( typeof ( group ) === 'number' ) {
143
+ return Promise . resolve ( group ) ;
144
+ }
108
145
return verify ( group , verificationTypes . group . album ) . then ( verifiedGroup => {
109
146
const keys = verifiedGroup . keys ( ) ;
110
147
const values = verifiedGroup . values ( ) ;
111
148
const length = verifiedGroup . size ;
112
149
113
- return db . query ( `insert into tracker.groups (${ [ ...keys ] . toString ( ) } ) values (${ upTo ( length ) } ) returning id` , [ ... values ] )
114
- . then ( result => {
150
+ return db . query ( `insert into tracker.groups (${ [ ...keys ] . toString ( ) } ) values (${ upTo ( length ) } ) returning id` ,
151
+ [ ... values ] ) . then ( result => {
115
152
group = result . rows [ 0 ] . id ;
116
153
return group ;
117
154
} ) ;
118
155
} ) ;
119
156
} ;
120
157
158
+ /**
159
+ * Stores a release to the database
160
+ *
161
+ * Verifies the releaseInfo and adds all of the metadata that is obtained from
162
+ * the other functions
163
+ *
164
+ * @param torrent: slightly modified user inputted torrent
165
+ * @param group: group (user inputted id or new group id)
166
+ * @param releaseInfo: extra user provided data
167
+ * @return Promise<number | error>
168
+ */
121
169
const store = ( torrent , group , releaseInfo ) => {
122
170
return verify ( releaseInfo , verificationTypes . release . album ) . then ( verifiedRelease => {
123
171
// Add more information to the verified user input
@@ -131,7 +179,13 @@ const store = (torrent, group, releaseInfo) => {
131
179
const values = verifiedRelease . values ( ) ;
132
180
const length = verifiedRelease . size ;
133
181
134
- return db . query ( `insert into tracker.torrents (${ [ ...keys ] . toString ( ) } ) values (${ upTo ( length ) } ) returning id, group_id` , [ ...values ] ) ;
182
+ return db . query ( `insert into tracker.torrents (${ [ ...keys ] . toString ( ) } ) values (${ upTo ( length ) } ) returning id, group_id` ,
183
+ [ ...values ] ) . then ( result => {
184
+ return {
185
+ groupId : result . rows [ 0 ] . group_id ,
186
+ torrentId : result . rows [ 0 ] . id
187
+ } ;
188
+ } ) ;
135
189
} )
136
190
} ;
137
191
@@ -144,15 +198,13 @@ app.post('/upload', torrentUpload, (req, res) => {
144
198
145
199
processTorrent ( torrent ) . then ( saveToDisk ) . then ( torrent => {
146
200
return getGroup ( group ) . then ( group => {
147
- return store ( torrent , group , releaseInfo ) . then ( result => {
148
- const torrentId = result . rows [ 0 ] . id ;
149
- if ( torrentId ) {
150
- res . send ( 200 , { id : result . rows [ 0 ] . group_id } ) ;
151
- }
201
+ return store ( torrent , group , releaseInfo ) . then ( output => {
202
+ res . status ( 200 ) . send ( output ) ;
152
203
} ) ;
153
204
} ) ;
154
205
} ) . catch ( err => {
155
206
console . log ( err ) ;
207
+ res . sendStatus ( 500 ) ;
156
208
} ) ;
157
209
} ) ;
158
210
0 commit comments