leeseohyeon

파일 이름 수정

......@@ -113,4 +113,6 @@ dist
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
\ No newline at end of file
.pnp.*
backend/routes/modules/s3/download/
\ No newline at end of file
......
......@@ -9,11 +9,13 @@ 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;
......@@ -23,25 +25,43 @@ router.post('/:name', function(req, res){
targetPath = curPath.substring(1, curPaht.length-1); // folder1/folder2
}
var tempDownloadDir = __dirname + '/../modules/s3/download/' + user_id + curPath + file_name;
fs.writeFileSync(tempDownloadDir, modified_content);
S3.coverFile(S3.BUCKET_NAME, user_id, file_name, targetPath, tempDownloadDir, function(result){
fs.unlink(tempDownloadDir, function(err){
if (result){
var sql = 'UPDATE files SET updated=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)';
connection.query(sql, [moment().format(), user_id, curPath, file_name], function(err){
if (err){
console.log(err);
res.send({error: 'update error'});
}else{
res.send('modify file success');
}
})
}else{
res.send({error: 'modify file failed'});
}
})
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=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)';
connection.query(sql, [modified_name, 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'});
}
})
});
......
{
"accessKeyId": "ASIAZQ5XTMMFTERCRTOO",
"secretAccessKey": "4D60bmLZb2IZje42JQp0UjYW9Y/RBVRR94ivjNrc",
"sessionToken": "FwoGZXIvYXdzEEkaDMTw/lUiPUptDH7EBCLDAdFVXAZkmKCB5Fj4jYanKhUHH0iOqXumrbibz4EGubeO5HKz6KCN+EBvJwBbjDpagzkTYdVQHZa8sZAdEX6+41Qyv6bHM0MpEaHZIpt/ku87++qwk47NNlFJn5/iTnif3GwTKf69F2FTh1Y/+qjaP/URN8di0LZAyRonD4UuXwpmZIykWLhj7VCxpyYXBaiKBuE5eRrMQANddl3BROtS5FttRz+GsYXlalrR87UdXAoDFRJhyCpggxZCRe7+b9k3Tge7YiiX5uf2BTItnh1CIWbGHwx0sYdBpnLCZpxg31RU95Le7pW/DBJIYzxoDjjhVbz/lF0WtwIi",
"accessKeyId": "ASIAZQ5XTMMFUHLZZ4M2",
"secretAccessKey": "0et6RCcKOQtE9uZLw4c4WTBkvoLhnfyF/gu+Izi+",
"sessionToken": "FwoGZXIvYXdzEE8aDC4plMfgaXo81Tt/QyLDASYTNLXjU0qWi+IAmgwl5sAMu6DPqCjeEWO4W22bSYhD178WdC/y8uz35EGd/pf5aRORKj71TiR6CsoxcEO8lW4VsN/Fdq8PfHbb5zuVqkILOtsBoAdR2/XvG3wHTqvx7LHMrFpUsgTjQkXAws7IiKV6siQrTS/p5lSmV8HZsekDktvnI+E+DkQrcvWy/BoGDJxXlpGsGZzIers2MjqzmgszC0CjTglw7aMKWriwIyhkUjo9ewIzIalCQUzk3Lr8mshmfCjOjOn2BTIt3OSehGo0BMhqYmik1RkykXo2bOqIWeZWS2lmb7Oa8GSqOcLDI4NRKgkLZ+1q",
"region": "us-east-1"
}
......
modify file
\ No newline at end of file
......@@ -89,10 +89,10 @@ var S3 = {
s3.upload(coverParams, function (err, data) {
if (err) {
console.log("Upload Error" + err);
console.log("Cover Error" + err);
callback(false);
} else {
console.log("Upload Success");
console.log("Cover Success");
callback(true);
}
})
......@@ -305,6 +305,75 @@ var S3 = {
})
},
// 파일 이름이 달라지는 경우
moveFile3: function (bucketName, userId, oldFile, newFile, targetPath, callback) {
if (targetPath != ''){
targetPath = targetPath + '/';
}
sourceFile = targetPath + oldFile; // test.txt or folder1/test.txt
var targetFile = targetPath + newFile;
S3.copyFile2(bucketName, userId, sourceFile, targetFile, function(result){
if (!result){
console.log("Move Error on Copying File3");
callback(false);
}else{
S3.deleteFile(bucketName, userId, sourceFile, function (res) {
if (!res) {
console.log("Move Error on Deleting File");
callback(false);
} else {
console.log("Move Success");
callback(true);
}
})
}
})
},
renameFile: function(bucketName, userId, sourceFile, modiFile, targetPath, callback){
// sourceFile = test.txt
// modiFile = test2.txt
// targetPath = '' or 'folder1/folder2'
if (sourceFile == modiFile){ // 이름 변경되지 않은 경우
callback(true, sourceFile);
}else{
if (targetPath != '') {
targetPath = targetpath + '/';
}
var targetFile = targetPath + modiFile;
S3.isFileOverlapped(bucketName, userId, targetFile, function (res, ans, lvNum) {
if (!res) {
console.log("Overlap Check failed");
callback(false, sourceFile);
} else {
if (ans) {
console.log("File Duplication");
S3.makeVersion(bucketName, userId, targetFile, lvNum + 1, function (res, versionedSourceFile) {
if (!res) {
console.log("Make version failed");
callback(false, sourceFile);
} else {
S3.renameFile(bucketName, userId, sourceFile, versionedSourceFile, targetPath, callback);
}
})
} else {
S3.moveFile3(bucketName, userId, sourceFile, modiFile, targetPath, function (result) {
if (result) {
callback(true, modiFile);
} else {
console.log('File Rename failed');
callback(false);
}
})
}
}
});
}
},
uploadFile: function (bucketName, userId, sourceFile, targetPath, body, callback) {
var pathbody = fs.createReadStream(body);
......