leeseohyeon

파일 수정, 빠른 액세스

......@@ -12,6 +12,7 @@ var registerRouter = require('./routes/userlogin/register');
var fileRouter = require('./routes/file/router');
//var trashcanRouter = require('./routes/trashcan/router');
var folderRouter = require('./routes/folders');
var quickRouter = require('./routes/quick');
var passport = require('passport');
......@@ -61,7 +62,7 @@ app.use('/api/RegistUser', registerRouter);
app.use('/api/folder', folderRouter);
//app.use('/users', usersRouter);
app.use('/api/file', fileRouter);
//app.use('/api/trashcan', trashcanRouter);
app.use('/api/quick', quickRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
......
......@@ -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_content = req.body.content;
var targetPath;
if (curPath == '/') {
targetPath = '';
} else {
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'});
}
})
})
});
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
......
......@@ -43,6 +43,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) { // 파일 여러개일 때
......
{
"accessKeyId": "ASIAZQ5XTMMFRYMR3VRB",
"secretAccessKey": "XG8dCozEdASwBXDpdDLoXHXRzXwdaUoZ7Iclpnn7",
"sessionToken": "FwoGZXIvYXdzENf//////////wEaDI+1QF+djlC4HKhgXSLDASLQ79zowPQb14gxxDVD3Due4qV58b2E7S7b2fus/4oOaF3B1+EnZ5ofHKEL3vBnlmzg7Oo37VlbzWh0Ik3QS6XRLg5tf7JxZqZyHhGx0/1k7f6BCZZuoVSBgyEBvGs4VJDZkmWNXv+MT9ytHyl/L1hhuiwqEWFoHJKMwO5jS9QieGJtUl9BP4YKrA1WamiqPZnIkb9TbbNk47Ud8bady5TBO/3u0bBBGL4wkGtAGHPeGSKNy6Ea6gIb9lzpagDN8AKCnSjd5s72BTItHPfO4igQfm5Bl277gKZLytgfzWe4iSPHh4BjmaNVK61zP0zS5wGfD7mcE+X6",
"accessKeyId": "ASIAZQ5XTMMFTERCRTOO",
"secretAccessKey": "4D60bmLZb2IZje42JQp0UjYW9Y/RBVRR94ivjNrc",
"sessionToken": "FwoGZXIvYXdzEEkaDMTw/lUiPUptDH7EBCLDAdFVXAZkmKCB5Fj4jYanKhUHH0iOqXumrbibz4EGubeO5HKz6KCN+EBvJwBbjDpagzkTYdVQHZa8sZAdEX6+41Qyv6bHM0MpEaHZIpt/ku87++qwk47NNlFJn5/iTnif3GwTKf69F2FTh1Y/+qjaP/URN8di0LZAyRonD4UuXwpmZIykWLhj7VCxpyYXBaiKBuE5eRrMQANddl3BROtS5FttRz+GsYXlalrR87UdXAoDFRJhyCpggxZCRe7+b9k3Tge7YiiX5uf2BTItnh1CIWbGHwx0sYdBpnLCZpxg31RU95Le7pW/DBJIYzxoDjjhVbz/lF0WtwIi",
"region": "us-east-1"
}
......
modify file
\ No newline at end of file
......@@ -13,7 +13,6 @@
// targetFile: folder2/file.txt
var AWS = require('aws-sdk');
AWS.config.update({ region: 'ap-northeast-2' });
......@@ -73,6 +72,32 @@ var S3 = {
})
},
// 파일 덮어쓰기
coverFile: function(bucketName, userId, sourceFile, targetPath, body, callback) {
var pathbody = fs.createReadStream(body);
if (targetPath != '' && targetPath[targetPath.length - 1] != '/') {
targetPath = targetPath + '/';
}
var targetFile = targetPath + sourceFile;
var coverParams = {
Bucket: bucketName,
Key: 'drive/' + userId + '/' + targetFile,
Body: pathbody
};
s3.upload(coverParams, function (err, data) {
if (err) {
console.log("Upload Error" + err);
callback(false);
} else {
console.log("Upload Success");
callback(true);
}
})
},
deleteFile: function (bucketName, userId, targetFile, callback) {
// targetFile => folder1/folder2/test.txt
var deleteParams = {
......@@ -104,10 +129,8 @@ var S3 = {
console.log("Download File Error", err);
callback(false);
} else {
if (data) {
console.log("Get File Success");
callback(true, data.body.toString());
}
console.log("Get File Success");
callback(true, data.Body.toString());
}
})
},
......@@ -119,8 +142,10 @@ var S3 = {
if (result) {
makeFolder(tempDownloadDir, function(result){
if (result) {
fs.writeFileSync(tempDownloadDir, data);
callback(true, tempDownloadDir);
if (data){
fs.writeFileSync(tempDownloadDir, data);
callback(true, tempDownloadDir);
}
}
})
}else{
......@@ -143,10 +168,13 @@ var S3 = {
Key: 'drive/' + userId + '/' + targetFile
};
try {
s3.getObject(params).createReadStream().pipe(file);
callback(true, tempDownloadDir);
var stream = s3.getObject(params).createReadStream().pipe(file);
stream.on('end', function(){
console.log('end!');
callback(true, tempDownloadDir);
});
}catch(err){
console.log('no such file');
console.log('no such file', err);
callback(false);
}
}else{
......
var express = require('express');
var router = express.Router();
router.get('/', function(req, res){
var user_id = req.query.id;
var sql = 'SELECT * FROM files WHERE updated>date_sub(now(), interval 3 day) AND user_id=(?) ORDER BY updated DESC';
connection.query(sql, user_id, function(err, result){
if (err){
console.log('select error');
res.send({error: 'quick error'})
}else{
res.send(result);
}
})
})
module.exports = router;
\ No newline at end of file
......@@ -2,34 +2,10 @@
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"axios": {
"version": "0.19.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
"integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
"requires": {
"follow-redirects": "1.5.10"
}
},
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"requires": {
"ms": "2.0.0"
}
},
"follow-redirects": {
"version": "1.5.10",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
"requires": {
"debug": "=3.1.0"
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
"readline": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/readline/-/readline-1.3.0.tgz",
"integrity": "sha1-xYDXfvLPyHUrEySYBg3JeTp6wBw="
}
}
}
......