박기범

Merge branch 'new_file'

This diff is collapsed. Click to expand it.
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
19 "morgan": "~1.9.1", 19 "morgan": "~1.9.1",
20 "mysql": "^2.18.1", 20 "mysql": "^2.18.1",
21 "passport": "^0.4.1", 21 "passport": "^0.4.1",
22 - "pug": "2.0.0-beta11", 22 + "pug": "^3.0.0",
23 "request": "^2.88.2" 23 "request": "^2.88.2"
24 } 24 }
25 } 25 }
......
...@@ -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.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.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.send("move to trashcan success");
41 - res.send("Upload Success");
42 } 37 }
43 }) 38 })
44 } 39 }
......
...@@ -8,23 +8,68 @@ AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); ...@@ -8,23 +8,68 @@ 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 -// /file/download/:name 11 +
12 router.get('/:name', function (req, res) { 12 router.get('/:name', function (req, res) {
13 var file_name = req.params.name; 13 var file_name = req.params.name;
14 - var user_id = req.session.user_id; 14 + var curPath = req.query.cur; // /folder1/folder2/
15 + var user_id = req.query.id;
16 +
17 + var targetFile = curPath.substring(1) + file_name; // folder1/folder2/test.txt
18 +
19 +
20 + S3.downloadFile3(S3.BUCKET_NAME, user_id, targetFile, function (result, downloadDir) {
21 + if (result) {
22 + res.send({ src: downloadDir })
23 + }else{
24 + res.send({ err: 'download error'})
25 + }
26 + })
27 +});
28 +
29 +/*
30 +router.get('/:name', function (req, res, next) {
31 + var file_name = req.params.name; // test.txt
32 + var user_id = req.query.id;
33 + var curPath = req.query.cur; // /folder1/folder2/
34 +
35 + var targetFile = (curPath + file_name).substring(1); // folder1/folder2/test.txt
15 36
16 - var sourceFile = file_name; 37 + var s3 = new AWS.S3();
17 - var tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + '/' + file_name; 38 + var params = {
39 + Bucket: S3.BUCKET_NAME,
40 + Key: 'drive/' + user_id + '/' + targetFile,
41 + };
18 42
19 - S3.downloadFile(S3.BUCKET_NAME, user_id, sourceFile, function (result, data) { 43 + res.attachment(file_name);
20 - !fs.existsSync(tempDownloadDir + '/../') && fs.mkdirSync(tempDownloadDir + '/../'); 44 + var fileStream = s3.getObject(params).createReadStream();
21 - fs.writeFileSync(tempDownloadDir, data); 45 + fileStream.pipe(res);
22 - res.download(tempDownloadDir, function (err) { 46 +});
23 - fs.unlink(tempDownloadDir, function (err) { 47 +*/
24 - console.log("Download Success"); 48 +
49 +/*
50 +// /file/download/:name
51 +router.get('/:name', function (req, res) {
52 + var file_name = req.params.name; // test.txt
53 + var user_id = req.query.id;
54 + var curPath = req.query.cur; // /folder1/folder2/
55 +
56 + var targetFile = (curPath + file_name).substring(1); // folder1/folder2/test.txt
57 +
58 + S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function (result, downloadDir) {
59 + if (result){
60 + res.download(downloadDir, function (err) {
61 + if (err){
62 + console.log(err);
63 + }else{
64 + console.log('download success');
65 + }
25 }); 66 });
26 - }); 67 + }else{
68 + res.send({error: 'download error'});
69 + }
27 }); 70 });
28 }); 71 });
72 +*/
73 +
29 74
30 module.exports = router; 75 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -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
......
1 { 1 {
2 - "accessKeyId": "ASIAZQ5XTMMFRBJO2LG5", 2 + "accessKeyId": "ASIAXZL2SWFEYG2GUYVJ",
3 - "secretAccessKey": "ltsYvzKAoQ5UnNWEk13Zf8n4wJdkQkemsJ7GdsbS", 3 + "secretAccessKey": "ceYvfLs25iiBDfigO7PbP67kJoKswJdfduqROrJt",
4 - "sessionToken": "FwoGZXIvYXdzEI///////////wEaDLleFCvWDhdhFpXCvCLDAX/Hka6vXTZKxdyEBUh7ZSxPYLH184u1Tpo2qWHYFziUVKWJSCm5wqPNv0QWoAp8sL5NB0W5kty8hoeKv16SYB9Z+yzT1qtfuKTmrro2jGSo7AnxDSBla6UQHAv82yVetiGwu+IKhm6tHUvdNSlkIV0Qi9rNrDc9eynhFN/emFI/6NsocbQ47mmW6BZx0Z4/hHKW9TL6Uec/E87Z+oxVRFsVOGyNtow980bQgZFYJMbh3AN6poTlofP1q/qn2nUWwAdsDCjfhr/2BTItlcDfO5PEH5Gwffzv69JMTVBIemfIpS/Ybx6F6i7GbI2fZKGtGfRSecobbj4m", 4 + "sessionToken": "FwoGZXIvYXdzEDMaDGvezwF9kQrAJ+uhfSLDASWFKBk7UNbRzZVEM1eoGJ3E+UI9d4xSpqEDqRYjsOTn5wYIGM3mUCZ8S6/XCnGP5yFz+qv9mBLDf311iP9ajY091OhQ8c2nB6l+JHuRKAz6FhGLrjHtD1AecHtjHOu5dXODU2+9ctvGj7V60KYqjToah5Tvv84EVac/41Les3rtrqcXS/bbmVwv5kDz9K1FJStln4sXZ/foueGHZAkAJQ6LAtIQWg4/reFUcWAi8E0bUjMckmymDQhnVNWcSbgQ+htBYiiPg+P2BTItOqWuZojM5lw32rTv7+PuOaeyHjx3+Vcd4bo3orSJr57P2ZPfmglZNKY7W5aw",
5 "region": "us-east-1" 5 "region": "us-east-1"
6 -}
...\ No newline at end of file ...\ No newline at end of file
6 +}
......
1 +asdf
...\ No newline at end of file ...\ No newline at end of file
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
17 var AWS = require('aws-sdk'); 17 var AWS = require('aws-sdk');
18 AWS.config.update({ region: 'ap-northeast-2' }); 18 AWS.config.update({ region: 'ap-northeast-2' });
19 19
20 -var BUCKET_NAME = 'dkhuloud'; 20 +var BUCKET_NAME = 'khuloud';
21 21
22 var s3 = new AWS.S3(); 22 var s3 = new AWS.S3();
23 var fs = require('fs'); 23 var fs = require('fs');
...@@ -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,12 +106,55 @@ var S3 = { ...@@ -103,12 +106,55 @@ 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.toString());
107 } 110 }
108 } 111 }
109 }) 112 })
110 }, 113 },
111 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);
151 + }
152 + }else{
153 + callback(false);
154 + }
155 + })
156 + },
157 +
112 getFileList: function (bucketName, userId, targetPath, callback) { 158 getFileList: function (bucketName, userId, targetPath, callback) {
113 var prefix; 159 var prefix;
114 160
...@@ -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
302 -module.exports = S3;
...\ No newline at end of file ...\ No newline at end of file
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 +
367 +module.exports = S3;
......
...@@ -5775,9 +5775,9 @@ ...@@ -5775,9 +5775,9 @@
5775 } 5775 }
5776 }, 5776 },
5777 "http-proxy": { 5777 "http-proxy": {
5778 - "version": "1.18.0", 5778 + "version": "1.18.1",
5779 - "resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.0.tgz", 5779 + "resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz",
5780 - "integrity": "sha512-84I2iJM/n1d4Hdgc6y2+qY5mDaz2PUVjlg9znE9byl+q0uC3DeByqBGReQu5tpLK0TAqTIXScRUV+dg7+bUPpQ==", 5780 + "integrity": "sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==",
5781 "dev": true, 5781 "dev": true,
5782 "requires": { 5782 "requires": {
5783 "eventemitter3": "^4.0.0", 5783 "eventemitter3": "^4.0.0",
......
...@@ -35,6 +35,52 @@ function moveFolder(folderData) { ...@@ -35,6 +35,52 @@ function moveFolder(folderData) {
35 return axios.post('/api/folder/move', folderData); 35 return axios.post('/api/folder/move', folderData);
36 } 36 }
37 37
38 +function file(curData){
39 + return axios.get('/api/file', {
40 + params: {
41 + id: curData.id,
42 + cur: curData.cur
43 + }
44 + });
45 +}
46 +
47 +
48 +function uploadFile(fileData){
49 + return axios.post('/api/file/upload', fileData, {
50 + headers :{
51 + 'Content-Type' : 'multipart/form-data'
52 + }
53 + });
54 +}
55 +
56 +function detailFile(fileData){
57 + return axios.get(`/api/file/${fileData.name}`, {
58 + params:{
59 + id: fileData.id,
60 + cur: fileData.cur
61 + }
62 + })
63 +}
64 +function deleteFile(fileData){
65 + return axios.get(`/api/file/delete/${fileData.fileName}`, {
66 + params:{
67 + //현재 접속한 사람의 id와 삭제할 파일의 이름, 현재 폴더위치를 파라미터로 넘긴다.
68 + id: fileData.user_id,
69 + cur: fileData.cur
70 + }
71 + });
72 +}
73 +function downloadFile(fileData){
74 + return axios.get(`api/file/download/${fileData.fileName}`, {
75 + params:{
76 + //현재 접속한 사람의 id와 다운로드를 위해 선택한 파일의 이름을 파라미터로 넘긴다.
77 + id: fileData.id,
78 + cur: fileData.cur
79 + }
80 + });
81 +}
82 +
38 83
39 84
40 -export { registerUser, loginUser, folder, makeFolder, deleteFolder, moveFolder }; 85 +export { registerUser, loginUser, folder, makeFolder, deleteFolder, moveFolder,
86 + file, uploadFile, deleteFile, downloadFile };
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
29 <v-list-item-title v-text="item.folder_name"></v-list-item-title> 29 <v-list-item-title v-text="item.folder_name"></v-list-item-title>
30 </v-list-item-content> 30 </v-list-item-content>
31 31
32 - 32 +
33 33
34 <v-menu 34 <v-menu
35 v-model="showMenu" 35 v-model="showMenu"
...@@ -94,18 +94,22 @@ ...@@ -94,18 +94,22 @@
94 <v-icon> mdi-file</v-icon> 94 <v-icon> mdi-file</v-icon>
95 </v-list-item-avatar> 95 </v-list-item-avatar>
96 <v-list-item-content> 96 <v-list-item-content>
97 - <v-list-item-title v-text="item"></v-list-item-title> 97 + <v-list-item-title v-text="item.file_name"></v-list-item-title>
98 </v-list-item-content> 98 </v-list-item-content>
99 <v-list-item-action> 99 <v-list-item-action>
100 - <v-btn icon> 100 + <v-btn icon @click="download_file(item.file_name)">
101 - <v-icon color="grey lighten-1">mdi-information</v-icon> 101 + <v-icon color="grey lighten-1">mdi-download</v-icon>
102 </v-btn> 102 </v-btn>
103 - <v-btn icon> 103 + <v-btn icon @click="delete_file(item.file_name)">
104 - <v-icon color="grey lighten-1">mdi-delted</v-icon> 104 + <v-icon color="grey lighten-1">mdi-delete</v-icon>
105 </v-btn> 105 </v-btn>
106 </v-list-item-action> 106 </v-list-item-action>
107 </v-list-item> 107 </v-list-item>
108 </v-list> 108 </v-list>
109 + <input id = "file-selector" ref='uploadedfile' type="file"
110 + v-on:change="handleFileUpload()">
111 + <br>
112 + <!--
109 <v-file-input 113 <v-file-input
110 v-model="files" 114 v-model="files"
111 color="deep-purple accent-4" 115 color="deep-purple accent-4"
...@@ -134,7 +138,10 @@ ...@@ -134,7 +138,10 @@
134 +{{ files.length - 2 }} File(s) 138 +{{ files.length - 2 }} File(s)
135 </span> 139 </span>
136 </template> 140 </template>
141 +
137 </v-file-input> 142 </v-file-input>
143 +-->
144 + <v-btn color="blue" @click = "upload_file">upload</v-btn>
138 <v-btn 145 <v-btn
139 bottom 146 bottom
140 color="blue" 147 color="blue"
...@@ -177,11 +184,12 @@ ...@@ -177,11 +184,12 @@
177 </div> 184 </div>
178 </template> 185 </template>
179 <script> 186 <script>
180 -import { folder, makeFolder, deleteFolder, moveFolder } from '../api/index'; 187 +import { folder, makeFolder, deleteFolder, moveFolder, file, uploadFile, deleteFile, downloadFile } from '../api/index';
181 import Axios from 'axios'; 188 import Axios from 'axios';
182 export default { 189 export default {
183 data() { 190 data() {
184 return { 191 return {
192 + uploadedfile:null,
185 foldername:'', 193 foldername:'',
186 curfName:'', 194 curfName:'',
187 folders: [], 195 folders: [],
...@@ -189,12 +197,18 @@ import Axios from 'axios'; ...@@ -189,12 +197,18 @@ import Axios from 'axios';
189 search:'', 197 search:'',
190 id: '', 198 id: '',
191 dialog:false, 199 dialog:false,
192 - search: '',
193 howMenu: false, 200 howMenu: false,
194 showMenu: false, 201 showMenu: false,
195 x: 0, 202 x: 0,
196 y: 0, 203 y: 0,
197 - dialog2:false 204 + dialog2:false,
205 + detail : {
206 + dataname: null,
207 + date: null,
208 + owner_id:null,
209 + datasize:null,
210 + datatype:null
211 + }
198 } 212 }
199 }, 213 },
200 async created(){ 214 async created(){
...@@ -203,18 +217,28 @@ import Axios from 'axios'; ...@@ -203,18 +217,28 @@ import Axios from 'axios';
203 id : this.$store.state.id, 217 id : this.$store.state.id,
204 cur: '/' 218 cur: '/'
205 } 219 }
220 + console.log(curData);
206 const response = await folder(curData); 221 const response = await folder(curData);
207 - console.log(response.data); 222 + const file_response = await file(curData);
208 this.$store.commit('setFolder', response.data.folders); 223 this.$store.commit('setFolder', response.data.folders);
209 this.$store.commit('setCur', response.data.cur); 224 this.$store.commit('setCur', response.data.cur);
210 this.$store.commit('setParent', response.data.parentPath); 225 this.$store.commit('setParent', response.data.parentPath);
211 this.folders = this.$store.getters.folderL; 226 this.folders = this.$store.getters.folderL;
227 + this.$store.commit('setFile', file_response.data.files);
228 + this.folders = this.$store.getters.folderL;
229 + console.log(this.$store.getters.fileL);
230 + this.files = this.$store.getters.fileL;
231 +
212 } catch (error) { 232 } catch (error) {
213 console.log("에러"); 233 console.log("에러");
214 - console.log(error.response.data); 234 + console.log(error);
215 } 235 }
216 }, 236 },
217 methods: { 237 methods: {
238 + handleFileUpload(){
239 + this.uploadedfile= this.$refs.uploadedfile.files[0]
240 + console.log(this.uploadedfile)
241 + },
218 initFolderName(){ 242 initFolderName(){
219 this.foldername = ''; 243 this.foldername = '';
220 }, 244 },
...@@ -316,6 +340,80 @@ import Axios from 'axios'; ...@@ -316,6 +340,80 @@ import Axios from 'axios';
316 this.dialog2 = false; 340 this.dialog2 = false;
317 } 341 }
318 }, 342 },
343 + async upload_file(){
344 + try{
345 + const formData = new FormData();
346 + formData.append('file', this.uploadedfile);
347 + formData.append('user_id', this.$store.state.id);
348 + formData.append('cur', this.$store.state.cur);
349 + const currentData={
350 + id: this.$store.state.id,
351 + cur: this.$store.state.cur
352 + }
353 + console.log(currentData)
354 + const response = await uploadFile(formData);
355 + const filelist = await file(currentData);
356 + console.log(filelist.data.files);
357 + this.$store.commit('setFile', filelist.data.files);
358 + console.log(this.$store.getters.fileL);
359 + this.files = this.$store.getters.fileL;;
360 +
361 + }catch(error){
362 + console.log("에러");
363 + console.log(error);
364 + }
365 + },
366 + async delete_file(itemName){
367 + try{
368 + var itemlist = this.$store.getters.fileL;
369 + console.log(itemlist)
370 + console.log(itemName)
371 + const currentData={
372 + fileName : null,
373 + user_id: null,
374 + cur: this.$store.state.cur
375 + }
376 +
377 + for(var i=0; i<itemlist.length; i++){
378 + console.log(itemlist[i].file_name)
379 + if(itemlist[i].file_name == itemName){
380 + currentData.fileName = itemlist[i].file_name;
381 + currentData.user_id = itemlist[i].user_id;
382 + }
383 + }
384 + const filelistData={
385 + id: currentData.user_id,
386 + cur: currentData.cur
387 + }
388 + console.log(currentData)
389 + const response = await deleteFile(currentData);
390 + setTimeout(function(){}, 500);
391 + const filelist = await file(filelistData);
392 + console.log(filelist.data.files);
393 + this.$store.commit('setFile', filelist.data.files);
394 + console.log(this.$store.getters.fileL);
395 + this.files = this.$store.getters.fileL;;
396 + }catch(error){
397 + console.log("에러");
398 + console.log(error);
399 + }
400 + },
401 + async download_file(namedata){
402 + try{
403 + const currentData={
404 + fileName : namedata,
405 + id: this.$store.state.id,
406 + cur: this.$store.state.cur
407 + }
408 + const result = await downloadFile(currentData);
409 + console.log(result)
410 +
411 + }catch(error){
412 + console.log("에러");
413 + console.log(error);
414 + }
415 + }
416 + },
319 show (folderN, e) { 417 show (folderN, e) {
320 e.preventDefault() 418 e.preventDefault()
321 this.curfName = folderN; 419 this.curfName = folderN;
...@@ -326,6 +424,6 @@ import Axios from 'axios'; ...@@ -326,6 +424,6 @@ import Axios from 'axios';
326 this.showMenu = true 424 this.showMenu = true
327 }) 425 })
328 } 426 }
329 - } 427 +
330 } 428 }
331 -</script>
...\ No newline at end of file ...\ No newline at end of file
429 +</script>
......