Showing
7 changed files
with
109 additions
and
59 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) { | ||
23 | - console.log('delete db error'); | ||
24 | - res.send({erorr: 'db delete error'}); | ||
25 | - } | ||
26 | - else { | ||
27 | - //connection.query(sql2, [file_name, '/trashcan'+curPath, user_id], function (err) { | ||
28 | - connection.query(sql2, [file_name, '/trashcan/', user_id], function (err) { | ||
29 | if (err) { | 20 | if (err) { |
30 | - console.log('insert in trashcan db error'); | 21 | + console.log('update db error'); |
31 | - res.send({error: 'db insert error'}); | 22 | + res.send({ erorr: 'db update error' }); |
32 | - } | 23 | + } else { |
33 | - else { | ||
34 | - // /drive/user_id/sourceFile --> /drive/user_id/trashcan/sourceFile | ||
35 | S3.moveFile4(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) { | 24 | S3.moveFile4(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) { |
36 | if (result) { | 25 | if (result) { |
37 | - res.send({message: "move to trashcan success"}); | 26 | + res.send("move to trashcan success"); |
38 | - }else{ | 27 | + } else { |
39 | - res.send({err: "no such file"}) | 28 | + 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
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment