Add #10 - Single item as a gallery

This commit is contained in:
Alan Grainger
2024-11-04 20:23:34 +01:00
parent 0afc820f7b
commit 71feba9ce8
10 changed files with 87 additions and 54 deletions
+26
View File
@@ -0,0 +1,26 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": { "node": true },
"plugins": [
"@typescript-eslint"
],
"extends": [
"standard",
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-new": 0,
"no-unused-vars": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off"
}
}
+7
View File
@@ -1,4 +1,11 @@
{
"ipp": {
"responseHeaders": {
"Cache-Control": "public, max-age=2592000"
},
"singleImageGallery": false,
"singleItemAutoOpen": true
},
"lightGallery": {
"controls": true,
"download": true,
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "immich-public-proxy",
"version": "1.3.6",
"version": "1.3.7",
"scripts": {
"dev": "ts-node src/index.ts",
"build": "npx tsc",
+54
View File
@@ -0,0 +1,54 @@
import dayjs from 'dayjs'
import { Request, Response } from 'express-serve-static-core'
import { ImageSize } from './types'
let config = {}
try {
const configJson = require('../config.json')
if (typeof configJson === 'object') config = configJson
} catch (e) { }
/**
* Get a configuration option using dotted notation.
*
* @param path
* @param [defaultOption] - Specify a default option to return if no configuation value is found
*
* @example
* getConfigOption('ipp.singleImageGallery')
*/
export const getConfigOption = (path: string, defaultOption?: unknown) => {
const value = path.split('.').reduce((obj: { [key: string]: unknown }, key) => (obj || {})[key], config)
if (value === undefined) {
return defaultOption
} else {
return value
}
}
/**
* Output a console.log message with timestamp
*/
export const log = (message: string) => console.log(dayjs().format() + ' ' + message)
/**
* Sanitise the data for an incoming query string `size` parameter
* e.g. https://example.com/share/abc...xyz?size=thumbnail
*/
export function getSize (req: Request) {
return req.query?.size === 'thumbnail' ? ImageSize.thumbnail : ImageSize.original
}
export function toString (value: unknown) {
return typeof value === 'string' ? value : ''
}
/**
* Add response headers from config.json
*/
export function addResponseHeaders (res: Response) {
Object.entries(getConfigOption('ipp.responseHeaders', {}) as { [key: string]: string })
.forEach(([header, value]) => {
res.set(header, value)
})
}
+7 -6
View File
@@ -1,6 +1,6 @@
import { Asset, AssetType, ImageSize, IncomingShareRequest, SharedLink, SharedLinkResult } from './types'
import dayjs from 'dayjs'
import { log } from './index'
import { getConfigOption, log } from './functions'
import render from './render'
import { Response } from 'express-serve-static-core'
import { encrypt } from './encrypt'
@@ -70,12 +70,13 @@ class Immich {
// This is an individual item (not a gallery)
log('Serving link ' + request.key)
const asset = link.assets[0]
if (asset.type === AssetType.image) {
// For photos, output the image directly
if (asset.type === AssetType.image && !getConfigOption('ipp.singleImageGallery')) {
// For photos, output the image directly unless configured to show a gallery
await render.assetBuffer(res, link.assets[0], request.size)
} else if (asset.type === AssetType.video) {
// For videos, show the video as a web player
await render.gallery(res, link, 1)
} else {
// Show a gallery page
const openItem = getConfigOption('ipp.singleItemAutoOpen', true) ? 1 : 0
await render.gallery(res, link, openItem)
}
} else {
// Multiple images - render as a gallery
+5 -22
View File
@@ -2,19 +2,19 @@ import express from 'express'
import immich from './immich'
import render from './render'
import dayjs from 'dayjs'
import { AssetType, ImageSize } from './types'
import { Request } from 'express-serve-static-core'
import { AssetType } from './types'
import { decrypt } from './encrypt'
import { log, getSize, toString, addResponseHeaders } from './functions'
require('dotenv').config()
const app = express()
// Add the EJS view engine, to render the gallery page
app.set('view engine', 'ejs')
// Serve static assets from the /public folder
app.use(express.static('public'))
// For parsing the password unlock form
app.use(express.json())
// Serve static assets from the /public folder
app.use(express.static('public', { setHeaders: addResponseHeaders }))
// An incoming request for a shared link
app.get('/share/:key', async (req, res) => {
@@ -34,7 +34,7 @@ app.post('/unlock', async (req, res) => {
// Output the buffer data for a photo or video
app.get('/:type(photo|video)/:key/:id', async (req, res) => {
res.set('Cache-Control', 'public, max-age=' + process.env.CACHE_AGE)
addResponseHeaders(res)
// Check for valid key and ID
if (immich.isKey(req.params.key) && immich.isId(req.params.id)) {
let password
@@ -83,23 +83,6 @@ app.get('*', (req, res) => {
res.status(404).send()
})
/**
* Output a console.log message with timestamp
*/
export const log = (message: string) => console.log(dayjs().format() + ' ' + message)
/**
* Sanitise the data for an incoming query string `size` parameter
* e.g. https://example.com/share/abc...xyz?size=thumbnail
*/
const getSize = (req: Request) => {
return req.query?.size === 'thumbnail' ? ImageSize.thumbnail : ImageSize.original
}
const toString = (value: unknown) => {
return typeof value === 'string' ? value : ''
}
app.listen(3000, () => {
console.log(dayjs().format() + ' Server started')
})
+4 -7
View File
@@ -1,16 +1,13 @@
import immich from './immich'
import { Response } from 'express-serve-static-core'
import { Asset, AssetType, ImageSize, SharedLink } from './types'
import { getConfigOption } from './functions'
class Render {
lgConfig = {}
lgConfig
constructor () {
try {
// Import user-provided lightGallery config (if exists)
const config = require('../config.json')
if (typeof config === 'object' && config.lightGallery) this.lgConfig = config.lightGallery
} catch (e) { }
this.lgConfig = getConfigOption('lightGallery', {})
}
async assetBuffer (res: Response, asset: Asset, size?: ImageSize) {
@@ -61,7 +58,7 @@ class Render {
items,
openItem,
title: this.title(share),
lgConfig: this.lgConfig
lgConfig: getConfigOption('lightGallery', {})
})
}