Add gallery
This commit is contained in:
+19
-8
@@ -1,4 +1,4 @@
|
||||
import { SharedLink } from './types'
|
||||
import { Asset, AssetType, ImageSize, SharedLink } from './types'
|
||||
|
||||
class Immich {
|
||||
async request (endpoint: string, json = true) {
|
||||
@@ -7,10 +7,12 @@ class Immich {
|
||||
'x-api-key': process.env.API_KEY || ''
|
||||
}
|
||||
})
|
||||
if (json) {
|
||||
return res.json()
|
||||
} else {
|
||||
return res
|
||||
if (res.status === 200) {
|
||||
if (json) {
|
||||
return res.json()
|
||||
} else {
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +21,18 @@ class Immich {
|
||||
return links.find(x => x.key === key)
|
||||
}
|
||||
|
||||
async getImage (id: string, size = 'original') {
|
||||
size = size === 'thumbnail' ? 'thumbnail' : 'original'
|
||||
return this.request('/assets/' + id + '/' + size, false)
|
||||
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)
|
||||
case AssetType.video:
|
||||
return this.request('/assets/' + asset.id + '/video/playback', false)
|
||||
}
|
||||
}
|
||||
|
||||
imageUrl (id: string, size?: ImageSize) {
|
||||
return `${process.env.SERVER_URL}/photo/${id}` + (size ? `?size=${size}` : '')
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+50
-13
@@ -1,34 +1,71 @@
|
||||
import express from 'express'
|
||||
import api from './immich'
|
||||
import immich from './immich'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
const app = express()
|
||||
import { AssetType, ImageSize } from './types'
|
||||
import { Request, Response } from 'express-serve-static-core'
|
||||
|
||||
require('dotenv').config()
|
||||
|
||||
const app = express()
|
||||
app.set('view engine', 'ejs')
|
||||
app.use(express.static('public'))
|
||||
|
||||
async function serveImage (res: Response, id: string, size?: ImageSize) {
|
||||
const image = await api.getAssetBuffer({
|
||||
id,
|
||||
type: AssetType.image
|
||||
}, size)
|
||||
if (image) {
|
||||
for (const header of ['content-type', 'content-length']) {
|
||||
res.set(header, image.headers[header])
|
||||
}
|
||||
console.log(`${dayjs().format()} Serving image ${id}`)
|
||||
res.send(Buffer.from(await image.arrayBuffer()))
|
||||
} else {
|
||||
res.status(404).send()
|
||||
}
|
||||
}
|
||||
|
||||
const getSize = (req: Request) => {
|
||||
return req?.query?.size === 'thumbnail' ? ImageSize.thumbnail : ImageSize.original
|
||||
}
|
||||
|
||||
app.get('/share/:key', async (req, res) => {
|
||||
if (req.params.key.match(/[^\w-]/)) {
|
||||
// Invalid characters in the incoming URL
|
||||
res.status(404).send()
|
||||
} else {
|
||||
const share = await api.getShareByKey(req.params.key)
|
||||
if (share?.assets?.length === 1) {
|
||||
if (!share || !share.assets.length) {
|
||||
res.status(404).send()
|
||||
} else if (share.assets.length === 1) {
|
||||
// This is an individual item (not a gallery)
|
||||
// Return the full-size image.
|
||||
const size = req.query.size === 'thumbnail' ? 'thumbnail' : 'original'
|
||||
const image = await api.getImage(share.assets[0].id, size)
|
||||
for (const header of ['content-type', 'content-length']) {
|
||||
res.set(header, image.headers[header])
|
||||
}
|
||||
const data = await image.arrayBuffer()
|
||||
console.log(dayjs().format() + ' Serving image ' + share.assets[0].id)
|
||||
res.send(Buffer.from(data))
|
||||
await serveImage(res, share.assets[0].id, getSize(req))
|
||||
} else {
|
||||
res.send()
|
||||
// Multiple images - render as a gallery
|
||||
res.render('gallery', {
|
||||
photos: share.assets.map(photo => {
|
||||
return {
|
||||
originalUrl: immich.imageUrl(photo.id),
|
||||
thumbnailUrl: immich.imageUrl(photo.id, ImageSize.thumbnail)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/photo/:id', (req, res) => {
|
||||
if (req.params.id.match(/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/)) {
|
||||
// Check for photo
|
||||
serveImage(res, req.params.id, getSize(req)).then()
|
||||
} else {
|
||||
// Invalid characters in the incoming URL
|
||||
res.status(404).send()
|
||||
}
|
||||
})
|
||||
|
||||
app.listen(3000, () => {
|
||||
console.log(dayjs().format() + ' Server started')
|
||||
})
|
||||
|
||||
+13
-2
@@ -1,8 +1,19 @@
|
||||
export interface SharedLinkAsset {
|
||||
export enum AssetType {
|
||||
image = 'IMAGE',
|
||||
video = 'VIDEO'
|
||||
}
|
||||
|
||||
export interface Asset {
|
||||
id: string;
|
||||
type: AssetType;
|
||||
}
|
||||
|
||||
export interface SharedLink {
|
||||
key: string;
|
||||
assets: SharedLinkAsset[]
|
||||
assets: Asset[]
|
||||
}
|
||||
|
||||
export enum ImageSize {
|
||||
thumbnail = 'thumbnail',
|
||||
original = 'original'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user