Clean up code

This commit is contained in:
Alan Grainger 2024-11-11 16:48:35 +01:00
parent 75f7ffb22d
commit e17137d1a8
2 changed files with 16 additions and 34 deletions

View File

@ -145,33 +145,14 @@ class Immich {
} }
/** /**
* Stream asset buffer data from Immich. * Get the content-type of a video, for passing back to lightGallery
*
* For photos, you can request 'thumbnail' or 'original' size.
* For videos, it is Immich's streaming quality, not the original quality.
*/ */
async getAssetBuffer (asset: Asset, size?: ImageSize) { async getVideoContentType (asset: Asset) {
switch (asset.type) { const data = await this.request(this.buildUrl('/assets/' + encodeURIComponent(asset.id) + '/video/playback', {
case AssetType.image: key: asset.key,
size = size === ImageSize.thumbnail ? ImageSize.thumbnail : ImageSize.original password: asset.password
return this.request(this.buildUrl('/assets/' + encodeURIComponent(asset.id) + '/' + size, { }))
key: asset.key, return data.headers.get('Content-Type')
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')
} }
/** /**

View File

@ -19,15 +19,18 @@ class Render {
size = size === ImageSize.thumbnail ? ImageSize.thumbnail : ImageSize.original size = size === ImageSize.thumbnail ? ImageSize.thumbnail : ImageSize.original
const subpath = asset.type === AssetType.video ? '/video/playback' : '/' + size const subpath = asset.type === AssetType.video ? '/video/playback' : '/' + size
const headers = { range: '' } const headers = { range: '' }
// Stream the video in 2.5MB chunks
if (asset.type === AssetType.video) { if (asset.type === AssetType.video) {
const start = (req.range || '').replace(/bytes=/, '').split('-')[0] const range = (req.range || '').replace(/bytes=/, '').split('-')
const startByte = parseInt(start, 10) || 0 const start = parseInt(range[0], 10) || 0
const endByte = startByte + 2499999 headers.range = `bytes=${start}-${start + 2499999}`
headers.range = `bytes=${startByte}-${endByte}`
headerList.push('cache-control', 'content-range') headerList.push('cache-control', 'content-range')
res.setHeader('accept-ranges', 'bytes') res.setHeader('accept-ranges', 'bytes')
res.status(206) // Partial Content res.status(206) // Partial Content
} }
// Request data from Immich
const url = immich.buildUrl(immich.apiUrl() + '/assets/' + encodeURIComponent(asset.id) + subpath, { const url = immich.buildUrl(immich.apiUrl() + '/assets/' + encodeURIComponent(asset.id) + subpath, {
key: asset.key, key: asset.key,
password: asset.password password: asset.password
@ -44,9 +47,7 @@ class Render {
// Return the body // Return the body
await data.body?.pipeTo( await data.body?.pipeTo(
new WritableStream({ new WritableStream({
write (chunk) { write (chunk) { res.write(chunk) }
res.write(chunk)
}
}) })
) )
res.end() res.end()
@ -72,7 +73,7 @@ class Render {
source: [ source: [
{ {
src: immich.videoUrl(share.key, asset.id, asset.password), src: immich.videoUrl(share.key, asset.id, asset.password),
type: await immich.getContentType(asset) type: await immich.getVideoContentType(asset)
} }
], ],
attributes: { attributes: {