aboutsummaryrefslogtreecommitdiff
path: root/src/api/utils/Util.js
blob: 46f56d55630d061dd97bd690062ca94ae56f4998 (plain) (blame)
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
const config = require('../../../config');
const jetpack = require('fs-jetpack');
const randomstring = require('randomstring');
const path = require('path');
const JWT = require('jsonwebtoken');
const db = require('knex')(config.server.database);
const moment = require('moment');
const log = require('../utils/Log');
const crypto = require('crypto');
const sharp = require('sharp');
const ffmpeg = require('fluent-ffmpeg');

const imageExtensions = ['.jpg', '.jpeg', '.bmp', '.gif', '.png', '.webp'];
const videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov'];

class Util {
	static isExtensionBlocked(extension) {
		return config.uploads.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, '..', '..', '..', config.uploads.uploadFolder, filename), 'buffer');
		const writer = jetpack.createWriteStream(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, `${filename}.noexif`));
		toStream(file).pipe(new ExifTransformer()).pipe(writer);
	}
	*/

	static async generateThumbnailForImage(filename, output) {
		const file = await jetpack.readAsync(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, filename), 'buffer');
		await sharp(file)
			.resize(64, 64)
			.toFormat('png')
			.toFile(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, 'thumbs', 'square', output));
		await sharp(file)
			.resize(225, null)
			.toFormat('png')
			.toFile(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, 'thumbs', output));
	}

	static generateThumbnailForVideo(filename) {
		ffmpeg(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, filename))
			.thumbnail({
				timestamps: [0],
				filename: '%b.png',
				folder: path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, 'thumbs', 'square'),
				size: '64x64'
			})
			.on('error', error => log.error(error.message));
		ffmpeg(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, filename))
			.thumbnail({
				timestamps: [0],
				filename: '%b.png',
				folder: path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, '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) {
		file.url = `${config.filesServeLocation}/${file.name}`;
		const thumb = this.getFileThumbnail(file.name);
		if (thumb) {
			file.thumb = `${config.filesServeLocation}/thumbs/${thumb}`;
			file.thumbSquare = `${config.filesServeLocation}/thumbs/square/${thumb}`;
		}
		return file;
	}

	static getUniqueFilename(name) {
		const retry = (i = 0) => {
			const filename = randomstring.generate({
				length: config.uploads.generatedFilenameLength,
				capitalization: 'lowercase'
			}) + path.extname(name);
			const exists = jetpack.exists(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, filename));
			if (!exists) return filename;
			if (i < config.uploads.retryFilenameTimes) return retry(i++);
			return null;
		};
		return retry();
	}

	static getUniqueAlbumIdentifier() {
		const retry = async (i = 0) => {
			const identifier = randomstring.generate({
				length: config.uploads.generatedAlbumLinkLength,
				capitalization: 'lowercase'
			});
			const exists = await db.table('links').where({ identifier }).first();
			if (!exists) return identifier;
			if (i < config.uploads.retryAlbumLinkTimes) return retry(i++);
			return null;
		};
		return retry();
	}

	static async getFileHash(filename) {
		const file = await jetpack.readAsync(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, 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) {
		try {
			await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, filename));
			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 files = await db.table('files').where({ albumId: id });
			for (const file of files) {
				await jetpack.removeAsync(path.join(__dirname, '..', '..', '..', config.uploads.uploadFolder, file));
			}
			await db.table('files').where({ albumId: id }).delete();
		} 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, config.server.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;
		});
	}
}

module.exports = Util;