From fb0fa742f561dfa6a91aaf89d29876245600834e Mon Sep 17 00:00:00 2001 From: Thanh Pham Date: Wed, 24 Aug 2022 10:21:41 +0700 Subject: [PATCH] fix(web): buffering for video player (#520) * fix(web): buffering for video player * chore(): missing file -_- * refactor(web): using URL builder * chore(): add semicolon * fix(web): video player * remove deadcode Co-authored-by: Alex --- web/src/api/index.ts | 1 + web/src/api/utils.ts | 12 +++ .../asset-viewer/video-viewer.svelte | 52 ++++------ .../shared-components/immich-thumbnail.svelte | 97 +++++++------------ 4 files changed, 67 insertions(+), 95 deletions(-) create mode 100644 web/src/api/utils.ts diff --git a/web/src/api/index.ts b/web/src/api/index.ts index 5fb6580e20..cf4e108bdb 100644 --- a/web/src/api/index.ts +++ b/web/src/api/index.ts @@ -1,2 +1,3 @@ export * from './open-api'; export * from './api'; +export * from './utils'; diff --git a/web/src/api/utils.ts b/web/src/api/utils.ts new file mode 100644 index 0000000000..e67896ad70 --- /dev/null +++ b/web/src/api/utils.ts @@ -0,0 +1,12 @@ +let _basePath = '/api'; + +export function getFileUrl(aid: string, did: string, isThumb?: boolean, isWeb?: boolean) { + const urlObj = new URL(`${window.location.origin}${_basePath}/asset/file`); + + urlObj.searchParams.append('aid', aid); + urlObj.searchParams.append('did', did); + if (isThumb !== undefined && isThumb !== null) urlObj.searchParams.append('isThumb', `${isThumb}`); + if (isWeb !== undefined && isWeb !== null) urlObj.searchParams.append('isWeb', `${isWeb}`); + + return urlObj.href; +} diff --git a/web/src/lib/components/asset-viewer/video-viewer.svelte b/web/src/lib/components/asset-viewer/video-viewer.svelte index 22d57e1669..4e38a17c62 100644 --- a/web/src/lib/components/asset-viewer/video-viewer.svelte +++ b/web/src/lib/components/asset-viewer/video-viewer.svelte @@ -3,7 +3,7 @@ import { createEventDispatcher, onMount } from 'svelte'; import LoadingSpinner from '../shared-components/loading-spinner.svelte'; - import { api, AssetResponseDto } from '@api'; + import { api, AssetResponseDto, getFileUrl } from '@api'; export let assetId: string; @@ -13,48 +13,32 @@ let videoPlayerNode: HTMLVideoElement; let isVideoLoading = true; + let videoUrl: string; onMount(async () => { const { data: assetInfo } = await api.assetApi.getAssetById(assetId); - asset = assetInfo; + await loadVideoData(assetInfo); - await loadVideoData(); + asset = assetInfo; }); - const loadVideoData = async () => { + const loadVideoData = async (assetInfo: AssetResponseDto) => { isVideoLoading = true; - try { - const { data } = await api.assetApi.serveFile( - asset.deviceAssetId, - asset.deviceId, - false, - true, - { - responseType: 'blob' - } - ); + videoUrl = getFileUrl(assetInfo.deviceAssetId, assetInfo.deviceId, false, true); - if (!(data instanceof Blob)) { - return; - } + return assetInfo; + }; - const videoData = URL.createObjectURL(data); - videoPlayerNode.src = videoData; + const handleCanPlay = (ev: Event) => { + const playerNode = ev.target as HTMLVideoElement; - videoPlayerNode.load(); + playerNode.muted = true; + playerNode.play(); + playerNode.muted = false; - videoPlayerNode.oncanplay = () => { - videoPlayerNode.muted = true; - videoPlayerNode.play(); - videoPlayerNode.muted = false; - - isVideoLoading = false; - }; - - return videoData; - } catch (e) {} + isVideoLoading = false; }; @@ -63,7 +47,13 @@ class="flex place-items-center place-content-center h-full select-none" > {#if asset} -