Hong

즐겨찾기,공유

......@@ -12,6 +12,8 @@ var registerRouter = require('./routes/userlogin/register');
var fileRouter = require('./routes/file/router');
//var trashcanRouter = require('./routes/trashcan/router');
var folderRouter = require('./routes/folders');
var favoritesRouter = require('./routes/favorites');
var shareRouter = require('./routes/share');
var passport = require('passport');
......@@ -59,6 +61,8 @@ app.use('/api/user', userRouter);
app.use('/api/login', loginRouter);
app.use('/api/RegistUser', registerRouter);
app.use('/api/folder', folderRouter);
app.use('/api/favorites', favoritesRouter);
app.use('/api/share', shareRouter);
//app.use('/users', usersRouter);
app.use('/api/file', fileRouter);
//app.use('/api/trashcan', trashcanRouter);
......
const express = require('express');
const router = express.Router();
router.get('/show', function(req, res, next) {
console.log(req.query);
user_id = req.query.id;
folders = [];
files = [];
let checkfolder = 'SELECT * FROM Folder_Favorites WHERE user_id = ?;';
let checkfiles = 'SELECT * FROM File_Favorites WHERE user_id = ?;';
connection.query(checkfolder, [user_id], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(404).send();
} else {
folders.push(rows);
}
});
connection.query(checkfiles, [user_id], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(404).send();
} else {
files.push(rows);
}
});
res.status(200).send({
folders: folders,
files: files
});
});
router.post('/addfolder', function(req, res, next) {
let user_id = req.body.id;
let folder_name = req.body.name;
console.log(req.body);
let getid = 'SELECT folder_id FROM folders WHERE user_id = ? AND folder_name = ?;';
connection.query(getid, [user_id, folder_name], function(err, folder, fields) {
if (err) {
console.log('getid error');
res.status(400).send({ error: err });
} else {
if (folder.length == 0) {
console.log('dont exist error');
res.status(400).send({ error: err });
} else {
let folder_id = folder[0].folder_id;
console.log(folder_id);
let checkfolder = 'SELECT * FROM Folder_Favorites WHERE user_id = ? AND folder_id = ?;';
connection.query(checkfolder, [user_id, folder_id], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(400).send({ error: err });
} else {
if (rows.length == 0) {
let sql = 'INSERT INTO Folder_Favorites (folder_id,user_id) values (?,?);';
connection.query(sql, [folder_id, user_id], function(err, result, fields) {
if (err) {
console.log('insert error');
res.status(400).send({ error: err });
} else {
res.status(200).send({
success: 'success',
error: ''
});
}
});
} else {
console.log('already exist error');
res.status(400).send({ error: err });
}
}
});
}
}
});
});
router.post('/addfile', function(req, res, next) {
let user_id = req.body.id;
let file_name = req.body.name;
let getid = 'SELECT file_id FROM files WHERE user_id = ? AND file_name = ?;';
connection.query(getid, [user_id, file_name], function(err, file, fields) {
if (err) {
console.log('getid error');
res.status(400).send({ error: err });
} else {
let file_id = file[0].file_id;
let checkfile = 'SELECT * FROM File_Favorites WHERE user_id = ? AND file_id = ?;';
connection.query(checkfile, [user_id, file_id], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(400).send({ error: err });
} else {
if (rows.length == 0) {
let sql = 'INSERT INTO File_Favorites (file_id,user_id) values (?,?);';
connection.query(sql, [file_id, user_id], function(err, result, fields) {
if (err) {
console.log('insert error');
res.status(400).send({ error: err });
} else {
res.status(200).send({
success: 'success',
error: ''
});
}
});
} else {
console.log('already exist error');
res.status(400).send({ error: err });
}
}
});
}
});
});
router.post('/delfolder', function(req, res, next) {
let user_id = req.body.id;
let folder_name = req.body.name;
let checkfolder = 'SELECT * FROM Folder_Favorites JOIN folders ON (Folder_Favorites.folder_id = folders.folder_id AND Folder_Favorites.user_id = ? AND folders.folder_name = ?);';
connection.query(checkfolder, [user_id, folder_name], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(400).send({ error: err });
} else {
if (rows.length != 0) {
let folder_id = rows[0].folder_id;
let sql = 'DELETE Folder_Favorites WHERE folder_id = ? AND user_id = ?;';
connection.query(sql, [folder_id, user_id], function(err, result, fields) {
if (err) {
console.log('delete error');
res.status(400).send({ error: err });
} else {
res.status(200).send({
success: 'success',
error: ''
});
}
})
} else {
console.log('dont exist error');
res.status(400).send({ error: err });
}
}
});
});
router.post('/delfile', function(req, res, next) {
let user_id = req.body.id;
let file_name = req.body.name;
let checkfile = 'SELECT * FROM File_Favorites JOIN files ON (File_Favorites.file_id = files.file_id AND File_Favorites.user_id = ? AND files.file_name = ?);';
connection.query(checkfile, [user_id, file_name], function(err, rows, fields) {
if (err) {
console.log('select error');
res.status(400).send({ error: err });
} else {
if (rows.length != 0) {
let file_id = rows[0].file_id;
let sql = 'DELETE File_Favorites WHERE file_id = ? AND user_id = ?;';
connection.query(sql, [file_id, user_id], function(err, result, fields) {
if (err) {
console.log('insert error');
res.status(400).send({ error: err });
} else {
res.status(200).send({
success: 'success',
error: ''
});
}
})
} else {
console.log('dont exist error');
res.status(400).send({ error: err });
}
}
});
});
module.exports = router;
\ No newline at end of file
......@@ -254,20 +254,89 @@ router.post('/move', function(req, res, next) {
});
router.post('/search/:target', function(req, res, next) {
user_id = req.params.id;
let cur = req.params.cur;
folders = {}
let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
connection.query(checkfolder, [cur, user_id], function(err, rows, fields) {
router.post('/modify', function(req, res, next) {
console.log(req.body);
user_id = req.body.id;
let cur = req.body.cur;
curPath = user_id + cur;
let name = req.body.folder_name;
let newName = req.body.newName;
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) {
res.status(200).send({
folders: folders
})
let copy_params = {
Bucket: BUCKET_NAME,
CopySource: BUCKET_NAME + '/' + curPath + name + '/',
Key: curPath + newName + '/'
};
let del_params = {
Bucket: BUCKET_NAME,
Key: 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 date = moment().format();
let values = [newName, date, cur, name, user_id];
let updatesql = 'UPDATE folders SET folder_name = ? AND modify = ? 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 {
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 {
res.send({ error: "Does not exist" });
console.log("Does not exist");
res.status(304).send({ error: "Does not exist" });
}
});
});
router.get('/search', function(req, res, next) {
console.log(req.query);
user_id = req.query.id;
let target = connection.escape('%' + req.query.target + '%');
console.log(target);
let folders = []
let files = []
let checkfolder = 'SELECT * FROM folders WHERE folder_name LIKE ' + target + ' AND user_id = ?;';
connection.query(checkfolder, [user_id], function(err, folder, fields) {
console.log(folder);
folders.push(folder)
let checkfile = 'SELECT * FROM files WHERE file_name LIKE ' + target + ' AND user_id = ?;';
connection.query(checkfile, [user_id], function(err, file, fields) {
console.log(file);
files.push(file)
res.status(200).send({
folders: folders,
files: files
})
});
});
});
module.exports = router;
\ No newline at end of file
......
const express = require('express');
const router = express.Router();
const AWS = require("aws-sdk");
const moment = require("moment");
const BUCKET_NAME = "hong-s3-cloud";
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: "ap-northeast-2"
});
router.post('/', function(req, res, next) {
console.log(req.body);
let user_id = req.body.id;
let cur = req.body.cur;
let curPath = user_id + cur;
let name = req.body.file_name;
let target_id = req.body.target_id;
let checkfile = 'SELECT * FROM files WHERE location = ? AND file_name = ? AND user_id = ?';
connection.query(checkfile, [curPath, name, user_id], function(err1, rows, fields) {
if (rows.length != 0) {
let copy_params = {
Bucket: BUCKET_NAME,
CopySource: BUCKET_NAME + '/' + curPath + name,
Key: target_id + '/share/' + name
};
s3.copyObject(copy_params, function(err, data) {
if (err) {
console.log(err, data);
res.status(304).send({ error: "copy error" });
} else {
let values = [name, cur, target_id];
let sharesql = 'INSERT INTO files (file_name,location,user_id) values (?,?,?);';
connection.query(sharesql, values, function(err3, result, field) {
if (err3) {
console.log("sharesql error");
res.status(304).send({ error: "sharesql error" });
} else {
res.status(200).send({});
}
});
}
});
} else {
res.status(304).send({ error: "Does not exist" });
}
});
});
module.exports = router;
\ No newline at end of file