leeseohyeon

파일 수정, 빠른 액세스

...@@ -12,6 +12,7 @@ var registerRouter = require('./routes/userlogin/register'); ...@@ -12,6 +12,7 @@ var registerRouter = require('./routes/userlogin/register');
12 var fileRouter = require('./routes/file/router'); 12 var fileRouter = require('./routes/file/router');
13 //var trashcanRouter = require('./routes/trashcan/router'); 13 //var trashcanRouter = require('./routes/trashcan/router');
14 var folderRouter = require('./routes/folders'); 14 var folderRouter = require('./routes/folders');
15 +var quickRouter = require('./routes/quick');
15 16
16 17
17 var passport = require('passport'); 18 var passport = require('passport');
...@@ -61,7 +62,7 @@ app.use('/api/RegistUser', registerRouter); ...@@ -61,7 +62,7 @@ app.use('/api/RegistUser', registerRouter);
61 app.use('/api/folder', folderRouter); 62 app.use('/api/folder', folderRouter);
62 //app.use('/users', usersRouter); 63 //app.use('/users', usersRouter);
63 app.use('/api/file', fileRouter); 64 app.use('/api/file', fileRouter);
64 -//app.use('/api/trashcan', trashcanRouter); 65 +app.use('/api/quick', quickRouter);
65 66
66 // catch 404 and forward to error handler 67 // catch 404 and forward to error handler
67 app.use(function(req, res, next) { 68 app.use(function(req, res, next) {
......
...@@ -35,13 +35,20 @@ router.get('/:name', function (req, res) { ...@@ -35,13 +35,20 @@ router.get('/:name', function (req, res) {
35 var extension = targetFile.split('.')[1].toLowerCase(); 35 var extension = targetFile.split('.')[1].toLowerCase();
36 36
37 37
38 - S3.downloadFile3(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir){ 38 + S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir){
39 if (result){ 39 if (result){
40 - if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') { 40 + var content;
41 - res.status(200).send({ type: 'image', src: downloadDir}) 41 + content = fs.readFileSync(downloadDir, 'utf8');
42 - }else{ 42 + res.send({file_name: file_name, content: content});
43 - res.status(200).send({type: 'text', src: downloadDir}) 43 +
44 - } 44 + //if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') {
45 + // res.send({ type: 'image', src: downloadDir})
46 + //}else{
47 + // var content;
48 + // content = fs.readFileSync(downloadDir, 'utf8');
49 + // res.send({file_name: file_name, content: content});
50 + // //res.status(200).send({type: 'text', src: downloadDir})
51 + //}
45 } 52 }
46 }) 53 })
47 }); 54 });
......
1 +var express = require('express');
2 +var router = express.Router();
3 +
4 +var AWS = require('aws-sdk');
5 +AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
6 +var S3 = require('../modules/s3/s3');
7 +
8 +var fs = require('fs');
9 +var moment = require('moment');
10 +
11 +
12 +// /file/modify/:name
13 +router.post('/:name', function(req, res){
14 + var user_id = req.body.user_id;
15 + var curPath = req.body.cur; // /folder1/folder2/
16 + var file_name = req.params.name;
17 + var modified_content = req.body.content;
18 +
19 + var targetPath;
20 + if (curPath == '/') {
21 + targetPath = '';
22 + } else {
23 + targetPath = curPath.substring(1, curPaht.length-1); // folder1/folder2
24 + }
25 +
26 + var tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + curPath + file_name;
27 + fs.writeFileSync(tempDownloadDir, modified_content);
28 +
29 + S3.coverFile(S3.BUCKET_NAME, user_id, file_name, targetPath, tempDownloadDir, function(result){
30 + fs.unlink(tempDownloadDir, function(err){
31 + if (result){
32 + var sql = 'UPDATE files SET updated=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)';
33 + connection.query(sql, [moment().format(), user_id, curPath, file_name], function(err){
34 + if (err){
35 + console.log(err);
36 + res.send({error: 'update error'});
37 + }else{
38 + res.send('modify file success');
39 + }
40 + })
41 + }else{
42 + res.send({error: 'modify file failed'});
43 + }
44 + })
45 + })
46 +});
47 +
48 +
49 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
...@@ -6,12 +6,14 @@ var files = require('./files'); ...@@ -6,12 +6,14 @@ var files = require('./files');
6 var uploadFile = require('./uploadFile'); 6 var uploadFile = require('./uploadFile');
7 var downloadFile = require('./downloadFile'); 7 var downloadFile = require('./downloadFile');
8 var deleteFile = require('./deleteFile'); 8 var deleteFile = require('./deleteFile');
9 +var modifyFile = require('./modifyFile');
9 10
10 11
11 router.use('/', files); 12 router.use('/', files);
12 router.use('/upload', uploadFile); 13 router.use('/upload', uploadFile);
13 router.use('/download', downloadFile); 14 router.use('/download', downloadFile);
14 router.use('/delete', deleteFile); 15 router.use('/delete', deleteFile);
16 +router.use('/modify', modifyFile);
15 17
16 18
17 module.exports = router; 19 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -43,6 +43,7 @@ router.post('/', function (req, res) { ...@@ -43,6 +43,7 @@ router.post('/', function (req, res) {
43 }else{ 43 }else{
44 sourceFiles.push(files.file.name); 44 sourceFiles.push(files.file.name);
45 bodies.push(files.file.path); 45 bodies.push(files.file.path);
46 + console.log('bodies', bodies);
46 } 47 }
47 }else{ 48 }else{
48 for (var file of files.file) { // 파일 여러개일 때 49 for (var file of files.file) { // 파일 여러개일 때
......
1 { 1 {
2 - "accessKeyId": "ASIAZQ5XTMMFRYMR3VRB", 2 + "accessKeyId": "ASIAZQ5XTMMFTERCRTOO",
3 - "secretAccessKey": "XG8dCozEdASwBXDpdDLoXHXRzXwdaUoZ7Iclpnn7", 3 + "secretAccessKey": "4D60bmLZb2IZje42JQp0UjYW9Y/RBVRR94ivjNrc",
4 - "sessionToken": "FwoGZXIvYXdzENf//////////wEaDI+1QF+djlC4HKhgXSLDASLQ79zowPQb14gxxDVD3Due4qV58b2E7S7b2fus/4oOaF3B1+EnZ5ofHKEL3vBnlmzg7Oo37VlbzWh0Ik3QS6XRLg5tf7JxZqZyHhGx0/1k7f6BCZZuoVSBgyEBvGs4VJDZkmWNXv+MT9ytHyl/L1hhuiwqEWFoHJKMwO5jS9QieGJtUl9BP4YKrA1WamiqPZnIkb9TbbNk47Ud8bady5TBO/3u0bBBGL4wkGtAGHPeGSKNy6Ea6gIb9lzpagDN8AKCnSjd5s72BTItHPfO4igQfm5Bl277gKZLytgfzWe4iSPHh4BjmaNVK61zP0zS5wGfD7mcE+X6", 4 + "sessionToken": "FwoGZXIvYXdzEEkaDMTw/lUiPUptDH7EBCLDAdFVXAZkmKCB5Fj4jYanKhUHH0iOqXumrbibz4EGubeO5HKz6KCN+EBvJwBbjDpagzkTYdVQHZa8sZAdEX6+41Qyv6bHM0MpEaHZIpt/ku87++qwk47NNlFJn5/iTnif3GwTKf69F2FTh1Y/+qjaP/URN8di0LZAyRonD4UuXwpmZIykWLhj7VCxpyYXBaiKBuE5eRrMQANddl3BROtS5FttRz+GsYXlalrR87UdXAoDFRJhyCpggxZCRe7+b9k3Tge7YiiX5uf2BTItnh1CIWbGHwx0sYdBpnLCZpxg31RU95Le7pW/DBJIYzxoDjjhVbz/lF0WtwIi",
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
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
13 // targetFile: folder2/file.txt 13 // targetFile: folder2/file.txt
14 14
15 15
16 -
17 var AWS = require('aws-sdk'); 16 var AWS = require('aws-sdk');
18 AWS.config.update({ region: 'ap-northeast-2' }); 17 AWS.config.update({ region: 'ap-northeast-2' });
19 18
...@@ -73,6 +72,32 @@ var S3 = { ...@@ -73,6 +72,32 @@ var S3 = {
73 }) 72 })
74 }, 73 },
75 74
75 + // 파일 덮어쓰기
76 + coverFile: function(bucketName, userId, sourceFile, targetPath, body, callback) {
77 + var pathbody = fs.createReadStream(body);
78 +
79 + if (targetPath != '' && targetPath[targetPath.length - 1] != '/') {
80 + targetPath = targetPath + '/';
81 + }
82 + var targetFile = targetPath + sourceFile;
83 +
84 + var coverParams = {
85 + Bucket: bucketName,
86 + Key: 'drive/' + userId + '/' + targetFile,
87 + Body: pathbody
88 + };
89 +
90 + s3.upload(coverParams, function (err, data) {
91 + if (err) {
92 + console.log("Upload Error" + err);
93 + callback(false);
94 + } else {
95 + console.log("Upload Success");
96 + callback(true);
97 + }
98 + })
99 + },
100 +
76 deleteFile: function (bucketName, userId, targetFile, callback) { 101 deleteFile: function (bucketName, userId, targetFile, callback) {
77 // targetFile => folder1/folder2/test.txt 102 // targetFile => folder1/folder2/test.txt
78 var deleteParams = { 103 var deleteParams = {
...@@ -104,10 +129,8 @@ var S3 = { ...@@ -104,10 +129,8 @@ var S3 = {
104 console.log("Download File Error", err); 129 console.log("Download File Error", err);
105 callback(false); 130 callback(false);
106 } else { 131 } else {
107 - if (data) {
108 console.log("Get File Success"); 132 console.log("Get File Success");
109 - callback(true, data.body.toString()); 133 + callback(true, data.Body.toString());
110 - }
111 } 134 }
112 }) 135 })
113 }, 136 },
...@@ -119,9 +142,11 @@ var S3 = { ...@@ -119,9 +142,11 @@ var S3 = {
119 if (result) { 142 if (result) {
120 makeFolder(tempDownloadDir, function(result){ 143 makeFolder(tempDownloadDir, function(result){
121 if (result) { 144 if (result) {
145 + if (data){
122 fs.writeFileSync(tempDownloadDir, data); 146 fs.writeFileSync(tempDownloadDir, data);
123 callback(true, tempDownloadDir); 147 callback(true, tempDownloadDir);
124 } 148 }
149 + }
125 }) 150 })
126 }else{ 151 }else{
127 console.log('Download File Error'); 152 console.log('Download File Error');
...@@ -143,10 +168,13 @@ var S3 = { ...@@ -143,10 +168,13 @@ var S3 = {
143 Key: 'drive/' + userId + '/' + targetFile 168 Key: 'drive/' + userId + '/' + targetFile
144 }; 169 };
145 try { 170 try {
146 - s3.getObject(params).createReadStream().pipe(file); 171 + var stream = s3.getObject(params).createReadStream().pipe(file);
172 + stream.on('end', function(){
173 + console.log('end!');
147 callback(true, tempDownloadDir); 174 callback(true, tempDownloadDir);
175 + });
148 }catch(err){ 176 }catch(err){
149 - console.log('no such file'); 177 + console.log('no such file', err);
150 callback(false); 178 callback(false);
151 } 179 }
152 }else{ 180 }else{
......
1 +var express = require('express');
2 +var router = express.Router();
3 +
4 +
5 +router.get('/', function(req, res){
6 + var user_id = req.query.id;
7 +
8 + var sql = 'SELECT * FROM files WHERE updated>date_sub(now(), interval 3 day) AND user_id=(?) ORDER BY updated DESC';
9 + connection.query(sql, user_id, function(err, result){
10 + if (err){
11 + console.log('select error');
12 + res.send({error: 'quick error'})
13 + }else{
14 + res.send(result);
15 + }
16 + })
17 +})
18 +
19 +
20 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
...@@ -2,34 +2,10 @@ ...@@ -2,34 +2,10 @@
2 "requires": true, 2 "requires": true,
3 "lockfileVersion": 1, 3 "lockfileVersion": 1,
4 "dependencies": { 4 "dependencies": {
5 - "axios": { 5 + "readline": {
6 - "version": "0.19.2", 6 + "version": "1.3.0",
7 - "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", 7 + "resolved": "https://registry.npmjs.org/readline/-/readline-1.3.0.tgz",
8 - "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", 8 + "integrity": "sha1-xYDXfvLPyHUrEySYBg3JeTp6wBw="
9 - "requires": {
10 - "follow-redirects": "1.5.10"
11 - }
12 - },
13 - "debug": {
14 - "version": "3.1.0",
15 - "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
16 - "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
17 - "requires": {
18 - "ms": "2.0.0"
19 - }
20 - },
21 - "follow-redirects": {
22 - "version": "1.5.10",
23 - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
24 - "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
25 - "requires": {
26 - "debug": "=3.1.0"
27 - }
28 - },
29 - "ms": {
30 - "version": "2.0.0",
31 - "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
32 - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
33 } 9 }
34 } 10 }
35 } 11 }
......