uploadFile.js
3.1 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
84
85
86
87
88
89
90
91
var express = require('express');
var router = express.Router();
var formidable = require('formidable');
var moment = require('moment');
var AWS = require('aws-sdk');
AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
var S3 = require('../modules/s3/s3');
// /file/upload
router.post('/', function (req, res) {
var form = new formidable.IncomingForm();
form.multiples = true;
form.parse(req, function (err, fields, files) {
console.log(files);
/* form-data로 받아야 함 */
var user_id = fields.user_id;
var curPath = fields.cur // /folder1/folder2/
console.log(user_id);
console.log(curPath);
var sourceFiles = [];
var errFiles = [];
var targetPath; // 'folder1/folder2'
var bodies = [];
var errMessage = 'upload error';
if (curPath == '/') {
targetPath = '';
} else {
targetPath = curPath.substring(1, curPath.length-1);
}
if(!files.file[0]){ // 파일 하나일 때
var checkFileName = files.file.name;
checkFileName = checkFileName.split('(').join(',').split(')').join(',').split(',');
if (checkFileName.length > 1) {
errFiles.push(files.file.name);
errMessage = 'check your file name(\'(\', \')\' x!)'
}else{
sourceFiles.push(files.file.name);
bodies.push(files.file.path);
console.log('bodies', bodies);
}
}else{
for (var file of files.file) { // 파일 여러개일 때
var checkFileName = file.name;
checkFileName = checkFileName.split('(').join(',').split(')').join(',').split(',');
if (checkFileName.length > 1){
errFiles.push(file.name);
} else {
sourceFiles.push(file.name);
bodies.push(file.path);
}
}
}
S3.uploadFiles(0, errFiles, S3.BUCKET_NAME, user_id, sourceFiles, targetPath, bodies, function (result, errFiles) {
var noErrSourceFiles = [];
for (var sourceFile of sourceFiles) {
if (!(sourceFile in errFiles)) {
noErrSourceFiles.push(sourceFile);
}
}
for (var sourceFile of noErrSourceFiles) {
var sql = 'INSERT INTO files (file_name, user_id, location, recent_access) VALUES (?, ?, ?, ?)';
connection.query(sql, [sourceFile, user_id, curPath, moment().format()], function (err, result) {
if (err) {
console.log('insert error');
res.send({error: 'insert error'});
}
})
}
if (errFiles.length){
res.send({err: errMessage,
errFiles: errFiles})
} else {
res.send({message: 'Upload Success',
finalFiles: sourceFiles});
}
})
})
})
module.exports = router;