박기범

modify file api

......@@ -2,6 +2,7 @@ var express = require('express');
var router = express.Router();
var fs = require('fs');
var moment = require('moment');
var AWS = require('aws-sdk');
AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
......@@ -19,7 +20,14 @@ router.get('/:name', function (req, res) {
S3.downloadFile3(S3.BUCKET_NAME, user_id, targetFile, function (result, downloadDir) {
if (result) {
res.send({ src: downloadDir })
var sql = 'UPDATE files SET recent_access=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)';
connection.query(sql, [moment().format(), user_id, curPath, file_name], function(err){
if (err){
res.send({err: 'update error'});
}else{
res.send({ src: downloadDir })
}
})
}else{
res.send({ err: 'download error'})
}
......
......@@ -35,13 +35,20 @@ router.get('/:name', function (req, res) {
var extension = targetFile.split('.')[1].toLowerCase();
S3.downloadFile3(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir){
S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir){
if (result){
if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') {
res.status(200).send({ type: 'image', src: downloadDir})
}else{
res.status(200).send({type: 'text', src: downloadDir})
}
var content;
content = fs.readFileSync(downloadDir, 'utf8');
res.send({file_name: file_name, content: content});
//if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') {
// res.send({ type: 'image', src: downloadDir})
//}else{
// var content;
// content = fs.readFileSync(downloadDir, 'utf8');
// res.send({file_name: file_name, content: content});
// //res.status(200).send({type: 'text', src: downloadDir})
//}
}
})
});
......
var express = require('express');
var router = express.Router();
var AWS = require('aws-sdk');
AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
var S3 = require('../modules/s3/s3');
var fs = require('fs');
var moment = require('moment');
// /file/modify/:name
router.post('/:name', function(req, res){
var user_id = req.body.user_id;
var curPath = req.body.cur; // /folder1/folder2/
var file_name = req.params.name;
var modified_name = req.body.name;
var modified_content = req.body.content;
var targetPath;
if (curPath == '/') {
targetPath = '';
} else {
targetPath = curPath.substring(1, curPaht.length-1); // folder1/folder2
}
var originalDir = __dirname + '/../modules/s3/download/' + user_id + curPath + file_name;
var tempDownloadDir;
// 파일 이름 변경
S3.renameFile(S3.BUCKET_NAME, user_id, file_name, modified_name, targetPath, function(result, r_modified_name){
if (result){
modified_name = r_modified_name;
tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + curPath + modified_name;
fs.unlink(originalDir, function(err){
if (err){
console.log(err);
res.send({error: 'original file not exists in server'});
}else{
// 파일 내용 변경
fs.writeFileSync(tempDownloadDir, modified_content);
S3.coverFile(S3.BUCKET_NAME, user_id, modified_name, targetPath, tempDownloadDir, function (result) {
if (result) {
var sql = 'UPDATE files SET file_name=(?), updated=(?), recent_access=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)';
connection.query(sql, [modified_name, moment().format(), moment().format(), user_id, curPath, file_name], function (err) {
if (err) {
console.log(err);
res.send({ error: 'update error' });
} else {
fs.unlinkSync(tempDownloadDir);
res.send('modify file success');
}
})
} else {
res.send({ error: 'modify file failed' });
}
})
}
});
}else{
res.send({error: 'rename error'});
}
})
});
module.exports = router;
\ No newline at end of file
......@@ -6,12 +6,14 @@ var files = require('./files');
var uploadFile = require('./uploadFile');
var downloadFile = require('./downloadFile');
var deleteFile = require('./deleteFile');
var modifyFile = require('./modifyFile');
router.use('/', files);
router.use('/upload', uploadFile);
router.use('/download', downloadFile);
router.use('/delete', deleteFile);
router.use('/modify', modifyFile);
module.exports = router;
\ No newline at end of file
......
......@@ -2,6 +2,7 @@ var express = require('express');
var router = express.Router();
var formidable = require('formidable');
var moment = require('moment');
var AWS = require('aws-sdk');
AWS.config.loadFromPath(__dirname + "/../modules/awsconfig.json");
......@@ -43,6 +44,7 @@ router.post('/', function (req, res) {
}else{
sourceFiles.push(files.file.name);
bodies.push(files.file.path);
console.log('bodies', bodies);
}
}else{
for (var file of files.file) { // 파일 여러개일 때
......@@ -65,8 +67,8 @@ router.post('/', function (req, res) {
}
}
for (var sourceFile of noErrSourceFiles) {
var sql = 'INSERT INTO files (file_name, user_id, location) VALUES (?, ?, ?)';
connection.query(sql, [sourceFile, user_id, curPath], function (err, result) {
var sql = 'INSERT INTO files (file_name, user_id, location, recent_access) VALUES (?, ?, ?, ?)';
connection.query(sql, [sourceFile, user_id, curPath, moment().format()], function (err, result) {
if (err) {
console.log('insert error');
res.send({error: 'insert error'});
......