From e17137d1a81fd18e0ea9c713729d0b2d745e3cf2 Mon Sep 17 00:00:00 2001 From: Alan Grainger Date: Mon, 11 Nov 2024 16:48:35 +0100 Subject: [PATCH] Clean up code --- app/src/immich.ts | 33 +++++++-------------------------- app/src/render.ts | 17 +++++++++-------- 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/app/src/immich.ts b/app/src/immich.ts index efb5696..b1a33ab 100644 --- a/app/src/immich.ts +++ b/app/src/immich.ts @@ -145,33 +145,14 @@ class Immich { } /** - * Stream asset buffer data from Immich. - * - * For photos, you can request 'thumbnail' or 'original' size. - * For videos, it is Immich's streaming quality, not the original quality. + * Get the content-type of a video, for passing back to lightGallery */ - async getAssetBuffer (asset: Asset, size?: ImageSize) { - switch (asset.type) { - case AssetType.image: - size = size === ImageSize.thumbnail ? ImageSize.thumbnail : ImageSize.original - return this.request(this.buildUrl('/assets/' + encodeURIComponent(asset.id) + '/' + size, { - key: asset.key, - password: asset.password - })) - case AssetType.video: - return this.request(this.buildUrl('/assets/' + encodeURIComponent(asset.id) + '/video/playback', { - key: asset.key, - password: asset.password - })) - } - } - - /** - * Get the content-type of an Immich asset - */ - async getContentType (asset: Asset) { - const assetBuffer = await this.getAssetBuffer(asset) - return assetBuffer.headers.get('Content-Type') + async getVideoContentType (asset: Asset) { + const data = await this.request(this.buildUrl('/assets/' + encodeURIComponent(asset.id) + '/video/playback', { + key: asset.key, + password: asset.password + })) + return data.headers.get('Content-Type') } /** diff --git a/app/src/render.ts b/app/src/render.ts index 09d4550..3d0e4dc 100644 --- a/app/src/render.ts +++ b/app/src/render.ts @@ -19,15 +19,18 @@ class Render { size = size === ImageSize.thumbnail ? ImageSize.thumbnail : ImageSize.original const subpath = asset.type === AssetType.video ? '/video/playback' : '/' + size const headers = { range: '' } + + // Stream the video in 2.5MB chunks if (asset.type === AssetType.video) { - const start = (req.range || '').replace(/bytes=/, '').split('-')[0] - const startByte = parseInt(start, 10) || 0 - const endByte = startByte + 2499999 - headers.range = `bytes=${startByte}-${endByte}` + const range = (req.range || '').replace(/bytes=/, '').split('-') + const start = parseInt(range[0], 10) || 0 + headers.range = `bytes=${start}-${start + 2499999}` headerList.push('cache-control', 'content-range') res.setHeader('accept-ranges', 'bytes') res.status(206) // Partial Content } + + // Request data from Immich const url = immich.buildUrl(immich.apiUrl() + '/assets/' + encodeURIComponent(asset.id) + subpath, { key: asset.key, password: asset.password @@ -44,9 +47,7 @@ class Render { // Return the body await data.body?.pipeTo( new WritableStream({ - write (chunk) { - res.write(chunk) - } + write (chunk) { res.write(chunk) } }) ) res.end() @@ -72,7 +73,7 @@ class Render { source: [ { src: immich.videoUrl(share.key, asset.id, asset.password), - type: await immich.getContentType(asset) + type: await immich.getVideoContentType(asset) } ], attributes: {