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
|
const Route = require('../../structures/Route');
const path = require('path');
const Util = require('../../utils/Util');
const moment = require('moment');
const log = require('../../utils/Log');
const jetpack = require('fs-jetpack');
const Busboy = require('busboy');
const fs = require('fs');
/*
TODO: Strip exif data if the owner/user configured it as such
TODO: If source has transparency generate a png thumbnail, otherwise a jpg.
TODO: If source is a gif, generate a thumb of the first frame and play the gif on hover.
TODO: If source is a video, generate a thumb of the first frame and save the video length.
TODO: Check that the async isAuthorized works and is not nulling out
*/
class uploadPOST extends Route {
constructor() {
super('/upload', 'post', { bypassAuth: true });
}
async run(req, res, db) {
const user = await Util.isAuthorized(req);
if (!user && !process.env.PUBLIC_MODE) return res.status(401).json({ message: 'Not authorized to use this resource' });
return this.uploadFile(req, res, db, user);
}
async processFile(req, res, db, user, file) {
/*
Check if the user is trying to upload to an album
*/
const albumId = req.body.albumid || req.headers.albumid;
if (albumId && !user) return res.status(401).json({ message: 'Only registered users can upload files to an album' });
if (albumId && user) {
const album = await db.table('albums').where({ id: albumId, userId: user.id }).first();
if (!album) return res.status(401).json({ message: 'Album doesn\'t exist or it doesn\'t belong to the user' });
}
/*
if (!albumId) log.info('Incoming file');
else log.info(`Incoming file for album ${albumId}`);
*/
let upload = file.data;
/*
If it's a chunked upload but this is not the last part of the chunk, just green light.
Otherwise, put the file together and process it
*/
if (file.body.uuid) {
if (file.body.chunkindex < file.body.totalchunkcount - 1) { // eslint-disable-line no-lonely-if
/*
We got a chunk that is not the last part, send smoke signal that we received it.
*/
return res.json({ message: 'Successfully uploaded chunk' });
}
/*
Seems we finally got the last part of a chunk upload
*/
const uploadsDir = path.join(__dirname, '..', '..', '..', '..', process.env.UPLOAD_FOLDER);
const chunkedFileDir = path.join(__dirname, '..', '..', '..', '..', process.env.UPLOAD_FOLDER, 'chunks', file.body.uuid);
const chunkFiles = await jetpack.findAsync(chunkedFileDir, { matching: '*' });
const originalname = Util.getFilenameFromPath(chunkFiles[0].substring(0, chunkFiles[0].lastIndexOf('.')));
const tempFile = {
filename: Util.getUniqueFilename(originalname),
originalname,
size: file.body.totalfilesize
};
for (const chunkFile of chunkFiles) {
try {
const data = await jetpack.readAsync(chunkFile, 'buffer'); // eslint-disable-line no-await-in-loop
await jetpack.appendAsync(path.join(uploadsDir, tempFile.filename), data); // eslint-disable-line no-await-in-loop
} catch (error) {
log.error(error);
}
}
try {
await jetpack.removeAsync(chunkedFileDir);
} catch (error) {
log.error(error);
}
upload = tempFile;
}
/*
First let's get the hash of the file. This will be useful to check if the file
has already been upload by either the user or an anonymous user.
In case this is true, instead of uploading it again we retrieve the url
of the file that is already saved and thus don't store extra copies of the same file.
*/
const hash = await Util.getFileHash(upload.filename); // eslint-disable-line no-await-in-loop
const exists = await db.table('files') // eslint-disable-line no-await-in-loop
.where(function() {
if (!user) this.whereNull('userId'); // eslint-disable-line no-invalid-this
else this.where('userId', user.id); // eslint-disable-line no-invalid-this
})
.where({ hash })
.first();
if (exists) {
res.json({
message: 'Successfully uploaded file BUT IT EXISTED ALREADY',
name: exists.name,
size: exists.size,
url: `${process.env.DOMAIN}/${exists.name}`,
deleteUrl: `${process.env.DOMAIN}/api/file/${exists.id}`
});
return Util.deleteFile(upload.filename);
}
/*
The file doesn't appear to exist yet for this user, so let's
store the details on the database.
*/
const now = moment.utc().toDate();
let inserted = null;
try {
inserted = await db.table('files').insert({
userId: user ? user.id : null,
name: upload.filename,
original: upload.originalname,
type: upload.mimetype || '',
size: upload.size,
hash,
ip: req.ip,
createdAt: now,
editedAt: now
}, 'id');
/*
TODO: Something funny here, I'm not sure since I don't use MySQL but I think the argument id
on the insert function on top behaves differently on psql/mysql/sqlite. Needs testing.
*/
} catch (error) {
log.error('There was an error saving the file to the database');
log.error(error);
return res.status(500).json({ message: 'There was an error uploading the file.' });
}
res.json({
message: 'Successfully uploaded file',
name: upload.filename,
size: upload.size,
url: `${process.env.DOMAIN}/${upload.filename}`
// deleteUrl: `${process.env.DOMAIN}/api/file/${exists.id}`
});
/*
If the upload had an album specified we make sure to create the relation
and update the according timestamps..
*/
if (albumId) {
try {
await db.table('albumsFiles').insert({ albumId, fileId: inserted[0] });
await db.table('albums').where('id', albumId).update('editedAt', now);
} catch (error) {
log.error('There was an error updating editedAt on an album');
log.error(error);
}
}
/*
If exif removal has been force service-wide or requested by the user, remove it
*/
if (process.env.STRIP_EXIF) { // || user.settings.stripExif) {
// Util.removeExif(upload.filename);
}
/*
Generate those thumbnails
*/
return Util.generateThumbnails(upload.filename);
}
uploadFile(req, res, db, user) {
const busboy = new Busboy({
headers: req.headers,
limits: {
fileSize: process.env.MAX_SIZE * (1000 * 1000),
files: 1
}
});
const fileToUpload = {
data: {},
body: {}
};
/*
Note: For this to work on every case, whoever is uploading a chunk
should really send the body first and the file last. Otherwise lolisafe
may not catch the field on time and the chunk may end up being saved
as a standalone file, completely broken.
*/
busboy.on('field', (fieldname, val) => {
if (/^dz/.test(fieldname)) {
fileToUpload.body[fieldname.substring(2)] = val;
} else {
fileToUpload.body[fieldname] = val;
}
});
/*
Hey ther's a file! Let's upload it.
*/
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
let name;
let saveTo;
/*
Let check whether the file is part of a chunk upload or if it's a standalone one.
If the former, we should store them separately and join all the pieces after we
receive the last one.
*/
const ext = path.extname(filename).toLowerCase();
if (Util.isExtensionBlocked(ext)) return res.status(400).json({ message: 'This extension is not allowed.' });
if (fileToUpload.body.uuid) {
name = `${filename}.${fileToUpload.body.chunkindex}`;
const chunkDir = path.join(__dirname, '..', '..', '..', '..', process.env.UPLOAD_FOLDER, 'chunks', fileToUpload.body.uuid);
jetpack.dir(chunkDir);
saveTo = path.join(__dirname, '..', '..', '..', '..', process.env.UPLOAD_FOLDER, 'chunks', fileToUpload.body.uuid, name);
} else {
name = Util.getUniqueFilename(filename);
if (!name) return res.status(500).json({ message: 'There was a problem allocating a filename for your upload' });
saveTo = path.join(__dirname, '..', '..', '..', '..', process.env.UPLOAD_FOLDER, name);
}
/*
Let's save some metadata for the db.
*/
fileToUpload.data = { filename: name, originalname: filename, encoding, mimetype };
const stream = fs.createWriteStream(saveTo);
file.on('data', data => {
fileToUpload.data.size = data.length;
});
/*
The file that is being uploaded is bigger than the limit specified on the config file
and thus we should close the stream and delete the file.
*/
file.on('limit', () => {
file.unpipe(stream);
stream.end();
jetpack.removeAsync(saveTo);
return res.status(400).json({ message: 'The file is too big.' });
});
file.on('error', err => {
log.error('There was an error uploading a file');
log.error(err);
return res.status(500).json({ message: 'There was an error uploading the file.' });
});
/*
TODO: Does this even work??
*/
return file.pipe(stream);
});
busboy.on('error', err => {
log.error('There was an error uploading a file');
log.error(err);
return res.status(500).json({ message: 'There was an error uploading the file.' });
});
busboy.on('finish', () => this.processFile(req, res, db, user, fileToUpload));
req.pipe(busboy);
}
}
module.exports = uploadPOST;
|