summaryrefslogtreecommitdiff
path: root/src/drive/gen-thumbnail.ts
blob: 455705cd3a62b7363241ba2a4f09240a1ebb0ade (plain)
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
import * as stream from 'stream';
import * as Gm from 'gm';
import { IDriveFile, getDriveFileBucket } from '../models/drive-file';

const gm = Gm.subClass({
	imageMagick: true
});

export default async function(file: IDriveFile): Promise<stream.Readable> {
	if (!/^image\/.*$/.test(file.contentType)) return null;

	const bucket = await getDriveFileBucket();
	const readable = bucket.openDownloadStream(file._id);

	const g = gm(readable);

	const stream = g
		.resize(256, 256)
		.compress('jpeg')
		.quality(70)
		.interlace('line')
		.stream();

	return stream;
}