{"version":3,"file":"preview-BIbJGxXF.mjs","sources":["../../lib/utils/imagePreload.ts","../../lib/composables/preview.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport PQueue from 'p-queue'\n\nconst queue = new PQueue({ concurrency: 5 })\n\n/**\n * Preload an image URL\n *\n * @param url - URL of the image\n */\nexport function preloadImage(url: string): Promise<boolean> {\n\tconst { resolve, promise } = Promise.withResolvers<boolean>()\n\tqueue.add(() => {\n\t\tconst image = new Image()\n\t\timage.onerror = () => resolve(false)\n\t\timage.onload = () => resolve(true)\n\t\timage.src = url\n\t\treturn promise\n\t})\n\n\treturn promise\n}\n","/**\n * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { INode } from '@nextcloud/files'\nimport type { MaybeRef } from 'vue'\n\nimport { FileType } from '@nextcloud/files'\nimport { generateUrl } from '@nextcloud/router'\nimport { ref, toValue, watchEffect } from 'vue'\nimport { preloadImage } from '../utils/imagePreload.ts'\n\ninterface PreviewOptions {\n\t/**\n\t * Size of the previews in px\n\t *\n\t * @default 32\n\t */\n\tsize?: number\n\t/**\n\t * Should the preview fall back to the mime type icon\n\t *\n\t * @default true\n\t */\n\tmimeFallback?: boolean\n\t/**\n\t * Should the preview be cropped or fitted\n\t *\n\t * @default false (meaning it gets fitted)\n\t */\n\tcropPreview?: boolean\n}\n\n/**\n * Generate the preview URL of a file node\n *\n * @param node The node to generate the preview for\n * @param options Preview options\n */\nexport function getPreviewURL(node: INode, options: PreviewOptions = {}) {\n\toptions = { size: 32, cropPreview: false, mimeFallback: true, ...options }\n\n\ttry {\n\t\tconst previewUrl = node.attributes?.previewUrl\n\t\t\t|| generateUrl('/core/preview?fileId={fileid}', {\n\t\t\t\tfileid: node.fileid,\n\t\t\t})\n\n\t\tlet url\n\t\ttry {\n\t\t\turl = new URL(previewUrl)\n\t\t} catch {\n\t\t\turl = new URL(previewUrl, window.location.origin)\n\t\t}\n\n\t\t// Request preview with params\n\t\turl.searchParams.set('x', `${options.size}`)\n\t\turl.searchParams.set('y', `${options.size}`)\n\t\turl.searchParams.set('mimeFallback', `${options.mimeFallback}`)\n\n\t\t// Handle cropping\n\t\turl.searchParams.set('a', options.cropPreview === true ? '0' : '1')\n\n\t\t// cache busting\n\t\turl.searchParams.set('c', `${node.attributes.etag}`)\n\t\treturn url\n\t} catch {\n\t\treturn null\n\t}\n}\n\n/**\n * @param node - The node to get the preview of\n * @param options - Preview options\n */\nexport function usePreviewURL(node: MaybeRef<INode>, options?: MaybeRef<PreviewOptions>) {\n\tconst previewURL = ref<URL | null>(null)\n\tconst previewLoaded = ref(false)\n\n\twatchEffect(() => {\n\t\tpreviewLoaded.value = false\n\t\tpreviewURL.value = getPreviewURL(toValue(node), toValue(options || {}))\n\t\tif (previewURL.value && toValue(node).type === FileType.File) {\n\t\t\tpreloadImage(previewURL.value.href).then((success: boolean) => {\n\t\t\t\tpreviewLoaded.value = success\n\t\t\t})\n\t\t}\n\t})\n\n\treturn {\n\t\tpreviewURL,\n\t\tpreviewLoaded,\n\t}\n}\n"],"names":[],"mappings":";;;;AAOA,MAAM,QAAQ,IAAI,OAAO,EAAE,aAAa,GAAG;AAOpC,SAAS,aAAa,KAA+B;AAC3D,QAAM,EAAE,SAAS,YAAY,QAAQ,cAAA;AACrC,QAAM,IAAI,MAAM;AACf,UAAM,QAAQ,IAAI,MAAA;AAClB,UAAM,UAAU,MAAM,QAAQ,KAAK;AACnC,UAAM,SAAS,MAAM,QAAQ,IAAI;AACjC,UAAM,MAAM;AACZ,WAAO;AAAA,EACR,CAAC;AAED,SAAO;AACR;ACeO,SAAS,cAAc,MAAa,UAA0B,IAAI;AACxE,YAAU,EAAE,MAAM,IAAI,aAAa,OAAO,cAAc,MAAM,GAAG,QAAA;AAEjE,MAAI;AACH,UAAM,aAAa,KAAK,YAAY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