-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRekonStorage.js
272 lines (247 loc) · 12.5 KB
/
RekonStorage.js
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
import express from 'express';
import { SimpleDB } from './modules/HSimpleDB.js';
import 'dotenv/config';
import ky from 'ky';
import fs from 'fs/promises';
import { createWriteStream } from 'fs';
import path from 'path';
import * as ThumbHash from 'thumbhash';
import sharp from 'sharp';
const required_tables = ['upload_tokens'];
const table_params = {
'upload_tokens': 'upload_token VARCHAR(255) PRIMARY KEY, user_token VARCHAR(255), account_id VARCHAR(255), num_chunks INT, file_type VARCHAR(255), thumb_hash VARCHAR(255), upload_complete BOOLEAN'
};
let app = express();
app.use(express.json({limit: '1mb'}));
app.use(
express.urlencoded({
extended: true,
})
);
app.use((err, req, res, next) => {
// Handle the error here
console.error(err.stack);
res.status(500).send('Something broke!');
});
process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err);
});
setInterval(async () => {
const gatewayResponse = await ky.put(`http://127.0.0.1:8234/storage/status/heartbeat?secret=${process.env.SERVER_SECRET}`).json();
}, 10000)
let db = new SimpleDB();
await db.init(process.env.DB_USER, process.env.DB_PASS, process.env.DB_NAME)
for (const table of required_tables) {
const table_exists = await db.checkIfTableExists(table);
if (!table_exists) {
await db.createTable(table, table_params[table]);
console.warn(`Table "${table}" was missing. If this is the first boot, ignore this message.`);
} else {
console.log(`Table "${table}" initialized.`);
}
}
app.post('/getUploadToken', async (req, res) => {
if (req.body.secret != process.env.SERVER_SECRET) {
return res.json({'error': true, 'message': 'Please access this endpoint through the API gateway server.', 'code': 'ms-direct-access-disallowed'});
}
const uploadLocation = req.body.type;
const userToken = req.body.token;
const numChunks = req.body.numChunks;
const fileType = req.body.fileType;
console.log(req.body);
const tokenInfo = await ky.post(`http://127.0.0.1:8234/internal/auth/verifyToken`, {json: {secret: process.env.SERVER_SECRET, token: userToken}}).json();
if (!tokenInfo.valid) {
return res.json({'error': true, 'valid': false, 'message': 'User token is invalid.', 'code': 'token-invalid'});
}
let uploadToken;
if (uploadLocation == 'profile') {
uploadToken = `${tokenInfo.info.account_id}-profile`;
} else if (uploadLocation == 'userblock') {
uploadToken = `${crypto.randomUUID()}-userblock`;
} else if (uploadLocation == 'groupblock') {
uploadToken = `${crypto.randomUUID()}-groupblock`;
} else {
return res.json({'error': true, 'message': 'Upload location invalid', 'code': 'store-location-invalid'});
}
if (await db.checkIfValueExists('upload_tokens', '*', 'upload_token', uploadToken)) {
await db.removeRow('upload_tokens', 'upload_token', uploadToken);
}
await db.addEntry('upload_tokens', [uploadToken, userToken, tokenInfo.info.account_id, numChunks, fileType, null, false]);
return res.json({'error': false, 'message': 'Upload token granted!', 'token': uploadToken});
});
app.post('/uploadChunk', async (req, res) => {
if (req.body.secret != process.env.SERVER_SECRET) {
return res.json({'error': true, 'message': 'Please access this endpoint through the API gateway server.', 'code': 'ms-direct-access-disallowed'});
}
const userToken = req.body.userToken;
const uploadToken = req.body.uploadToken;
const fileChunk = req.body.chunk;
const index = req.body.index;
const tokenInfo = await ky.post(`http://127.0.0.1:8234/internal/auth/verifyToken`, {json: {secret: process.env.SERVER_SECRET, token: userToken}}).json();
if (!tokenInfo.valid) {
return res.json({'error': true, 'valid': false, 'message': 'User token is invalid.', 'code': 'token-invalid'});
}
const tokenDB = await db.selectRow('upload_tokens', '*', 'upload_token', uploadToken);
if (!tokenDB) {
return res.json({'error': true, 'valid': false, 'message': 'Upload token is invalid.', 'code': 'token-invalid'});
}
if (tokenDB.user_token != userToken) {
return res.json({'error': true, 'valid': false, 'message': 'User token does not match upload token.', 'code': 'token-mismatch'});
}
const fileChunkPath = path.join(process.env.STORAGE_PATH, tokenDB.account_id, tokenDB.upload_token, `${index}.chunk`);
await fs.mkdir(path.dirname(fileChunkPath), { recursive: true });
await fs.writeFile(fileChunkPath, fileChunk);
return res.json({'error': false, 'valid': true, 'message': 'Chunk uploaded successfully.'});
});
app.post('/completeUpload', async (req, res) => {
if (req.body.secret != process.env.SERVER_SECRET) {
return res.json({'error': true, 'message': 'Please access this endpoint through the API gateway server.', 'code': 'ms-direct-access-disallowed'});
}
const userToken = req.body.userToken;
const uploadToken = req.body.uploadToken;
const tokenInfo = await ky.post(`http://127.0.0.1:8234/internal/auth/verifyToken`, {json: {secret: process.env.SERVER_SECRET, token: userToken}}).json();
if (!tokenInfo.valid) {
return res.json({'error': true, 'valid': false, 'message': 'User token is invalid.', 'code': 'token-invalid'});
}
const tokenDB = await db.selectRow('upload_tokens', '*', 'upload_token', uploadToken);
if (!tokenDB) {
return res.json({'error': true, 'valid': false, 'message': 'Upload token is invalid.', 'code': 'token-invalid'});
}
const fileChunkPath = path.join(process.env.STORAGE_PATH, tokenDB.account_id, tokenDB.upload_token);
// merge chunks
const fileType = tokenDB.file_type;
const files = await fs.readdir(fileChunkPath);
const chunkFiles = files.filter(f => f.endsWith('.chunk'));
if (chunkFiles.length !== tokenDB.num_chunks) {
// Delete all chunks if count doesn't match
console.log("Number of chunks: ", tokenDB.num_chunks);
console.log("Number of chunks uploaded: ", chunkFiles.length);
console.log("Chunk files: ", chunkFiles);
for (const chunk of chunkFiles) {
await fs.unlink(path.join(fileChunkPath, chunk));
}
return res.json({
'error': true,
'message': 'Number of uploaded chunks does not match expected count',
'code': 'chunk-count-mismatch'
});
}
// Delete existing file if it exists
const existingFile = path.join(fileChunkPath, `file${fileType}`);
try {
await fs.access(existingFile);
// File exists, delete it
await fs.unlink(existingFile);
} catch {
// File doesn't exist, continue
}
const finalFilePath = path.join(fileChunkPath, `file${fileType}`);
const writeStream = createWriteStream(finalFilePath);
// Sort chunks numerically to ensure correct order
const sortedChunks = chunkFiles.sort((a, b) => {
return parseInt(a.split('.')[0]) - parseInt(b.split('.')[0]);
});
for (const chunk of sortedChunks) {
const base64Data = await fs.readFile(path.join(fileChunkPath, chunk), 'utf8');
const binaryData = Buffer.from(base64Data, 'base64');
writeStream.write(binaryData);
await fs.unlink(path.join(fileChunkPath, chunk));
}
await new Promise((resolve) => writeStream.end(resolve));
let base64ThumbHash;
try {
const image = sharp(finalFilePath).resize(100, 100, { fit: 'inside' })
const { data, info } = await image.ensureAlpha().raw().toBuffer({ resolveWithObject: true })
const binaryThumbHash = ThumbHash.rgbaToThumbHash(info.width, info.height, data)
// Convert the binary ThumbHash to base64 string for storage
base64ThumbHash = Buffer.from(binaryThumbHash).toString('base64')
} catch (err) {
console.error('Error generating thumbnail hash:', err);
base64ThumbHash = null;
}
await db.updateEntry('upload_tokens', 'upload_token', uploadToken, 'upload_complete', true);
await db.updateEntry('upload_tokens', 'upload_token', uploadToken, 'thumb_hash', base64ThumbHash);
return res.json({'error': false, 'valid': true, 'message': 'Upload completed successfully.'});
});
app.get('/getUploadedFile', async (req, res) => {
const secret = req.query.secret;
if (secret != process.env.SERVER_SECRET) {
return res.json({'error': true, 'message': 'Please access this endpoint through the API gateway server.', 'code': 'ms-direct-access-disallowed'});
}
const userToken = req.query.userToken;
const uploadToken = req.query.uploadToken;
console.log(req.query);
const tokenInfo = await ky.post(`http://127.0.0.1:8234/internal/auth/verifyToken`, {json: {secret: process.env.SERVER_SECRET, token: userToken}}).json();
if (!tokenInfo.valid) {
return res.json({'error': true, 'valid': false, 'message': 'User token is invalid.', 'code': 'token-invalid'});
}
console.log(tokenInfo);
const fileChunkPath = path.join(process.env.STORAGE_PATH, tokenInfo.info.account_id, uploadToken);
try {
const files = await fs.readdir(fileChunkPath);
if (files.length === 0) {
return res.json({'error': true, 'message': 'File not found.', 'code': 'file-not-found'});
}
const file = await fs.readFile(path.join(fileChunkPath, files[0]));
return res.json({'error': false, 'valid': true, 'message': 'File retrieved successfully.', 'file': file, 'fileType': files[0].split('.')[1]});
} catch (err) {
return res.json({'error': true, 'message': 'File not found.', 'code': 'file-not-found'});
}
});
app.get('/getUserFiles', async (req, res) => {
const userToken = req.query.userToken;
const secret = req.query.secret;
if (secret != process.env.SERVER_SECRET) {
return res.json({'error': true, 'message': 'Please access this endpoint through the API gateway server.', 'code': 'ms-direct-access-disallowed'});
}
const tokenInfo = await ky.post(`http://127.0.0.1:8234/internal/auth/verifyToken`, {json: {secret: process.env.SERVER_SECRET, token: userToken}}).json();
if (!tokenInfo.valid) {
return res.json({'error': true, 'valid': false, 'message': 'User token is invalid.', 'code': 'token-invalid'});
}
const accountID = tokenInfo.info.account_id;
const files = await db.selectRows('upload_tokens', '*', 'account_id', accountID);
return res.json({'error': false, 'valid': true, 'message': 'Files retrieved successfully.', 'files': files});
})
app.post('/deleteFile', async (req, res) => {
const userToken = req.body.userToken;
const uploadToken = req.body.uploadToken;
const secret = req.body.secret;
if (secret != process.env.SERVER_SECRET) {
return res.json({'error': true, 'message': 'Please access this endpoint through the API gateway server.', 'code': 'ms-direct-access-disallowed'});
}
const tokenInfo = await ky.post(`http://127.0.0.1:8234/internal/auth/verifyToken`, {json: {secret: process.env.SERVER_SECRET, token: userToken}}).json();
if (!tokenInfo.valid) {
return res.json({'error': true, 'valid': false, 'message': 'User token is invalid.', 'code': 'token-invalid'});
}
const accountID = tokenInfo.info.account_id;
const fileChunkPath = path.join(process.env.STORAGE_PATH, accountID, uploadToken);
try {
await fs.rm(fileChunkPath, { recursive: true, force: true });
await db.removeRow('upload_tokens', 'upload_token', uploadToken);
return res.json({'error': false, 'valid': true, 'message': 'File deleted successfully.'});
} catch (err) {
console.error('Error deleting file:', err);
return res.json({'error': true, 'message': 'Error deleting file', 'code': 'file-delete-error'});
}
})
app.get('/getProfilePicture', async (req, res) => {
const accountID = req.query.accountID;
const secret = req.query.secret;
if (secret != process.env.SERVER_SECRET) {
return res.json({'error': true, 'message': 'Please access this endpoint through the API gateway server.', 'code': 'ms-direct-access-disallowed'});
}
const profilePicturePath = path.join(process.env.STORAGE_PATH, accountID, accountID + '-profile');
try {
const files = await fs.readdir(profilePicturePath);
if (files.length === 0) {
return res.send(null);
}
const profilePicture = await fs.readFile(path.join(profilePicturePath, files[0]));
return res.send(profilePicture);
} catch (err) {
return res.send(null);
}
})
app.listen(8237, () => {
console.log(`Rekon storage server running at port 8237`);
});