Line data Source code
1 : import { api } from '@/scripts/api'
2 :
3 2356 : const HDR_EXTENSIONS = ['.exr', '.hdr'] as const
4 :
5 14014 : export function isHdrImageFilename(filename: string | undefined): boolean {
6 14014 : if (!filename) return false
7 13073 : const lower = filename.toLowerCase()
8 13073 : return HDR_EXTENSIONS.some((ext) => lower.endsWith(ext))
9 : }
10 :
11 234 : export function getImageFilenameFromUrl(url: string): string | undefined {
12 234 : if (!url) return undefined
13 234 : try {
14 234 : const parsed = new URL(url, window.location.origin)
15 234 : return (
16 : parsed.searchParams.get('filename') ??
17 : parsed.pathname.split('/').pop() ??
18 : undefined
19 : )
20 : } catch {
21 0 : return url.split('/').pop()
22 : }
23 : }
24 :
25 230 : export function isHdrImageUrl(url: string | undefined): boolean {
26 230 : if (!url) return false
27 230 : return isHdrImageFilename(getImageFilenameFromUrl(url))
28 : }
29 :
30 224 : export function getHdrPreviewUrl(url: string | undefined): string | undefined {
31 224 : const previewId =
32 : url &&
33 : isHdrImageUrl(url) &&
34 : new URL(url, window.location.origin).searchParams.get('preview_id')
35 224 : return previewId ? hdrPreviewAssetUrl(previewId) : undefined
36 : }
37 :
38 3 : export function fullResolutionSrc(img: HTMLImageElement): string {
39 3 : return img.dataset.fullSrc ?? img.src
40 : }
41 :
42 : /** The file an image shows, rather than the stored preview it may display. */
43 4 : export function getImageFileUrl(img: HTMLImageElement): URL {
44 4 : const url = new URL(
45 : img.dataset.fileUrl ?? fullResolutionSrc(img),
46 : img.baseURI
47 : )
48 4 : url.searchParams.delete('preview')
49 4 : return url
50 : }
51 :
52 2 : export function hdrPreviewAssetUrl(previewId: string): string {
53 2 : return api.apiURL(`/assets/${previewId}/content?disposition=inline`)
54 : }
55 :
56 2 : export function toFullResolutionUrl(url: string): string {
57 2 : try {
58 2 : const parsed = new URL(url, window.location.origin)
59 2 : parsed.searchParams.delete('preview')
60 2 : return url.startsWith('http')
61 : ? parsed.toString()
62 : : `${parsed.pathname}${parsed.search}`
63 : } catch {
64 0 : return url
65 : }
66 : }
|