-
Notifications
You must be signed in to change notification settings - Fork 274
/
meta.ts
59 lines (52 loc) · 1.52 KB
/
meta.ts
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
import type { ImageInfo, ImageCTX } from '../../types/image'
export async function imageMeta(_ctx: ImageCTX, url: string): Promise<ImageInfo> {
// TODO: Reimplement cache using storage
// const cache = getCache<ImageInfo>(ctx)
// const cacheKey = 'image:meta:' + url
// if (cache.has(cacheKey)) {
// return cache.get(cacheKey)
// }
const meta = await _imageMeta(url).catch((err) => {
console.error('Failed to get image meta for ' + url, err + '')
return {
width: 0,
height: 0,
ratio: 0,
}
})
// cache.set(cacheKey, meta)
return meta
}
async function _imageMeta(url: string): Promise<ImageInfo> {
if (import.meta.server) {
const imageMeta = await import('image-meta').then(r => r.imageMeta)
const data: Buffer = await fetch(url).then((res: any) => res.buffer())
const metadata = imageMeta(data)
if (!metadata) {
throw new Error(`No metadata could be extracted from the image \`${url}\`.`)
}
const { width, height } = metadata
const meta = {
width: width!,
height: height!,
ratio: width && height ? width / height : undefined,
}
return meta
}
if (typeof Image === 'undefined') {
throw new TypeError('Image not supported')
}
return new Promise((resolve, reject) => {
const img = new Image()
img.onload = () => {
const meta = {
width: img.width,
height: img.height,
ratio: img.width / img.height,
}
resolve(meta)
}
img.onerror = err => reject(err)
img.src = url
})
}