downloadFile.js
2.36 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var express = require('express');
var router = express.Router();
var fs = require('fs');
var moment = require('moment');
var AWS = require('aws-sdk');
AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
var S3 = require('../modules/s3/s3');
router.get('/:name', function (req, res) {
var file_name = req.params.name;
var curPath = req.query.cur; // /folder1/folder2/
var user_id = req.query.id;
var targetFile = curPath.substring(1) + file_name; // folder1/folder2/test.txt
S3.downloadFile3(S3.BUCKET_NAME, user_id, targetFile, function (result, downloadDir) {
if (result) {
var sql = 'UPDATE files SET recent_access=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)';
connection.query(sql, [moment().format(), user_id, curPath, file_name], function(err){
if (err){
res.send({err: 'update error'});
}else{
res.send({ src: downloadDir })
}
})
}else{
res.send({ err: 'download error'})
}
})
});
/*
router.get('/:name', function (req, res, next) {
var file_name = req.params.name; // test.txt
var user_id = req.query.id;
var curPath = req.query.cur; // /folder1/folder2/
var targetFile = (curPath + file_name).substring(1); // folder1/folder2/test.txt
var s3 = new AWS.S3();
var params = {
Bucket: S3.BUCKET_NAME,
Key: 'drive/' + user_id + '/' + targetFile,
};
res.attachment(file_name);
var fileStream = s3.getObject(params).createReadStream();
fileStream.pipe(res);
});
*/
/*
// /file/download/:name
router.get('/:name', function (req, res) {
var file_name = req.params.name; // test.txt
var user_id = req.query.id;
var curPath = req.query.cur; // /folder1/folder2/
var targetFile = (curPath + file_name).substring(1); // folder1/folder2/test.txt
S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function (result, downloadDir) {
if (result){
res.download(downloadDir, function (err) {
if (err){
console.log(err);
}else{
console.log('download success');
}
});
}else{
res.send({error: 'download error'});
}
});
});
*/
module.exports = router;