leeseohyeon

다중 파일 업로드

...@@ -33,6 +33,7 @@ var app = express(); ...@@ -33,6 +33,7 @@ var app = express();
33 // view engine setup 33 // view engine setup
34 app.set('views', [path.join(__dirname, 'views'),path.join(__dirname ,'dist')]); 34 app.set('views', [path.join(__dirname, 'views'),path.join(__dirname ,'dist')]);
35 // app.set('view engine', 'ejs'); 35 // app.set('view engine', 'ejs');
36 +app.set('view engine', 'pug');
36 37
37 app.use(logger('dev')); 38 app.use(logger('dev'));
38 app.use(express.json()); 39 app.use(express.json());
......
1 var express = require('express'); 1 var express = require('express');
2 var router = express.Router(); 2 var router = express.Router();
3 3
4 +var AWS = require('aws-sdk');
5 +AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
6 +var S3 = require('../modules/s3/s3');
7 +
4 8
5 // /file/delete/:name 9 // /file/delete/:name
6 router.get('/:name', function (req, res) { 10 router.get('/:name', function (req, res) {
7 var file_name = req.params.name; 11 var file_name = req.params.name;
8 - var user_id = req.session.user_id; 12 + //var user_id = req.session.user_id;
13 + var user_id = 'shlee';
9 14
10 var sourceFile = file_name; 15 var sourceFile = file_name;
11 var targetPath = 'trashcan'; 16 var targetPath = 'trashcan';
...@@ -30,6 +35,7 @@ router.get('/:name', function (req, res) { ...@@ -30,6 +35,7 @@ router.get('/:name', function (req, res) {
30 S3.moveFile(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) { 35 S3.moveFile(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) {
31 if (result) { 36 if (result) {
32 console.log("file move to trashcan success"); 37 console.log("file move to trashcan success");
38 + res.send(result);
33 } 39 }
34 }) 40 })
35 } 41 }
......
1 var express = require('express'); 1 var express = require('express');
2 var router = express.Router(); 2 var router = express.Router();
3 3
4 +var fs = require('fs');
5 +
4 var AWS = require('aws-sdk'); 6 var AWS = require('aws-sdk');
5 AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); 7 AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
6 var S3 = require('../modules/s3/s3'); 8 var S3 = require('../modules/s3/s3');
...@@ -9,12 +11,13 @@ var S3 = require('../modules/s3/s3'); ...@@ -9,12 +11,13 @@ var S3 = require('../modules/s3/s3');
9 // /file/download/:name 11 // /file/download/:name
10 router.get('/:name', function (req, res) { 12 router.get('/:name', function (req, res) {
11 var file_name = req.params.name; 13 var file_name = req.params.name;
12 - var user_id = req.session.user_id; 14 + //var user_id = req.session.user_id;
15 + var user_id = 'shlee';
13 16
14 var sourceFile = file_name; 17 var sourceFile = file_name;
15 - var tempDownloadDir = './public/download/' + user_id + '/' + file_name; 18 + var tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + '/' + file_name;
16 19
17 - S3.downloadFile(S3.BUCKET_NAME, userId, sourceFile, function (result, data) { 20 + S3.downloadFile(S3.BUCKET_NAME, user_id, sourceFile, function (result, data) {
18 !fs.existsSync(tempDownloadDir + '/../') && fs.mkdirSync(tempDownloadDir + '/../'); 21 !fs.existsSync(tempDownloadDir + '/../') && fs.mkdirSync(tempDownloadDir + '/../');
19 fs.writeFileSync(tempDownloadDir, data); 22 fs.writeFileSync(tempDownloadDir, data);
20 res.download(tempDownloadDir, function (err) { 23 res.download(tempDownloadDir, function (err) {
......
1 var express = require('express'); 1 var express = require('express');
2 var router = express.Router(); 2 var router = express.Router();
3 3
4 +var AWS = require('aws-sdk');
5 +AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
6 +var S3 = require('../modules/s3/s3');
4 7
5 8
6 router.get('/', function (req, res) { 9 router.get('/', function (req, res) {
7 - var userId = req.session.user_id; 10 + //var userId = req.session.user_id;
11 + var userId = 'shlee';
8 12
9 var sql = 'SELECT * FROM files WHERE user_id = (?) ORDER BY date DESC'; 13 var sql = 'SELECT * FROM files WHERE user_id = (?) ORDER BY date DESC';
10 connection.query(sql, [userId], function (err, result) { 14 connection.query(sql, [userId], function (err, result) {
...@@ -13,6 +17,7 @@ router.get('/', function (req, res) { ...@@ -13,6 +17,7 @@ router.get('/', function (req, res) {
13 } 17 }
14 else { 18 else {
15 console.log('bringing fileList success'); 19 console.log('bringing fileList success');
20 + res.send(result);
16 //res.render('file/files'); 21 //res.render('file/files');
17 } 22 }
18 }) 23 })
...@@ -21,7 +26,10 @@ router.get('/', function (req, res) { ...@@ -21,7 +26,10 @@ router.get('/', function (req, res) {
21 // /file/:name 26 // /file/:name
22 router.get('/:name', function (req, res) { 27 router.get('/:name', function (req, res) {
23 var file_name = req.params.name; 28 var file_name = req.params.name;
24 - var user_id = req.session.user_id; 29 + //var user_id = req.session.user_id;
30 + var user_id = 'shlee';
31 +
32 + var s3 = new AWS.S3();
25 33
26 var sourceFile = 'drive/' + user_id + '/' + file_name; 34 var sourceFile = 'drive/' + user_id + '/' + file_name;
27 var params = { Bucket: S3.BUCKET_NAME, Key: sourceFile }; 35 var params = { Bucket: S3.BUCKET_NAME, Key: sourceFile };
......
...@@ -8,9 +8,58 @@ AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); ...@@ -8,9 +8,58 @@ AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
8 var S3 = require('../modules/s3/s3'); 8 var S3 = require('../modules/s3/s3');
9 9
10 10
11 +
12 +// /file/upload
13 +router.post('/', function (req, res) {
14 + //var userId = req.user.userId;
15 + var userId = 'shlee';
16 +
17 + var sourceFiles = [];
18 + var errFiles = [];
19 + var targetPath = '';
20 + var bodies = [];
21 +
22 +
23 + var form = new formidable.IncomingForm();
24 + form.multiples = true;
25 + form.parse(req, function (err, fields, files) {
26 + if(!files.file[0]){
27 + sourceFiles.push(files.file.name);
28 + bodies.push(files.file.path);
29 + }else{
30 + for (var file of files.file) {
31 + sourceFiles.push(file.name);
32 + bodies.push(file.path);
33 + }
34 + }
35 +
36 + S3.uploadFiles(0, errFiles, S3.BUCKET_NAME, userId, sourceFiles, targetPath, bodies, function (result, errFiles) {
37 + var newSourceFiles = [];
38 + if (!result) { // 에러 파일이 있는 경우
39 + for (var sourceFile of sourceFiles) {
40 + if (!(sourceFile in errFiles)) {
41 + newSourceFiles.push(sourceFile);
42 + }
43 + }
44 + }
45 + for (var sourceFile of sourceFiles) {
46 + var sql = 'INSERT INTO files (file_name, user_id) VALUES (?, ?)';
47 + connection.query(sql, [sourceFile, userId], function (err, result) {
48 + if (err) {
49 + console.log('insert file {', sourceFile, '} in db failed');
50 + }
51 + })
52 + }
53 + res.end("Errfiles: ", errFiles);
54 + })
55 + })
56 +})
57 +
58 +/*
11 // /file/upload 59 // /file/upload
12 router.post('/', function (req, res) { 60 router.post('/', function (req, res) {
13 - var user_id = req.session.user_id; 61 + //var user_id = req.session.user_id;
62 + var user_id = 'shlee';
14 63
15 var form = new formidable.IncomingForm(); 64 var form = new formidable.IncomingForm();
16 form.parse(req, function (err, fields, files) { 65 form.parse(req, function (err, fields, files) {
...@@ -24,9 +73,10 @@ router.post('/', function (req, res) { ...@@ -24,9 +73,10 @@ router.post('/', function (req, res) {
24 console.log("Upload db error"); 73 console.log("Upload db error");
25 throw err; 74 throw err;
26 } else { 75 } else {
27 - S3.uploadFile(S3.BUCKET_NAME, userId, sourceFile, targetPath, files.image.path, function (result) { 76 + S3.uploadFile(S3.BUCKET_NAME, user_id, sourceFile, targetPath, files.file.path, function (result) {
28 if (result) { 77 if (result) {
29 console.log("Upload Success"); 78 console.log("Upload Success");
79 + res.send(result);
30 } else { 80 } else {
31 console.log("Upload Fail: Check FIle Duplication"); 81 console.log("Upload Fail: Check FIle Duplication");
32 } 82 }
...@@ -35,6 +85,7 @@ router.post('/', function (req, res) { ...@@ -35,6 +85,7 @@ router.post('/', function (req, res) {
35 }); 85 });
36 }) 86 })
37 }); 87 });
88 +*/
38 89
39 90
40 module.exports = router; 91 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
1 { 1 {
2 - "accessKeyId": "ASIAZQ5XTMMF3SGBG6XI", 2 + "accessKeyId": "ASIAZQ5XTMMF6YISFPTT",
3 - "secretAccessKey": "brD35WfaNAOco96AwZ8wQvQtD3euWp+SV0BbLNpS", 3 + "secretAccessKey": "xA+qE27WeUITzCN2aCaOPjwuVsLPP7oUulJQMepo",
4 - "sessionToken": "FwoGZXIvYXdzEF0aDFLlulf6CC4JrCDMgSLDAWd1325F2sEbAiR/ea87hSFVl6+gjp5DZQmxn5mku4BmWKFjIvMG8pUqfhHmzlFkmwh9++TlzaJpMDjyjYr7Prg2yXaKtFhH60nC4v0pUcZGwwaaFnpDG3DgJx/XCZdTpc1+ExLeQWMO6dm9LkVBPRRKPniMQD4+Sa2ISI0s68kmSSz0v+5h4Ab2YuotWUW4feFJ2LulYuRrSwQF5fA6SnLH+yLI5DRyyXgwT57Wzw/NoZDqvPtUWDJsOqBZRZMNRV0llij2/7P2BTIt1p6WCYcwz3P/QY90HM9j2Th+kUFVilYEqzSILk5byof0v/tUl5ZrGn/6znI8", 4 + "sessionToken": "FwoGZXIvYXdzEHYaDFT5RqkwQ+aGLKTBWSLDAa6JZ9gBHVnbifynA5/p7y83dZ0nu0LFsk2o6qU/GRgDUtc3eYXaOK9apAW8Hr47+FTKzS3ZLVE6eqafoCN0iPjkj/6wrLLZCAsVOA682O5nsNC3VTyrzLFr2tKIXpH4sUj6/pgqakH4O3Z36HNIe/icwzaamOQI2sheShLMKFL80RU6xV+MrmKSkcgP7cw87lPfSvMhcA5IQwu2sy37L/nVdQGPNAuljUP/jMCoEqCKc74iHka0MelLa7ImWF3SXX69nyjbt7n2BTItMKGD0w4ypJBEnA3wOaIks7MgxHeuVPNMsZkZawUAkJYbl8OgzWLBIS/xGWnL",
5 "region": "us-east-1" 5 "region": "us-east-1"
6 } 6 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -143,9 +143,10 @@ var S3 = { ...@@ -143,9 +143,10 @@ var S3 = {
143 var targetPath = targetFile.substring(0, index); 143 var targetPath = targetFile.substring(0, index);
144 144
145 S3.getFileList(bucketName, userId, targetPath, function (res, data) { 145 S3.getFileList(bucketName, userId, targetPath, function (res, data) {
146 + var answer = false;
146 if (!res) { 147 if (!res) {
147 console.log("Overlap Check Error on Get List"); 148 console.log("Overlap Check Error on Get List");
148 - callback(false); 149 + callback(false, false);
149 } else { 150 } else {
150 if (data) { 151 if (data) {
151 for (var i = 0; i < data.Contents.length; i++) { 152 for (var i = 0; i < data.Contents.length; i++) {
...@@ -153,11 +154,12 @@ var S3 = { ...@@ -153,11 +154,12 @@ var S3 = {
153 var paths = fullpath.split('/'); 154 var paths = fullpath.split('/');
154 var index = paths[0].length + paths[1].length + 2; 155 var index = paths[0].length + paths[1].length + 2;
155 if (fullpath.substring(index) == targetFile) { 156 if (fullpath.substring(index) == targetFile) {
156 - callback(true); 157 + answer = true;
158 + break;
157 } 159 }
158 } 160 }
159 console.log("Overlap Check Success"); 161 console.log("Overlap Check Success");
160 - callback(false); 162 + callback(true, answer);
161 } 163 }
162 } 164 }
163 }) 165 })
...@@ -222,25 +224,48 @@ var S3 = { ...@@ -222,25 +224,48 @@ var S3 = {
222 Key: 'drive/' + userId + '/' + targetFile, 224 Key: 'drive/' + userId + '/' + targetFile,
223 Body: pathbody 225 Body: pathbody
224 }; 226 };
225 - S3.isFileOverlapped(bucketName, userId, targetFile, function (res) { 227 + S3.isFileOverlapped(bucketName, userId, targetFile, function (res, ans) {
226 - if (res) { 228 + if (!res) {
227 - console.log("Upload Error FileOverlapped"); 229 + console.log("Overlap Check failed");
228 callback(false); 230 callback(false);
229 } else { 231 } else {
230 - s3.upload(uploadParams, function (err, data) { 232 + if (ans) {
231 - if (err) { 233 + console.log("File Duplication");
232 - console.log("Upload Error" + err); 234 + callback(false);
233 - callback(false); 235 + } else {
234 - } else { 236 + s3.upload(uploadParams, function (err, data) {
235 - console.log("Upload Success"); 237 + if (err) {
236 - console.log(data); 238 + console.log("Upload Error" + err);
237 - callback(true); 239 + callback(false);
238 - } 240 + } else {
239 - }) 241 + console.log("Upload Success");
242 + callback(true);
243 + }
244 + })
245 + }
240 } 246 }
241 }) 247 })
242 248
243 }, 249 },
250 +
251 + uploadFiles: function (iter, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback) {
252 + if (iter < sourceFiles.length) {
253 + S3.uploadFile(bucketName, userId, sourceFiles[iter], targetPath, bodies[iter], function (res) {
254 + if (!res) {
255 + errFiles.push(sourceFiles[iter]);
256 + }
257 + S3.uploadFiles(iter + 1, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback);
258 + })
259 + } else {
260 + if (errFiles.length != 0) {
261 + console.log("Upload Files Error on Uploading Some Files");
262 + callback(false, errFiles);
263 + } else {
264 + console.log("Upload Files Success");
265 + callback(true, errFiles);
266 + }
267 + }
268 + },
244 } 269 }
245 270
246 module.exports = S3; 271 module.exports = S3;
...\ No newline at end of file ...\ No newline at end of file
......