this repo has no description
1export function extractDataUriMime(uri: string): string {
2 return uri.substring(uri.indexOf(':') + 1, uri.indexOf(';'))
3}
4
5// Fairly accurate estimate that is more performant
6// than decoding and checking length of URI
7export function getDataUriSize(uri: string): number {
8 return Math.round((uri.length * 3) / 4)
9}
10
11export function isUriImage(uri: string): boolean {
12 return /\.(jpg|jpeg|png|webp).*$/.test(uri)
13}
14
15export function blobToDataUri(blob: Blob): Promise<string> {
16 return new Promise((resolve, reject) => {
17 const reader = new FileReader()
18 reader.onloadend = () => {
19 if (typeof reader.result === 'string') {
20 resolve(reader.result)
21 } else {
22 reject(new Error('Failed to read blob'))
23 }
24 }
25 reader.onerror = reject
26 reader.readAsDataURL(blob)
27 })
28}
29
30export type ImgproxyPreset =
31 | 'default'
32 | 'avatar_thumbnail'
33 | 'avatar'
34 | 'banner'
35 | 'feed_fullsize'
36 | 'feed_thumbnail'
37 | 'download'
38
39// Using capturing groups here instead of lookbehinds in order to support older versions of Safari.
40// https://bugs.webkit.org/show_bug.cgi?id=174931
41const IMGPROXY_PRESET_RE =
42 /(\/img\/)(default|avatar_thumbnail|avatar|banner|feed_fullsize|feed_thumbnail|download)(\/)/
43
44/**
45 * Replaces any imgproxy preset in a CDN URI with the given preset.
46 */
47export function convertCdnPreset(uri: string, preset: ImgproxyPreset): string {
48 return uri.replace(IMGPROXY_PRESET_RE, `$1${preset}$3`)
49}