aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/image/src/utils.ts
blob: 80dff1b6ea4e20a352e86d71b2aaa7ef71dc604b (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
import fs from 'fs';
import path from 'path';
import { shorthash } from './shorthash.js';
import type { OutputFormat, TransformOptions } from './types';

export function isOutputFormat(value: string): value is OutputFormat {
	return ['avif', 'jpeg', 'png', 'webp'].includes(value);
}

export function isAspectRatioString(value: string): value is `${number}:${number}` {
	return /^\d*:\d*$/.test(value);
}

export function ensureDir(dir: string) {
	fs.mkdirSync(dir, { recursive: true });
}

export function isRemoteImage(src: string) {
	return /^http(s?):\/\//.test(src);
}

export async function loadLocalImage(src: string) {
	try {
		return await fs.promises.readFile(src);
	} catch {
		return undefined;
	}
}

export async function loadRemoteImage(src: string) {
	try {
		const res = await fetch(src);

		if (!res.ok) {
			return undefined;
		}

		return Buffer.from(await res.arrayBuffer());
	} catch {
		return undefined;
	}
}

export async function loadImage(src: string) {
	return isRemoteImage(src) ? await loadRemoteImage(src) : await loadLocalImage(src);
}

export function propsToFilename({ src, width, height, format }: TransformOptions) {
	const ext = path.extname(src);
	let filename = src.replace(ext, '');

	// for remote images, add a hash of the full URL to dedupe images with the same filename
	if (isRemoteImage(src)) {
		filename += `-${shorthash(src)}`;
	}

	if (width && height) {
		return `${filename}_${width}x${height}.${format}`;
	} else if (width) {
		return `${filename}_${width}w.${format}`;
	} else if (height) {
		return `${filename}_${height}h.${format}`;
	}

	return format ? src.replace(ext, format) : src;
}

export function parseAspectRatio(aspectRatio: TransformOptions['aspectRatio']) {
	if (!aspectRatio) {
		return undefined;
	}

	// parse aspect ratio strings, if required (ex: "16:9")
	if (typeof aspectRatio === 'number') {
		return aspectRatio;
	} else {
		const [width, height] = aspectRatio.split(':');
		return parseInt(width) / parseInt(height);
	}
}