leeseohyeon

파일 이름 수정

...@@ -113,4 +113,6 @@ dist ...@@ -113,4 +113,6 @@ dist
113 .yarn/unplugged 113 .yarn/unplugged
114 .yarn/build-state.yml 114 .yarn/build-state.yml
115 .yarn/install-state.gz 115 .yarn/install-state.gz
116 -.pnp.*
...\ No newline at end of file ...\ No newline at end of file
116 +.pnp.*
117 +
118 +backend/routes/modules/s3/download/
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -9,11 +9,13 @@ var fs = require('fs'); ...@@ -9,11 +9,13 @@ var fs = require('fs');
9 var moment = require('moment'); 9 var moment = require('moment');
10 10
11 11
12 +
12 // /file/modify/:name 13 // /file/modify/:name
13 router.post('/:name', function(req, res){ 14 router.post('/:name', function(req, res){
14 var user_id = req.body.user_id; 15 var user_id = req.body.user_id;
15 var curPath = req.body.cur; // /folder1/folder2/ 16 var curPath = req.body.cur; // /folder1/folder2/
16 var file_name = req.params.name; 17 var file_name = req.params.name;
18 + var modified_name = req.body.name;
17 var modified_content = req.body.content; 19 var modified_content = req.body.content;
18 20
19 var targetPath; 21 var targetPath;
...@@ -23,25 +25,43 @@ router.post('/:name', function(req, res){ ...@@ -23,25 +25,43 @@ router.post('/:name', function(req, res){
23 targetPath = curPath.substring(1, curPaht.length-1); // folder1/folder2 25 targetPath = curPath.substring(1, curPaht.length-1); // folder1/folder2
24 } 26 }
25 27
26 - var tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + curPath + file_name; 28 + var originalDir = __dirname + '/../modules/s3/download/' + user_id + curPath + file_name;
27 - fs.writeFileSync(tempDownloadDir, modified_content); 29 + var tempDownloadDir;
28 - 30 +
29 - S3.coverFile(S3.BUCKET_NAME, user_id, file_name, targetPath, tempDownloadDir, function(result){ 31 + // 파일 이름 변경
30 - fs.unlink(tempDownloadDir, function(err){ 32 + S3.renameFile(S3.BUCKET_NAME, user_id, file_name, modified_name, targetPath, function(result, r_modified_name){
31 - if (result){ 33 + if (result){
32 - var sql = 'UPDATE files SET updated=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)'; 34 + modified_name = r_modified_name;
33 - connection.query(sql, [moment().format(), user_id, curPath, file_name], function(err){ 35 + tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + curPath + modified_name;
34 - if (err){ 36 + fs.unlink(originalDir, function(err){
35 - console.log(err); 37 + if (err){
36 - res.send({error: 'update error'}); 38 + console.log(err);
37 - }else{ 39 + res.send({error: 'original file not exists in server'});
38 - res.send('modify file success'); 40 + }else{
39 - } 41 + // 파일 내용 변경
40 - }) 42 + fs.writeFileSync(tempDownloadDir, modified_content);
41 - }else{ 43 +
42 - res.send({error: 'modify file failed'}); 44 + S3.coverFile(S3.BUCKET_NAME, user_id, modified_name, targetPath, tempDownloadDir, function (result) {
43 - } 45 + if (result) {
44 - }) 46 + var sql = 'UPDATE files SET file_name=(?), updated=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)';
47 + connection.query(sql, [modified_name, moment().format(), user_id, curPath, file_name], function (err) {
48 + if (err) {
49 + console.log(err);
50 + res.send({ error: 'update error' });
51 + } else {
52 + fs.unlinkSync(tempDownloadDir);
53 + res.send('modify file success');
54 + }
55 + })
56 + } else {
57 + res.send({ error: 'modify file failed' });
58 + }
59 + })
60 + }
61 + });
62 + }else{
63 + res.send({error: 'rename error'});
64 + }
45 }) 65 })
46 }); 66 });
47 67
......
1 { 1 {
2 - "accessKeyId": "ASIAZQ5XTMMFTERCRTOO", 2 + "accessKeyId": "ASIAZQ5XTMMFUHLZZ4M2",
3 - "secretAccessKey": "4D60bmLZb2IZje42JQp0UjYW9Y/RBVRR94ivjNrc", 3 + "secretAccessKey": "0et6RCcKOQtE9uZLw4c4WTBkvoLhnfyF/gu+Izi+",
4 - "sessionToken": "FwoGZXIvYXdzEEkaDMTw/lUiPUptDH7EBCLDAdFVXAZkmKCB5Fj4jYanKhUHH0iOqXumrbibz4EGubeO5HKz6KCN+EBvJwBbjDpagzkTYdVQHZa8sZAdEX6+41Qyv6bHM0MpEaHZIpt/ku87++qwk47NNlFJn5/iTnif3GwTKf69F2FTh1Y/+qjaP/URN8di0LZAyRonD4UuXwpmZIykWLhj7VCxpyYXBaiKBuE5eRrMQANddl3BROtS5FttRz+GsYXlalrR87UdXAoDFRJhyCpggxZCRe7+b9k3Tge7YiiX5uf2BTItnh1CIWbGHwx0sYdBpnLCZpxg31RU95Le7pW/DBJIYzxoDjjhVbz/lF0WtwIi", 4 + "sessionToken": "FwoGZXIvYXdzEE8aDC4plMfgaXo81Tt/QyLDASYTNLXjU0qWi+IAmgwl5sAMu6DPqCjeEWO4W22bSYhD178WdC/y8uz35EGd/pf5aRORKj71TiR6CsoxcEO8lW4VsN/Fdq8PfHbb5zuVqkILOtsBoAdR2/XvG3wHTqvx7LHMrFpUsgTjQkXAws7IiKV6siQrTS/p5lSmV8HZsekDktvnI+E+DkQrcvWy/BoGDJxXlpGsGZzIers2MjqzmgszC0CjTglw7aMKWriwIyhkUjo9ewIzIalCQUzk3Lr8mshmfCjOjOn2BTIt3OSehGo0BMhqYmik1RkykXo2bOqIWeZWS2lmb7Oa8GSqOcLDI4NRKgkLZ+1q",
5 "region": "us-east-1" 5 "region": "us-east-1"
6 } 6 }
......
1 -modify file
...\ No newline at end of file ...\ No newline at end of file
...@@ -89,10 +89,10 @@ var S3 = { ...@@ -89,10 +89,10 @@ var S3 = {
89 89
90 s3.upload(coverParams, function (err, data) { 90 s3.upload(coverParams, function (err, data) {
91 if (err) { 91 if (err) {
92 - console.log("Upload Error" + err); 92 + console.log("Cover Error" + err);
93 callback(false); 93 callback(false);
94 } else { 94 } else {
95 - console.log("Upload Success"); 95 + console.log("Cover Success");
96 callback(true); 96 callback(true);
97 } 97 }
98 }) 98 })
...@@ -305,6 +305,75 @@ var S3 = { ...@@ -305,6 +305,75 @@ var S3 = {
305 }) 305 })
306 }, 306 },
307 307
308 + // 파일 이름이 달라지는 경우
309 + moveFile3: function (bucketName, userId, oldFile, newFile, targetPath, callback) {
310 + if (targetPath != ''){
311 + targetPath = targetPath + '/';
312 + }
313 + sourceFile = targetPath + oldFile; // test.txt or folder1/test.txt
314 + var targetFile = targetPath + newFile;
315 +
316 + S3.copyFile2(bucketName, userId, sourceFile, targetFile, function(result){
317 + if (!result){
318 + console.log("Move Error on Copying File3");
319 + callback(false);
320 + }else{
321 + S3.deleteFile(bucketName, userId, sourceFile, function (res) {
322 + if (!res) {
323 + console.log("Move Error on Deleting File");
324 + callback(false);
325 + } else {
326 + console.log("Move Success");
327 + callback(true);
328 + }
329 + })
330 + }
331 + })
332 + },
333 +
334 + renameFile: function(bucketName, userId, sourceFile, modiFile, targetPath, callback){
335 + // sourceFile = test.txt
336 + // modiFile = test2.txt
337 + // targetPath = '' or 'folder1/folder2'
338 +
339 + if (sourceFile == modiFile){ // 이름 변경되지 않은 경우
340 + callback(true, sourceFile);
341 + }else{
342 + if (targetPath != '') {
343 + targetPath = targetpath + '/';
344 + }
345 + var targetFile = targetPath + modiFile;
346 +
347 + S3.isFileOverlapped(bucketName, userId, targetFile, function (res, ans, lvNum) {
348 + if (!res) {
349 + console.log("Overlap Check failed");
350 + callback(false, sourceFile);
351 + } else {
352 + if (ans) {
353 + console.log("File Duplication");
354 + S3.makeVersion(bucketName, userId, targetFile, lvNum + 1, function (res, versionedSourceFile) {
355 + if (!res) {
356 + console.log("Make version failed");
357 + callback(false, sourceFile);
358 + } else {
359 + S3.renameFile(bucketName, userId, sourceFile, versionedSourceFile, targetPath, callback);
360 + }
361 + })
362 + } else {
363 + S3.moveFile3(bucketName, userId, sourceFile, modiFile, targetPath, function (result) {
364 + if (result) {
365 + callback(true, modiFile);
366 + } else {
367 + console.log('File Rename failed');
368 + callback(false);
369 + }
370 + })
371 + }
372 + }
373 + });
374 + }
375 + },
376 +
308 uploadFile: function (bucketName, userId, sourceFile, targetPath, body, callback) { 377 uploadFile: function (bucketName, userId, sourceFile, targetPath, body, callback) {
309 var pathbody = fs.createReadStream(body); 378 var pathbody = fs.createReadStream(body);
310 379
......