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
|
const jetpack = require('fs-jetpack');
const randomstring = require('randomstring');
const path = require('path');
const JWT = require('jsonwebtoken');
const db = require('knex')({
client: process.env.DB_CLIENT,
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
filename: path.join(__dirname, '..', '..', '..', 'database.sqlite')
},
useNullAsDefault: process.env.DB_CLIENT === 'sqlite' ? true : false
});
const moment = require('moment');
const log = require('../utils/Log');
const crypto = require('crypto');
const sharp = require('sharp');
const ffmpeg = require('fluent-ffmpeg');
const Zip = require('adm-zip');
const uuidv4 = require('uuid/v4');
const imageExtensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webp'];
const videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov'];
const blockedExtensions = process.env.BLOCKED_EXTENSIONS.split(',');
class Util {
static uuid() {
return uuidv4();
}
static isExtensionBlocked(extension) {
return blockedExtensions.includes(extension);
}
static generateThumbnails(filename) {
const ext = path.extname(filename).toLowerCase();
const output = `${filename.slice(0, -ext.length)}.png`;
if (imageExtensions.includes(ext)) return this.generateThumbnailForImage(filename, output);
if (videoExtensions.includes(ext)) return this.generateThumbnailForVideo(filename);
return null;
}
/*
static async removeExif(filename) {
This needs more testing.
Even though the exif data seems to be stripped, no other online service
is recognizing the file as an image file.
const ExifTransformer = require('exif-be-gone');
const toStream = require('buffer-to-stream');
const file = await jetpack.readAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename), 'buffer');
const writer = jetpack.createWriteStream(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, `${filename}.noexif`));
toStream(file).pipe(new ExifTransformer()).pipe(writer);
}
*/
static async generateThumbnailForImage(filename, output) {
const file = await jetpack.readAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename), 'buffer');
await sharp(file)
.resize(64, 64)
.toFormat('png')
.toFile(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square', output));
await sharp(file)
.resize(225, null)
.toFormat('png')
.toFile(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', output));
}
static generateThumbnailForVideo(filename) {
ffmpeg(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename))
.thumbnail({
timestamps: [0],
filename: '%b.png',
folder: path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square'),
size: '64x64'
})
.on('error', error => log.error(error.message));
ffmpeg(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename))
.thumbnail({
timestamps: [0],
filename: '%b.png',
folder: path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs'),
size: '150x?'
})
.on('error', error => log.error(error.message));
}
static getFileThumbnail(filename) {
const ext = path.extname(filename).toLowerCase();
if (!imageExtensions.includes(ext) && !videoExtensions.includes(ext)) return null;
return `${filename.slice(0, -ext.length)}.png`;
}
static constructFilePublicLink(file) {
/*
TODO: This wont work without a reverse proxy serving both
the site and the API under the same domain. Pls fix.
*/
file.url = `${process.env.DOMAIN}/${file.name}`;
const thumb = this.getFileThumbnail(file.name);
if (thumb) {
file.thumb = `${process.env.DOMAIN}/thumbs/${thumb}`;
file.thumbSquare = `${process.env.DOMAIN}/thumbs/square/${thumb}`;
}
return file;
}
static getUniqueFilename(name) {
const retry = (i = 0) => {
const filename = randomstring.generate({
length: parseInt(process.env.GENERATED_FILENAME_LENGTH, 10),
capitalization: 'lowercase'
}) + path.extname(name).toLowerCase();
// TODO: Change this to look for the file in the db instead of in the filesystem
const exists = jetpack.exists(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename));
if (!exists) return filename;
if (i < 5) return retry(i + 1);
log.error('Couldnt allocate identifier for file');
return null;
};
return retry();
}
static getUniqueAlbumIdentifier() {
const retry = async (i = 0) => {
const identifier = randomstring.generate({
length: parseInt(process.env.GENERATED_ALBUM_LENGTH, 10),
capitalization: 'lowercase'
});
const exists = await db.table('links').where({ identifier }).first();
if (!exists) return identifier;
/*
It's funny but if you do i++ the asignment never gets done resulting in an infinite loop
*/
if (i < 5) return retry(i + 1);
log.error('Couldnt allocate identifier for album');
return null;
};
return retry();
}
static async getFileHash(filename) {
const file = await jetpack.readAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename), 'buffer');
if (!file) {
log.error(`There was an error reading the file < ${filename} > for hashing`);
return null;
}
const hash = crypto.createHash('md5');
hash.update(file, 'utf8');
return hash.digest('hex');
}
static getFilenameFromPath(fullPath) {
return fullPath.replace(/^.*[\\\/]/, ''); // eslint-disable-line no-useless-escape
}
static async deleteFile(filename, deleteFromDB = false) {
const thumbName = this.getFileThumbnail(filename);
try {
await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename));
await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', thumbName));
await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square', thumbName));
if (deleteFromDB) {
await db.table('files').where('name', filename).delete();
}
} catch (error) {
log.error(`There was an error removing the file < ${filename} >`);
log.error(error);
}
}
static async deleteAllFilesFromAlbum(id) {
try {
const fileAlbums = await db.table('albumsFiles').where({ albumId: id });
for (const fileAlbum of fileAlbums) {
const file = await db.table('files')
.where({ id: fileAlbum.fileId })
.first();
if (!file) continue;
await this.deleteFile(file.name, true);
}
} catch (error) {
log.error(error);
}
}
static async deleteAllFilesFromUser(id) {
try {
const files = await db.table('files').where({ userId: id });
for (const file of files) {
await this.deleteFile(file.name, true);
}
} catch (error) {
log.error(error);
}
}
static async deleteAllFilesFromTag(id) {
try {
const fileTags = await db.table('fileTags').where({ tagId: id });
for (const fileTag of fileTags) {
const file = await db.table('files')
.where({ id: fileTag.fileId })
.first();
if (!file) continue;
await this.deleteFile(file.name, true);
}
} catch (error) {
log.error(error);
}
}
static isAuthorized(req) {
if (!req.headers.authorization) return false;
const token = req.headers.authorization.split(' ')[1];
if (!token) return false;
return JWT.verify(token, process.env.SECRET, async (error, decoded) => {
if (error) {
log.error(error);
return false;
}
const id = decoded ? decoded.sub : '';
const iat = decoded ? decoded.iat : '';
const user = await db.table('users').where({ id }).first();
if (!user || !user.enabled) return false;
if (iat && iat < moment(user.passwordEditedAt).format('x')) return false;
return user;
});
}
static createZip(files, album) {
try {
const zip = new Zip();
for (const file of files) {
zip.addLocalFile(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, file));
}
zip.writeZip(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'zips', `${album.userId}-${album.id}.zip`));
} catch (error) {
log.error(error);
}
}
}
module.exports = Util;
|