박기범

modify file api

...@@ -2,6 +2,7 @@ var express = require('express'); ...@@ -2,6 +2,7 @@ var express = require('express');
2 var router = express.Router(); 2 var router = express.Router();
3 3
4 var fs = require('fs'); 4 var fs = require('fs');
5 +var moment = require('moment');
5 6
6 var AWS = require('aws-sdk'); 7 var AWS = require('aws-sdk');
7 AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); 8 AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
...@@ -19,7 +20,14 @@ router.get('/:name', function (req, res) { ...@@ -19,7 +20,14 @@ router.get('/:name', function (req, res) {
19 20
20 S3.downloadFile3(S3.BUCKET_NAME, user_id, targetFile, function (result, downloadDir) { 21 S3.downloadFile3(S3.BUCKET_NAME, user_id, targetFile, function (result, downloadDir) {
21 if (result) { 22 if (result) {
23 + var sql = 'UPDATE files SET recent_access=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)';
24 + connection.query(sql, [moment().format(), user_id, curPath, file_name], function(err){
25 + if (err){
26 + res.send({err: 'update error'});
27 + }else{
22 res.send({ src: downloadDir }) 28 res.send({ src: downloadDir })
29 + }
30 + })
23 }else{ 31 }else{
24 res.send({ err: 'download error'}) 32 res.send({ err: 'download error'})
25 } 33 }
......
...@@ -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 +
13 +// /file/modify/:name
14 +router.post('/:name', function(req, res){
15 + var user_id = req.body.user_id;
16 + var curPath = req.body.cur; // /folder1/folder2/
17 + var file_name = req.params.name;
18 + var modified_name = req.body.name;
19 + var modified_content = req.body.content;
20 +
21 + var targetPath;
22 + if (curPath == '/') {
23 + targetPath = '';
24 + } else {
25 + targetPath = curPath.substring(1, curPaht.length-1); // folder1/folder2
26 + }
27 +
28 + var originalDir = __dirname + '/../modules/s3/download/' + user_id + curPath + file_name;
29 + var tempDownloadDir;
30 +
31 + // 파일 이름 변경
32 + S3.renameFile(S3.BUCKET_NAME, user_id, file_name, modified_name, targetPath, function(result, r_modified_name){
33 + if (result){
34 + modified_name = r_modified_name;
35 + tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + curPath + modified_name;
36 + fs.unlink(originalDir, function(err){
37 + if (err){
38 + console.log(err);
39 + res.send({error: 'original file not exists in server'});
40 + }else{
41 + // 파일 내용 변경
42 + fs.writeFileSync(tempDownloadDir, modified_content);
43 +
44 + S3.coverFile(S3.BUCKET_NAME, user_id, modified_name, targetPath, tempDownloadDir, function (result) {
45 + if (result) {
46 + var sql = 'UPDATE files SET file_name=(?), updated=(?), recent_access=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)';
47 + connection.query(sql, [modified_name, moment().format(), moment().format(), user_id, curPath, file_name], function (err) {
48 + if (err) {
49 + console.log(err);
50 + res.send({ error: 'update error' });
51 + } else {
52 + fs.unlinkSync(tempDownloadDir);
53 + res.send('modify file success');
54 + }
55 + })
56 + } else {
57 + res.send({ error: 'modify file failed' });
58 + }
59 + })
60 + }
61 + });
62 + }else{
63 + res.send({error: 'rename error'});
64 + }
65 + })
66 +});
67 +
68 +
69 +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
......
...@@ -2,6 +2,7 @@ var express = require('express'); ...@@ -2,6 +2,7 @@ var express = require('express');
2 var router = express.Router(); 2 var router = express.Router();
3 3
4 var formidable = require('formidable'); 4 var formidable = require('formidable');
5 +var moment = require('moment');
5 6
6 var AWS = require('aws-sdk'); 7 var AWS = require('aws-sdk');
7 AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json"); 8 AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
...@@ -43,6 +44,7 @@ router.post('/', function (req, res) { ...@@ -43,6 +44,7 @@ router.post('/', function (req, res) {
43 }else{ 44 }else{
44 sourceFiles.push(files.file.name); 45 sourceFiles.push(files.file.name);
45 bodies.push(files.file.path); 46 bodies.push(files.file.path);
47 + console.log('bodies', bodies);
46 } 48 }
47 }else{ 49 }else{
48 for (var file of files.file) { // 파일 여러개일 때 50 for (var file of files.file) { // 파일 여러개일 때
...@@ -65,8 +67,8 @@ router.post('/', function (req, res) { ...@@ -65,8 +67,8 @@ router.post('/', function (req, res) {
65 } 67 }
66 } 68 }
67 for (var sourceFile of noErrSourceFiles) { 69 for (var sourceFile of noErrSourceFiles) {
68 - var sql = 'INSERT INTO files (file_name, user_id, location) VALUES (?, ?, ?)'; 70 + var sql = 'INSERT INTO files (file_name, user_id, location, recent_access) VALUES (?, ?, ?, ?)';
69 - connection.query(sql, [sourceFile, user_id, curPath], function (err, result) { 71 + connection.query(sql, [sourceFile, user_id, curPath, moment().format()], function (err, result) {
70 if (err) { 72 if (err) {
71 console.log('insert error'); 73 console.log('insert error');
72 res.send({error: 'insert error'}); 74 res.send({error: 'insert error'});
......