Check for upload service before by status 204

This commit is contained in:
Alan Grainger
2026-10-07 15:50:34 +13:00
parent 5f2edc5f74
commit 3bade3c34b
11 changed files with 178 additions and 25 deletions
+21
View File
@@ -19,8 +19,29 @@ function readInitParams (): InitParams {
try { return JSON.parse(el.textContent || '{}') } catch (e) { return {} }
}
/**
* Show the "Add photos" button once the upload service answers. The button
* is rendered hidden when the service is assumed to be at `/upload` on this
* hostname rather than configured, so an install without the upload
* container never shows a dead button. The upload service answers `ok`;
* IPP itself answers the same path with an empty 204 when nothing is routed
* there. The header may exist only for this button, in which case it is
* hidden too.
*/
function revealUploadLink () {
const link = document.getElementById('upload-link')
const url = link?.dataset.healthcheck
if (!link || !url) return
fetch(url, { cache: 'no-store' }).then(async res => {
if (res.status !== 200 || (await res.text()).trim() !== 'ok') return
link.hidden = false
document.getElementById('header')?.removeAttribute('hidden')
}).catch(() => {})
}
function init () {
const params = readInitParams()
revealUploadLink()
state.items = params.items || []
state.lightboxConfig = params.lightboxConfig || {}
if (params.metadataConfig) state.metadataConfig = params.metadataConfig
+3 -2
View File
@@ -6,7 +6,7 @@ import {
} from '../immich'
import { Response } from 'express-serve-static-core'
import { ImageSize } from '../types'
import { canDownload, expiryDate, uploadLink } from '../share'
import { canDownload, expiryDate, uploadHealthcheck, uploadLink } from '../share'
import { h } from 'preact'
import { Gallery, GalleryItem, GalleryProps } from '../view/gallery'
import type { GroupByDateMode } from '../shared/types'
@@ -156,7 +156,8 @@ export async function gallery (res: Response, share: SharedLink, openItem?: numb
},
groupByDate,
metaBase,
uploadLink: uploadLink(share)
uploadLink: uploadLink(share),
uploadHealthcheck: uploadHealthcheck()
}
// HTML gallery page cache time. A password-protected gallery keeps the
+37 -6
View File
@@ -22,16 +22,47 @@ export function canDownload (share: SharedLink): boolean {
let warnedInvalidDownloadPolicy = false
/** Where the upload service lives when `ipp.uploadUrl` is not set: a path on IPP's own hostname */
const DEFAULT_UPLOAD_URL = '/upload'
/**
* The upload service's public base from `ipp.uploadUrl`, without a trailing
* slash: an absolute http(s) URL for a hostname of its own, or a path on
* IPP's hostname such as the default `/upload`. Undefined when the option is
* empty, so the button is off, or not one of those two forms.
*/
export function uploadBase (): string | undefined {
const configured = getConfigOption('ipp.uploadUrl', DEFAULT_UPLOAD_URL)
if (typeof configured !== 'string') return undefined
const base = configured.trim().replace(/\/+$/, '')
if (/^https?:\/\//i.test(base)) return base
// A path, but not a protocol-relative `//host`
if (/^\/[^/\\]/.test(base)) return base
return undefined
}
/**
* The upload service's healthcheck URL when it is assumed to be at a path on
* IPP's hostname, or undefined when the owner gave an absolute URL. The
* gallery shows the "Add photos" button only once this answers, so an Immich
* share with uploads allowed never shows a dead button on an install that has
* no upload container. An absolute URL is the owner's word and is trusted; a
* cross-origin check would need CORS headers anyway.
*/
export function uploadHealthcheck (): string | undefined {
const base = uploadBase()
return base && !/^https?:/i.test(base) ? base + '/healthcheck' : undefined
}
/**
* The "Add photos" link to this share on the upload service, or undefined
* when `ipp.uploadUrl` is unset or not an http(s) URL, or the owner has not
* turned on "Allow public user to upload". A share opened by its slug links
* by its slug, so the upload service resolves it the same way.
* when there is no usable `ipp.uploadUrl` (see `uploadBase`) or the owner has
* not turned on "Allow public user to upload". A share opened by its slug
* links by its slug, so the upload service resolves it the same way.
*/
export function uploadLink (share: SharedLink): string | undefined {
const configured = getConfigOption('ipp.uploadUrl', '')
if (!share.allowUpload || typeof configured !== 'string' || !/^https?:\/\//i.test(configured.trim())) return undefined
const base = configured.trim().replace(/\/+$/, '')
const base = uploadBase()
if (!share.allowUpload || !base) return undefined
return share.keyType === KeyType.slug && share.slug
? base + '/s/' + encodeURIComponent(share.slug)
: base + '/share/' + encodeURIComponent(share.key)
+6 -2
View File
@@ -23,6 +23,9 @@ export interface GalleryProps {
metaBase?: string
// The upload service's page for this share, when it accepts uploads
uploadLink?: string
// Set when the upload service is assumed rather than configured: the button
// stays hidden until the client finds this URL answering
uploadHealthcheck?: string
}
export function Gallery (props: GalleryProps) {
@@ -72,7 +75,7 @@ export function Gallery (props: GalleryProps) {
</head>
<body>
{(showHeaderText || props.showDownloadZip || props.uploadLink) && (
<header id="header">
<header id="header" hidden={!showHeaderText && !props.showDownloadZip && !!props.uploadHealthcheck}>
{showHeaderText && (
<div class="header-text">
{props.showTitle && <h1>{props.title || 'Gallery'}</h1>}
@@ -87,7 +90,8 @@ export function Gallery (props: GalleryProps) {
)}
<div class="header-actions">
{props.uploadLink && (
<a id="upload-link" class="header-action" href={props.uploadLink} title="Add photos" aria-label="Add photos">
<a id="upload-link" class="header-action" href={props.uploadLink} title="Add photos" aria-label="Add photos"
hidden={!!props.uploadHealthcheck} data-healthcheck={props.uploadHealthcheck}>
<svg viewBox="0 0 24 24" aria-hidden="true">
<path fill="currentColor" d="M7 19L12 14L13.88 15.88C13.33 16.79 13 17.86 13 19H7M10 10.5C10 9.67 9.33 9 8.5 9S7 9.67 7 10.5 7.67 12 8.5 12 10 11.33 10 10.5M13.09 20H6V4H13V9H18V13.09C18.33 13.04 18.66 13 19 13C19.34 13 19.67 13.04 20 13.09V8L14 2H6C4.89 2 4 2.9 4 4V20C4 21.11 4.89 22 6 22H13.81C13.46 21.39 13.21 20.72 13.09 20M18 15V18H15V20H18V23H20V20H23V18H20V15H18Z"/>
</svg>