diff --git a/app/config.json b/app/config.json index 52f0f2a..89c25c6 100644 --- a/app/config.json +++ b/app/config.json @@ -9,7 +9,7 @@ "motionPhotos": true, "downloadedFilename": 0, "allowDownload": 0, - "uploadUrl": "", + "uploadUrl": "/upload", "allowSlugLinks": true, "showHomePage": true, "showMetadata": { diff --git a/app/src/client/init.ts b/app/src/client/init.ts index 824e919..4eaf3fd 100644 --- a/app/src/client/init.ts +++ b/app/src/client/init.ts @@ -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 diff --git a/app/src/gallery/builder.ts b/app/src/gallery/builder.ts index 44f5213..8ba5d47 100644 --- a/app/src/gallery/builder.ts +++ b/app/src/gallery/builder.ts @@ -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 diff --git a/app/src/share.ts b/app/src/share.ts index fe26b78..ece8df4 100644 --- a/app/src/share.ts +++ b/app/src/share.ts @@ -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) diff --git a/app/src/view/gallery.tsx b/app/src/view/gallery.tsx index 4d701bf..e173051 100644 --- a/app/src/view/gallery.tsx +++ b/app/src/view/gallery.tsx @@ -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) { {(showHeaderText || props.showDownloadZip || props.uploadLink) && ( -