Hong

휴지통

...@@ -7,7 +7,9 @@ var S3 = require('../modules/s3/s3'); ...@@ -7,7 +7,9 @@ var S3 = require('../modules/s3/s3');
7 7
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 + console.log(req.query);
12 + console.log(req.params);
11 var file_name = req.params.name; 13 var file_name = req.params.name;
12 var user_id = req.query.id; 14 var user_id = req.query.id;
13 var curPath = req.query.cur; 15 var curPath = req.query.cur;
...@@ -15,32 +17,56 @@ router.get('/:name', function (req, res) { ...@@ -15,32 +17,56 @@ router.get('/:name', function (req, res) {
15 var targetPath = 'trashcan'; 17 var targetPath = 'trashcan';
16 var sourceFile = curPath.substring(1) + file_name; 18 var sourceFile = curPath.substring(1) + file_name;
17 19
18 - var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)'; 20 + 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 (?, ?, ?)'; 21 + 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) { 22 if (err) {
23 - console.log('delete db error'); 23 + console.log('update db error');
24 - res.send({erorr: 'db delete error'}); 24 + res.send({ erorr: 'db update error' });
25 - } 25 + } else {
26 - else { 26 + S3.moveFile2(S3.BUCKET_NAME, user_id, sourceFile, file_name, targetPath, function(result) {
27 - connection.query(sql2, [file_name, '/trashcan'+curPath, user_id], function (err) { 27 + if (result) {
28 - if (err) { 28 + res.send("move to trashcan success");
29 - console.log('insert in trashcan db error');
30 - res.send({error: 'db insert error'});
31 - }
32 - else {
33 - // /drive/user_id/sourceFile --> /drive/user_id/trashcan/sourceFile
34 - S3.moveFile2(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) {
35 - if (result) {
36 - res.send("move to trashcan success");
37 - }
38 - })
39 } 29 }
40 }) 30 })
41 } 31 }
42 }) 32 })
43 }); 33 });
44 34
35 +// // /file/delete/:name
36 +// router.get('/:name', function (req, res) {
37 +// var file_name = req.params.name;
38 +// var user_id = req.query.id;
39 +// var curPath = req.query.cur;
40 +
41 +// var targetPath = 'trashcan';
42 +// var sourceFile = curPath.substring(1) + file_name;
43 +
44 +// var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)';
45 +// var sql2 = 'INSERT INTO trashcan (trash_name, location, user_Id) VALUES (?, ?, ?)';
46 +
47 +// connection.query(sql1, [file_name, curPath, user_id], function (err) {
48 +// if (err) {
49 +// console.log('delete db error');
50 +// res.send({erorr: 'db delete error'});
51 +// }
52 +// else {
53 +// connection.query(sql2, [file_name, '/trashcan'+curPath, user_id], function (err) {
54 +// if (err) {
55 +// console.log('insert in trashcan db error');
56 +// res.send({error: 'db insert error'});
57 +// }
58 +// else {
59 +// // /drive/user_id/sourceFile --> /drive/user_id/trashcan/sourceFile
60 +// S3.moveFile2(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) {
61 +// if (result) {
62 +// res.send("move to trashcan success");
63 +// }
64 +// })
65 +// }
66 +// })
67 +// }
68 +// })
69 +// });
70 +
45 71
46 module.exports = router; 72 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -9,38 +9,39 @@ var fs = require('fs'); ...@@ -9,38 +9,39 @@ var fs = require('fs');
9 9
10 10
11 // /file 11 // /file
12 -router.get('/', function (req, res) { 12 +router.get('/', function(req, res) {
13 + console.log(req.query);
13 var user_id = req.query.id; 14 var user_id = req.query.id;
14 - var curPath = req.query.cur; // /folder1/folder2/ 15 + var curPath = req.query.cur; // /folder1/folder2/
15 16
16 var sql = 'SELECT * FROM files WHERE user_id=(?) AND location=(?) ORDER BY date DESC'; 17 var sql = 'SELECT * FROM files WHERE user_id=(?) AND location=(?) ORDER BY date DESC';
17 - connection.query(sql, [user_id, curPath], function (err, result) { 18 + connection.query(sql, [user_id, curPath], function(err, result) {
18 - if (err){ 19 + if (err) {
19 - res.send({error: 'select error'}); 20 + res.send({ error: 'select error' });
20 - }else{ 21 + } else {
21 22
22 - res.status(200).send({files: result}); 23 + res.status(200).send({ files: result });
23 } 24 }
24 }) 25 })
25 }); 26 });
26 27
27 28
28 29
29 -router.get('/:name', function (req, res) { 30 +router.get('/:name', function(req, res) {
30 var file_name = req.params.name; 31 var file_name = req.params.name;
31 - var curPath = req.query.cur; // /folder1/folder2/ 32 + var curPath = req.query.cur; // /folder1/folder2/
32 var user_id = req.query.id; 33 var user_id = req.query.id;
33 34
34 - var targetFile = curPath.substring(1) + file_name; // folder1/folder2/test.txt 35 + var targetFile = curPath.substring(1) + file_name; // folder1/folder2/test.txt
35 var extension = targetFile.split('.')[1].toLowerCase(); 36 var extension = targetFile.split('.')[1].toLowerCase();
36 37
37 38
38 - S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir){ 39 + S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir) {
39 - if (result){ 40 + if (result) {
40 var content; 41 var content;
41 content = fs.readFileSync(downloadDir, 'utf8'); 42 content = fs.readFileSync(downloadDir, 'utf8');
42 - res.send({file_name: file_name, content: content}); 43 + res.send({ file_name: file_name, content: content });
43 - 44 +
44 //if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') { 45 //if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') {
45 // res.send({ type: 'image', src: downloadDir}) 46 // res.send({ type: 'image', src: downloadDir})
46 //}else{ 47 //}else{
...@@ -51,7 +52,7 @@ router.get('/:name', function (req, res) { ...@@ -51,7 +52,7 @@ router.get('/:name', function (req, res) {
51 //} 52 //}
52 } 53 }
53 }) 54 })
54 -}); 55 +});
55 56
56 57
57 module.exports = router; 58 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -160,63 +160,59 @@ router.post('/makefolder', function(req, res, next) { ...@@ -160,63 +160,59 @@ router.post('/makefolder', function(req, res, next) {
160 160
161 161
162 router.post('/delfolder', function(req, res, next) { 162 router.post('/delfolder', function(req, res, next) {
163 - 163 + console.log(req.body);
164 user_id = req.body.id; 164 user_id = req.body.id;
165 let cur = req.body.cur; 165 let cur = req.body.cur;
166 curPath = user_id + cur; 166 curPath = user_id + cur;
167 - let folder_name = req.body.folder_name; 167 + let name = req.body.folder_name;
168 - let params = { 168 + let newPath = user_id + '/trashcan/';
169 - Bucket: BUCKET_NAME, 169 + let checkfolder = 'SELECT * FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;';
170 - Key: 'drive/' + curPath + folder_name + '/' 170 + connection.query(checkfolder, [cur, name, user_id], function(err1, rows, fields) {
171 - }; 171 + console.log(rows);
172 - let infolderpath = cur + folder_name + '/'; 172 + if (rows.length != 0) {
173 - let checkinfolder = 'SELECT location FROM folders WHERE location = ? AND user_id = ? UNION ALL SELECT location FROM files WHERE location = ? AND user_id = ?;'; 173 + let copy_params = {
174 - connection.query(checkinfolder, [infolderpath, user_id, infolderpath, user_id], function(err, infolder) { 174 + Bucket: BUCKET_NAME,
175 - if (err) { 175 + CopySource: BUCKET_NAME + '/drive/' + curPath + name + '/',
176 - console.log("copy error"); 176 + Key: 'drive/' + newPath + name + '/'
177 - res.status(304).send({ error: "checkinfolder error" }); 177 + };
178 - } else { 178 + let del_params = {
179 - if (infolder.length != 0) { 179 + Bucket: BUCKET_NAME,
180 - res.status(204).send({ error: "infolder exist!" }) 180 + Key: 'drive/' + curPath + name + '/'
181 - } else { 181 + };
182 - let checksql = 'SELECT * FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;'; 182 + s3.copyObject(copy_params, function(err, data) {
183 - let values = [cur, folder_name, user_id]; 183 + if (err) {
184 - connection.query(checksql, values, function(err, rows, fields) { 184 + console.log(err, data);
185 - if (err) { 185 + console.log("copy error");
186 - res.status(404).send({ error: "error" }); 186 + res.status(304).send({ error: "copy error" });
187 - } else { 187 + } else {
188 - if (rows.length != 0) { 188 + s3.deleteObject(del_params, function(err, data) {
189 - s3.deleteObject(params, function(err, data) { 189 + if (err) {
190 - if (err) { 190 + console.log(err, data);
191 - console.log('s3 error'); 191 + console.log("delete error");
192 - res.status(400).send({ err: err }); 192 + res.status(304).send({ error: "delete error" });
193 + } else {
194 + let values = ['/trashcan/', cur, name, user_id];
195 + let updatesql = 'UPDATE folders SET location = ? WHERE location = ? AND folder_name = ? AND user_id = ?;';
196 + connection.query(updatesql, values, function(err3, result, field) {
197 + if (err3) {
198 + console.log("updatesql error");
199 + res.status(304).send({ error: "updatesql error" });
193 } else { 200 } else {
194 - console.log(data); 201 + let resultsql = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
195 - let sql = 'DELETE FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;'; 202 + connection.query(resultsql, [cur, user_id], function(err, rows, fields) {
196 - connection.query(sql, values, function(err, result, field) { 203 + res.status(200).send({
197 - if (err) { 204 + folders: rows
198 - res.status(400).send({ err: err }); 205 + });
199 - } else {
200 - console.log(cur);
201 - console.log(user_id);
202 - let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
203 - connection.query(checkfolder, [cur, user_id], function(err, rows, fields) {
204 - res.status(200).send({
205 - folders: rows
206 - })
207 - });
208 -
209 - }
210 }); 206 });
211 } 207 }
212 }); 208 });
213 - } else {
214 - console.log(req.body);
215 - res.status(304).send({ error: "Does not exist" });
216 } 209 }
217 - } 210 + });
218 - }); 211 + }
219 - } 212 + });
213 + } else {
214 + console.log("Does not exist folder");
215 + res.status(304).send({ error: "Does not exist" });
220 } 216 }
221 }); 217 });
222 }); 218 });
......
1 +const express = require('express');
2 +const router = express.Router();
3 +const AWS = require("aws-sdk");
4 +const moment = require("moment");
5 +AWS.config.loadFromPath(__dirname + "/modules/awsconfig.json");
6 +var S3 = require('../modules/s3/s3');
7 +
8 +router.get('/delfile', function(req, res, next) {
9 + var file_id = req.query.file_id;
10 + var user_id = req.query.id;
11 +
12 +
13 + var getsql = 'SELECT * FROM files WHERE file_id = (?)';
14 + connection.query(getsql, [file_id], function(err, rows) {
15 + if (err) {
16 + console.log('select db error');
17 + res.send({ erorr: 'db select error' });
18 + } else {
19 + if (rows.length == 0) {
20 + console.log('exist error');
21 + res.send({ erorr: 'exist error' });
22 + } else {
23 + var curPath = rows[0].location;
24 + var file_name = rows[0].file_name;
25 + var sourceFile = curPath.substring(1) + file_name;
26 +
27 + var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)';
28 + connection.query(sql1, [file_name, curPath, user_id], function(err) {
29 + if (err) {
30 + console.log('delete db error');
31 + res.send({ erorr: 'db delete error' });
32 + } else {
33 + S3.deleteFile(S3.BUCKET_NAME, user_id, sourceFile, function(result) {
34 + if (result) {
35 + let checkfile = 'SELECT * FROM files WHERE location = ? AND user_id = ?;';
36 + connection.query(checkfile, ['/trashcan/', user_id], function(err, file) {
37 + res.status(200).send({
38 + files: file
39 + })
40 + });
41 + }
42 + })
43 + }
44 + })
45 + }
46 + }
47 + })
48 +});
49 +
50 +
51 +router.get('/delfolder', function(req, res, next) {
52 + var folder_id = req.query.folder_id;
53 + var user_id = req.query.id;
54 +
55 +
56 + var getsql = 'SELECT * FROM folders WHERE folder_id = (?)';
57 + connection.query(getsql, [folder_id], function(err, rows) {
58 + if (err) {
59 + console.log('select db error');
60 + res.send({ erorr: 'db select error' });
61 + } else {
62 + if (rows.length == 0) {
63 + console.log('exist error');
64 + res.send({ erorr: 'exist error' });
65 + } else {
66 + var curPath = '/trashcan/'
67 + var folder_name = rows[0].folder_name;
68 + var sourcefolder = curPath.substring(1) + folder_name + '/';
69 +
70 + var sql1 = 'DELETE FROM folders WHERE folder_name = (?) AND location=(?) AND user_Id = (?)';
71 + connection.query(sql1, [folder_name, curPath, user_id], function(err) {
72 + if (err) {
73 + console.log('delete db error');
74 + res.send({ erorr: 'db delete error' });
75 + } else {
76 + var params = {
77 + Bucket: S3.BUCKET_NAME,
78 + Key: 'drive/' + user_id + curPath + folder_name + '/'
79 + };
80 + s3.deleteObject(params, function(err, data) {
81 + if (err) {
82 + res.status(400).send({ err: err });
83 + } else {
84 + let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
85 + connection.query(checkfolder, [curPath, user_id], function(err, rows) {
86 + res.status(200).send({
87 + folders: rows
88 + })
89 + });
90 +
91 + }
92 + })
93 + }
94 + })
95 + }
96 + }
97 + })
98 +});
99 +
100 +
101 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file