Showing
7 changed files
with
608 additions
and
61 deletions
... | @@ -3,7 +3,7 @@ var router = express.Router(); | ... | @@ -3,7 +3,7 @@ var router = express.Router(); |
3 | 3 | ||
4 | var AWS = require('aws-sdk'); | 4 | 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_new'); |
7 | 7 | ||
8 | 8 | ||
9 | // /file/delete/:name | 9 | // /file/delete/:name |
... | @@ -15,30 +15,17 @@ router.get('/:name', function (req, res) { | ... | @@ -15,30 +15,17 @@ router.get('/:name', function (req, res) { |
15 | var targetPath = 'trashcan'; | 15 | var targetPath = 'trashcan'; |
16 | var sourceFile = curPath.substring(1) + file_name; | 16 | var sourceFile = curPath.substring(1) + file_name; |
17 | 17 | ||
18 | - var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)'; | 18 | + var sql1 = 'UPDATE files SET location = ? WHERE location = ? AND file_name = ? AND user_id = ?;'; |
19 | - var sql2 = 'INSERT INTO trashcan (trash_name, location, user_Id) VALUES (?, ?, ?)'; | 19 | + connection.query(sql1, ['/trashcan/', curPath, file_name, user_id], function (err) { |
20 | - | ||
21 | - connection.query(sql1, [file_name, curPath, user_id], function (err) { | ||
22 | if (err) { | 20 | if (err) { |
23 | - console.log('delete db error'); | 21 | + console.log('update db error'); |
24 | - res.send({erorr: 'db delete error'}); | 22 | + res.send({ erorr: 'db update error' }); |
25 | - } | 23 | + } else { |
26 | - else { | 24 | + S3.moveFile4(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) { |
27 | - //connection.query(sql2, [file_name, '/trashcan'+curPath, user_id], function (err) { | 25 | + if (result) { |
28 | - connection.query(sql2, [file_name, '/trashcan/', user_id], function (err) { | 26 | + res.send("move to trashcan success"); |
29 | - if (err) { | 27 | + } else { |
30 | - console.log('insert in trashcan db error'); | 28 | + res.send({ err: "no such file" }) |
31 | - res.send({error: 'db insert error'}); | ||
32 | - } | ||
33 | - else { | ||
34 | - // /drive/user_id/sourceFile --> /drive/user_id/trashcan/sourceFile | ||
35 | - S3.moveFile4(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) { | ||
36 | - if (result) { | ||
37 | - res.send({message: "move to trashcan success"}); | ||
38 | - }else{ | ||
39 | - res.send({err: "no such file"}) | ||
40 | - } | ||
41 | - }) | ||
42 | } | 29 | } |
43 | }) | 30 | }) |
44 | } | 31 | } |
... | @@ -46,4 +33,67 @@ router.get('/:name', function (req, res) { | ... | @@ -46,4 +33,67 @@ router.get('/:name', function (req, res) { |
46 | }); | 33 | }); |
47 | 34 | ||
48 | 35 | ||
36 | +// /file/delete/:name | ||
37 | +// router.get('/:name', function (req, res) { | ||
38 | +// console.log(req.query); | ||
39 | +// console.log(req.params); | ||
40 | +// var file_name = req.params.name; | ||
41 | +// var user_id = req.query.id; | ||
42 | +// var curPath = req.query.cur; | ||
43 | + | ||
44 | +// var targetPath = 'trashcan'; | ||
45 | +// var sourceFile = curPath.substring(1) + file_name; | ||
46 | + | ||
47 | +// var sql1 = 'UPDATE files SET location = ? WHERE location = ? AND file_name = ? AND user_id = ?;'; | ||
48 | +// connection.query(sql1, ['/trashcan/', curPath, file_name, user_id], function (err) { | ||
49 | +// if (err) { | ||
50 | +// console.log('update db error'); | ||
51 | +// res.send({ erorr: 'db update error' }); | ||
52 | +// } else { | ||
53 | +// S3.moveFile2(S3.BUCKET_NAME, user_id, sourceFile, file_name, targetPath, function (result) { | ||
54 | +// if (result) { | ||
55 | +// res.send("move to trashcan success"); | ||
56 | +// } | ||
57 | +// }) | ||
58 | +// } | ||
59 | +// }) | ||
60 | +// }); | ||
61 | + | ||
62 | +// // /file/delete/:name | ||
63 | +// router.get('/:name', function (req, res) { | ||
64 | +// var file_name = req.params.name; | ||
65 | +// var user_id = req.query.id; | ||
66 | +// var curPath = req.query.cur; | ||
67 | + | ||
68 | +// var targetPath = 'trashcan'; | ||
69 | +// var sourceFile = curPath.substring(1) + file_name; | ||
70 | + | ||
71 | +// var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)'; | ||
72 | +// var sql2 = 'INSERT INTO trashcan (trash_name, location, user_Id) VALUES (?, ?, ?)'; | ||
73 | + | ||
74 | +// connection.query(sql1, [file_name, curPath, user_id], function (err) { | ||
75 | +// if (err) { | ||
76 | +// console.log('delete db error'); | ||
77 | +// res.send({erorr: 'db delete error'}); | ||
78 | +// } | ||
79 | +// else { | ||
80 | +// connection.query(sql2, [file_name, '/trashcan'+curPath, user_id], function (err) { | ||
81 | +// if (err) { | ||
82 | +// console.log('insert in trashcan db error'); | ||
83 | +// res.send({error: 'db insert error'}); | ||
84 | +// } | ||
85 | +// else { | ||
86 | +// // /drive/user_id/sourceFile --> /drive/user_id/trashcan/sourceFile | ||
87 | +// S3.moveFile2(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) { | ||
88 | +// if (result) { | ||
89 | +// res.send("move to trashcan success"); | ||
90 | +// } | ||
91 | +// }) | ||
92 | +// } | ||
93 | +// }) | ||
94 | +// } | ||
95 | +// }) | ||
96 | +// }); | ||
97 | + | ||
98 | + | ||
49 | module.exports = router; | 99 | module.exports = router; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -6,7 +6,7 @@ var moment = require('moment'); | ... | @@ -6,7 +6,7 @@ var moment = require('moment'); |
6 | 6 | ||
7 | var AWS = require('aws-sdk'); | 7 | var AWS = require('aws-sdk'); |
8 | AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); | 8 | AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); |
9 | -var S3 = require('../modules/s3/s3'); | 9 | +var S3 = require('../modules/s3/s3_new'); |
10 | 10 | ||
11 | 11 | ||
12 | 12 | ... | ... |
... | @@ -3,7 +3,7 @@ var router = express.Router(); | ... | @@ -3,7 +3,7 @@ var router = express.Router(); |
3 | 3 | ||
4 | var AWS = require('aws-sdk'); | 4 | 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_new'); |
7 | 7 | ||
8 | var fs = require('fs'); | 8 | var fs = require('fs'); |
9 | 9 | ... | ... |
... | @@ -3,7 +3,7 @@ var router = express.Router(); | ... | @@ -3,7 +3,7 @@ var router = express.Router(); |
3 | 3 | ||
4 | var AWS = require('aws-sdk'); | 4 | 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_new'); |
7 | 7 | ||
8 | var fs = require('fs'); | 8 | var fs = require('fs'); |
9 | var moment = require('moment'); | 9 | var moment = require('moment'); | ... | ... |
... | @@ -6,7 +6,7 @@ var moment = require('moment'); | ... | @@ -6,7 +6,7 @@ var moment = require('moment'); |
6 | 6 | ||
7 | var AWS = require('aws-sdk'); | 7 | var AWS = require('aws-sdk'); |
8 | AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); | 8 | AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); |
9 | -var S3 = require('../modules/s3/s3'); | 9 | +var S3 = require('../modules/s3/s3_new'); |
10 | 10 | ||
11 | 11 | ||
12 | 12 | ... | ... |
... | @@ -16,7 +16,7 @@ | ... | @@ -16,7 +16,7 @@ |
16 | var AWS = require('aws-sdk'); | 16 | var AWS = require('aws-sdk'); |
17 | AWS.config.update({ region: 'ap-northeast-2' }); | 17 | AWS.config.update({ region: 'ap-northeast-2' }); |
18 | 18 | ||
19 | -var BUCKET_NAME = 'khuloud'; | 19 | +var BUCKET_NAME = 'qkrrlqja-test'; |
20 | 20 | ||
21 | var s3 = new AWS.S3(); | 21 | var s3 = new AWS.S3(); |
22 | var fs = require('fs'); | 22 | var fs = require('fs'); |
... | @@ -73,7 +73,7 @@ var S3 = { | ... | @@ -73,7 +73,7 @@ var S3 = { |
73 | }, | 73 | }, |
74 | 74 | ||
75 | // 파일 덮어쓰기 | 75 | // 파일 덮어쓰기 |
76 | - coverFile: function(bucketName, userId, sourceFile, targetPath, body, callback) { | 76 | + coverFile: function (bucketName, userId, sourceFile, targetPath, body, callback) { |
77 | var pathbody = fs.createReadStream(body); | 77 | var pathbody = fs.createReadStream(body); |
78 | 78 | ||
79 | if (targetPath != '' && targetPath[targetPath.length - 1] != '/') { | 79 | if (targetPath != '' && targetPath[targetPath.length - 1] != '/') { |
... | @@ -136,17 +136,17 @@ var S3 = { | ... | @@ -136,17 +136,17 @@ var S3 = { |
136 | }, | 136 | }, |
137 | 137 | ||
138 | // /routes/modules/s3/download에 저장 | 138 | // /routes/modules/s3/download에 저장 |
139 | - downloadFile2: function(bucketName, userId, targetFile, callback){ | 139 | + downloadFile2: function (bucketName, userId, targetFile, callback) { |
140 | tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile; | 140 | tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile; |
141 | - S3.downloadFile(bucketName, userId, targetFile, function(result, data){ | 141 | + S3.downloadFile(bucketName, userId, targetFile, function (result, data) { |
142 | if (result) { | 142 | if (result) { |
143 | - makeFolder(tempDownloadDir, function(result){ | 143 | + makeFolder(tempDownloadDir, function (result) { |
144 | if (result) { | 144 | if (result) { |
145 | fs.writeFileSync(tempDownloadDir, data); | 145 | fs.writeFileSync(tempDownloadDir, data); |
146 | callback(true, tempDownloadDir); | 146 | callback(true, tempDownloadDir); |
147 | } | 147 | } |
148 | }) | 148 | }) |
149 | - }else{ | 149 | + } else { |
150 | console.log('Download File Error'); | 150 | console.log('Download File Error'); |
151 | callback(false); | 151 | callback(false); |
152 | } | 152 | } |
... | @@ -154,12 +154,12 @@ var S3 = { | ... | @@ -154,12 +154,12 @@ var S3 = { |
154 | }, | 154 | }, |
155 | 155 | ||
156 | // 최종 | 156 | // 최종 |
157 | - downloadFile3: function(bucketName, userId, targetFile, callback){ | 157 | + downloadFile3: function (bucketName, userId, targetFile, callback) { |
158 | // targetFile 예1 => test.txt | 158 | // targetFile 예1 => test.txt |
159 | // targetFile 예2 => folder1/folder2/test.txt | 159 | // targetFile 예2 => folder1/folder2/test.txt |
160 | var tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile; | 160 | var tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile; |
161 | - makeFolder(tempDownloadDir, function(result){ | 161 | + makeFolder(tempDownloadDir, function (result) { |
162 | - if (result){ | 162 | + if (result) { |
163 | var file = fs.createWriteStream(tempDownloadDir); | 163 | var file = fs.createWriteStream(tempDownloadDir); |
164 | var params = { | 164 | var params = { |
165 | Bucket: bucketName, | 165 | Bucket: bucketName, |
... | @@ -167,15 +167,15 @@ var S3 = { | ... | @@ -167,15 +167,15 @@ var S3 = { |
167 | }; | 167 | }; |
168 | try { | 168 | try { |
169 | var stream = s3.getObject(params).createReadStream().pipe(file); | 169 | var stream = s3.getObject(params).createReadStream().pipe(file); |
170 | - stream.on('end', function(){ | 170 | + stream.on('end', function () { |
171 | console.log('end!'); | 171 | console.log('end!'); |
172 | callback(true, tempDownloadDir); | 172 | callback(true, tempDownloadDir); |
173 | }); | 173 | }); |
174 | - }catch(err){ | 174 | + } catch (err) { |
175 | console.log('no such file', err); | 175 | console.log('no such file', err); |
176 | callback(false); | 176 | callback(false); |
177 | } | 177 | } |
178 | - }else{ | 178 | + } else { |
179 | callback(false); | 179 | callback(false); |
180 | } | 180 | } |
181 | }) | 181 | }) |
... | @@ -230,9 +230,9 @@ var S3 = { | ... | @@ -230,9 +230,9 @@ var S3 = { |
230 | if (fullpath.substring(index) == targetFile) { | 230 | if (fullpath.substring(index) == targetFile) { |
231 | answer = true; | 231 | answer = true; |
232 | var splited = targetFile.split('(').join(',').split(')').join(',').split(','); | 232 | var splited = targetFile.split('(').join(',').split(')').join(',').split(','); |
233 | - if (splited.length != 3){ | 233 | + if (splited.length != 3) { |
234 | lvNum = 0; | 234 | lvNum = 0; |
235 | - }else{ | 235 | + } else { |
236 | lvNum = parseInt(splited[1]); | 236 | lvNum = parseInt(splited[1]); |
237 | console.log('isNum', lvNum); | 237 | console.log('isNum', lvNum); |
238 | } | 238 | } |
... | @@ -305,17 +305,17 @@ var S3 = { | ... | @@ -305,17 +305,17 @@ var S3 = { |
305 | 305 | ||
306 | // 파일 이름이 달라지는 경우 | 306 | // 파일 이름이 달라지는 경우 |
307 | moveFile3: function (bucketName, userId, oldFile, newFile, targetPath, callback) { | 307 | moveFile3: function (bucketName, userId, oldFile, newFile, targetPath, callback) { |
308 | - if (targetPath != ''){ | 308 | + if (targetPath != '') { |
309 | targetPath = targetPath + '/'; | 309 | targetPath = targetPath + '/'; |
310 | } | 310 | } |
311 | sourceFile = targetPath + oldFile; // test.txt or folder1/test.txt | 311 | sourceFile = targetPath + oldFile; // test.txt or folder1/test.txt |
312 | var targetFile = targetPath + newFile; | 312 | var targetFile = targetPath + newFile; |
313 | 313 | ||
314 | - S3.copyFile2(bucketName, userId, sourceFile, targetFile, function(result){ | 314 | + S3.copyFile2(bucketName, userId, sourceFile, targetFile, function (result) { |
315 | - if (!result){ | 315 | + if (!result) { |
316 | console.log("Move Error on Copying File3"); | 316 | console.log("Move Error on Copying File3"); |
317 | callback(false); | 317 | callback(false); |
318 | - }else{ | 318 | + } else { |
319 | S3.deleteFile(bucketName, userId, sourceFile, function (res) { | 319 | S3.deleteFile(bucketName, userId, sourceFile, function (res) { |
320 | if (!res) { | 320 | if (!res) { |
321 | console.log("Move Error on Deleting File"); | 321 | console.log("Move Error on Deleting File"); |
... | @@ -329,14 +329,14 @@ var S3 = { | ... | @@ -329,14 +329,14 @@ var S3 = { |
329 | }) | 329 | }) |
330 | }, | 330 | }, |
331 | 331 | ||
332 | - renameFile: function(bucketName, userId, sourceFile, modiFile, targetPath, callback){ | 332 | + renameFile: function (bucketName, userId, sourceFile, modiFile, targetPath, callback) { |
333 | // sourceFile = test.txt | 333 | // sourceFile = test.txt |
334 | // modiFile = test2.txt | 334 | // modiFile = test2.txt |
335 | // targetPath = '' or 'folder1/folder2' | 335 | // targetPath = '' or 'folder1/folder2' |
336 | 336 | ||
337 | - if (sourceFile == modiFile){ // 이름 변경되지 않은 경우 | 337 | + if (sourceFile == modiFile) { // 이름 변경되지 않은 경우 |
338 | callback(true, sourceFile); | 338 | callback(true, sourceFile); |
339 | - }else{ | 339 | + } else { |
340 | if (targetPath != '') { | 340 | if (targetPath != '') { |
341 | targetPath = targetpath + '/'; | 341 | targetPath = targetpath + '/'; |
342 | } | 342 | } |
... | @@ -375,7 +375,7 @@ var S3 = { | ... | @@ -375,7 +375,7 @@ var S3 = { |
375 | uploadFile: function (bucketName, userId, sourceFile, targetPath, body, callback) { | 375 | uploadFile: function (bucketName, userId, sourceFile, targetPath, body, callback) { |
376 | var pathbody = fs.createReadStream(body); | 376 | var pathbody = fs.createReadStream(body); |
377 | 377 | ||
378 | - if (targetPath!='' && targetPath[targetPath.length-1]!='/') { | 378 | + if (targetPath != '' && targetPath[targetPath.length - 1] != '/') { |
379 | targetPath = targetPath + '/'; | 379 | targetPath = targetPath + '/'; |
380 | } | 380 | } |
381 | var targetFile = targetPath + sourceFile; | 381 | var targetFile = targetPath + sourceFile; |
... | @@ -393,11 +393,11 @@ var S3 = { | ... | @@ -393,11 +393,11 @@ var S3 = { |
393 | } else { | 393 | } else { |
394 | if (ans) { | 394 | if (ans) { |
395 | console.log("File Duplication"); | 395 | console.log("File Duplication"); |
396 | - S3.makeVersion(bucketName, userId, sourceFile, lvNum+1, function(res, versionedSourceFile){ | 396 | + S3.makeVersion(bucketName, userId, sourceFile, lvNum + 1, function (res, versionedSourceFile) { |
397 | - if (!res){ | 397 | + if (!res) { |
398 | console.log("Make version failed"); | 398 | console.log("Make version failed"); |
399 | callback(false, sourceFile); | 399 | callback(false, sourceFile); |
400 | - }else{ | 400 | + } else { |
401 | S3.uploadFile(bucketName, userId, versionedSourceFile, targetPath, body, callback); | 401 | S3.uploadFile(bucketName, userId, versionedSourceFile, targetPath, body, callback); |
402 | } | 402 | } |
403 | }) | 403 | }) |
... | @@ -423,7 +423,7 @@ var S3 = { | ... | @@ -423,7 +423,7 @@ var S3 = { |
423 | if (!res) { | 423 | if (!res) { |
424 | errFiles.push(sourceFiles[iter]); | 424 | errFiles.push(sourceFiles[iter]); |
425 | } | 425 | } |
426 | - if (sourceFiles[iter] != resSourceFile){ // 파일 중복으로 다른 버전이 생긴 경우 | 426 | + if (sourceFiles[iter] != resSourceFile) { // 파일 중복으로 다른 버전이 생긴 경우 |
427 | sourceFiles[iter] = resSourceFile; | 427 | sourceFiles[iter] = resSourceFile; |
428 | } | 428 | } |
429 | S3.uploadFiles(iter + 1, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback); | 429 | S3.uploadFiles(iter + 1, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback); |
... | @@ -440,21 +440,21 @@ var S3 = { | ... | @@ -440,21 +440,21 @@ var S3 = { |
440 | }, | 440 | }, |
441 | } | 441 | } |
442 | 442 | ||
443 | -var makeFolder = function(dir, callback){ | 443 | +var makeFolder = function (dir, callback) { |
444 | - var paths = dir.substring(__dirname.length+1); // download/userId/folder1/folder2/test.txt | 444 | + var paths = dir.substring(__dirname.length + 1); // download/userId/folder1/folder2/test.txt |
445 | paths = paths.split('/'); | 445 | paths = paths.split('/'); |
446 | 446 | ||
447 | var folders = __dirname; | 447 | var folders = __dirname; |
448 | - for(var i=0; i<paths.length-1; i++){ | 448 | + for (var i = 0; i < paths.length - 1; i++) { |
449 | - folders += '/'+paths[i]; | 449 | + folders += '/' + paths[i]; |
450 | - if(!fs.existsSync(folders)){ | 450 | + if (!fs.existsSync(folders)) { |
451 | fs.mkdirSync(folders) | 451 | fs.mkdirSync(folders) |
452 | } | 452 | } |
453 | } | 453 | } |
454 | 454 | ||
455 | - if (fs.existsSync(folders)){ | 455 | + if (fs.existsSync(folders)) { |
456 | callback(true); | 456 | callback(true); |
457 | - }else{ | 457 | + } else { |
458 | callback(false); | 458 | callback(false); |
459 | } | 459 | } |
460 | } | 460 | } | ... | ... |
backend/routes/modules/s3/s3_new.js
0 → 100644
1 | +// 사용자 폴더: /drive/userId/ | ||
2 | +// 휴지통 폴더: /drive/userId/trashcan | ||
3 | + | ||
4 | +// /drive/userId/folder1/file.txt를 /drive/userId/trashcan/folder1/file.txt로 옮길 때 | ||
5 | +// (휴지통의 경우 폴더까지 휴지통으로 그대로 복사) | ||
6 | +// sourceFile: folder1/file.txt | ||
7 | +// targetPath: trashcan | ||
8 | +// targetFile: trashcan/folder1/file.txt | ||
9 | + | ||
10 | +// /drive/userId/folder1/file.txt를 /drive/userId/folder2/file.txt로 옮길 때 | ||
11 | +// sourceFile: folder1/file.txt | ||
12 | +// targetPath: folder2 | ||
13 | +// targetFile: folder2/file.txt | ||
14 | + | ||
15 | + | ||
16 | +var AWS = require('aws-sdk'); | ||
17 | +AWS.config.update({ region: 'ap-northeast-2' }); | ||
18 | + | ||
19 | +var BUCKET_NAME = 'khuloud'; | ||
20 | + | ||
21 | +var s3 = new AWS.S3(); | ||
22 | +var fs = require('fs'); | ||
23 | +var path = require('path'); | ||
24 | + | ||
25 | + | ||
26 | +var S3 = { | ||
27 | + BUCKET_NAME: BUCKET_NAME, | ||
28 | + | ||
29 | + copyFile: function (bucketName, userId, sourceFile, targetPath, callback) { | ||
30 | + if (targetPath != '') { | ||
31 | + targetPath = targetPath + '/'; | ||
32 | + } | ||
33 | + | ||
34 | + var copyParams = { | ||
35 | + Bucket: bucketName, | ||
36 | + CopySource: bucketName + '/drive/' + userId + '/' + sourceFile, | ||
37 | + Key: 'drive/' + userId + '/' + targetPath + sourceFile // drive/userId/trashcan/file.txt | ||
38 | + } | ||
39 | + | ||
40 | + s3.copyObject(copyParams, function (err, data) { | ||
41 | + if (err) { | ||
42 | + console.log("Copy Error", err); | ||
43 | + callback(false); | ||
44 | + } else { | ||
45 | + if (data) { | ||
46 | + console.log("Copy Success"); | ||
47 | + callback(true); | ||
48 | + } | ||
49 | + } | ||
50 | + }) | ||
51 | + }, | ||
52 | + | ||
53 | + copyFile2: function (bucketName, userId, sourceFile, targetFile, callback) { | ||
54 | + // sourceFile => folder1/folder2/test.txt | ||
55 | + // targetFile => trashcan/folder1/folder2/test.txt | ||
56 | + var copyParams = { | ||
57 | + Bucket: bucketName, | ||
58 | + CopySource: bucketName + '/drive/' + userId + '/' + sourceFile, | ||
59 | + Key: 'drive/' + userId + '/' + targetFile | ||
60 | + } | ||
61 | + | ||
62 | + s3.copyObject(copyParams, function (err, data) { | ||
63 | + if (err) { | ||
64 | + console.log("Copy Error", err); | ||
65 | + callback(false); | ||
66 | + } else { | ||
67 | + if (data) { | ||
68 | + console.log("Copy Success"); | ||
69 | + callback(true); | ||
70 | + } | ||
71 | + } | ||
72 | + }) | ||
73 | + }, | ||
74 | + | ||
75 | + // 파일 덮어쓰기 | ||
76 | + coverFile: function (bucketName, userId, sourceFile, targetPath, body, callback) { | ||
77 | + var pathbody = fs.createReadStream(body); | ||
78 | + | ||
79 | + if (targetPath != '' && targetPath[targetPath.length - 1] != '/') { | ||
80 | + targetPath = targetPath + '/'; | ||
81 | + } | ||
82 | + var targetFile = targetPath + sourceFile; | ||
83 | + | ||
84 | + var coverParams = { | ||
85 | + Bucket: bucketName, | ||
86 | + Key: 'drive/' + userId + '/' + targetFile, | ||
87 | + Body: pathbody | ||
88 | + }; | ||
89 | + | ||
90 | + s3.upload(coverParams, function (err, data) { | ||
91 | + if (err) { | ||
92 | + console.log("Cover Error" + err); | ||
93 | + callback(false); | ||
94 | + } else { | ||
95 | + console.log("Cover Success"); | ||
96 | + callback(true); | ||
97 | + } | ||
98 | + }) | ||
99 | + }, | ||
100 | + | ||
101 | + deleteFile: function (bucketName, userId, targetFile, callback) { | ||
102 | + // targetFile => folder1/folder2/test.txt | ||
103 | + var deleteParams = { | ||
104 | + Bucket: bucketName, | ||
105 | + Key: 'drive/' + userId + '/' + targetFile | ||
106 | + }; | ||
107 | + | ||
108 | + s3.deleteObject(deleteParams, function (err, data) { | ||
109 | + if (err) { | ||
110 | + console.log("Delete Error", err); | ||
111 | + callback(false); | ||
112 | + } else { | ||
113 | + if (data) { | ||
114 | + console.log("DeleteSuccess"); | ||
115 | + callback(true); | ||
116 | + } | ||
117 | + } | ||
118 | + }) | ||
119 | + }, | ||
120 | + | ||
121 | + downloadFile: function (bucketName, userId, targetFile, callback) { | ||
122 | + var downloadParams = { | ||
123 | + Bucket: bucketName, | ||
124 | + Key: 'drive/' + userId + '/' + targetFile | ||
125 | + }; | ||
126 | + console.log('drive/' + userId + '/' + targetFile); | ||
127 | + s3.getObject(downloadParams, function (err, data) { | ||
128 | + if (err) { | ||
129 | + console.log("Download File Error", err); | ||
130 | + callback(false); | ||
131 | + } else { | ||
132 | + console.log("Get File Success"); | ||
133 | + callback(true, data.Body.toString()); | ||
134 | + } | ||
135 | + }) | ||
136 | + }, | ||
137 | + | ||
138 | + // /routes/modules/s3/download에 저장 | ||
139 | + downloadFile2: function (bucketName, userId, targetFile, callback) { | ||
140 | + tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile; | ||
141 | + S3.downloadFile(bucketName, userId, targetFile, function (result, data) { | ||
142 | + if (result) { | ||
143 | + makeFolder(tempDownloadDir, function (result) { | ||
144 | + if (result) { | ||
145 | + fs.writeFileSync(tempDownloadDir, data); | ||
146 | + callback(true, tempDownloadDir); | ||
147 | + } | ||
148 | + }) | ||
149 | + } else { | ||
150 | + console.log('Download File Error'); | ||
151 | + callback(false); | ||
152 | + } | ||
153 | + }) | ||
154 | + }, | ||
155 | + | ||
156 | + // 최종 | ||
157 | + downloadFile3: function (bucketName, userId, targetFile, callback) { | ||
158 | + // targetFile 예1 => test.txt | ||
159 | + // targetFile 예2 => folder1/folder2/test.txt | ||
160 | + var tempDownloadDir = __dirname + '/download/' + userId + '/' + targetFile; | ||
161 | + makeFolder(tempDownloadDir, function (result) { | ||
162 | + if (result) { | ||
163 | + var file = fs.createWriteStream(tempDownloadDir); | ||
164 | + var params = { | ||
165 | + Bucket: bucketName, | ||
166 | + Key: 'drive/' + userId + '/' + targetFile | ||
167 | + }; | ||
168 | + s3.getObject(params) | ||
169 | + .createReadStream() | ||
170 | + .on('error', function (e) { | ||
171 | + callback(false); | ||
172 | + }) | ||
173 | + .pipe(file) | ||
174 | + .on('close', function () { | ||
175 | + callback(true, tempDownloadDir); | ||
176 | + }) | ||
177 | + //try { | ||
178 | + // var file = s3.getObject(params); | ||
179 | + // file.createReadStream().pipe(file); | ||
180 | + // callback(true, tempDownloadDir); | ||
181 | + //} catch (err) { | ||
182 | + // console.log('no such file'); | ||
183 | + // callback(false); | ||
184 | + //} | ||
185 | + } else { | ||
186 | + callback(false); | ||
187 | + } | ||
188 | + }) | ||
189 | + }, | ||
190 | + | ||
191 | + getFileList: function (bucketName, userId, targetPath, callback) { | ||
192 | + var prefix; | ||
193 | + | ||
194 | + if (targetPath == '') { | ||
195 | + prefix = 'drive/' + userId + '/'; | ||
196 | + } else { | ||
197 | + prefix = 'drive/' + userId + '/' + targetPath + '/'; | ||
198 | + } | ||
199 | + | ||
200 | + var bucketParams = { | ||
201 | + Bucket: bucketName, | ||
202 | + Prefix: prefix | ||
203 | + }; | ||
204 | + | ||
205 | + s3.listObjects(bucketParams, function (err, data) { | ||
206 | + if (err) { | ||
207 | + console.log("getList Error", err); | ||
208 | + callback(false, data); | ||
209 | + } else { | ||
210 | + if (data) { | ||
211 | + console.log("getList Success"); | ||
212 | + console.log("data : ", data); | ||
213 | + callback(true, data); | ||
214 | + } | ||
215 | + } | ||
216 | + }) | ||
217 | + }, | ||
218 | + | ||
219 | + isFileOverlapped: function (bucketName, userId, targetFile, callback) { | ||
220 | + var paths = targetFile.split('/'); | ||
221 | + var index = targetFile.length - (paths[paths.length - 1].length + 1); | ||
222 | + var targetPath = targetFile.substring(0, index); | ||
223 | + | ||
224 | + S3.getFileList(bucketName, userId, targetPath, function (res, data) { | ||
225 | + var answer = false; | ||
226 | + var lvNum; | ||
227 | + | ||
228 | + if (!res) { | ||
229 | + console.log("Overlap Check Error on Get List"); | ||
230 | + callback(false, answer, lvNum); | ||
231 | + } else { | ||
232 | + if (data) { | ||
233 | + for (var i = 0; i < data.Contents.length; i++) { | ||
234 | + var fullpath = data.Contents[i].Key; | ||
235 | + var paths = fullpath.split('/'); | ||
236 | + var index = paths[0].length + paths[1].length + 2; | ||
237 | + if (fullpath.substring(index) == targetFile) { | ||
238 | + answer = true; | ||
239 | + var splited = targetFile.split('(').join(',').split(')').join(',').split(','); | ||
240 | + if (splited.length != 3) { | ||
241 | + lvNum = 0; | ||
242 | + } else { | ||
243 | + lvNum = parseInt(splited[1]); | ||
244 | + console.log('isNum', lvNum); | ||
245 | + } | ||
246 | + break; | ||
247 | + } | ||
248 | + } | ||
249 | + console.log("Overlap Check Success"); | ||
250 | + callback(true, answer, lvNum); | ||
251 | + } | ||
252 | + } | ||
253 | + }) | ||
254 | + }, | ||
255 | + | ||
256 | + // 중복된 파일 버전 만들기(test.txt -> text(1).txt) | ||
257 | + makeVersion: function (bucketName, userId, sourceFile, lvNum, callback) { | ||
258 | + var sourceFile; | ||
259 | + var splited = sourceFile.split('(').join(',').split(')').join(',').split(','); | ||
260 | + if (splited.length != 3) { | ||
261 | + sourceFile = sourceFile.split('.')[0] + '(' + lvNum.toString() + ').' + sourceFile.split('.')[1]; | ||
262 | + } else { | ||
263 | + sourceFile = sourceFile.split('(')[0] + '(' + lvNum.toString() + ')' + sourceFile.split(')')[1]; | ||
264 | + } | ||
265 | + console.log('makeVersion ', sourceFile); | ||
266 | + | ||
267 | + callback(true, sourceFile); | ||
268 | + }, | ||
269 | + | ||
270 | + // sourceFile이 단순히 filename과 같은 경우 | ||
271 | + moveFile: function (bucketName, userId, sourceFile, targetPath, callback) { | ||
272 | + S3.copyFile(bucketName, userId, sourceFile, targetPath, function (res) { | ||
273 | + if (!res) { | ||
274 | + console.log("Move Error on Copying File"); | ||
275 | + callback(false); | ||
276 | + } else { | ||
277 | + S3.deleteFile(bucketName, userId, sourceFile, function (res) { | ||
278 | + if (!res) { | ||
279 | + console.log("Move Error on Deleting File"); | ||
280 | + callback(false); | ||
281 | + } else { | ||
282 | + console.log("Move Success"); | ||
283 | + callback(true); | ||
284 | + } | ||
285 | + }) | ||
286 | + } | ||
287 | + }) | ||
288 | + }, | ||
289 | + | ||
290 | + // sourceFile에 임의의 경로가 포함된 경우 | ||
291 | + moveFile2: function (bucketName, userId, sourceFile, targetPath, callback) { | ||
292 | + // sourceFile => folder1/folder2/test.txt | ||
293 | + var targetFile = targetPath + '/' + sourceFile; | ||
294 | + | ||
295 | + S3.copyFile2(bucketName, userId, sourceFile, targetFile, function (res) { | ||
296 | + if (!res) { | ||
297 | + console.log("Move Error on Copying File2"); | ||
298 | + callback(false); | ||
299 | + } else { | ||
300 | + S3.deleteFile(bucketName, userId, sourceFile, function (res) { | ||
301 | + if (!res) { | ||
302 | + console.log("Move Error on Deleting File"); | ||
303 | + callback(false); | ||
304 | + } else { | ||
305 | + console.log("Move Success"); | ||
306 | + callback(true); | ||
307 | + } | ||
308 | + }) | ||
309 | + } | ||
310 | + }) | ||
311 | + }, | ||
312 | + | ||
313 | + // 파일 이름이 달라지는 경우 | ||
314 | + moveFile3: function (bucketName, userId, oldFile, newFile, targetPath, callback) { | ||
315 | + if (targetPath != '') { | ||
316 | + targetPath = targetPath + '/'; | ||
317 | + } | ||
318 | + sourceFile = targetPath + oldFile; // test.txt or folder1/test.txt | ||
319 | + var targetFile = targetPath + newFile; | ||
320 | + | ||
321 | + S3.copyFile2(bucketName, userId, sourceFile, targetFile, function (result) { | ||
322 | + if (!result) { | ||
323 | + console.log("Move Error on Copying File3"); | ||
324 | + callback(false); | ||
325 | + } else { | ||
326 | + S3.deleteFile(bucketName, userId, sourceFile, function (res) { | ||
327 | + if (!res) { | ||
328 | + console.log("Move Error on Deleting File"); | ||
329 | + callback(false); | ||
330 | + } else { | ||
331 | + console.log("Move Success"); | ||
332 | + callback(true); | ||
333 | + } | ||
334 | + }) | ||
335 | + } | ||
336 | + }) | ||
337 | + }, | ||
338 | + | ||
339 | + // 파일 삭제 때 쓰임 | ||
340 | + moveFile4: function (bucketName, userId, sourceFile, targetPath, callback) { | ||
341 | + // sourceFile => folder1/folder2/test.txt | ||
342 | + var paths = sourceFile.split('/'); | ||
343 | + var filename = paths[paths.length - 1]; | ||
344 | + //var targetFile = targetPath + '/' + sourceFile; | ||
345 | + targetFile = targetPath + '/' + filename; | ||
346 | + | ||
347 | + S3.copyFile2(bucketName, userId, sourceFile, targetFile, function (res) { | ||
348 | + if (!res) { | ||
349 | + console.log("Move Error on Copying File2"); | ||
350 | + callback(false); | ||
351 | + } else { | ||
352 | + S3.deleteFile(bucketName, userId, sourceFile, function (res) { | ||
353 | + if (!res) { | ||
354 | + console.log("Move Error on Deleting File"); | ||
355 | + callback(false); | ||
356 | + } else { | ||
357 | + console.log("Move Success"); | ||
358 | + callback(true); | ||
359 | + } | ||
360 | + }) | ||
361 | + } | ||
362 | + }) | ||
363 | + }, | ||
364 | + | ||
365 | + renameFile: function (bucketName, userId, sourceFile, modiFile, targetPath, callback) { | ||
366 | + // sourceFile = test.txt | ||
367 | + // modiFile = test2.txt | ||
368 | + // targetPath = '' or 'folder1/folder2' | ||
369 | + | ||
370 | + if (sourceFile == modiFile) { // 이름 변경되지 않은 경우 | ||
371 | + callback(true, sourceFile); | ||
372 | + } else { | ||
373 | + var targetFile; | ||
374 | + if (targetPath != '') { | ||
375 | + targetFile = targetPath + '/' + modiFile; | ||
376 | + } else { | ||
377 | + targetFile = modiFile; | ||
378 | + } | ||
379 | + | ||
380 | + S3.isFileOverlapped(bucketName, userId, targetFile, function (res, ans, lvNum) { | ||
381 | + if (!res) { | ||
382 | + console.log("Overlap Check failed"); | ||
383 | + callback(false, sourceFile); | ||
384 | + } else { | ||
385 | + if (ans) { | ||
386 | + console.log("File Duplication"); | ||
387 | + S3.makeVersion(bucketName, userId, targetFile, lvNum + 1, function (res, versionedSourceFile) { | ||
388 | + if (!res) { | ||
389 | + console.log("Make version failed"); | ||
390 | + callback(false, sourceFile); | ||
391 | + } else { | ||
392 | + S3.renameFile(bucketName, userId, sourceFile, versionedSourceFile, targetPath, callback); | ||
393 | + } | ||
394 | + }) | ||
395 | + } else { | ||
396 | + S3.moveFile3(bucketName, userId, sourceFile, modiFile, targetPath, function (result) { | ||
397 | + if (result) { | ||
398 | + callback(true, modiFile); | ||
399 | + } else { | ||
400 | + console.log('File Rename failed'); | ||
401 | + callback(false); | ||
402 | + } | ||
403 | + }) | ||
404 | + } | ||
405 | + } | ||
406 | + }); | ||
407 | + } | ||
408 | + }, | ||
409 | + | ||
410 | + uploadFile: function (bucketName, userId, sourceFile, targetPath, body, callback) { | ||
411 | + var pathbody = fs.createReadStream(body); | ||
412 | + | ||
413 | + if (targetPath != '' && targetPath[targetPath.length - 1] != '/') { | ||
414 | + targetPath = targetPath + '/'; | ||
415 | + } | ||
416 | + var targetFile = targetPath + sourceFile; | ||
417 | + console.log('targetFile', targetFile); | ||
418 | + | ||
419 | + var uploadParams = { | ||
420 | + Bucket: bucketName, | ||
421 | + Key: 'drive/' + userId + '/' + targetFile, | ||
422 | + Body: pathbody | ||
423 | + }; | ||
424 | + S3.isFileOverlapped(bucketName, userId, targetFile, function (res, ans, lvNum) { | ||
425 | + if (!res) { | ||
426 | + console.log("Overlap Check failed"); | ||
427 | + callback(false, sourceFile); | ||
428 | + } else { | ||
429 | + if (ans) { | ||
430 | + console.log("File Duplication"); | ||
431 | + S3.makeVersion(bucketName, userId, sourceFile, lvNum + 1, function (res, versionedSourceFile) { | ||
432 | + if (!res) { | ||
433 | + console.log("Make version failed"); | ||
434 | + callback(false, sourceFile); | ||
435 | + } else { | ||
436 | + S3.uploadFile(bucketName, userId, versionedSourceFile, targetPath, body, callback); | ||
437 | + } | ||
438 | + }) | ||
439 | + } else { | ||
440 | + s3.upload(uploadParams, function (err, data) { | ||
441 | + if (err) { | ||
442 | + console.log("Upload Error" + err); | ||
443 | + callback(false, sourceFile); | ||
444 | + } else { | ||
445 | + console.log("Upload Success"); | ||
446 | + callback(true, sourceFile); | ||
447 | + } | ||
448 | + }) | ||
449 | + } | ||
450 | + } | ||
451 | + }) | ||
452 | + | ||
453 | + }, | ||
454 | + | ||
455 | + uploadFiles: function (iter, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback) { | ||
456 | + if (iter < sourceFiles.length) { | ||
457 | + S3.uploadFile(bucketName, userId, sourceFiles[iter], targetPath, bodies[iter], function (res, resSourceFile) { | ||
458 | + if (!res) { | ||
459 | + errFiles.push(sourceFiles[iter]); | ||
460 | + } | ||
461 | + if (sourceFiles[iter] != resSourceFile) { // 파일 중복으로 다른 버전이 생긴 경우 | ||
462 | + sourceFiles[iter] = resSourceFile; | ||
463 | + } | ||
464 | + S3.uploadFiles(iter + 1, errFiles, bucketName, userId, sourceFiles, targetPath, bodies, callback); | ||
465 | + }) | ||
466 | + } else { | ||
467 | + if (errFiles.length != 0) { | ||
468 | + console.log("Upload Files Error on Uploading Some Files"); | ||
469 | + callback(false, errFiles); | ||
470 | + } else { | ||
471 | + console.log("Upload Files Success"); | ||
472 | + callback(true, errFiles); | ||
473 | + } | ||
474 | + } | ||
475 | + }, | ||
476 | +} | ||
477 | + | ||
478 | +var makeFolder = function (dir, callback) { | ||
479 | + var paths = dir.substring(__dirname.length + 1); // download/userId/folder1/folder2/test.txt | ||
480 | + paths = paths.split('/'); | ||
481 | + | ||
482 | + var folders = __dirname; | ||
483 | + for (var i = 0; i < paths.length - 1; i++) { | ||
484 | + folders += '/' + paths[i]; | ||
485 | + if (!fs.existsSync(folders)) { | ||
486 | + fs.mkdirSync(folders) | ||
487 | + } | ||
488 | + } | ||
489 | + | ||
490 | + if (fs.existsSync(folders)) { | ||
491 | + callback(true); | ||
492 | + } else { | ||
493 | + callback(false); | ||
494 | + } | ||
495 | +} | ||
496 | + | ||
497 | +module.exports = S3; |
-
Please register or login to post a comment