aboutsummaryrefslogtreecommitdiff
path: root/src/api/utils/ThumbUtil.js
blob: b449a6c45770e7268108b3b25f9f55e3a80f1246 (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
const jetpack = require('fs-jetpack');
const path = require('path');
const sharp = require('sharp');
const ffmpeg = require('fluent-ffmpeg');
const previewUtil = require('./videoPreview/FragmentPreview');

const log = require('./Log');

class ThumbUtil {
	static imageExtensions = ['.jpg', '.jpeg', '.gif', '.png', '.webp'];
	static videoExtensions = ['.webm', '.mp4', '.wmv', '.avi', '.mov'];

	static thumbPath = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs');
	static squareThumbPath = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'square');
	static videoPreviewPath = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, 'thumbs', 'preview');

	static generateThumbnails(filename) {
		const ext = path.extname(filename).toLowerCase();
		const output = `${filename.slice(0, -ext.length)}.png`;
		const previewOutput = `${filename.slice(0, -ext.length)}.webm`;

		if (ThumbUtil.imageExtensions.includes(ext)) return this.generateThumbnailForImage(filename, output);
		if (ThumbUtil.videoExtensions.includes(ext)) return this.generateThumbnailForVideo(filename, previewOutput);
		return null;
	}

	static async generateThumbnailForImage(filename, output) {
		const filePath = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename);

		const file = await jetpack.readAsync(filePath, 'buffer');
		await sharp(file)
			.resize(64, 64)
			.toFormat('png')
			.toFile(path.join(ThumbUtil.squareThumbPath, output));
		await sharp(file)
			.resize(225, null)
			.toFormat('png')
			.toFile(path.join(ThumbUtil.thumbPath, output));
	}

	static async generateThumbnailForVideo(filename, output) {
		const filePath = path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename);

		ffmpeg(filePath)
			.thumbnail({
				timestamps: [0],
				filename: '%b.png',
				folder: ThumbUtil.squareThumbPath,
				size: '64x64'
			})
			.on('error', error => log.error(error.message));

		ffmpeg(filePath)
			.thumbnail({
				timestamps: [0],
				filename: '%b.png',
				folder: ThumbUtil.thumbPath,
				size: '150x?'
			})
			.on('error', error => log.error(error.message));

		try {
			await previewUtil({
				input: filePath,
				width: 150,
				output: path.join(ThumbUtil.videoPreviewPath, output),
				log: console.log
			});
		} catch (e) {
			console.error(e);
		}
	}

	static getFileThumbnail(filename) {
		// TODO: refactor so we don't do the same compare multiple times (poor cpu cycles)
		if (!filename) return null;
		const ext = path.extname(filename).toLowerCase();
		if (!ThumbUtil.imageExtensions.includes(ext) && !ThumbUtil.videoExtensions.includes(ext)) return null;
		if (ThumbUtil.imageExtensions.includes(ext)) return { thumb: `${filename.slice(0, -ext.length)}.png` };
		if (ThumbUtil.videoExtensions.includes(ext))
			return {
				thumb: `${filename.slice(0, -ext.length)}.png`,
				preview: `${filename.slice(0, -ext.length)}.webm`
			};
	}

	static async removeThumbs(thumbName) {
		await jetpack.removeAsync(path.join(ThumbUtil.thumbPath, thumbName));
		await jetpack.removeAsync(ThumbUtil.squareThumbPath, thumbName);
	}
}

module.exports = ThumbUtil;