Add video player

This commit is contained in:
Alan Grainger
2024-10-29 10:29:14 +01:00
parent 7be2508eac
commit 65eeba2df6
7 changed files with 157 additions and 54 deletions
+23 -9
View File
@@ -1,14 +1,15 @@
import { Asset, AssetType, ImageSize, SharedLink } from './types'
class Immich {
async request (endpoint: string, json = true) {
async request (endpoint: string) {
const res = await fetch(process.env.IMMICH_URL + '/api' + endpoint, {
headers: {
'x-api-key': process.env.API_KEY || ''
}
})
if (res.status === 200) {
if (json) {
const contentType = res.headers.get('Content-Type') || ''
if (contentType.includes('application/json')) {
return res.json()
} else {
return res
@@ -17,25 +18,38 @@ class Immich {
}
async getShareByKey (key: string) {
const links = await this.request('/shared-links') as SharedLink[]
return links.find(x => x.key === key)
const res = (await this.request('/shared-links') || []) as SharedLink[]
return res?.find(x => x.key === key)
}
async getAssetBuffer (asset: Asset, size?: ImageSize) {
switch (asset.type) {
case AssetType.image:
size = size === ImageSize.thumbnail ? ImageSize.thumbnail : ImageSize.original
return this.request('/assets/' + asset.id + '/' + size, false)
return this.request('/assets/' + asset.id + '/' + size)
case AssetType.video:
return this.request('/assets/' + asset.id + '/video/playback', false)
return this.request('/assets/' + asset.id + '/video/playback')
}
}
imageUrl (id: string, size?: ImageSize) {
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}` : '')
}
videoUrl (id: string) {
return `${process.env.SERVER_URL}/video/${id}`
}
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}$/)
}
}
const api = new Immich()
const immich = new Immich()
export default api
export default immich