Skip to content

Commit 801baef

Browse files
committed
added comments to upload.js
1 parent 1494a2f commit 801baef

File tree

3 files changed

+156
-100
lines changed

3 files changed

+156
-100
lines changed

api/upload.js

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const bencode = require('bencode');
44
const crypto = require('crypto');
55
const db = require('./../db.js');
66
const fs = require('fs');
7-
const _ = require('lodash');
87

98
const app = express.Router();
109

@@ -25,6 +24,15 @@ const verificationTypes = {
2524
}
2625
};
2726

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+
*/
2836
const saveToDisk = torrent => {
2937
const now = new Date();
3038
const month = now.getMonth() + 1;
@@ -46,22 +54,29 @@ const storage = multer.memoryStorage();
4654

4755
const upload = multer({ storage });
4856

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+
*/
4966
const processTorrent = torrent => {
5067
const decodedTorrent = bencode.decode(torrent.buffer);
51-
const processedTorrent = _.cloneDeep(torrent);
52-
5368
delete decodedTorrent['announce'];
5469

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 });
5873
return total += file.length;
5974
}, 0);
6075

61-
processedTorrent.buffer = bencode.encode(decodedTorrent);
62-
processedTorrent.hash = hashTorrent(processedTorrent.buffer);
76+
torrent.buffer = bencode.encode(decodedTorrent);
77+
torrent.hash = hashTorrent(torrent.buffer);
6378

64-
return Promise.resolve(processedTorrent);
79+
return Promise.resolve(torrent);
6580
};
6681

6782
const hashTorrent = torrentBuffer => {
@@ -71,6 +86,17 @@ const hashTorrent = torrentBuffer => {
7186
return hash.digest('hex');
7287
};
7388

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+
*/
74100
const verify = (input, type) => {
75101
const required = type.required;
76102
const allowed = type.allowed;
@@ -103,21 +129,43 @@ const upTo = to => {
103129
return upToArray.toString();
104130
};
105131

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+
*/
106141
const getGroup = group => {
107-
if (typeof (group) === 'number') return Promise.resolve(group);
142+
if (typeof (group) === 'number') {
143+
return Promise.resolve(group);
144+
}
108145
return verify(group, verificationTypes.group.album).then(verifiedGroup => {
109146
const keys = verifiedGroup.keys();
110147
const values = verifiedGroup.values();
111148
const length = verifiedGroup.size;
112149

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 => {
115152
group = result.rows[0].id;
116153
return group;
117154
});
118155
});
119156
};
120157

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+
*/
121169
const store = (torrent, group, releaseInfo) => {
122170
return verify(releaseInfo, verificationTypes.release.album).then(verifiedRelease => {
123171
// Add more information to the verified user input
@@ -131,7 +179,13 @@ const store = (torrent, group, releaseInfo) => {
131179
const values = verifiedRelease.values();
132180
const length = verifiedRelease.size;
133181

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+
});
135189
})
136190
};
137191

@@ -144,15 +198,13 @@ app.post('/upload', torrentUpload, (req, res) => {
144198

145199
processTorrent(torrent).then(saveToDisk).then(torrent => {
146200
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);
152203
});
153204
});
154205
}).catch(err => {
155206
console.log(err);
207+
res.sendStatus(500);
156208
});
157209
});
158210

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"express": "^4.14.0",
1717
"express-session": "^1.14.2",
1818
"express-subdomain": "^1.0.5",
19-
"lodash": "^4.17.2",
2019
"multer": "^1.2.1",
2120
"nodemailer": "^2.6.4",
2221
"nodemailer-sendmail-transport": "^1.0.0",

0 commit comments

Comments
 (0)