1
0
Fork 0
mirror of https://github.com/alangrainger/immich-public-proxy.git synced 2025-01-15 12:36:44 +01:00

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.
*
* 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')
}
/**

View file

@ -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: {