Hong

휴지통

......@@ -7,7 +7,9 @@ var S3 = require('../modules/s3/s3');
// /file/delete/:name
router.get('/:name', function (req, res) {
router.get('/:name', function(req, res) {
console.log(req.query);
console.log(req.params);
var file_name = req.params.name;
var user_id = req.query.id;
var curPath = req.query.cur;
......@@ -15,32 +17,56 @@ router.get('/:name', function (req, res) {
var targetPath = 'trashcan';
var sourceFile = curPath.substring(1) + file_name;
var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)';
var sql2 = 'INSERT INTO trashcan (trash_name, location, user_Id) VALUES (?, ?, ?)';
connection.query(sql1, [file_name, curPath, user_id], function (err) {
var sql1 = 'UPDATE files SET location = ? WHERE location = ? AND file_name = ? AND user_id = ?;';
connection.query(sql1, ['/trashcan/', curPath, file_name, user_id], function(err) {
if (err) {
console.log('delete db error');
res.send({erorr: 'db delete error'});
}
else {
connection.query(sql2, [file_name, '/trashcan'+curPath, user_id], function (err) {
if (err) {
console.log('insert in trashcan db error');
res.send({error: 'db insert error'});
}
else {
// /drive/user_id/sourceFile --> /drive/user_id/trashcan/sourceFile
S3.moveFile2(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) {
if (result) {
res.send("move to trashcan success");
}
})
console.log('update db error');
res.send({ erorr: 'db update error' });
} else {
S3.moveFile2(S3.BUCKET_NAME, user_id, sourceFile, file_name, targetPath, function(result) {
if (result) {
res.send("move to trashcan success");
}
})
}
})
});
// // /file/delete/:name
// router.get('/:name', function (req, res) {
// var file_name = req.params.name;
// var user_id = req.query.id;
// var curPath = req.query.cur;
// var targetPath = 'trashcan';
// var sourceFile = curPath.substring(1) + file_name;
// var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)';
// var sql2 = 'INSERT INTO trashcan (trash_name, location, user_Id) VALUES (?, ?, ?)';
// connection.query(sql1, [file_name, curPath, user_id], function (err) {
// if (err) {
// console.log('delete db error');
// res.send({erorr: 'db delete error'});
// }
// else {
// connection.query(sql2, [file_name, '/trashcan'+curPath, user_id], function (err) {
// if (err) {
// console.log('insert in trashcan db error');
// res.send({error: 'db insert error'});
// }
// else {
// // /drive/user_id/sourceFile --> /drive/user_id/trashcan/sourceFile
// S3.moveFile2(S3.BUCKET_NAME, user_id, sourceFile, targetPath, function (result) {
// if (result) {
// res.send("move to trashcan success");
// }
// })
// }
// })
// }
// })
// });
module.exports = router;
\ No newline at end of file
......
......@@ -9,38 +9,39 @@ var fs = require('fs');
// /file
router.get('/', function (req, res) {
router.get('/', function(req, res) {
console.log(req.query);
var user_id = req.query.id;
var curPath = req.query.cur; // /folder1/folder2/
var curPath = req.query.cur; // /folder1/folder2/
var sql = 'SELECT * FROM files WHERE user_id=(?) AND location=(?) ORDER BY date DESC';
connection.query(sql, [user_id, curPath], function (err, result) {
if (err){
res.send({error: 'select error'});
}else{
connection.query(sql, [user_id, curPath], function(err, result) {
if (err) {
res.send({ error: 'select error' });
} else {
res.status(200).send({files: result});
res.status(200).send({ files: result });
}
})
});
router.get('/:name', function (req, res) {
router.get('/:name', function(req, res) {
var file_name = req.params.name;
var curPath = req.query.cur; // /folder1/folder2/
var curPath = req.query.cur; // /folder1/folder2/
var user_id = req.query.id;
var targetFile = curPath.substring(1) + file_name; // folder1/folder2/test.txt
var targetFile = curPath.substring(1) + file_name; // folder1/folder2/test.txt
var extension = targetFile.split('.')[1].toLowerCase();
S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir){
if (result){
S3.downloadFile2(S3.BUCKET_NAME, user_id, targetFile, function(result, downloadDir) {
if (result) {
var content;
content = fs.readFileSync(downloadDir, 'utf8');
res.send({file_name: file_name, content: content});
res.send({ file_name: file_name, content: content });
//if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') {
// res.send({ type: 'image', src: downloadDir})
//}else{
......@@ -51,7 +52,7 @@ router.get('/:name', function (req, res) {
//}
}
})
});
});
module.exports = router;
\ No newline at end of file
......
......@@ -160,63 +160,59 @@ router.post('/makefolder', function(req, res, next) {
router.post('/delfolder', function(req, res, next) {
console.log(req.body);
user_id = req.body.id;
let cur = req.body.cur;
curPath = user_id + cur;
let folder_name = req.body.folder_name;
let params = {
Bucket: BUCKET_NAME,
Key: 'drive/' + curPath + folder_name + '/'
};
let infolderpath = cur + folder_name + '/';
let checkinfolder = 'SELECT location FROM folders WHERE location = ? AND user_id = ? UNION ALL SELECT location FROM files WHERE location = ? AND user_id = ?;';
connection.query(checkinfolder, [infolderpath, user_id, infolderpath, user_id], function(err, infolder) {
if (err) {
console.log("copy error");
res.status(304).send({ error: "checkinfolder error" });
} else {
if (infolder.length != 0) {
res.status(204).send({ error: "infolder exist!" })
} else {
let checksql = 'SELECT * FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;';
let values = [cur, folder_name, user_id];
connection.query(checksql, values, function(err, rows, fields) {
if (err) {
res.status(404).send({ error: "error" });
} else {
if (rows.length != 0) {
s3.deleteObject(params, function(err, data) {
if (err) {
console.log('s3 error');
res.status(400).send({ err: err });
let name = req.body.folder_name;
let newPath = user_id + '/trashcan/';
let checkfolder = 'SELECT * FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;';
connection.query(checkfolder, [cur, name, user_id], function(err1, rows, fields) {
console.log(rows);
if (rows.length != 0) {
let copy_params = {
Bucket: BUCKET_NAME,
CopySource: BUCKET_NAME + '/drive/' + curPath + name + '/',
Key: 'drive/' + newPath + name + '/'
};
let del_params = {
Bucket: BUCKET_NAME,
Key: 'drive/' + curPath + name + '/'
};
s3.copyObject(copy_params, function(err, data) {
if (err) {
console.log(err, data);
console.log("copy error");
res.status(304).send({ error: "copy error" });
} else {
s3.deleteObject(del_params, function(err, data) {
if (err) {
console.log(err, data);
console.log("delete error");
res.status(304).send({ error: "delete error" });
} else {
let values = ['/trashcan/', cur, name, user_id];
let updatesql = 'UPDATE folders SET location = ? WHERE location = ? AND folder_name = ? AND user_id = ?;';
connection.query(updatesql, values, function(err3, result, field) {
if (err3) {
console.log("updatesql error");
res.status(304).send({ error: "updatesql error" });
} else {
console.log(data);
let sql = 'DELETE FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;';
connection.query(sql, values, function(err, result, field) {
if (err) {
res.status(400).send({ err: err });
} else {
console.log(cur);
console.log(user_id);
let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
connection.query(checkfolder, [cur, user_id], function(err, rows, fields) {
res.status(200).send({
folders: rows
})
});
}
let resultsql = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
connection.query(resultsql, [cur, user_id], function(err, rows, fields) {
res.status(200).send({
folders: rows
});
});
}
});
} else {
console.log(req.body);
res.status(304).send({ error: "Does not exist" });
}
}
});
}
});
}
});
} else {
console.log("Does not exist folder");
res.status(304).send({ error: "Does not exist" });
}
});
});
......
const express = require('express');
const router = express.Router();
const AWS = require("aws-sdk");
const moment = require("moment");
AWS.config.loadFromPath(__dirname + "/modules/awsconfig.json");
var S3 = require('../modules/s3/s3');
router.get('/delfile', function(req, res, next) {
var file_id = req.query.file_id;
var user_id = req.query.id;
var getsql = 'SELECT * FROM files WHERE file_id = (?)';
connection.query(getsql, [file_id], function(err, rows) {
if (err) {
console.log('select db error');
res.send({ erorr: 'db select error' });
} else {
if (rows.length == 0) {
console.log('exist error');
res.send({ erorr: 'exist error' });
} else {
var curPath = rows[0].location;
var file_name = rows[0].file_name;
var sourceFile = curPath.substring(1) + file_name;
var sql1 = 'DELETE FROM files WHERE file_name = (?) AND location=(?) AND user_Id = (?)';
connection.query(sql1, [file_name, curPath, user_id], function(err) {
if (err) {
console.log('delete db error');
res.send({ erorr: 'db delete error' });
} else {
S3.deleteFile(S3.BUCKET_NAME, user_id, sourceFile, function(result) {
if (result) {
let checkfile = 'SELECT * FROM files WHERE location = ? AND user_id = ?;';
connection.query(checkfile, ['/trashcan/', user_id], function(err, file) {
res.status(200).send({
files: file
})
});
}
})
}
})
}
}
})
});
router.get('/delfolder', function(req, res, next) {
var folder_id = req.query.folder_id;
var user_id = req.query.id;
var getsql = 'SELECT * FROM folders WHERE folder_id = (?)';
connection.query(getsql, [folder_id], function(err, rows) {
if (err) {
console.log('select db error');
res.send({ erorr: 'db select error' });
} else {
if (rows.length == 0) {
console.log('exist error');
res.send({ erorr: 'exist error' });
} else {
var curPath = '/trashcan/'
var folder_name = rows[0].folder_name;
var sourcefolder = curPath.substring(1) + folder_name + '/';
var sql1 = 'DELETE FROM folders WHERE folder_name = (?) AND location=(?) AND user_Id = (?)';
connection.query(sql1, [folder_name, curPath, user_id], function(err) {
if (err) {
console.log('delete db error');
res.send({ erorr: 'db delete error' });
} else {
var params = {
Bucket: S3.BUCKET_NAME,
Key: 'drive/' + user_id + curPath + folder_name + '/'
};
s3.deleteObject(params, function(err, data) {
if (err) {
res.status(400).send({ err: err });
} else {
let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
connection.query(checkfolder, [curPath, user_id], function(err, rows) {
res.status(200).send({
folders: rows
})
});
}
})
}
})
}
}
})
});
module.exports = router;
\ No newline at end of file