folders.js
9.51 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
const express = require('express');
const router = express.Router();
const AWS = require("aws-sdk");
const moment = require("moment");
const BUCKET_NAME = "hong-s3-cloud";
let curPath = "";
let user_id = "";
let parentPath = "";
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: "ap-northeast-2"
});
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(404).send()
} else {
res.status(200).send({
folders: rows,
cur: curPath,
parentPath: parentPath
})
}
});
});
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: 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) {
} else {
if (rows.length == 0) {
s3.putObject(params, function(err, data) {
if (err) {
console.log('s3 error');
throw 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');
throw 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: curPath + folder_name + '/'
};
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');
//throw 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) {
//throw 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) {
user_id = req.body.user_id;
curPath = user_id + req.body.cur;
let name = req.body.mfile;
let newPath = req.body.newPath;
let checkfolder = 'SELECT * FROM folders WHERE location = ? AND folder_name = ?;';
if (req.body.isfolder) {
connection.query(checkfolder, [curPath, name], function(err1, rows, fields) {
if (rows.length != 0) {
let copy_params = {
Bucket: BUCKET_NAME + curPath,
CopySource: BUCKET_NAME + curPath + file + '/',
Key: newPath + file + '/'
};
let del_params = {
Bucket: BUCKET_NAME + curPath,
Key: file + '/'
};
s3.copyObject(copy_params, function(err, data) {
console.log(err, data);
});
s3.deleteObject(del_params, function(err, data) {
console.log(err, data);
});
let values = [newPath, curPath, name];
let updatesql = 'UPDATE folders SET location = ? WHERE location = ? AND folder_name = ?;';
connection.query(updatesql, values, function(err3, result, field) {
if (err3) {
throw err;
} else {
folders = {}
connection.query(checkfolder, [cur, user_id], function(err, rows, fields) {
if (rows.length != 0) {
res.status(200).send({
folders: rows,
cur: curPath
})
} else {
res.send({ error: "Does not exist" });
}
});
}
});
} else {
res.send({ error: "Does not exist" });
}
});
} else {
let checkfile = 'SELECT * FROM files WHERE location = ? AND file_name = ?';
connection.query(checkfile, [curPath, name], function(err1, rows, fields) {
if (rows.length != 0) {
let copy_params = {
Bucket: BUCKET_NAME + curPath,
CopySource: BUCKET_NAME + curPath + file,
Key: newPath + file
};
let del_params = {
Bucket: BUCKET_NAME + curPath,
Key: file
};
s3.copyObject(copy_params, function(err, data) {
console.log(err, data);
});
s3.deleteObject(del_params, function(err, data) {
console.log(err, data);
});
let values = [newPath, curPath, name];
let updatesql = 'UPDATE files SET location = ? WHERE location = ? AND file_name = ?;';
connection.query(updatesql, values, function(err3, result, field) {
if (err3) {
throw err;
} else {
folders = {}
connection.query(checkfolder, [cur, user_id], function(err, rows, fields) {
if (rows.length != 0) {
res.status(200).send({
folders: rows,
cur: curPath
})
} else {
res.send({ error: "Does not exist" });
}
});
}
});
} else {
res.send({ error: "Does not exist" });
}
});
}
});
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) {
if (rows.length != 0) {
res.status(200).send({
folders: folders
})
} else {
res.send({ error: "Does not exist" });
}
});
});
module.exports = router;