박기범

'.'

...@@ -8,37 +8,32 @@ var S3 = require('../modules/s3/s3'); ...@@ -8,37 +8,32 @@ var S3 = require('../modules/s3/s3');
8 8
9 // /file/delete/:name 9 // /file/delete/:name
10 router.get('/:name', function (req, res) { 10 router.get('/:name', function (req, res) {
11 - var user_id = req.session.user_id; 11 + var file_name = req.params.name;
12 + var user_id = req.query.id;
13 + var curPath = req.query.cur;
12 14
13 - var sourceFile = req.params.name;
14 var targetPath = 'trashcan'; 15 var targetPath = 'trashcan';
15 - 16 + var sourceFile = curPath.substring(1) + file_name;
16 - var paths = sourceFile.split('/');
17 - var index = sourceFile.length - (paths[paths.length - 1].length + 1);
18 - var file_name = paths[paths.length - 1];
19 - var location = sourceFile.substring(6 + user_id.length, index);
20 17
21 var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)'; 18 var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)';
22 var sql2 = 'INSERT INTO trashcan (trash_name, location, user_Id) VALUES (?, ?, ?)'; 19 var sql2 = 'INSERT INTO trashcan (trash_name, location, user_Id) VALUES (?, ?, ?)';
23 20
24 - 21 + connection.query(sql1, [file_name, curPath, user_id], function (err) {
25 - connection.query(sql1, [file_name, location, user_id], function (err) {
26 if (err) { 22 if (err) {
27 console.log('delete db error'); 23 console.log('delete db error');
28 - throw err; 24 + res.status(404).send({erorr: 'db delete error'});
29 } 25 }
30 else { 26 else {
31 - connection.query(sql2, [file_name, location, user_id], function (err) { 27 + connection.query(sql2, [file_name, '/trashcan'+curPath, user_id], function (err) {
32 if (err) { 28 if (err) {
33 console.log('insert in trashcan db error'); 29 console.log('insert in trashcan db error');
34 - throw err; 30 + res.status(404).send({error: 'db insert error'});
35 } 31 }
36 else { 32 else {
37 // /drive/user_id/sourceFile --> /drive/user_id/trashcan/sourceFile 33 // /drive/user_id/sourceFile --> /drive/user_id/trashcan/sourceFile
38 - S3.moveFile(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) { 34 + S3.moveFile2(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) {
39 if (result) { 35 if (result) {
40 - console.log("file move to trashcan success"); 36 + res.satus(200).send("move to trashcan success");
41 - res.send("Upload Success");
42 } 37 }
43 }) 38 })
44 } 39 }
......
...@@ -8,22 +8,25 @@ AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); ...@@ -8,22 +8,25 @@ 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 +
11 // /file/download/:name 12 // /file/download/:name
12 router.get('/:name', function (req, res) { 13 router.get('/:name', function (req, res) {
13 - var file_name = req.params.name; 14 + var file_name = req.params.name; // test.txt
14 - var user_id = req.session.user_id; 15 + var user_id = req.query.id;
15 - 16 + var curPath = req.query.cur; // /folder1/folder2/
16 - var sourceFile = file_name; 17 +
17 - var tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + '/' + file_name; 18 + var targetFile = (curPath + file_name).substring(1); // folder1/folder2/test.txt
18 - 19 +
19 - S3.downloadFile(S3.BUCKET_NAME, user_id, sourceFile, function (result, data) { 20 + S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function (result, downloadDir) {
20 - !fs.existsSync(tempDownloadDir + '/../') && fs.mkdirSync(tempDownloadDir + '/../'); 21 + if (result){
21 - fs.writeFileSync(tempDownloadDir, data); 22 + res.download(downloadDir, function (err) {
22 - res.download(tempDownloadDir, function (err) { 23 + fs.unlink(downloadDir, function (err) {
23 - fs.unlink(tempDownloadDir, function (err) { 24 + console.log('download success');
24 - console.log("Download Success");
25 }); 25 });
26 }); 26 });
27 + }else{
28 + res.status(404).send({error: 'download error'});
29 + }
27 }); 30 });
28 }); 31 });
29 32
......
...@@ -5,33 +5,43 @@ var AWS = require('aws-sdk'); ...@@ -5,33 +5,43 @@ var AWS = require('aws-sdk');
5 AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); 5 AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
6 var S3 = require('../modules/s3/s3'); 6 var S3 = require('../modules/s3/s3');
7 7
8 +var fs = require('fs');
8 9
10 +
11 +// /file
9 router.get('/', function (req, res) { 12 router.get('/', function (req, res) {
10 - var user_id = req.session.user_id; 13 + var user_id = req.query.id;
14 + var curPath = req.query.cur; // /folder1/folder2/
11 15
12 - var sql = 'SELECT * FROM files WHERE user_id = (?) ORDER BY date DESC'; 16 + var sql = 'SELECT * FROM files WHERE user_id=(?) AND location=(?) ORDER BY date DESC';
13 - connection.query(sql, user_id, function (err, result) { 17 + connection.query(sql, [user_id, curPath], function (err, result) {
14 - if (result.length == 0){ 18 + if (err){
15 - res.send({error: 'Exist Nothing'}); 19 + res.send({error: 'select error'});
16 }else{ 20 }else{
21 +
17 res.status(200).send({files: result}); 22 res.status(200).send({files: result});
18 } 23 }
19 }) 24 })
20 }); 25 });
21 26
22 -// /file/:name 27 +
28 +
23 router.get('/:name', function (req, res) { 29 router.get('/:name', function (req, res) {
24 var file_name = req.params.name; 30 var file_name = req.params.name;
25 - var user_id = req.session.user_id; 31 + var curPath = req.query.cur; // /folder1/folder2/
32 + var user_id = req.query.id;
26 33
27 - var s3 = new AWS.S3(); 34 + var targetFile = curPath.substring(1) + file_name; // folder1/folder2/test.txt
35 + var extension = targetFile.split('.')[1].toLowerCase();
28 36
29 - var sourceFile = 'drive/' + user_id + '/' + file_name; 37 +
30 - var params = { Bucket: S3.BUCKET_NAME, Key: sourceFile }; 38 + S3.downloadFile3(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir){
31 - var stream = s3.getObject(params).createReadStream(); 39 + if (result){
32 - stream.pipe(res, function (err) { 40 + if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') {
33 - if (err) { 41 + res.status(200).send({ type: 'image', src: downloadDir})
34 - throw err; 42 + }else{
43 + res.status(200).send({type: 'text', src: downloadDir})
44 + }
35 } 45 }
36 }) 46 })
37 }); 47 });
......
...@@ -11,80 +11,79 @@ var S3 = require('../modules/s3/s3'); ...@@ -11,80 +11,79 @@ var S3 = require('../modules/s3/s3');
11 11
12 // /file/upload 12 // /file/upload
13 router.post('/', function (req, res) { 13 router.post('/', function (req, res) {
14 - var user_id = req.session.user_id; 14 + var form = new formidable.IncomingForm();
15 + form.multiples = true;
16 + form.parse(req, function (err, fields, files) {
17 + console.log(files);
18 +
19 + /* form-data로 받아야 함 */
20 + var user_id = fields.user_id;
21 + var curPath = fields.cur // /folder1/folder2/
22 + console.log(user_id);
23 + console.log(curPath);
15 24
16 var sourceFiles = []; 25 var sourceFiles = [];
17 var errFiles = []; 26 var errFiles = [];
18 - var targetPath = ''; 27 + var targetPath; // 'folder1/folder2'
19 var bodies = []; 28 var bodies = [];
29 + var errMessage = 'upload error';
20 30
31 + if (curPath == '/') {
32 + targetPath = '';
33 + } else {
34 + targetPath = curPath.substring(1, curPath.length-1);
35 + }
21 36
22 - var form = new formidable.IncomingForm(); 37 + if(!files.file[0]){ // 파일 하나일 때
23 - form.multiples = true; 38 + var checkFileName = files.file.name;
24 - form.parse(req, function (err, fields, files) { 39 + checkFileName = checkFileName.split('(').join(',').split(')').join(',').split(',');
25 - if(!files.file[0]){ 40 + if (checkFileName.length > 1) {
41 + errFiles.push(files.file.name);
42 + errMessage = 'check your file name(\'(\', \')\' x!)'
43 + }else{
26 sourceFiles.push(files.file.name); 44 sourceFiles.push(files.file.name);
27 bodies.push(files.file.path); 45 bodies.push(files.file.path);
46 + }
28 }else{ 47 }else{
29 - for (var file of files.file) { 48 + for (var file of files.file) { // 파일 여러개일 때
49 + var checkFileName = file.name;
50 + checkFileName = checkFileName.split('(').join(',').split(')').join(',').split(',');
51 + if (checkFileName.length > 1){
52 + errFiles.push(file.name);
53 + } else {
30 sourceFiles.push(file.name); 54 sourceFiles.push(file.name);
31 bodies.push(file.path); 55 bodies.push(file.path);
32 } 56 }
33 } 57 }
58 + }
34 59
35 S3.uploadFiles(0, errFiles, S3.BUCKET_NAME, user_id, sourceFiles, targetPath, bodies, function (result, errFiles) { 60 S3.uploadFiles(0, errFiles, S3.BUCKET_NAME, user_id, sourceFiles, targetPath, bodies, function (result, errFiles) {
36 - var newSourceFiles = []; 61 + var noErrSourceFiles = [];
37 - if (!result) { // 에러 파일이 있는 경우
38 for (var sourceFile of sourceFiles) { 62 for (var sourceFile of sourceFiles) {
39 if (!(sourceFile in errFiles)) { 63 if (!(sourceFile in errFiles)) {
40 - newSourceFiles.push(sourceFile); 64 + noErrSourceFiles.push(sourceFile);
41 - }
42 } 65 }
43 } 66 }
44 - for (var sourceFile of sourceFiles) { 67 + for (var sourceFile of noErrSourceFiles) {
45 var sql = 'INSERT INTO files (file_name, user_id, location) VALUES (?, ?, ?)'; 68 var sql = 'INSERT INTO files (file_name, user_id, location) VALUES (?, ?, ?)';
46 - connection.query(sql, [file_name, user_id, targetPath], function (err, result) { 69 + connection.query(sql, [sourceFile, user_id, curPath], function (err, result) {
47 if (err) { 70 if (err) {
48 - console.log('insert file {', sourceFile, '} in db failed'); 71 + console.log('insert error');
72 + res.send({error: 'insert error'});
49 } 73 }
50 }) 74 })
51 } 75 }
52 - res.send({Errfiles: errFiles}); 76 + if (errFiles.length){
53 - }) 77 + res.send({err: errMessage,
54 - }) 78 + errFiles: errFiles})
55 -})
56 -
57 -/*
58 -// /file/upload
59 -router.post('/', function (req, res) {
60 - //var user_id = req.session.user_id;
61 - var user_id = 'shlee';
62 -
63 - var form = new formidable.IncomingForm();
64 - form.parse(req, function (err, fields, files) {
65 - var file_name = files.file.name;
66 - var sourceFile = file_name;
67 - var targetPath = '';
68 -
69 - var sql = 'INSERT INTO files (file_name, user_id) VALUES (?)';
70 - connection.query(sql, [[file_name, user_id]], function (err) {
71 - if (err) {
72 - console.log("Upload db error");
73 - throw err;
74 - } else {
75 - S3.uploadFile(S3.BUCKET_NAME, user_id, sourceFile, targetPath, files.file.path, function (result) {
76 - if (result) {
77 - console.log("Upload Success");
78 - res.send(result);
79 } else { 79 } else {
80 - console.log("Upload Fail: Check FIle Duplication"); 80 + res.send({message: 'Upload Success',
81 + finalFiles: sourceFiles});
81 } 82 }
82 }) 83 })
83 - }
84 - });
85 }) 84 })
86 -}); 85 +})
87 -*/ 86 +
88 87
89 88
90 module.exports = router; 89 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
This diff is collapsed. Click to expand it.
1 { 1 {
2 - "accessKeyId": "ASIAZQ5XTMMFRBJO2LG5", 2 + "accessKeyId": "ASIAZQ5XTMMFRYJNKBIQ",
3 - "secretAccessKey": "ltsYvzKAoQ5UnNWEk13Zf8n4wJdkQkemsJ7GdsbS", 3 + "secretAccessKey": "NNsqT/NamEdilVYzp/dS8ys2MMlEDEmyreZb6VLJ",
4 - "sessionToken": "FwoGZXIvYXdzEI///////////wEaDLleFCvWDhdhFpXCvCLDAX/Hka6vXTZKxdyEBUh7ZSxPYLH184u1Tpo2qWHYFziUVKWJSCm5wqPNv0QWoAp8sL5NB0W5kty8hoeKv16SYB9Z+yzT1qtfuKTmrro2jGSo7AnxDSBla6UQHAv82yVetiGwu+IKhm6tHUvdNSlkIV0Qi9rNrDc9eynhFN/emFI/6NsocbQ47mmW6BZx0Z4/hHKW9TL6Uec/E87Z+oxVRFsVOGyNtow980bQgZFYJMbh3AN6poTlofP1q/qn2nUWwAdsDCjfhr/2BTItlcDfO5PEH5Gwffzv69JMTVBIemfIpS/Ybx6F6i7GbI2fZKGtGfRSecobbj4m", 4 + "sessionToken": "FwoGZXIvYXdzELP//////////wEaDJMOjkb8lIC9vGm9uCLDAWLA9QiZ4tlf+5oJaJE3MQeD6PKFGE1xTVNBTIKOY6ROuMC+sHfKVONokm+comkRD5z2Mi4l7XxSQDmOM3TtcuxwsJWw5aIKKUUUqCQbzqEy2JSAsJxBMUDiUGQnIlYJDCuB+uWWvfgAxUrDz/ReJQeXmpuFOVIymn12jTXy4scLZcz22cpiZj4pr9d05VOkzUyXozIHGEVEHgIV1qdy5sJLMicz4RQi4+bIuE5w4lCYdpk0nzduAHStgyWEN0/VjEbJSCiG48b2BTIt675GjnR/TJwYd8BOnqKzAwQyF4WxMzAgJ2NaCwPcE0ZR2/KVYQoIO1Os/Vk6",
5 "region": "us-east-1" 5 "region": "us-east-1"
6 } 6 }
...\ No newline at end of file ...\ No newline at end of file
......
1 var mysql = require('mysql'); 1 var mysql = require('mysql');
2 2
3 connection = mysql.createConnection({ 3 connection = mysql.createConnection({
4 - host: '1.201.142.243', 4 + //host: '1.201.142.243',
5 - user: 'khuloud', 5 + //user: 'khuloud',
6 + //port: 3306,
7 + //password: 'password1234',
8 + //database: 'khuloud',
9 + //connectionLimit: 30
10 + host: 'localhost',
11 + user: 'root',
6 port: 3306, 12 port: 3306,
7 - password: 'password1234', 13 + password: '1234',
8 - database: 'khuloud', 14 + database: 'temp'
9 - connectionLimit: 30
10 }); 15 });
...\ No newline at end of file ...\ No newline at end of file
......
1 +안녕
...\ No newline at end of file ...\ No newline at end of file
...@@ -52,6 +52,8 @@ var S3 = { ...@@ -52,6 +52,8 @@ var S3 = {
52 }, 52 },
53 53
54 copyFile2: function (bucketName, userId, sourceFile, targetFile, callback) { 54 copyFile2: function (bucketName, userId, sourceFile, targetFile, callback) {
55 + // sourceFile => folder1/folder2/test.txt
56 + // targetFile => trashcan/folder1/folder2/test.txt
55 var copyParams = { 57 var copyParams = {
56 Bucket: bucketName, 58 Bucket: bucketName,
57 CopySource: bucketName + '/drive/' + userId + '/' + sourceFile, 59 CopySource: bucketName + '/drive/' + userId + '/' + sourceFile,
...@@ -72,6 +74,7 @@ var S3 = { ...@@ -72,6 +74,7 @@ var S3 = {
72 }, 74 },
73 75
74 deleteFile: function (bucketName, userId, targetFile, callback) { 76 deleteFile: function (bucketName, userId, targetFile, callback) {
77 + // targetFile => folder1/folder2/test.txt
75 var deleteParams = { 78 var deleteParams = {
76 Bucket: bucketName, 79 Bucket: bucketName,
77 Key: 'drive/' + userId + '/' + targetFile 80 Key: 'drive/' + userId + '/' + targetFile
...@@ -95,7 +98,7 @@ var S3 = { ...@@ -95,7 +98,7 @@ var S3 = {
95 Bucket: bucketName, 98 Bucket: bucketName,
96 Key: 'drive/' + userId + '/' + targetFile 99 Key: 'drive/' + userId + '/' + targetFile
97 }; 100 };
98 - 101 + console.log('drive/' + userId + '/' + targetFile);
99 s3.getObject(downloadParams, function (err, data) { 102 s3.getObject(downloadParams, function (err, data) {
100 if (err) { 103 if (err) {
101 console.log("Download File Error", err); 104 console.log("Download File Error", err);
...@@ -103,8 +106,51 @@ var S3 = { ...@@ -103,8 +106,51 @@ var S3 = {
103 } else { 106 } else {
104 if (data) { 107 if (data) {
105 console.log("Get File Success"); 108 console.log("Get File Success");
106 - callback(1, data.Body); 109 + callback(true, data.body);
110 + }
111 + }
112 + })
113 + },
114 +
115 + // /routes/modules/s3/download에 저장
116 + downloadFile2: function(bucketName, userId, targetFile, callback){
117 + tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile;
118 + S3.downloadFile(bucketName, userId, targetFile, function(result, data){
119 + if (result) {
120 + makeFolder(tempDownloadDir, function(result){
121 + if (result) {
122 + fs.writeFileSync(tempDownloadDir, data);
123 + callback(true, tempDownloadDir);
124 + }
125 + })
126 + }else{
127 + console.log('Download File Error');
128 + callback(false);
129 + }
130 + })
131 + },
132 +
133 + // 최종
134 + downloadFile3: function(bucketName, userId, targetFile, callback){
135 + // targetFile 예1 => test.txt
136 + // targetFile 예2 => folder1/folder2/test.txt
137 + var tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile;
138 + makeFolder(tempDownloadDir, function(result){
139 + if (result){
140 + var file = fs.createWriteStream(tempDownloadDir);
141 + var params = {
142 + Bucket: bucketName,
143 + Key: 'drive/' + userId + '/' + targetFile
144 + };
145 + try {
146 + s3.getObject(params).createReadStream().pipe(file);
147 + callback(true, tempDownloadDir);
148 + }catch(err){
149 + console.log('no such file');
150 + callback(false);
107 } 151 }
152 + }else{
153 + callback(false);
108 } 154 }
109 }) 155 })
110 }, 156 },
...@@ -174,6 +220,20 @@ var S3 = { ...@@ -174,6 +220,20 @@ var S3 = {
174 }) 220 })
175 }, 221 },
176 222
223 + // 중복된 파일 버전 만들기(test.txt -> text(1).txt)
224 + makeVersion: function (bucketName, userId, sourceFile, lvNum, callback) {
225 + var sourceFile;
226 + var splited = sourceFile.split('(').join(',').split(')').join(',').split(',');
227 + if (splited.length != 3) {
228 + sourceFile = sourceFile.split('.')[0] + '(' + lvNum.toString() + ').' + sourceFile.split('.')[1];
229 + } else {
230 + sourceFile = sourceFile.split('(')[0] + '(' + lvNum.toString() + ')' + sourceFile.split(')')[1];
231 + }
232 + console.log('makeVersion ', sourceFile);
233 +
234 + callback(true, sourceFile);
235 + },
236 +
177 // sourceFile이 단순히 filename과 같은 경우 237 // sourceFile이 단순히 filename과 같은 경우
178 moveFile: function (bucketName, userId, sourceFile, targetPath, callback) { 238 moveFile: function (bucketName, userId, sourceFile, targetPath, callback) {
179 S3.copyFile(bucketName, userId, sourceFile, targetPath, function (res) { 239 S3.copyFile(bucketName, userId, sourceFile, targetPath, function (res) {
...@@ -194,25 +254,10 @@ var S3 = { ...@@ -194,25 +254,10 @@ var S3 = {
194 }) 254 })
195 }, 255 },
196 256
197 - makeVersion: function(bucketName, userId, sourceFile, lvNum, callback){
198 - var sourceFile;
199 - var splited = sourceFile.split('(').join(',').split(')').join(',').split(',');
200 - if (splited.length != 3){
201 - sourceFile = sourceFile.split('.')[0] + '(' + lvNum.toString() + ')' + sourceFile.split('.')[1];
202 - }else{
203 - sourceFile = sourceFile.split('(')[0] + '(' + lvNum.toString() + ')' + sourceFile.split(')')[1];
204 - }
205 - console.log('makeVersion ', sourceFile);
206 - callback(true, sourceFile);
207 - },
208 -
209 // sourceFile에 임의의 경로가 포함된 경우 257 // sourceFile에 임의의 경로가 포함된 경우
210 moveFile2: function (bucketName, userId, sourceFile, targetPath, callback) { 258 moveFile2: function (bucketName, userId, sourceFile, targetPath, callback) {
211 - var paths = sourceFile.split('/'); 259 + // sourceFile => folder1/folder2/test.txt
212 - var filename = paths[paths.length - 1]; 260 + var targetFile = targetPath + '/' + sourceFile;
213 - console.log('paths = ' + paths);
214 - console.log('filename = ' + filename);
215 - var targetFile = targetPath + filename;
216 261
217 S3.copyFile2(bucketName, userId, sourceFile, targetFile, function (res) { 262 S3.copyFile2(bucketName, userId, sourceFile, targetFile, function (res) {
218 if (!res) { 263 if (!res) {
...@@ -235,10 +280,11 @@ var S3 = { ...@@ -235,10 +280,11 @@ var S3 = {
235 uploadFile: function (bucketName, userId, sourceFile, targetPath, body, callback) { 280 uploadFile: function (bucketName, userId, sourceFile, targetPath, body, callback) {
236 var pathbody = fs.createReadStream(body); 281 var pathbody = fs.createReadStream(body);
237 282
238 - if (targetPath != '') { 283 + if (targetPath!='' && targetPath[targetPath.length-1]!='/') {
239 targetPath = targetPath + '/'; 284 targetPath = targetPath + '/';
240 } 285 }
241 var targetFile = targetPath + sourceFile; 286 var targetFile = targetPath + sourceFile;
287 + console.log('targetFile', targetFile);
242 288
243 var uploadParams = { 289 var uploadParams = {
244 Bucket: bucketName, 290 Bucket: bucketName,
...@@ -299,4 +345,23 @@ var S3 = { ...@@ -299,4 +345,23 @@ var S3 = {
299 }, 345 },
300 } 346 }
301 347
348 +var makeFolder = function(dir, callback){
349 + var paths = dir.substring(__dirname.length+1); // download/userId/folder1/folder2/test.txt
350 + paths = paths.split('/');
351 +
352 + var folders = __dirname;
353 + for(var i=0; i<paths.length-1; i++){
354 + folders += '/'+paths[i];
355 + if(!fs.existsSync(folders)){
356 + fs.mkdirSync(folders)
357 + }
358 + }
359 +
360 + if (fs.existsSync(folders)){
361 + callback(true);
362 + }else{
363 + callback(false);
364 + }
365 +}
366 +
302 module.exports = S3; 367 module.exports = S3;
...\ No newline at end of file ...\ No newline at end of file
......