Skip to content

Commit 8cdb8e5

Browse files
committed
upload now saves to disk, need to fix db storage
1 parent f688982 commit 8cdb8e5

File tree

3 files changed

+62
-30
lines changed

3 files changed

+62
-30
lines changed

api/upload.js

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,58 @@
11
const express = require('express');
22
const multer = require('multer');
33
const bencode = require('bencode');
4-
const cyrpto = require('crypto');
4+
const crypto = require('crypto');
55
const db = require('./../db.js');
6-
const _ = require('lodash');
6+
const fs = require('fs');
77

88
const app = express.Router();
99

10-
const storage = multer.diskStorage({
10+
const util = require('util');
11+
12+
/*const storage = multer.diskStorage({
1113
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}/`);
1320
},
1421
filename: (req, file, cb) => {
15-
cb(null, file.hash);
22+
cb(null, `${file.hash}.torrent`);
1623
}
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+
});
2241
};
2342

24-
const upload = multer({ storage, fileFilter });
43+
const storage = multer.memoryStorage();
44+
45+
const upload = multer({ storage });
2546

2647
const stripTrackers = torrentBuffer => {
2748
const torrent = bencode.decode(torrentBuffer);
28-
delete torrent.announce;
49+
delete torrent['announce'];
2950
return bencode.encode(torrent);
3051
};
3152

3253
const hashTorrent = torrentBuffer => {
3354
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();
3556
hash.update(code);
3657
return hash.digest('hex');
3758
};
@@ -51,7 +72,7 @@ const verifyAlbum = group => {
5172
}
5273
});
5374

54-
requiredField.forEach(field => {
75+
requiredFields.forEach(field => {
5576
if (!verifiedGroup.has(field)) {
5677
return Promise.reject(new Error('Missing required fields.'));
5778
}
@@ -72,7 +93,7 @@ const createGroup = group => {
7293
return verifyAlbum(group).then(verifiedGroup => {
7394
const keys = verifiedGroup.keys();
7495
const values = verifiedGroup.values();
75-
const length = verifiedGroup.size();
96+
const length = verifiedGroup.size;
7697

7798
return db.query(`insert into tracker.groups (${keys.toString()}) values (${upTo(length)}) return id`, values);
7899
});
@@ -82,20 +103,32 @@ const store = (torrent, group) => {
82103
return db.query('insert into tracker.torrents (hash, path, group) values ($1, $2, $3)', [torrent.hash, torrent.path, group]);
83104
};
84105

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+
})
98128
}
129+
}).catch(err => {
130+
console.log(err);
131+
res.sendStatus(500);
99132
});
100133
});
101134

app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ app.use(bodyParser.json());
2828
app.use(bodyParser.urlencoded({
2929
extended: true
3030
}));
31-
app.use(bodyParser.text());
3231

3332
// cookieParser and session should use the same secret
3433
app.use(cookieParser(secret));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"scripts": {
2727
"start": "NODE_ENV=production node app.js",
28-
"dev": "nodemon app.js"
28+
"dev": "node --inspect & nodemon --debug app.js"
2929
},
3030
"devDependencies": {
3131
"chokidar-cli": "^1.2.0",

0 commit comments

Comments
 (0)