Showing
9 changed files
with
347 additions
and
264 deletions
... | @@ -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,36 +5,46 @@ var AWS = require('aws-sdk'); | ... | @@ -5,36 +5,46 @@ 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; | ||
33 | + | ||
34 | + var targetFile = curPath.substring(1) + file_name; // folder1/folder2/test.txt | ||
35 | + var extension = targetFile.split('.')[1].toLowerCase(); | ||
26 | 36 | ||
27 | - var s3 = new AWS.S3(); | ||
28 | 37 | ||
29 | - var sourceFile = 'drive/' + user_id + '/' + file_name; | 38 | + S3.downloadFile3(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir){ |
30 | - var params = { Bucket: S3.BUCKET_NAME, Key: sourceFile }; | 39 | + if (result){ |
31 | - var stream = s3.getObject(params).createReadStream(); | 40 | + if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') { |
32 | - stream.pipe(res, function (err) { | 41 | + res.status(200).send({ type: 'image', src: downloadDir}) |
33 | - if (err) { | 42 | + }else{ |
34 | - throw err; | 43 | + res.status(200).send({type: 'text', src: downloadDir}) |
44 | + } | ||
35 | } | 45 | } |
36 | }) | 46 | }) |
37 | -}); | 47 | +}); |
38 | 48 | ||
39 | 49 | ||
40 | module.exports = router; | 50 | module.exports = router; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -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; | ||
15 | - | ||
16 | - var sourceFiles = []; | ||
17 | - var errFiles = []; | ||
18 | - var targetPath = ''; | ||
19 | - var bodies = []; | ||
20 | - | ||
21 | - | ||
22 | var form = new formidable.IncomingForm(); | 14 | var form = new formidable.IncomingForm(); |
23 | form.multiples = true; | 15 | form.multiples = true; |
24 | form.parse(req, function (err, fields, files) { | 16 | form.parse(req, function (err, fields, files) { |
25 | - if(!files.file[0]){ | 17 | + console.log(files); |
26 | - sourceFiles.push(files.file.name); | 18 | + |
27 | - bodies.push(files.file.path); | 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); | ||
24 | + | ||
25 | + var sourceFiles = []; | ||
26 | + var errFiles = []; | ||
27 | + var targetPath; // 'folder1/folder2' | ||
28 | + var bodies = []; | ||
29 | + var errMessage = 'upload error'; | ||
30 | + | ||
31 | + if (curPath == '/') { | ||
32 | + targetPath = ''; | ||
33 | + } else { | ||
34 | + targetPath = curPath.substring(1, curPath.length-1); | ||
35 | + } | ||
36 | + | ||
37 | + if(!files.file[0]){ // 파일 하나일 때 | ||
38 | + var checkFileName = files.file.name; | ||
39 | + checkFileName = checkFileName.split('(').join(',').split(')').join(',').split(','); | ||
40 | + if (checkFileName.length > 1) { | ||
41 | + errFiles.push(files.file.name); | ||
42 | + errMessage = 'check your file name(\'(\', \')\' x!)' | ||
43 | + }else{ | ||
44 | + sourceFiles.push(files.file.name); | ||
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) { // 파일 여러개일 때 |
30 | - sourceFiles.push(file.name); | 49 | + var checkFileName = file.name; |
31 | - bodies.push(file.path); | 50 | + checkFileName = checkFileName.split('(').join(',').split(')').join(',').split(','); |
51 | + if (checkFileName.length > 1){ | ||
52 | + errFiles.push(file.name); | ||
53 | + } else { | ||
54 | + sourceFiles.push(file.name); | ||
55 | + bodies.push(file.path); | ||
56 | + } | ||
32 | } | 57 | } |
33 | } | 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) { // 에러 파일이 있는 경우 | 62 | + for (var sourceFile of sourceFiles) { |
38 | - for (var sourceFile of sourceFiles) { | 63 | + if (!(sourceFile in errFiles)) { |
39 | - if (!(sourceFile in errFiles)) { | 64 | + noErrSourceFiles.push(sourceFile); |
40 | - newSourceFiles.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){ |
77 | + res.send({err: errMessage, | ||
78 | + errFiles: errFiles}) | ||
79 | + } else { | ||
80 | + res.send({message: 'Upload Success', | ||
81 | + finalFiles: sourceFiles}); | ||
82 | + } | ||
53 | }) | 83 | }) |
54 | }) | 84 | }) |
55 | }) | 85 | }) |
56 | 86 | ||
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 { | ||
80 | - console.log("Upload Fail: Check FIle Duplication"); | ||
81 | - } | ||
82 | - }) | ||
83 | - } | ||
84 | - }); | ||
85 | - }) | ||
86 | -}); | ||
87 | -*/ | ||
88 | 87 | ||
89 | 88 | ||
90 | module.exports = router; | 89 | module.exports = router; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -29,17 +29,26 @@ router.get('/show', function(req, res, next) { | ... | @@ -29,17 +29,26 @@ router.get('/show', function(req, res, next) { |
29 | parentPath += '/'; | 29 | parentPath += '/'; |
30 | } | 30 | } |
31 | } | 31 | } |
32 | + folders = {} | ||
32 | let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;'; | 33 | let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;'; |
33 | connection.query(checkfolder, [curPath, user_id], function(err, rows, fields) { | 34 | connection.query(checkfolder, [curPath, user_id], function(err, rows, fields) { |
34 | if (err) { | 35 | if (err) { |
35 | console.log('select error'); | 36 | console.log('select error'); |
36 | res.status(404).send() | 37 | res.status(404).send() |
37 | } else { | 38 | } else { |
38 | - res.status(200).send({ | 39 | + if (rows.length != 0) { |
39 | - folders: rows, | 40 | + res.status(200).send({ |
40 | - cur: curPath, | 41 | + folders: rows, |
41 | - parentPath: parentPath | 42 | + cur: curPath, |
42 | - }) | 43 | + parentPath: parentPath |
44 | + }) | ||
45 | + } else { | ||
46 | + res.status(200).send({ | ||
47 | + folders: rows, | ||
48 | + cur: curPath, | ||
49 | + parentPath: parentPath | ||
50 | + }) | ||
51 | + } | ||
43 | } | 52 | } |
44 | }); | 53 | }); |
45 | }); | 54 | }); |
... | @@ -61,37 +70,39 @@ router.post('/makefolder', function(req, res, next) { | ... | @@ -61,37 +70,39 @@ router.post('/makefolder', function(req, res, next) { |
61 | let checksql = 'SELECT * FROM folders WHERE location = ? AND folder_name = ?;'; | 70 | let checksql = 'SELECT * FROM folders WHERE location = ? AND folder_name = ?;'; |
62 | console.log(req.body) | 71 | console.log(req.body) |
63 | connection.query(checksql, [cur, folder_name], function(err, rows, fields) { | 72 | connection.query(checksql, [cur, folder_name], function(err, rows, fields) { |
64 | - if (err) { | 73 | + if (rows.length == 0) { |
74 | + s3.putObject(params, function(err, data) { | ||
75 | + if (err) { | ||
76 | + console.log('s3 error'); | ||
77 | + throw err; | ||
78 | + } else { | ||
79 | + console.log(data); | ||
80 | + } | ||
81 | + }); | ||
82 | + let sql = 'INSERT INTO folders (folder_name,location,user_id,created) values (?,?,?,?);'; | ||
83 | + let values = [folder_name, cur, user_id, date]; | ||
84 | + connection.query(sql, values, function(err, result, field) { | ||
85 | + if (err) { | ||
86 | + console.log('insert error'); | ||
87 | + throw err; | ||
88 | + } else { | ||
89 | + folders = {} | ||
90 | + let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;'; | ||
91 | + connection.query(checkfolder, [cur, user_id], function(err, rows, fields) { | ||
92 | + if (rows.length != 0) { | ||
93 | + res.status(200).send({ | ||
94 | + folders: rows | ||
95 | + }) | ||
96 | + } else { | ||
97 | + res.send({ error: "Does not exist" }); | ||
98 | + } | ||
99 | + }); | ||
100 | + } | ||
101 | + }); | ||
102 | + | ||
65 | 103 | ||
66 | } else { | 104 | } else { |
67 | - if (rows.length == 0) { | 105 | + res.status(404).send({ error: "same name error" }); |
68 | - s3.putObject(params, function(err, data) { | ||
69 | - if (err) { | ||
70 | - console.log('s3 error'); | ||
71 | - throw err; | ||
72 | - } else { | ||
73 | - console.log(data); | ||
74 | - let sql = 'INSERT INTO folders (folder_name,location,user_id,created) values (?,?,?,?);'; | ||
75 | - let values = [folder_name, cur, user_id, date]; | ||
76 | - connection.query(sql, values, function(err, result, field) { | ||
77 | - if (err) { | ||
78 | - console.log('insert error'); | ||
79 | - throw err; | ||
80 | - } else { | ||
81 | - let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;'; | ||
82 | - connection.query(checkfolder, [cur, user_id], function(err, rows, fields) { | ||
83 | - console.log(rows); | ||
84 | - res.status(200).send({ | ||
85 | - folders: rows | ||
86 | - }) | ||
87 | - }); | ||
88 | - } | ||
89 | - }); | ||
90 | - } | ||
91 | - }); | ||
92 | - } else { | ||
93 | - res.status(404).send({ error: "same name error" }); | ||
94 | - } | ||
95 | } | 106 | } |
96 | }); | 107 | }); |
97 | }); | 108 | }); |
... | @@ -99,154 +110,148 @@ router.post('/makefolder', function(req, res, next) { | ... | @@ -99,154 +110,148 @@ router.post('/makefolder', function(req, res, next) { |
99 | 110 | ||
100 | router.post('/delfolder', function(req, res, next) { | 111 | router.post('/delfolder', function(req, res, next) { |
101 | 112 | ||
102 | - user_id = req.body.id; | 113 | + user_id = req.body.user_id; |
103 | - let cur = req.body.cur; | 114 | + curPath = user_id + req.body.cur; |
104 | - curPath = user_id + cur; | ||
105 | let folder_name = req.body.folder_name; | 115 | let folder_name = req.body.folder_name; |
106 | let params = { | 116 | let params = { |
107 | - Bucket: BUCKET_NAME, | 117 | + Bucket: BUCKET_NAME + curPath, |
108 | - Key: curPath + folder_name + '/' | 118 | + Key: folder_name + '/' |
109 | }; | 119 | }; |
110 | - let checksql = 'SELECT * FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;'; | 120 | + let checksql = 'SELECT * FROM folders WHERE location = ? AND folder_name = ?;'; |
111 | - let values = [cur, folder_name, user_id]; | 121 | + let values = [curPath, folder_name]; |
122 | + | ||
112 | connection.query(checksql, values, function(err, rows, fields) { | 123 | connection.query(checksql, values, function(err, rows, fields) { |
113 | - if (err) { | 124 | + if (rows.length != 0) { |
114 | - res.status(404).send({ error: "error" }); | 125 | + s3.deleteObject(params, function(err, data) { |
115 | - } else { | 126 | + if (err) { |
116 | - if (rows.length != 0) { | 127 | + //throw err; |
117 | - s3.deleteObject(params, function(err, data) { | 128 | + } else { |
118 | - if (err) { | 129 | + let sql = 'DELETE FROM folders WHERE location = ? AND folder_name = ?;'; |
119 | - console.log('s3 error'); | 130 | + connection.query(sql, values, function(err, result, field) { |
120 | - //throw err; | 131 | + if (err) { |
121 | - } else { | 132 | + //throw err; |
122 | - console.log(data); | 133 | + } else { |
123 | - let sql = 'DELETE FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;'; | 134 | + folders = {} |
124 | - connection.query(sql, values, function(err, result, field) { | 135 | + let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;'; |
125 | - if (err) { | 136 | + connection.query(checkfolder, [cur, user_id], function(err, rows, fields) { |
126 | - //throw err; | 137 | + if (rows.length != 0) { |
127 | - } else { | ||
128 | - console.log(cur); | ||
129 | - console.log(user_id); | ||
130 | - let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;'; | ||
131 | - connection.query(checkfolder, [cur, user_id], function(err, rows, fields) { | ||
132 | res.status(200).send({ | 138 | res.status(200).send({ |
133 | - folders: rows | 139 | + folders: rows, |
140 | + cur: curPath | ||
134 | }) | 141 | }) |
135 | - }); | 142 | + } else { |
143 | + res.send({ error: "Does not exist" }); | ||
144 | + } | ||
145 | + }); | ||
136 | 146 | ||
137 | - } | 147 | + } |
138 | - }); | 148 | + }); |
139 | - } | 149 | + } |
140 | - }); | 150 | + }); |
141 | - } else { | 151 | + |
142 | - console.log(req.body); | 152 | + |
143 | - res.status(304).send({ error: "Does not exist" }); | 153 | + } else { |
144 | - } | 154 | + res.send({ error: "Does not exist" }); |
145 | } | 155 | } |
146 | }); | 156 | }); |
147 | }); | 157 | }); |
148 | 158 | ||
149 | 159 | ||
150 | router.post('/move', function(req, res, next) { | 160 | router.post('/move', function(req, res, next) { |
151 | - console.log(req.body); | 161 | + |
152 | - user_id = req.body.id; | 162 | + user_id = req.body.user_id; |
153 | - let cur = req.body.cur; | 163 | + curPath = user_id + req.body.cur; |
154 | - curPath = user_id + cur; | 164 | + let name = req.body.mfile; |
155 | - let name = req.body.folder_name; | 165 | + let newPath = req.body.newPath; |
156 | - let newPath = user_id + req.body.newPath; | 166 | + let checkfolder = 'SELECT * FROM folders WHERE location = ? AND folder_name = ?;'; |
157 | if (req.body.isfolder) { | 167 | if (req.body.isfolder) { |
158 | - let checkfolder = 'SELECT * FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;'; | 168 | + connection.query(checkfolder, [curPath, name], function(err1, rows, fields) { |
159 | - connection.query(checkfolder, [cur, name, user_id], function(err1, rows, fields) { | ||
160 | - console.log(rows); | ||
161 | if (rows.length != 0) { | 169 | if (rows.length != 0) { |
162 | let copy_params = { | 170 | let copy_params = { |
163 | - Bucket: BUCKET_NAME, | 171 | + Bucket: BUCKET_NAME + curPath, |
164 | - CopySource: BUCKET_NAME + '/' + curPath + name + '/', | 172 | + CopySource: BUCKET_NAME + curPath + file + '/', |
165 | - Key: newPath + name + '/' | 173 | + Key: newPath + file + '/' |
166 | }; | 174 | }; |
175 | + | ||
167 | let del_params = { | 176 | let del_params = { |
168 | - Bucket: BUCKET_NAME, | 177 | + Bucket: BUCKET_NAME + curPath, |
169 | - Key: curPath + name + '/' | 178 | + Key: file + '/' |
170 | }; | 179 | }; |
171 | s3.copyObject(copy_params, function(err, data) { | 180 | s3.copyObject(copy_params, function(err, data) { |
172 | - if (err) { | 181 | + console.log(err, data); |
173 | - console.log(err, data); | 182 | + }); |
174 | - console.log("copy error"); | 183 | + s3.deleteObject(del_params, function(err, data) { |
175 | - res.status(304).send({ error: "copy error" }); | 184 | + console.log(err, data); |
185 | + }); | ||
186 | + let values = [newPath, curPath, name]; | ||
187 | + let updatesql = 'UPDATE folders SET location = ? WHERE location = ? AND folder_name = ?;'; | ||
188 | + connection.query(updatesql, values, function(err3, result, field) { | ||
189 | + if (err3) { | ||
190 | + throw err; | ||
176 | } else { | 191 | } else { |
177 | - s3.deleteObject(del_params, function(err, data) { | 192 | + folders = {} |
178 | - if (err) { | 193 | + connection.query(checkfolder, [cur, user_id], function(err, rows, fields) { |
179 | - console.log(err, data); | 194 | + if (rows.length != 0) { |
180 | - console.log("delete error"); | 195 | + res.status(200).send({ |
181 | - res.status(304).send({ error: "delete error" }); | 196 | + folders: rows, |
197 | + cur: curPath | ||
198 | + }) | ||
182 | } else { | 199 | } else { |
183 | - let values = [req.body.newPath, cur, name, user_id]; | 200 | + res.send({ error: "Does not exist" }); |
184 | - let updatesql = 'UPDATE folders SET location = ? WHERE location = ? AND folder_name = ? AND user_id = ?;'; | ||
185 | - connection.query(updatesql, values, function(err3, result, field) { | ||
186 | - if (err3) { | ||
187 | - console.log("updatesql error"); | ||
188 | - res.status(304).send({ error: "updatesql error" }); | ||
189 | - } else { | ||
190 | - let resultsql = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;'; | ||
191 | - connection.query(resultsql, [cur, user_id], function(err, rows, fields) { | ||
192 | - res.status(200).send({ | ||
193 | - folders: rows | ||
194 | - }); | ||
195 | - }); | ||
196 | - } | ||
197 | - }); | ||
198 | } | 201 | } |
199 | }); | 202 | }); |
200 | } | 203 | } |
201 | }); | 204 | }); |
205 | + | ||
206 | + | ||
202 | } else { | 207 | } else { |
203 | - console.log("Does not exist"); | 208 | + res.send({ error: "Does not exist" }); |
204 | - res.status(304).send({ error: "Does not exist" }); | ||
205 | } | 209 | } |
206 | }); | 210 | }); |
207 | } else { | 211 | } else { |
208 | - let checkfile = 'SELECT * FROM files WHERE location = ? AND file_name = ? AND user_id = ?'; | 212 | + let checkfile = 'SELECT * FROM files WHERE location = ? AND file_name = ?'; |
209 | - connection.query(checkfile, [curPath, name, user_id], function(err1, rows, fields) { | 213 | + |
214 | + connection.query(checkfile, [curPath, name], function(err1, rows, fields) { | ||
210 | if (rows.length != 0) { | 215 | if (rows.length != 0) { |
211 | let copy_params = { | 216 | let copy_params = { |
212 | - Bucket: BUCKET_NAME, | 217 | + Bucket: BUCKET_NAME + curPath, |
213 | - CopySource: BUCKET_NAME + '/' + curPath + file, | 218 | + CopySource: BUCKET_NAME + curPath + file, |
214 | Key: newPath + file | 219 | Key: newPath + file |
215 | }; | 220 | }; |
221 | + | ||
216 | let del_params = { | 222 | let del_params = { |
217 | - Bucket: BUCKET_NAME, | 223 | + Bucket: BUCKET_NAME + curPath, |
218 | - Key: curPath + file | 224 | + Key: file |
219 | }; | 225 | }; |
220 | s3.copyObject(copy_params, function(err, data) { | 226 | s3.copyObject(copy_params, function(err, data) { |
221 | - if (err) { | 227 | + console.log(err, data); |
222 | - console.log(err, data); | 228 | + }); |
223 | - res.status(304).send({ error: "copy error" }); | 229 | + s3.deleteObject(del_params, function(err, data) { |
230 | + console.log(err, data); | ||
231 | + }); | ||
232 | + let values = [newPath, curPath, name]; | ||
233 | + let updatesql = 'UPDATE files SET location = ? WHERE location = ? AND file_name = ?;'; | ||
234 | + connection.query(updatesql, values, function(err3, result, field) { | ||
235 | + if (err3) { | ||
236 | + throw err; | ||
224 | } else { | 237 | } else { |
225 | - s3.deleteObject(del_params, function(err, data) { | 238 | + folders = {} |
226 | - if (err) { | 239 | + connection.query(checkfolder, [cur, user_id], function(err, rows, fields) { |
227 | - console.log(err, data); | 240 | + if (rows.length != 0) { |
228 | - res.status(304).send({ error: "delete error" }); | 241 | + res.status(200).send({ |
242 | + folders: rows, | ||
243 | + cur: curPath | ||
244 | + }) | ||
229 | } else { | 245 | } else { |
230 | - let values = [newPath, cur, name, user_id]; | 246 | + res.send({ error: "Does not exist" }); |
231 | - let updatesql = 'UPDATE files SET location = ? WHERE location = ? AND file_name = ? AND user_id = ?;'; | ||
232 | - connection.query(updatesql, values, function(err3, result, field) { | ||
233 | - if (err3) { | ||
234 | - res.status(304).send({ error: "updatesql error" }); | ||
235 | - } else { | ||
236 | - let resultsql = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;'; | ||
237 | - connection.query(resultsql, [cur, user_id], function(err, rows, fields) { | ||
238 | - res.status(200).send({ | ||
239 | - folders: rows | ||
240 | - }); | ||
241 | - }); | ||
242 | - } | ||
243 | - }); | ||
244 | } | 247 | } |
245 | }); | 248 | }); |
246 | } | 249 | } |
247 | }); | 250 | }); |
251 | + | ||
252 | + | ||
248 | } else { | 253 | } else { |
249 | - res.status(304).send({ error: "Does not exist" }); | 254 | + res.send({ error: "Does not exist" }); |
250 | } | 255 | } |
251 | }); | 256 | }); |
252 | 257 | ... | ... |
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 | ... | ... |
-
Please register or login to post a comment