files.js
1.72 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
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');
var fs = require('fs');
// /file
router.get('/', function(req, res) {
console.log(req.query);
var user_id = req.query.id;
var curPath = req.query.cur; // /folder1/folder2/
var sql = 'SELECT * FROM files WHERE user_id=(?) AND location=(?) ORDER BY date DESC';
connection.query(sql, [user_id, curPath], function(err, result) {
if (err) {
res.send({ error: 'select error' });
} else {
res.status(200).send({ files: result });
}
})
});
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
var extension = targetFile.split('.')[1].toLowerCase();
S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir) {
if (result) {
var content;
content = fs.readFileSync(downloadDir, 'utf8');
res.send({ file_name: file_name, content: content });
//if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') {
// res.send({ type: 'image', src: downloadDir})
//}else{
// var content;
// content = fs.readFileSync(downloadDir, 'utf8');
// res.send({file_name: file_name, content: content});
// //res.status(200).send({type: 'text', src: downloadDir})
//}
}
})
});
module.exports = router;