Hong

즐겨찾기,공유

...@@ -12,6 +12,8 @@ var registerRouter = require('./routes/userlogin/register'); ...@@ -12,6 +12,8 @@ var registerRouter = require('./routes/userlogin/register');
12 var fileRouter = require('./routes/file/router'); 12 var fileRouter = require('./routes/file/router');
13 //var trashcanRouter = require('./routes/trashcan/router'); 13 //var trashcanRouter = require('./routes/trashcan/router');
14 var folderRouter = require('./routes/folders'); 14 var folderRouter = require('./routes/folders');
15 +var favoritesRouter = require('./routes/favorites');
16 +var shareRouter = require('./routes/share');
15 17
16 18
17 var passport = require('passport'); 19 var passport = require('passport');
...@@ -59,6 +61,8 @@ app.use('/api/user', userRouter); ...@@ -59,6 +61,8 @@ app.use('/api/user', userRouter);
59 app.use('/api/login', loginRouter); 61 app.use('/api/login', loginRouter);
60 app.use('/api/RegistUser', registerRouter); 62 app.use('/api/RegistUser', registerRouter);
61 app.use('/api/folder', folderRouter); 63 app.use('/api/folder', folderRouter);
64 +app.use('/api/favorites', favoritesRouter);
65 +app.use('/api/share', shareRouter);
62 //app.use('/users', usersRouter); 66 //app.use('/users', usersRouter);
63 app.use('/api/file', fileRouter); 67 app.use('/api/file', fileRouter);
64 //app.use('/api/trashcan', trashcanRouter); 68 //app.use('/api/trashcan', trashcanRouter);
......
1 +const express = require('express');
2 +const router = express.Router();
3 +
4 +router.get('/show', function(req, res, next) {
5 + console.log(req.query);
6 + user_id = req.query.id;
7 + folders = [];
8 + files = [];
9 +
10 + let checkfolder = 'SELECT * FROM Folder_Favorites WHERE user_id = ?;';
11 + let checkfiles = 'SELECT * FROM File_Favorites WHERE user_id = ?;';
12 + connection.query(checkfolder, [user_id], function(err, rows, fields) {
13 + if (err) {
14 + console.log('select error');
15 + res.status(404).send();
16 + } else {
17 + folders.push(rows);
18 + }
19 + });
20 + connection.query(checkfiles, [user_id], function(err, rows, fields) {
21 + if (err) {
22 + console.log('select error');
23 + res.status(404).send();
24 + } else {
25 + files.push(rows);
26 + }
27 + });
28 + res.status(200).send({
29 + folders: folders,
30 + files: files
31 + });
32 +});
33 +
34 +router.post('/addfolder', function(req, res, next) {
35 + let user_id = req.body.id;
36 + let folder_name = req.body.name;
37 + console.log(req.body);
38 + let getid = 'SELECT folder_id FROM folders WHERE user_id = ? AND folder_name = ?;';
39 + connection.query(getid, [user_id, folder_name], function(err, folder, fields) {
40 + if (err) {
41 + console.log('getid error');
42 + res.status(400).send({ error: err });
43 + } else {
44 + if (folder.length == 0) {
45 + console.log('dont exist error');
46 + res.status(400).send({ error: err });
47 + } else {
48 + let folder_id = folder[0].folder_id;
49 + console.log(folder_id);
50 + let checkfolder = 'SELECT * FROM Folder_Favorites WHERE user_id = ? AND folder_id = ?;';
51 + connection.query(checkfolder, [user_id, folder_id], function(err, rows, fields) {
52 + if (err) {
53 + console.log('select error');
54 + res.status(400).send({ error: err });
55 + } else {
56 + if (rows.length == 0) {
57 + let sql = 'INSERT INTO Folder_Favorites (folder_id,user_id) values (?,?);';
58 + connection.query(sql, [folder_id, user_id], function(err, result, fields) {
59 + if (err) {
60 + console.log('insert error');
61 + res.status(400).send({ error: err });
62 + } else {
63 + res.status(200).send({
64 + success: 'success',
65 + error: ''
66 + });
67 + }
68 + });
69 + } else {
70 + console.log('already exist error');
71 + res.status(400).send({ error: err });
72 + }
73 + }
74 + });
75 + }
76 + }
77 + });
78 +});
79 +
80 +router.post('/addfile', function(req, res, next) {
81 + let user_id = req.body.id;
82 + let file_name = req.body.name;
83 +
84 + let getid = 'SELECT file_id FROM files WHERE user_id = ? AND file_name = ?;';
85 + connection.query(getid, [user_id, file_name], function(err, file, fields) {
86 + if (err) {
87 + console.log('getid error');
88 + res.status(400).send({ error: err });
89 + } else {
90 + let file_id = file[0].file_id;
91 + let checkfile = 'SELECT * FROM File_Favorites WHERE user_id = ? AND file_id = ?;';
92 + connection.query(checkfile, [user_id, file_id], function(err, rows, fields) {
93 + if (err) {
94 + console.log('select error');
95 + res.status(400).send({ error: err });
96 + } else {
97 + if (rows.length == 0) {
98 + let sql = 'INSERT INTO File_Favorites (file_id,user_id) values (?,?);';
99 + connection.query(sql, [file_id, user_id], function(err, result, fields) {
100 + if (err) {
101 + console.log('insert error');
102 + res.status(400).send({ error: err });
103 + } else {
104 + res.status(200).send({
105 + success: 'success',
106 + error: ''
107 + });
108 + }
109 + });
110 + } else {
111 + console.log('already exist error');
112 + res.status(400).send({ error: err });
113 + }
114 + }
115 + });
116 + }
117 + });
118 +});
119 +
120 +router.post('/delfolder', function(req, res, next) {
121 + let user_id = req.body.id;
122 + let folder_name = req.body.name;
123 + 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 = ?);';
124 +
125 + connection.query(checkfolder, [user_id, folder_name], function(err, rows, fields) {
126 + if (err) {
127 + console.log('select error');
128 + res.status(400).send({ error: err });
129 + } else {
130 + if (rows.length != 0) {
131 + let folder_id = rows[0].folder_id;
132 + let sql = 'DELETE Folder_Favorites WHERE folder_id = ? AND user_id = ?;';
133 + connection.query(sql, [folder_id, user_id], function(err, result, fields) {
134 + if (err) {
135 + console.log('delete error');
136 + res.status(400).send({ error: err });
137 + } else {
138 + res.status(200).send({
139 + success: 'success',
140 + error: ''
141 + });
142 + }
143 + })
144 + } else {
145 + console.log('dont exist error');
146 + res.status(400).send({ error: err });
147 + }
148 + }
149 + });
150 +});
151 +
152 +router.post('/delfile', function(req, res, next) {
153 + let user_id = req.body.id;
154 + let file_name = req.body.name;
155 + 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 = ?);';
156 +
157 + connection.query(checkfile, [user_id, file_name], function(err, rows, fields) {
158 + if (err) {
159 + console.log('select error');
160 + res.status(400).send({ error: err });
161 + } else {
162 + if (rows.length != 0) {
163 + let file_id = rows[0].file_id;
164 + let sql = 'DELETE File_Favorites WHERE file_id = ? AND user_id = ?;';
165 + connection.query(sql, [file_id, user_id], function(err, result, fields) {
166 + if (err) {
167 + console.log('insert error');
168 + res.status(400).send({ error: err });
169 + } else {
170 + res.status(200).send({
171 + success: 'success',
172 + error: ''
173 + });
174 + }
175 + })
176 + } else {
177 + console.log('dont exist error');
178 + res.status(400).send({ error: err });
179 + }
180 + }
181 + });
182 +});
183 +
184 +
185 +
186 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
...@@ -254,20 +254,89 @@ router.post('/move', function(req, res, next) { ...@@ -254,20 +254,89 @@ router.post('/move', function(req, res, next) {
254 }); 254 });
255 255
256 256
257 -router.post('/search/:target', function(req, res, next) { 257 +router.post('/modify', function(req, res, next) {
258 - user_id = req.params.id; 258 + console.log(req.body);
259 - let cur = req.params.cur; 259 + user_id = req.body.id;
260 - folders = {} 260 + let cur = req.body.cur;
261 - let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;'; 261 + curPath = user_id + cur;
262 - connection.query(checkfolder, [cur, user_id], function(err, rows, fields) { 262 + let name = req.body.folder_name;
263 + let newName = req.body.newName;
264 +
265 + let checkfolder = 'SELECT * FROM folders WHERE location = ? AND folder_name = ? AND user_id = ?;';
266 + connection.query(checkfolder, [cur, name, user_id], function(err1, rows, fields) {
267 + console.log(rows);
263 if (rows.length != 0) { 268 if (rows.length != 0) {
264 - res.status(200).send({ 269 + let copy_params = {
265 - folders: folders 270 + Bucket: BUCKET_NAME,
266 - }) 271 + CopySource: BUCKET_NAME + '/' + curPath + name + '/',
272 + Key: curPath + newName + '/'
273 + };
274 + let del_params = {
275 + Bucket: BUCKET_NAME,
276 + Key: curPath + name + '/'
277 + };
278 + s3.copyObject(copy_params, function(err, data) {
279 + if (err) {
280 + console.log(err, data);
281 + console.log("copy error");
282 + res.status(304).send({ error: "copy error" });
283 + } else {
284 + s3.deleteObject(del_params, function(err, data) {
285 + if (err) {
286 + console.log(err, data);
287 + console.log("delete error");
288 + res.status(304).send({ error: "delete error" });
289 + } else {
290 + let date = moment().format();
291 + let values = [newName, date, cur, name, user_id];
292 + let updatesql = 'UPDATE folders SET folder_name = ? AND modify = ? WHERE location = ? AND folder_name = ? AND user_id = ?;';
293 + connection.query(updatesql, values, function(err3, result, field) {
294 + if (err3) {
295 + console.log("updatesql error");
296 + res.status(304).send({ error: "updatesql error" });
297 + } else {
298 + let resultsql = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
299 + connection.query(resultsql, [cur, user_id], function(err, rows, fields) {
300 + res.status(200).send({
301 + folders: rows
302 + });
303 + });
304 + }
305 + });
306 + }
307 + });
308 + }
309 + });
267 } else { 310 } else {
268 - res.send({ error: "Does not exist" }); 311 + console.log("Does not exist");
312 + res.status(304).send({ error: "Does not exist" });
269 } 313 }
270 }); 314 });
315 +
316 +});
317 +
318 +router.get('/search', function(req, res, next) {
319 + console.log(req.query);
320 + user_id = req.query.id;
321 + let target = connection.escape('%' + req.query.target + '%');
322 + console.log(target);
323 + let folders = []
324 + let files = []
325 + let checkfolder = 'SELECT * FROM folders WHERE folder_name LIKE ' + target + ' AND user_id = ?;';
326 + connection.query(checkfolder, [user_id], function(err, folder, fields) {
327 + console.log(folder);
328 + folders.push(folder)
329 + let checkfile = 'SELECT * FROM files WHERE file_name LIKE ' + target + ' AND user_id = ?;';
330 + connection.query(checkfile, [user_id], function(err, file, fields) {
331 + console.log(file);
332 + files.push(file)
333 + res.status(200).send({
334 + folders: folders,
335 + files: files
336 + })
337 + });
338 + });
339 +
271 }); 340 });
272 341
273 module.exports = router; 342 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
1 +const express = require('express');
2 +const router = express.Router();
3 +const AWS = require("aws-sdk");
4 +const moment = require("moment");
5 +
6 +const BUCKET_NAME = "hong-s3-cloud";
7 +
8 +const s3 = new AWS.S3({
9 + accessKeyId: process.env.AWS_ACCESS_KEY_ID,
10 + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
11 + region: "ap-northeast-2"
12 +});
13 +
14 +
15 +router.post('/', function(req, res, next) {
16 + console.log(req.body);
17 + let user_id = req.body.id;
18 + let cur = req.body.cur;
19 + let curPath = user_id + cur;
20 + let name = req.body.file_name;
21 + let target_id = req.body.target_id;
22 +
23 + let checkfile = 'SELECT * FROM files WHERE location = ? AND file_name = ? AND user_id = ?';
24 + connection.query(checkfile, [curPath, name, user_id], function(err1, rows, fields) {
25 + if (rows.length != 0) {
26 + let copy_params = {
27 + Bucket: BUCKET_NAME,
28 + CopySource: BUCKET_NAME + '/' + curPath + name,
29 + Key: target_id + '/share/' + name
30 + };
31 + s3.copyObject(copy_params, function(err, data) {
32 + if (err) {
33 + console.log(err, data);
34 + res.status(304).send({ error: "copy error" });
35 + } else {
36 + let values = [name, cur, target_id];
37 + let sharesql = 'INSERT INTO files (file_name,location,user_id) values (?,?,?);';
38 + connection.query(sharesql, values, function(err3, result, field) {
39 + if (err3) {
40 + console.log("sharesql error");
41 + res.status(304).send({ error: "sharesql error" });
42 + } else {
43 + res.status(200).send({});
44 + }
45 + });
46 + }
47 + });
48 + } else {
49 + res.status(304).send({ error: "Does not exist" });
50 + }
51 + });
52 +
53 +});
54 +
55 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file