This commit is contained in:
Alan Grainger
2024-10-29 15:07:54 +01:00
parent 85b1d8de09
commit 9ddfbcec67
6 changed files with 141 additions and 67 deletions
+38 -4
View File
@@ -1,6 +1,10 @@
import { Asset, AssetType, ImageSize, SharedLink } from './types'
class Immich {
/**
* Make a request to Immich API. We're not using the SDK to limit
* the possible attack surface of this app.
*/
async request (endpoint: string) {
const res = await fetch(process.env.IMMICH_URL + '/api' + endpoint, {
headers: {
@@ -17,11 +21,22 @@ class Immich {
}
}
/**
* Query Immich for the SharedLink metadata for a given key.
* The key is what is returned in the URL when you create a share in Immich.
* Immich doesn't have a method to query by key, so this method gets all
* known shared links, and returns the link which matches the provided key.
*/
async getShareByKey (key: string) {
const res = (await this.request('/shared-links') || []) as SharedLink[]
return res?.find(x => x.key === key)
}
/**
* 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.
*/
async getAssetBuffer (asset: Asset, size?: ImageSize) {
switch (asset.type) {
case AssetType.image:
@@ -32,22 +47,41 @@ class Immich {
}
}
/**
* Get the content-type of an Immich asset
*/
async getContentType (asset: Asset) {
const assetBuffer = await this.getAssetBuffer(asset)
return assetBuffer.headers.get('Content-Type')
}
photoUrl (id: string, size?: ImageSize) {
return `${process.env.SERVER_URL}/photo/${id}` + (size ? `?size=${size}` : '')
/**
* Return the image data URL for a photo
*/
photoUrl (key: string, id: string, size?: ImageSize) {
return `${process.env.SERVER_URL}/photo/${key}/${id}` + (size ? `?size=${size}` : '')
}
videoUrl (id: string) {
return `${process.env.SERVER_URL}/video/${id}`
/**
* Return the video data URL for a video
*/
videoUrl (key: string, id: string) {
return `${process.env.SERVER_URL}/video/${key}/${id}`
}
/**
* Check if a provided ID matches the Immich ID format
*/
isId (id: string) {
return !!id.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)
}
/**
* Check if a provided key matches the Immich shared-link key format
*/
isKey (key: string) {
return !!key.match(/^[\w-]+$/)
}
}
const immich = new Immich()
+23 -19
View File
@@ -16,42 +16,46 @@ const getSize = (req: Request) => {
}
app.get('/share/:key', async (req, res) => {
if (req.params.key.match(/[^\w-]/)) {
// Invalid characters in the incoming URL
if (!immich.isKey(req.params.key)) {
res.status(404).send()
} else {
const share = await immich.getShareByKey(req.params.key)
if (!share || !share.assets.length) {
const sharedLink = await immich.getShareByKey(req.params.key)
if (!sharedLink || !sharedLink.assets.length) {
res.status(404).send()
} else if (share.assets.length === 1) {
} else if (sharedLink.assets.length === 1) {
// This is an individual item (not a gallery)
const asset = share.assets[0]
const asset = sharedLink.assets[0]
if (asset.type === AssetType.image) {
// For photos, output the image directly
await render.assetBuffer(res, share.assets[0], getSize(req))
await render.assetBuffer(res, sharedLink.assets[0], getSize(req))
} else if (asset.type === AssetType.video) {
// For videos, show the video as a web player
await render.gallery(res, share.assets, 1)
await render.gallery(res, sharedLink, 1)
}
} else {
// Multiple images - render as a gallery
await render.gallery(res, share.assets)
await render.gallery(res, sharedLink)
}
}
})
// Output the buffer data for an photo or video
app.get('/:type(photo|video)/:id', (req, res) => {
if (!immich.isId(req.params.id) || !['photo', 'video'].includes(req.params.type)) {
// Invalid characters in the incoming URL
res.status(404).send()
return
app.get('/:type(photo|video)/:key/:id', async (req, res) => {
// Check for valid key and ID
if (immich.isKey(req.params.key) && immich.isId(req.params.id)) {
// Check if the key is a valid share link
const sharedLink = await immich.getShareByKey(req.params.key)
if (sharedLink?.assets.length) {
// Check that the requested asset exists in this share
const asset = sharedLink.assets.find(x => x.id === req.params.id)
if (asset) {
asset.type = req.params.type === 'video' ? AssetType.video : AssetType.image
render.assetBuffer(res, asset, getSize(req)).then()
return
}
}
}
const asset = {
id: req.params.id,
type: req.params.type === 'video' ? AssetType.video : AssetType.image
}
render.assetBuffer(res, asset, getSize(req)).then()
res.status(404).send()
})
// Send a 404 for all other unmatched routes
+6 -6
View File
@@ -1,6 +1,6 @@
import immich from './immich'
import { Response } from 'express-serve-static-core'
import { Asset, AssetType, ImageSize } from './types'
import { Asset, AssetType, ImageSize, SharedLink } from './types'
import dayjs from 'dayjs'
class Render {
@@ -17,16 +17,16 @@ class Render {
}
}
async gallery (res: Response, assets: Asset[], openItem?: number) {
async gallery (res: Response, share: SharedLink, openItem?: number) {
const items = []
for (const asset of assets) {
for (const asset of share.assets) {
let video
if (asset.type === AssetType.video) {
// Populate the data-video property
video = JSON.stringify({
source: [
{
src: immich.videoUrl(asset.id),
src: immich.videoUrl(share.key, asset.id),
type: await immich.getContentType(asset)
}
],
@@ -37,8 +37,8 @@ class Render {
})
}
items.push({
originalUrl: immich.photoUrl(asset.id),
thumbnailUrl: immich.photoUrl(asset.id, ImageSize.thumbnail),
originalUrl: immich.photoUrl(share.key, asset.id),
thumbnailUrl: immich.photoUrl(share.key, asset.id, ImageSize.thumbnail),
video
})
}