Check for upload service before by status 204
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@
|
||||
"motionPhotos": true,
|
||||
"downloadedFilename": 0,
|
||||
"allowDownload": 0,
|
||||
"uploadUrl": "",
|
||||
"uploadUrl": "/upload",
|
||||
"allowSlugLinks": true,
|
||||
"showHomePage": true,
|
||||
"showMetadata": {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
@@ -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)
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { renderPage } from '@ipp/core'
|
||||
import { h } from 'preact'
|
||||
import { Gallery, GalleryProps } from '../src/view/gallery'
|
||||
|
||||
/*
|
||||
The "Add photos" button is rendered hidden, with the upload service's
|
||||
healthcheck URL on it, when the service is assumed to be at a path on IPP's
|
||||
hostname rather than configured. The client reveals it once the healthcheck
|
||||
answers. When the header holds nothing else it is hidden too, so a gallery
|
||||
with no title and no download button does not show an empty bar.
|
||||
*/
|
||||
|
||||
function galleryProps (overrides: Partial<GalleryProps> = {}): GalleryProps {
|
||||
return {
|
||||
items: [{ id: 'a', type: 'IMAGE', previewUrl: '/p', thumbnailUrl: '/t', downloadFilename: 'photo.jpg' }],
|
||||
title: 'Test gallery',
|
||||
description: '',
|
||||
publicBaseUrl: 'https://example.com',
|
||||
path: '/share/k',
|
||||
showDownloadZip: false,
|
||||
showTitle: false,
|
||||
lightboxConfig: { showArrows: true, showDownload: true, mobileArrows: false, autoPlayVideos: false },
|
||||
metadataConfig: { descriptionInCaption: false, descriptionInSidebar: false, sidebarHasContent: false, locationWebLink: false },
|
||||
groupByDate: false,
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
const header = (html: string) => html.match(/<header[^>]*>/)?.[0]
|
||||
const button = (html: string) => html.match(/<a id="upload-link"[^>]*>/)?.[0]
|
||||
|
||||
describe('gallery upload button', () => {
|
||||
it('is hidden with the healthcheck to probe when the upload service is assumed', () => {
|
||||
const html = renderPage(h(Gallery, galleryProps({ uploadLink: '/upload/share/k', uploadHealthcheck: '/upload/healthcheck' })))
|
||||
expect(button(html)).toContain(' hidden')
|
||||
expect(button(html)).toContain('data-healthcheck="/upload/healthcheck"')
|
||||
expect(header(html)).toContain(' hidden')
|
||||
})
|
||||
|
||||
it('shows at once when the owner configured the upload URL', () => {
|
||||
const html = renderPage(h(Gallery, galleryProps({ uploadLink: 'https://upload.example.com/share/k' })))
|
||||
expect(button(html)).toContain('href="https://upload.example.com/share/k"')
|
||||
expect(button(html)).not.toContain('hidden')
|
||||
expect(button(html)).not.toContain('data-healthcheck')
|
||||
expect(header(html)).toBe('<header id="header">')
|
||||
})
|
||||
|
||||
it('keeps the header visible when it holds other content', () => {
|
||||
const html = renderPage(h(Gallery, galleryProps({ showTitle: true, uploadLink: '/upload/share/k', uploadHealthcheck: '/upload/healthcheck' })))
|
||||
expect(header(html)).toBe('<header id="header">')
|
||||
expect(button(html)).toContain(' hidden')
|
||||
})
|
||||
|
||||
it('renders no button and no header when the share takes no uploads', () => {
|
||||
const html = renderPage(h(Gallery, galleryProps({ uploadHealthcheck: '/upload/healthcheck' })))
|
||||
expect(button(html)).toBeUndefined()
|
||||
expect(header(html)).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { KeyType, loadConfig, SharedLink } from '@ipp/core'
|
||||
import { uploadLink } from '../src/share'
|
||||
import { uploadHealthcheck, uploadLink } from '../src/share'
|
||||
|
||||
function setConfig (config: unknown) {
|
||||
process.env.CONFIG = JSON.stringify(config)
|
||||
@@ -23,8 +23,26 @@ describe('uploadLink', () => {
|
||||
loadConfig()
|
||||
})
|
||||
|
||||
it('is absent while ipp.uploadUrl is unset', () => {
|
||||
it('assumes /upload on this hostname while ipp.uploadUrl is unset, and checks it is there', () => {
|
||||
expect(uploadLink(share())).toBe('/upload/share/the-key')
|
||||
expect(uploadHealthcheck()).toBe('/upload/healthcheck')
|
||||
})
|
||||
|
||||
it('is absent when ipp.uploadUrl is empty', () => {
|
||||
setConfig({ ipp: { uploadUrl: '' } })
|
||||
expect(uploadLink(share())).toBeUndefined()
|
||||
expect(uploadHealthcheck()).toBeUndefined()
|
||||
})
|
||||
|
||||
it('takes another path on this hostname, without a trailing slash', () => {
|
||||
setConfig({ ipp: { uploadUrl: '/photos-in/' } })
|
||||
expect(uploadLink(share())).toBe('/photos-in/share/the-key')
|
||||
expect(uploadHealthcheck()).toBe('/photos-in/healthcheck')
|
||||
})
|
||||
|
||||
it('trusts an absolute URL without a check', () => {
|
||||
setConfig({ ipp: { uploadUrl: 'https://upload.example.com' } })
|
||||
expect(uploadHealthcheck()).toBeUndefined()
|
||||
})
|
||||
|
||||
it('links a key share by its key, without a doubled slash', () => {
|
||||
@@ -43,9 +61,13 @@ describe('uploadLink', () => {
|
||||
expect(uploadLink(share({ allowUpload: undefined }))).toBeUndefined()
|
||||
})
|
||||
|
||||
it('ignores a value that is not an http(s) URL', () => {
|
||||
it('ignores a value that is neither an http(s) URL nor a path', () => {
|
||||
setConfig({ ipp: { uploadUrl: 'javascript:alert(1)' } })
|
||||
expect(uploadLink(share())).toBeUndefined()
|
||||
setConfig({ ipp: { uploadUrl: '//evil.example.com' } })
|
||||
expect(uploadLink(share())).toBeUndefined()
|
||||
setConfig({ ipp: { uploadUrl: '/' } })
|
||||
expect(uploadLink(share())).toBeUndefined()
|
||||
setConfig({ ipp: { uploadUrl: 42 } })
|
||||
expect(uploadLink(share())).toBeUndefined()
|
||||
})
|
||||
|
||||
@@ -106,11 +106,13 @@ The bulk-zip and per-asset buttons can be toggled independently once downloads a
|
||||
|
||||
## `uploadUrl`
|
||||
|
||||
**Type:** `string` · **Default:** `""`
|
||||
**Type:** `string` · **Default:** `"/upload"`
|
||||
|
||||
The public URL of the optional upload service (the `immich-public-proxy-upload` container), for example `https://photos.example.com/upload`. When it is set, a share whose "Allow public user to upload" option is on in Immich shows an "Add photos" button in the gallery header. The button opens that share's page on the upload service. Leave it empty to show no button.
|
||||
Where the optional upload service (the `immich-public-proxy-upload` container) is served. A share whose "Allow public user to upload" option is on in Immich then shows an "Add photos" button in the gallery header, which opens that share's page on the upload service. To set up the service, see [Let visitors send photos back](/visitor-uploads).
|
||||
|
||||
The value is wherever your reverse proxy serves the upload service, with the path if it has one. To set up the service, see [Let visitors send photos back](/visitor-uploads).
|
||||
- **A path**, such as the default `/upload`, means the service is on IPP's own hostname under that path. Before showing the button, the gallery page checks that something answers at `<path>/healthcheck`, so an install without the upload container never shows a dead button.
|
||||
- **An absolute URL**, such as `https://upload.example.com`, means the service has a hostname of its own. It is trusted without a check. The URL may include a path, such as `https://photos.example.com/upload`, which points at the same place as the default but without the check.
|
||||
- **An empty string** never shows the button.
|
||||
|
||||
## `allowSlugLinks`
|
||||
|
||||
|
||||
+2
-1
@@ -26,7 +26,8 @@ version. Breaking changes and renamed config keys land in major versions and are
|
||||
- **A custom `invalidRequestHandler.js` must be copied again from the 4.0 image.** A copy taken from 3.x fails to
|
||||
load. See [Custom function](/config/error-responses#custom-function).
|
||||
|
||||
Visitor uploads are new in 4.0. An existing install is not affected until you add the upload service; see
|
||||
Visitor uploads are new in 4.0. An existing install is not affected until you add the upload service: IPP looks for
|
||||
it at `/upload` on its own hostname and shows the "Add photos" button only when something answers there. See
|
||||
[Let visitors send photos back](/visitor-uploads).
|
||||
|
||||
### Compose file
|
||||
|
||||
+12
-7
@@ -162,18 +162,23 @@ it cannot.
|
||||
|
||||
## Show the button in IPP
|
||||
|
||||
Set [`uploadUrl`](/config/ipp-options#uploadurl) in IPP's config to the public URL you routed in the previous step:
|
||||
With the upload service on a path on the IPP hostname, there is nothing to set. IPP assumes the service is at
|
||||
`/upload` on its own hostname, and the gallery shows the button once `/upload/healthcheck` answers.
|
||||
|
||||
With a hostname of its own, set [`uploadUrl`](/config/ipp-options#uploadurl) in IPP's config to that URL and restart
|
||||
IPP:
|
||||
|
||||
```json
|
||||
{
|
||||
"ipp": {
|
||||
"uploadUrl": "https://photos.example.com/upload"
|
||||
"uploadUrl": "https://upload.example.com"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Restart IPP. One `config.json` can serve both containers: IPP ignores the `ipp.upload.*` keys, and the upload service
|
||||
reads only those and [two shared options](/config/upload-service#shared-options).
|
||||
IPP reads its config from a `config.json` mounted at `/app/config.json`, or inline from the `CONFIG` environment
|
||||
variable; see [Configuration](/config/). One `config.json` can serve both containers: IPP ignores the `ipp.upload.*`
|
||||
keys, and the upload service reads only those and [two shared options](/config/upload-service#shared-options).
|
||||
|
||||
## Turn on uploads for a share
|
||||
|
||||
@@ -187,7 +192,7 @@ separate from IPP's, and the password never passes between the two.
|
||||
> [!NOTE]
|
||||
> A share with one photo opens as the image file itself, so there is no header and no button. Set
|
||||
> [`gallery.singleImage`](/config/gallery#singleimage) to `true` to show a gallery page instead, or give visitors the
|
||||
> upload page's own link: the share's path on the upload URL, such as `https://upload.example.com/share/<key>`.
|
||||
> upload page's own link: the share's path on the upload URL, such as `https://photos.example.com/upload/share/<key>`.
|
||||
|
||||
## Set a storage quota
|
||||
|
||||
@@ -267,5 +272,5 @@ visitor separately, set a rate limit in your reverse proxy.
|
||||
### How do I turn uploads off
|
||||
|
||||
For one share, turn off **Allow public user to upload** in Immich. Immich refuses new uploads at once, and the button
|
||||
leaves the gallery on the same cache times as a new photo takes to appear. For all shares, remove `uploadUrl` from
|
||||
IPP's config and stop the upload container.
|
||||
leaves the gallery on the same cache times as a new photo takes to appear. For all shares, stop the upload container; with the
|
||||
default `uploadUrl` the button goes with it. To make sure, set [`uploadUrl`](/config/ipp-options#uploadurl) to `""`.
|
||||
|
||||
@@ -122,6 +122,12 @@ html, body {
|
||||
}
|
||||
|
||||
/* Round icon button in the header: an anchor or a button */
|
||||
/* Both set display explicitly, which would otherwise defeat the hidden attribute */
|
||||
#header[hidden],
|
||||
.header-action[hidden] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.header-action {
|
||||
flex: 0 0 auto;
|
||||
display: inline-flex;
|
||||
|
||||
Reference in New Issue
Block a user