folders.js
18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
const express = require('express');
const router = express.Router();
const AWS = require("aws-sdk");
const moment = require("moment");
AWS.config.loadFromPath(__dirname + "/modules/awsconfig.json");
const BUCKET_NAME = "hong-s3-cloud";
let curPath = "";
let user_id = "";
let parentPath = "";
const s3 = new AWS.S3();
// router.get('/show', function(req, res, next) {
// console.log(req.query);
// user_id = req.query.id;
// curPath = req.query.cur;
// if (curPath == '/') {
// parentPath = '/';
// } else {
// let pathSplit = curPath.split('/')
// console.log(pathSplit);
// parentPath = '/';
// for (let i = 1; i < pathSplit.length - 2; i++) {
// parentPath += pathSplit[i];
// parentPath += '/';
// }
// }
// let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
// connection.query(checkfolder, [curPath, user_id], function(err, rows, fields) {
// if (err) {
// console.log('select error');
// res.status(400).send({ err: err });
// } else {
// res.status(200).send({
// folders: rows,
// cur: curPath,
// parentPath: parentPath
// })
// }
// });
// });
router.get('/show', function(req, res, next) {
console.log(req.query);
user_id = req.query.user_id;
folder_id = req.query.folder_id;
let checkfolder = 'SELECT * FROM folders WHERE folder_id = ? AND user_id = ?;';
connection.query(checkfolder, [curPath, user_id], function(err, rows) {
if (err) {
console.log('select1 error');
res.status(400).send({ err: err });
} else {
let location = rows[0].location + rows[0].folder_name + '/';
if (rows.length == 0) {
console.log('does not exist');
res.status(400).send({ err: 'does not exist' });
} else {
let gerFolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
connection.query(gerFolder, [location, user_id], function(err, folder) {
if (err) {
console.log('select2 error');
res.status(400).send({ err: err });
} else {
let gerFile = 'SELECT * FROM files WHERE location = ? AND user_id = ?;';
connection.query(gerFile, [location, user_id], function(err, folder) {
if (err) {
console.log('select3 error');
res.status(400).send({ err: err });
} else {
res.status(200).send({
folders: folder,
files: file
});
}
});
}
});
}
}
});
});
router.post('/makefolder', function(req, res, next) {
user_id = req.body.user_id;
let cur = req.body.cur;
curPath = user_id + cur;
let folder_name = req.body.folder_name;
let date = moment().format();
let params = {
Bucket: BUCKET_NAME,
Key: 'drive/' + curPath + folder_name + '/',
Body: "",
ACL: "public-read-write"
};
let checksql = 'SELECT * FROM folders WHERE location = ? AND folder_name = ?;';
console.log(req.body)
connection.query(checksql, [cur, folder_name], function(err, rows, fields) {
if (err) {
res.status(400).send({ err: err });
} else {
if (rows.length == 0) {
s3.putObject(params, function(err, data) {
if (err) {
console.log('s3 error');
res.status(400).send({ err: err });
} else {
console.log(data);
let sql = 'INSERT INTO folders (folder_name,location,user_id,created) values (?,?,?,?);';
let values = [folder_name, cur, user_id, date];
connection.query(sql, values, function(err, result, field) {
if (err) {
console.log('insert error');
res.status(400).send({ err: err });
} else {
let checkfolder = 'SELECT * FROM folders WHERE location = ? AND user_id = ?;';
connection.query(checkfolder, [cur, user_id], function(err, rows, fields) {
console.log(rows);
res.status(200).send({
folders: rows
})
});
}
});
}
});
} else {
res.status(404).send({ error: "same name error" });
}
}
});
});
router.post('/delfolder', function(req, res, next) {
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 + 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 });
} 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
})
});
}
});
}
});
} else {
console.log(req.body);
res.status(304).send({ error: "Does not exist" });
}
}
});
}
}
});
});
router.post('/move', 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.name;
let newPath = user_id + req.body.newPath;
if (req.body.isfolder) {
let infolderpath = cur + 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 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 = [req.body.newPath, 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 {
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("Does not exist folder");
res.status(304).send({ error: "Does not exist" });
}
});
}
}
});
} else {
let checkfile = 'SELECT * FROM files WHERE location = ? AND file_name = ? AND user_id = ?;';
connection.query(checkfile, [cur, name, user_id], function(err1, rows, fields) {
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);
res.status(304).send({ error: "copy error" });
} else {
s3.deleteObject(del_params, function(err, data) {
if (err) {
console.log(err, data);
res.status(304).send({ error: "delete error" });
} else {
let values = [req.body.newPath, cur, name, user_id];
let updatesql = 'UPDATE files SET location = ? WHERE location = ? AND file_name = ? AND user_id = ?;';
connection.query(updatesql, values, function(err3, result, field) {
if (err3) {
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 {
console.log("Does not exist file");
res.status(304).send({ error: "Does not exist" });
}
});
}
});
// 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) {
// let copy_params = {
// Bucket: BUCKET_NAME,
// CopySource: BUCKET_NAME + '/drive/' + curPath + name + '/',
// Key: 'drive/' + curPath + newName + '/'
// };
// 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 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 {
// 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;