leeseohyeon

다중 파일 업로드

......@@ -33,6 +33,7 @@ var app = express();
// view engine setup
app.set('views', [path.join(__dirname, 'views'),path.join(__dirname ,'dist')]);
// app.set('view engine', 'ejs');
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
......
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');
// /file/delete/:name
router.get('/:name', function (req, res) {
var file_name = req.params.name;
var user_id = req.session.user_id;
//var user_id = req.session.user_id;
var user_id = 'shlee';
var sourceFile = file_name;
var targetPath = 'trashcan';
......@@ -30,6 +35,7 @@ router.get('/:name', function (req, res) {
S3.moveFile(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) {
if (result) {
console.log("file move to trashcan success");
res.send(result);
}
})
}
......
var express = require('express');
var router = express.Router();
var fs = require('fs');
var AWS = require('aws-sdk');
AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
var S3 = require('../modules/s3/s3');
......@@ -9,12 +11,13 @@ var S3 = require('../modules/s3/s3');
// /file/download/:name
router.get('/:name', function (req, res) {
var file_name = req.params.name;
var user_id = req.session.user_id;
//var user_id = req.session.user_id;
var user_id = 'shlee';
var sourceFile = file_name;
var tempDownloadDir = './public/download/' + user_id + '/' + file_name;
var tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + '/' + file_name;
S3.downloadFile(S3.BUCKET_NAME, userId, sourceFile, function (result, data) {
S3.downloadFile(S3.BUCKET_NAME, user_id, sourceFile, function (result, data) {
!fs.existsSync(tempDownloadDir + '/../') && fs.mkdirSync(tempDownloadDir + '/../');
fs.writeFileSync(tempDownloadDir, data);
res.download(tempDownloadDir, function (err) {
......
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 userId = req.session.user_id;
//var userId = req.session.user_id;
var userId = 'shlee';
var sql = 'SELECT * FROM files WHERE user_id = (?) ORDER BY date DESC';
connection.query(sql, [userId], function (err, result) {
......@@ -13,6 +17,7 @@ router.get('/', function (req, res) {
}
else {
console.log('bringing fileList success');
res.send(result);
//res.render('file/files');
}
})
......@@ -21,7 +26,10 @@ router.get('/', function (req, res) {
// /file/:name
router.get('/:name', function (req, res) {
var file_name = req.params.name;
var user_id = req.session.user_id;
//var user_id = req.session.user_id;
var user_id = 'shlee';
var s3 = new AWS.S3();
var sourceFile = 'drive/' + user_id + '/' + file_name;
var params = { Bucket: S3.BUCKET_NAME, Key: sourceFile };
......
......@@ -8,9 +8,58 @@ AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
var S3 = require('../modules/s3/s3');
// /file/upload
router.post('/', function (req, res) {
//var userId = req.user.userId;
var userId = 'shlee';
var sourceFiles = [];
var errFiles = [];
var targetPath = '';
var bodies = [];
var form = new formidable.IncomingForm();
form.multiples = true;
form.parse(req, function (err, fields, files) {
if(!files.file[0]){
sourceFiles.push(files.file.name);
bodies.push(files.file.path);
}else{
for (var file of files.file) {
sourceFiles.push(file.name);
bodies.push(file.path);
}
}
S3.uploadFiles(0, errFiles, S3.BUCKET_NAME, userId, sourceFiles, targetPath, bodies, function (result, errFiles) {
var newSourceFiles = [];
if (!result) { // 에러 파일이 있는 경우
for (var sourceFile of sourceFiles) {
if (!(sourceFile in errFiles)) {
newSourceFiles.push(sourceFile);
}
}
}
for (var sourceFile of sourceFiles) {
var sql = 'INSERT INTO files (file_name, user_id) VALUES (?, ?)';
connection.query(sql, [sourceFile, userId], function (err, result) {
if (err) {
console.log('insert file {', sourceFile, '} in db failed');
}
})
}
res.end("Errfiles: ", errFiles);
})
})
})
/*
// /file/upload
router.post('/', function (req, res) {
var user_id = req.session.user_id;
//var user_id = req.session.user_id;
var user_id = 'shlee';
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
......@@ -24,9 +73,10 @@ router.post('/', function (req, res) {
console.log("Upload db error");
throw err;
} else {
S3.uploadFile(S3.BUCKET_NAME, userId, sourceFile, targetPath, files.image.path, function (result) {
S3.uploadFile(S3.BUCKET_NAME, user_id, sourceFile, targetPath, files.file.path, function (result) {
if (result) {
console.log("Upload Success");
res.send(result);
} else {
console.log("Upload Fail: Check FIle Duplication");
}
......@@ -35,6 +85,7 @@ router.post('/', function (req, res) {
});
})
});
*/
module.exports = router;
\ No newline at end of file
......
{
"accessKeyId": "ASIAZQ5XTMMF3SGBG6XI",
"secretAccessKey": "brD35WfaNAOco96AwZ8wQvQtD3euWp+SV0BbLNpS",
"sessionToken": "FwoGZXIvYXdzEF0aDFLlulf6CC4JrCDMgSLDAWd1325F2sEbAiR/ea87hSFVl6+gjp5DZQmxn5mku4BmWKFjIvMG8pUqfhHmzlFkmwh9++TlzaJpMDjyjYr7Prg2yXaKtFhH60nC4v0pUcZGwwaaFnpDG3DgJx/XCZdTpc1+ExLeQWMO6dm9LkVBPRRKPniMQD4+Sa2ISI0s68kmSSz0v+5h4Ab2YuotWUW4feFJ2LulYuRrSwQF5fA6SnLH+yLI5DRyyXgwT57Wzw/NoZDqvPtUWDJsOqBZRZMNRV0llij2/7P2BTIt1p6WCYcwz3P/QY90HM9j2Th+kUFVilYEqzSILk5byof0v/tUl5ZrGn/6znI8",
"accessKeyId": "ASIAZQ5XTMMF6YISFPTT",
"secretAccessKey": "xA+qE27WeUITzCN2aCaOPjwuVsLPP7oUulJQMepo",
"sessionToken": "FwoGZXIvYXdzEHYaDFT5RqkwQ+aGLKTBWSLDAa6JZ9gBHVnbifynA5/p7y83dZ0nu0LFsk2o6qU/GRgDUtc3eYXaOK9apAW8Hr47+FTKzS3ZLVE6eqafoCN0iPjkj/6wrLLZCAsVOA682O5nsNC3VTyrzLFr2tKIXpH4sUj6/pgqakH4O3Z36HNIe/icwzaamOQI2sheShLMKFL80RU6xV+MrmKSkcgP7cw87lPfSvMhcA5IQwu2sy37L/nVdQGPNAuljUP/jMCoEqCKc74iHka0MelLa7ImWF3SXX69nyjbt7n2BTItMKGD0w4ypJBEnA3wOaIks7MgxHeuVPNMsZkZawUAkJYbl8OgzWLBIS/xGWnL",
"region": "us-east-1"
}
\ No newline at end of file
......
......@@ -143,9 +143,10 @@ var S3 = {
var targetPath = targetFile.substring(0, index);
S3.getFileList(bucketName, userId, targetPath, function (res, data) {
var answer = false;
if (!res) {
console.log("Overlap Check Error on Get List");
callback(false);
callback(false, false);
} else {
if (data) {
for (var i = 0; i < data.Contents.length; i++) {
......@@ -153,11 +154,12 @@ var S3 = {
var paths = fullpath.split('/');
var index = paths[0].length + paths[1].length + 2;
if (fullpath.substring(index) == targetFile) {
callback(true);
answer = true;
break;
}
}
console.log("Overlap Check Success");
callback(false);
callback(true, answer);
}
}
})
......@@ -222,25 +224,48 @@ var S3 = {
Key: 'drive/' + userId + '/' + targetFile,
Body: pathbody
};
S3.isFileOverlapped(bucketName, userId, targetFile, function (res) {
if (res) {
console.log("Upload Error FileOverlapped");
S3.isFileOverlapped(bucketName, userId, targetFile, function (res, ans) {
if (!res) {
console.log("Overlap Check failed");
callback(false);
} else {
s3.upload(uploadParams, function (err, data) {
if (err) {
console.log("Upload Error" + err);
callback(false);
} else {
console.log("Upload Success");
console.log(data);
callback(true);
}
})
if (ans) {
console.log("File Duplication");
callback(false);
} else {
s3.upload(uploadParams, function (err, data) {
if (err) {
console.log("Upload Error" + err);
callback(false);
} else {
console.log("Upload Success");
callback(true);
}
})
}
}
})
},
uploadFiles: function (iter, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback) {
if (iter < sourceFiles.length) {
S3.uploadFile(bucketName, userId, sourceFiles[iter], targetPath, bodies[iter], function (res) {
if (!res) {
errFiles.push(sourceFiles[iter]);
}
S3.uploadFiles(iter + 1, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback);
})
} else {
if (errFiles.length != 0) {
console.log("Upload Files Error on Uploading Some Files");
callback(false, errFiles);
} else {
console.log("Upload Files Success");
callback(true, errFiles);
}
}
},
}
module.exports = S3;
\ No newline at end of file
......