files.js
1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var express = require('express');
var router = express.Router();
var AWS = require('aws-sdk');
AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
var S3 = require('../modules/s3/s3');
router.get('/', function (req, res) {
var user_id = req.query.user_id;
var sql = 'SELECT * FROM files WHERE user_id = (?) ORDER BY date DESC';
connection.query(sql, user_id, function (err, result) {
if (result.length == 0){
res.send({error: 'Exist Nothing'});
}else{
res.status(200).send({files: result});
}
})
});
// /file/:name
router.get('/:name', function (req, res) {
var file_name = req.params.name;
var user_id = req.query.user_id;
var s3 = new AWS.S3();
var sourceFile = 'drive/' + user_id + '/' + file_name;
var params = { Bucket: S3.BUCKET_NAME, Key: sourceFile };
var stream = s3.getObject(params).createReadStream();
stream.pipe(res, function (err) {
if (err) {
throw err;
}
})
});
module.exports = router;