Add upload page and move styles to shared assets
This commit is contained in:
+4
-3
@@ -48,7 +48,7 @@ shared/ @ipp/core - read-side only, knows nothing about galler
|
||||
version.ts App version from APP_VERSION or the app's package.json
|
||||
utils/ log, sanitize (filenames), text (escaping), ttlLruCache, webStream (incl. streaming request bodies)
|
||||
view/ Page renderer, theme script, password page
|
||||
public/ Static assets both apps serve: pico, Inter font, favicon, theme.css (tokens and page base)
|
||||
public/ Static assets both apps serve: pico, Inter font, favicon, theme.css (tokens, page base, page header)
|
||||
tests/ Vitest unit tests for core
|
||||
app/
|
||||
config.json Runtime configuration, overrideable via volume or inline
|
||||
@@ -91,8 +91,9 @@ upload-app/ immich-public-proxy-upload - one page per share; strea
|
||||
filename.ts Stored filename: sanitised, prefixed, extension kept
|
||||
notify.ts Optional JSON webhook per stored file
|
||||
idleTimeoutStream.ts Drops a request body that stalls
|
||||
view/upload.tsx The upload page
|
||||
shared/ Rules and types shared by the server and the page client
|
||||
view/upload.tsx The upload page, in the look of Immich's upload UI
|
||||
client/upload.ts The page client: queue, XHR uploads with retry, the upload panel
|
||||
shared/ Rules, types and icons shared by the server and the page client
|
||||
tests/ Vitest unit tests for the upload app
|
||||
docs/ User docs site (VitePress); docs/README.md explains its structure
|
||||
Dockerfile Multi-stage build; IPP runs from /app as the non-root `node` user
|
||||
|
||||
+1
-77
@@ -1,17 +1,4 @@
|
||||
/* Theme tokens, the Inter font and the page base come from @ipp/core's theme.css */
|
||||
|
||||
:root {
|
||||
/* Horizontal inset for textual page content (title, description, group
|
||||
headers). The grid itself goes edge-to-edge on mobile so it doesn't use
|
||||
this value. */
|
||||
--gutter: 32px;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
:root {
|
||||
--gutter: 8px;
|
||||
}
|
||||
}
|
||||
/* Theme tokens, the Inter font, the page base and the page header come from @ipp/core's theme.css */
|
||||
|
||||
/* ===== Justified-rows / square-grid gallery (tiles are absolute-positioned by JS) ===== */
|
||||
|
||||
@@ -193,62 +180,6 @@
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
/* ===== Header (title + asset count + download action) ===== */
|
||||
|
||||
#header {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
padding: 28px var(--gutter) 12px;
|
||||
color: var(--color-fg);
|
||||
}
|
||||
|
||||
.header-text {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
#header h1 {
|
||||
margin: 0;
|
||||
font-size: 28px;
|
||||
font-weight: 500;
|
||||
line-height: 1.2;
|
||||
letter-spacing: -0.01em;
|
||||
overflow-wrap: break-word;
|
||||
}
|
||||
|
||||
#header .subtitle {
|
||||
margin: 4px 0 0;
|
||||
font-size: 14px;
|
||||
font-weight: 400;
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
#download-all {
|
||||
flex: 0 0 auto;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
border-radius: 50%;
|
||||
text-decoration: none;
|
||||
color: var(--color-fg);
|
||||
transition: background-color 0.15s;
|
||||
}
|
||||
|
||||
#download-all:hover,
|
||||
#download-all:focus-visible {
|
||||
background-color: color-mix(in srgb, var(--color-fg) 10%, transparent);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
#download-all svg {
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
}
|
||||
|
||||
#album-description {
|
||||
margin: 0;
|
||||
padding: 0 var(--gutter) 16px;
|
||||
@@ -260,13 +191,6 @@
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
#header {
|
||||
padding-top: 16px;
|
||||
padding-bottom: 8px;
|
||||
}
|
||||
#header h1 {
|
||||
font-size: 22px;
|
||||
}
|
||||
#album-description {
|
||||
padding-bottom: 12px;
|
||||
font-size: 14px;
|
||||
|
||||
@@ -84,7 +84,7 @@ export function Gallery (props: GalleryProps) {
|
||||
</div>
|
||||
)}
|
||||
{props.showDownloadZip && (
|
||||
<a id="download-all" href={props.path + '/download'} title="Download all" aria-label="Download all">
|
||||
<a id="download-all" class="header-action" href={props.path + '/download'} title="Download all" aria-label="Download all">
|
||||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||||
<path fill="currentColor" d="M5,20H19V18H5M19,9H15V3H9V9H5L12,16L19,9Z"/>
|
||||
</svg>
|
||||
|
||||
@@ -37,8 +37,19 @@
|
||||
--color-primary: rgb(var(--immich-primary));
|
||||
--color-gray: rgb(var(--immich-gray));
|
||||
|
||||
/* Status colours from @immich/ui 0.90.0 (dist/theme/default.css), the -500 shades */
|
||||
--color-success: oklch(0.708 0.204 147.56);
|
||||
--color-danger: oklch(0.633 0.24 28.79);
|
||||
--color-warning: oklch(0.812 0.17 76.3);
|
||||
/* @immich/ui's `subtle` surface */
|
||||
--color-subtle: oklch(97% 0 271);
|
||||
|
||||
--font-sans: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
|
||||
/* Horizontal inset for textual page content. IPP's grid goes edge-to-edge
|
||||
on mobile so it doesn't use this value. */
|
||||
--gutter: 32px;
|
||||
|
||||
color-scheme: light;
|
||||
}
|
||||
|
||||
@@ -47,10 +58,20 @@ html.dark {
|
||||
--color-fg: rgb(var(--immich-dark-fg));
|
||||
--color-primary: rgb(var(--immich-dark-primary));
|
||||
--color-gray: rgb(var(--immich-dark-gray));
|
||||
--color-success: oklch(0.842 0.181 156.04);
|
||||
--color-danger: oklch(0.727 0.149 21.41);
|
||||
--color-warning: oklch(0.888 0.089 72.24);
|
||||
--color-subtle: oklch(17.89% 0.0104 276.38);
|
||||
|
||||
color-scheme: dark;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
:root {
|
||||
--gutter: 8px;
|
||||
}
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
html, body {
|
||||
@@ -60,3 +81,74 @@ html, body {
|
||||
color: var(--color-fg);
|
||||
font-family: var(--font-sans);
|
||||
}
|
||||
|
||||
/* ===== Page header: title, subtitle and round icon actions, shared by both apps ===== */
|
||||
|
||||
#header {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
padding: 28px var(--gutter) 12px;
|
||||
color: var(--color-fg);
|
||||
}
|
||||
|
||||
.header-text {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
#header h1 {
|
||||
margin: 0;
|
||||
font-size: 28px;
|
||||
font-weight: 500;
|
||||
line-height: 1.2;
|
||||
letter-spacing: -0.01em;
|
||||
overflow-wrap: break-word;
|
||||
}
|
||||
|
||||
#header .subtitle {
|
||||
margin: 4px 0 0;
|
||||
font-size: 14px;
|
||||
font-weight: 400;
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
/* Round icon button in the header: an anchor or a button */
|
||||
.header-action {
|
||||
flex: 0 0 auto;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
padding: 0;
|
||||
border: none;
|
||||
border-radius: 50%;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
text-decoration: none;
|
||||
color: var(--color-fg);
|
||||
transition: background-color 0.15s;
|
||||
}
|
||||
|
||||
.header-action:hover,
|
||||
.header-action:focus-visible {
|
||||
background-color: color-mix(in srgb, var(--color-fg) 10%, transparent);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.header-action svg {
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
#header {
|
||||
padding-top: 16px;
|
||||
padding-bottom: 8px;
|
||||
}
|
||||
#header h1 {
|
||||
font-size: 22px;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Mirrors Immich's FOUC-prevention pattern: read color-theme from localStorage
|
||||
// (or fall back to OS preference), then add/remove .dark on <html> before paint.
|
||||
// `data-theme` is pico's switch, so pages that use pico follow the same choice.
|
||||
// Source: immich-app/immich v2.7.5 web/src/app.html (2026-05-23).
|
||||
const themeScript = `
|
||||
const key = 'color-theme';
|
||||
@@ -18,6 +19,7 @@ const themeScript = `
|
||||
}
|
||||
if (v === 'dark') document.documentElement.classList.add('dark');
|
||||
else document.documentElement.classList.remove('dark');
|
||||
document.documentElement.dataset.theme = v;
|
||||
`
|
||||
|
||||
export function ThemeScript () {
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
/*
|
||||
Theme tokens, the Inter font, the page base and the page header come from
|
||||
@ipp/core's theme.css. The rest follows Immich's own upload UI: the
|
||||
placeholder after its EmptyPlaceholder, the panel and rows after
|
||||
UploadPanel and UploadAssetPreview, the overlay after
|
||||
DragAndDropUploadOverlay (immich-app/immich web/src, 2026-10).
|
||||
*/
|
||||
|
||||
#upload-main {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 24px;
|
||||
max-width: 720px;
|
||||
padding: 12px var(--gutter) 48px;
|
||||
}
|
||||
|
||||
button {
|
||||
font: inherit;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
/* ===== Placeholder: click or drop to add photos ===== */
|
||||
|
||||
.upload-placeholder {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
width: 100%;
|
||||
padding: 48px 20px;
|
||||
border: 1px solid color-mix(in srgb, var(--color-fg) 10%, transparent);
|
||||
border-radius: 24px;
|
||||
background: var(--color-subtle);
|
||||
cursor: pointer;
|
||||
transition: background-color 0.15s;
|
||||
}
|
||||
|
||||
.upload-placeholder:hover,
|
||||
.upload-placeholder:focus-visible {
|
||||
background: color-mix(in srgb, var(--color-primary) 5%, var(--color-subtle));
|
||||
outline: none;
|
||||
}
|
||||
|
||||
html.dark .upload-placeholder:hover,
|
||||
html.dark .upload-placeholder:focus-visible {
|
||||
background: color-mix(in srgb, var(--color-primary) 25%, var(--color-subtle));
|
||||
}
|
||||
|
||||
.upload-placeholder-icon {
|
||||
width: 72px;
|
||||
height: 72px;
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
.upload-placeholder-title {
|
||||
margin-top: 8px;
|
||||
font-size: 20px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.upload-placeholder-text {
|
||||
font-weight: 300;
|
||||
text-align: center;
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
/* Nothing to drop on a touch screen */
|
||||
@media (hover: none) {
|
||||
.upload-drop-hint {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* ===== Upload panel ===== */
|
||||
|
||||
#upload-panel {
|
||||
padding: 16px;
|
||||
border: 1px solid color-mix(in srgb, var(--color-fg) 12%, transparent);
|
||||
border-radius: 12px;
|
||||
background: var(--color-subtle);
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
#upload-panel[hidden] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#upload-progress {
|
||||
margin: 0 0 4px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
#upload-counts,
|
||||
#upload-note {
|
||||
margin: 0;
|
||||
font-size: 12px;
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
#upload-note {
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
#upload-note[hidden] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.upload-count-success { color: var(--color-success); }
|
||||
.upload-count-danger { color: var(--color-danger); }
|
||||
.upload-count-warning { color: var(--color-warning); }
|
||||
|
||||
#upload-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
max-height: 480px;
|
||||
margin: 16px 0 0;
|
||||
padding: 0;
|
||||
overflow-y: auto;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.upload-row {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
padding: 8px;
|
||||
border: 1px solid color-mix(in srgb, var(--color-fg) 15%, transparent);
|
||||
border-radius: 12px;
|
||||
background: color-mix(in srgb, var(--color-primary) 10%, transparent);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.upload-row-line {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.upload-row-icon {
|
||||
display: flex;
|
||||
flex: 0 0 auto;
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
.upload-row-icon svg {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.upload-row[data-state="started"] .upload-row-icon svg {
|
||||
animation: upload-spin 1s linear infinite;
|
||||
}
|
||||
|
||||
.upload-row[data-state="done"] .upload-row-icon { color: var(--color-success); }
|
||||
.upload-row[data-state="duplicate"] .upload-row-icon { color: var(--color-warning); }
|
||||
.upload-row[data-state="error"] .upload-row-icon { color: var(--color-danger); }
|
||||
|
||||
.upload-row-name {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.upload-row-action {
|
||||
display: flex;
|
||||
flex: 0 0 auto;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
padding: 4px;
|
||||
border: none;
|
||||
border-radius: 50%;
|
||||
background: transparent;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.upload-row-action:hover,
|
||||
.upload-row-action:focus-visible {
|
||||
background: color-mix(in srgb, var(--color-fg) 10%, transparent);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.upload-row-action svg {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
/* Progress bar with its text on top, as Immich draws it */
|
||||
.upload-bar {
|
||||
position: relative;
|
||||
height: 18px;
|
||||
margin-top: 5px;
|
||||
border-radius: 6px;
|
||||
background: rgb(209 213 219);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
html.dark .upload-bar {
|
||||
background: rgb(55 65 81);
|
||||
}
|
||||
|
||||
.upload-bar-fill {
|
||||
height: 100%;
|
||||
border-radius: 6px;
|
||||
background: rgb(var(--immich-primary));
|
||||
transition: width 0.2s linear;
|
||||
}
|
||||
|
||||
.upload-bar-text {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
margin: 0;
|
||||
color: #fff;
|
||||
font-size: 10px;
|
||||
line-height: 18px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.upload-row-error {
|
||||
margin: 0;
|
||||
color: var(--color-danger);
|
||||
}
|
||||
|
||||
@keyframes upload-spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
/* ===== Drop overlay ===== */
|
||||
|
||||
#upload-drop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 200;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: rgb(243 244 246 / 0.9);
|
||||
color: rgb(var(--immich-dark-gray));
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
html.dark #upload-drop {
|
||||
background: rgb(var(--immich-dark-bg) / 0.9);
|
||||
color: rgb(var(--immich-gray));
|
||||
}
|
||||
|
||||
#upload-drop[hidden] {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.upload-drop-icon {
|
||||
width: 128px;
|
||||
height: 128px;
|
||||
margin: 64px;
|
||||
color: var(--color-primary);
|
||||
animation: upload-bounce 1s infinite;
|
||||
}
|
||||
|
||||
#upload-drop p {
|
||||
margin: 0;
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
/* Tailwind's animate-bounce, which Immich uses on its overlay */
|
||||
@keyframes upload-bounce {
|
||||
0%, 100% {
|
||||
transform: translateY(-25%);
|
||||
animation-timing-function: cubic-bezier(0.8, 0, 1, 1);
|
||||
}
|
||||
50% {
|
||||
transform: none;
|
||||
animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.upload-drop-icon,
|
||||
.upload-row[data-state="started"] .upload-row-icon svg {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
// The upload page: the placeholder button and dropping files on the page
|
||||
// feed one queue. Each file is its own request, so one failure is one file.
|
||||
// The panel follows Immich's own upload panel: summary lines, then one row
|
||||
// per file with a status icon, a progress bar while sending, and the error.
|
||||
//
|
||||
// Nothing here reads a file's contents; the bytes go straight from the File
|
||||
// to the request body.
|
||||
|
||||
import { createLimiter } from '../shared/limiter.js'
|
||||
import { fileType, formatSize, isUploadableType } from '../shared/upload.js'
|
||||
import type { UploadErrorResponse, UploadPageConfig, UploadResponse } from '../shared/upload.js'
|
||||
import {
|
||||
ICON_ALERT_CIRCLE,
|
||||
ICON_CHECK_CIRCLE,
|
||||
ICON_CIRCLE_OUTLINE,
|
||||
ICON_CLOSE,
|
||||
ICON_LOADING,
|
||||
ICON_RESTART,
|
||||
iconSvg
|
||||
} from '../shared/icons.js'
|
||||
|
||||
// Two at a time: enough to keep a phone's uplink busy without stalling every file behind one big video
|
||||
const CONCURRENT_UPLOADS = 2
|
||||
const ATTEMPTS = 3
|
||||
const RETRY_BASE_MS = 2000
|
||||
// A Retry-After longer than this is shown as a failure rather than waited out
|
||||
const MAX_RETRY_WAIT_MS = 60_000
|
||||
|
||||
const config = JSON.parse(document.getElementById('upload-init')?.textContent || '{}') as UploadPageConfig
|
||||
const run = createLimiter(CONCURRENT_UPLOADS)
|
||||
|
||||
const panelEl = document.getElementById('upload-panel') as HTMLElement
|
||||
const progressEl = document.getElementById('upload-progress') as HTMLElement
|
||||
const countsEl = document.getElementById('upload-counts') as HTMLElement
|
||||
const noteEl = document.getElementById('upload-note') as HTMLElement
|
||||
const listEl = document.getElementById('upload-list') as HTMLElement
|
||||
const dropEl = document.getElementById('upload-drop') as HTMLElement
|
||||
|
||||
type RowState = 'pending' | 'started' | 'done' | 'duplicate' | 'error'
|
||||
|
||||
const STATE_ICONS: Record<RowState, string> = {
|
||||
pending: ICON_CIRCLE_OUTLINE,
|
||||
started: ICON_LOADING,
|
||||
done: ICON_CHECK_CIRCLE,
|
||||
duplicate: ICON_ALERT_CIRCLE,
|
||||
error: ICON_ALERT_CIRCLE
|
||||
}
|
||||
|
||||
interface UploadRow {
|
||||
file: File
|
||||
type: string
|
||||
li: HTMLLIElement
|
||||
icon: HTMLElement
|
||||
action: HTMLButtonElement
|
||||
xhr?: XMLHttpRequest
|
||||
// Ends a retry wait early
|
||||
wake?: () => void
|
||||
cancelled: boolean
|
||||
}
|
||||
|
||||
/*
|
||||
Counts for the batch on screen, as Immich shows them. Files added while a
|
||||
batch is still sending join it; the first file after a batch has finished
|
||||
starts a new one.
|
||||
*/
|
||||
const stats = { total: 0, remaining: 0, uploaded: 0, errors: 0, duplicates: 0 }
|
||||
|
||||
function updateSummary () {
|
||||
progressEl.textContent = `Remaining ${stats.remaining} - Processed ${stats.total - stats.remaining}/${stats.total}`
|
||||
countsEl.innerHTML = 'Uploaded <span class="upload-count-success"></span> - Errors <span class="upload-count-danger"></span> - Duplicates <span class="upload-count-warning"></span>'
|
||||
const [uploaded, errors, duplicates] = countsEl.querySelectorAll('span')
|
||||
uploaded.textContent = String(stats.uploaded)
|
||||
errors.textContent = String(stats.errors)
|
||||
duplicates.textContent = String(stats.duplicates)
|
||||
noteEl.hidden = stats.remaining > 0 || stats.uploaded === 0
|
||||
}
|
||||
|
||||
function setState (row: UploadRow, state: RowState) {
|
||||
row.li.dataset.state = state
|
||||
row.icon.innerHTML = iconSvg(STATE_ICONS[state])
|
||||
}
|
||||
|
||||
/** Swap the row's button: cancel while queued or sending, retry or dismiss afterwards. */
|
||||
function setAction (row: UploadRow, kind: 'cancel' | 'retry' | 'dismiss') {
|
||||
const labels = { cancel: 'Cancel', retry: 'Retry', dismiss: 'Dismiss' }
|
||||
row.action.dataset.kind = kind
|
||||
row.action.setAttribute('aria-label', labels[kind] + ' ' + row.file.name)
|
||||
row.action.title = labels[kind]
|
||||
row.action.innerHTML = iconSvg(kind === 'retry' ? ICON_RESTART : ICON_CLOSE)
|
||||
}
|
||||
|
||||
/** Remove any progress bar or error line under the row's main line. */
|
||||
function clearDetail (row: UploadRow) {
|
||||
row.li.querySelector('.upload-row-detail')?.remove()
|
||||
}
|
||||
|
||||
function showProgress (row: UploadRow, text: string, percent: number) {
|
||||
let bar = row.li.querySelector('.upload-row-detail.upload-bar') as HTMLElement | null
|
||||
if (!bar) {
|
||||
clearDetail(row)
|
||||
bar = document.createElement('div')
|
||||
bar.className = 'upload-row-detail upload-bar'
|
||||
bar.innerHTML = '<div class="upload-bar-fill"></div><p class="upload-bar-text"></p>'
|
||||
row.li.appendChild(bar)
|
||||
}
|
||||
const fill = bar.firstElementChild as HTMLElement
|
||||
const label = bar.lastElementChild as HTMLElement
|
||||
fill.style.width = percent + '%'
|
||||
label.textContent = text
|
||||
}
|
||||
|
||||
function showError (row: UploadRow, message: string) {
|
||||
clearDetail(row)
|
||||
const p = document.createElement('p')
|
||||
p.className = 'upload-row-detail upload-row-error'
|
||||
p.textContent = message
|
||||
row.li.appendChild(p)
|
||||
}
|
||||
|
||||
/** Record a finished row: its state, its counter, and the button it keeps. */
|
||||
function finish (row: UploadRow, state: 'done' | 'duplicate' | 'error', message?: string, retryable = false) {
|
||||
setState(row, state)
|
||||
clearDetail(row)
|
||||
if (state === 'error') {
|
||||
stats.errors++
|
||||
if (message) showError(row, message)
|
||||
} else if (state === 'duplicate') {
|
||||
stats.duplicates++
|
||||
} else {
|
||||
stats.uploaded++
|
||||
}
|
||||
setAction(row, retryable ? 'retry' : 'dismiss')
|
||||
}
|
||||
|
||||
/** Build the panel row for one file: status icon, name, and its button. */
|
||||
function addRow (file: File, type: string): UploadRow {
|
||||
const li = document.createElement('li')
|
||||
li.className = 'upload-row'
|
||||
const line = document.createElement('div')
|
||||
line.className = 'upload-row-line'
|
||||
const icon = document.createElement('span')
|
||||
icon.className = 'upload-row-icon'
|
||||
const name = document.createElement('span')
|
||||
name.className = 'upload-row-name'
|
||||
name.textContent = file.name
|
||||
const action = document.createElement('button')
|
||||
action.type = 'button'
|
||||
action.className = 'upload-row-action'
|
||||
line.append(icon, name, action)
|
||||
li.appendChild(line)
|
||||
listEl.appendChild(li)
|
||||
|
||||
const row: UploadRow = { file, type, li, icon, action, cancelled: false }
|
||||
setState(row, 'pending')
|
||||
setAction(row, 'cancel')
|
||||
action.addEventListener('click', () => {
|
||||
if (action.dataset.kind === 'retry') {
|
||||
// A retried file rejoins the batch on screen
|
||||
stats.errors--
|
||||
queue(row)
|
||||
return
|
||||
}
|
||||
// Cancel and dismiss both take the row away; a cancelled file is not counted
|
||||
if (action.dataset.kind === 'cancel') {
|
||||
row.cancelled = true
|
||||
row.xhr?.abort()
|
||||
row.wake?.()
|
||||
}
|
||||
li.remove()
|
||||
})
|
||||
return row
|
||||
}
|
||||
|
||||
type Attempt =
|
||||
| { outcome: 'sent', duplicate: boolean }
|
||||
| { outcome: 'failed', message: string }
|
||||
| { outcome: 'retry', waitMs: number }
|
||||
| { outcome: 'cancelled' }
|
||||
|
||||
/** The visitor-facing reason for a refused upload. */
|
||||
function failureMessage (xhr: XMLHttpRequest): string {
|
||||
let reply: Partial<UploadErrorResponse> | undefined
|
||||
try { reply = JSON.parse(xhr.responseText) } catch { }
|
||||
if (reply?.error) return reply.error
|
||||
// Not our JSON, so a proxy in front of the upload service answered
|
||||
if (xhr.status === 413) return 'Too large for this server'
|
||||
if (xhr.status === 404) return 'This share no longer accepts photos. Reload the page.'
|
||||
return 'Could not be sent'
|
||||
}
|
||||
|
||||
/** One request for one file. XHR rather than fetch, because only XHR reports upload progress. */
|
||||
function attempt (row: UploadRow, attemptNumber: number): Promise<Attempt> {
|
||||
return new Promise(resolve => {
|
||||
const timestamp = new Date(row.file.lastModified || Date.now()).toISOString()
|
||||
const query = new URLSearchParams({ filename: row.file.name, fileCreatedAt: timestamp, fileModifiedAt: timestamp })
|
||||
const xhr = new XMLHttpRequest()
|
||||
row.xhr = xhr
|
||||
const started = Date.now()
|
||||
xhr.open('POST', config.uploadUrl + '?' + query.toString())
|
||||
xhr.setRequestHeader('Content-Type', row.type)
|
||||
xhr.upload.addEventListener('progress', e => {
|
||||
if (!e.lengthComputable) return
|
||||
const percent = Math.round((e.loaded / e.total) * 100)
|
||||
const seconds = (Date.now() - started) / 1000
|
||||
const speed = seconds > 0 ? e.loaded / seconds : 0
|
||||
const eta = speed > 0 ? Math.ceil((e.total - e.loaded) / speed) : 0
|
||||
showProgress(row, `${percent}% - ${formatSize(Math.round(speed))}/s - ${eta}s`, percent)
|
||||
})
|
||||
|
||||
const backoff = { outcome: 'retry', waitMs: RETRY_BASE_MS * 2 ** (attemptNumber - 1) } as const
|
||||
xhr.addEventListener('load', () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
let reply: Partial<UploadResponse> | undefined
|
||||
try { reply = JSON.parse(xhr.responseText) } catch { }
|
||||
resolve({ outcome: 'sent', duplicate: reply?.status === 'duplicate' })
|
||||
} else if (xhr.status === 429) {
|
||||
const seconds = Number(xhr.getResponseHeader('Retry-After'))
|
||||
resolve(Number.isFinite(seconds) && seconds > 0 ? { outcome: 'retry', waitMs: seconds * 1000 } : backoff)
|
||||
} else {
|
||||
resolve({ outcome: 'failed', message: failureMessage(xhr) })
|
||||
}
|
||||
})
|
||||
// A refusal that closes the connection mid-body often arrives as a network error
|
||||
xhr.addEventListener('error', () => resolve(backoff))
|
||||
xhr.addEventListener('abort', () => resolve({ outcome: 'cancelled' }))
|
||||
xhr.send(row.file)
|
||||
})
|
||||
}
|
||||
|
||||
/** Wait before a retry; cancelling the row ends the wait. */
|
||||
function waitToRetry (row: UploadRow, ms: number): Promise<void> {
|
||||
return new Promise(resolve => {
|
||||
const timer = setTimeout(resolve, ms)
|
||||
row.wake = () => {
|
||||
clearTimeout(timer)
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** Send one file, retrying a rate limit or a network error, and record the result on its row. */
|
||||
async function sendFile (row: UploadRow) {
|
||||
for (let attemptNumber = 1; !row.cancelled; attemptNumber++) {
|
||||
setState(row, 'started')
|
||||
showProgress(row, '0%', 0)
|
||||
const result = await attempt(row, attemptNumber)
|
||||
if (result.outcome === 'cancelled') return
|
||||
if (result.outcome === 'sent') {
|
||||
finish(row, result.duplicate ? 'duplicate' : 'done')
|
||||
return
|
||||
}
|
||||
if (result.outcome === 'failed') {
|
||||
finish(row, 'error', result.message, true)
|
||||
return
|
||||
}
|
||||
if (attemptNumber >= ATTEMPTS || result.waitMs > MAX_RETRY_WAIT_MS) {
|
||||
finish(row, 'error', 'Could not be sent. Try again later.', true)
|
||||
return
|
||||
}
|
||||
setState(row, 'pending')
|
||||
showProgress(row, 'Waiting to retry', 0)
|
||||
await waitToRetry(row, result.waitMs)
|
||||
}
|
||||
}
|
||||
|
||||
/** Start a new batch if the last one has finished. */
|
||||
function startBatchIfIdle () {
|
||||
if (stats.remaining > 0) return
|
||||
stats.total = stats.uploaded = stats.errors = stats.duplicates = 0
|
||||
}
|
||||
|
||||
/** Put a row in the send queue and count it as remaining. */
|
||||
function queue (row: UploadRow) {
|
||||
row.cancelled = false
|
||||
setState(row, 'pending')
|
||||
setAction(row, 'cancel')
|
||||
clearDetail(row)
|
||||
stats.remaining++
|
||||
updateSummary()
|
||||
run(() => row.cancelled ? Promise.resolve() : sendFile(row)).finally(() => {
|
||||
stats.remaining--
|
||||
// A cancelled file leaves the batch altogether
|
||||
if (row.cancelled) stats.total--
|
||||
updateSummary()
|
||||
})
|
||||
}
|
||||
|
||||
/** Queue files. Anything the server would refuse anyway is refused here, before it costs the visitor's uplink. */
|
||||
function queueFiles (files: File[]) {
|
||||
if (!files.length) return
|
||||
startBatchIfIdle()
|
||||
panelEl.hidden = false
|
||||
for (const file of files) {
|
||||
const type = fileType(file.name, file.type)
|
||||
const row = addRow(file, type)
|
||||
stats.total++
|
||||
if (!isUploadableType(type)) {
|
||||
finish(row, 'error', 'Only photos and videos can be sent')
|
||||
} else if (file.size > config.maxFileSize) {
|
||||
finish(row, 'error', 'Larger than the ' + formatSize(config.maxFileSize) + ' limit')
|
||||
} else {
|
||||
queue(row)
|
||||
}
|
||||
}
|
||||
updateSummary()
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the drop overlay while a drag carrying files is over the window.
|
||||
* `dragenter` and `dragleave` fire per element, so the depth counter stops
|
||||
* the overlay flickering as the pointer crosses child nodes.
|
||||
*/
|
||||
function setupDragAndDrop () {
|
||||
let depth = 0
|
||||
const carriesFiles = (e: DragEvent) => !!e.dataTransfer?.types?.includes('Files')
|
||||
const hide = () => {
|
||||
depth = 0
|
||||
dropEl.hidden = true
|
||||
}
|
||||
window.addEventListener('dragenter', e => {
|
||||
if (!carriesFiles(e)) return
|
||||
depth++
|
||||
dropEl.hidden = false
|
||||
})
|
||||
window.addEventListener('dragover', e => {
|
||||
// Without this the browser opens the dropped file instead
|
||||
if (carriesFiles(e)) e.preventDefault()
|
||||
})
|
||||
window.addEventListener('dragleave', () => {
|
||||
if (--depth <= 0) hide()
|
||||
})
|
||||
window.addEventListener('drop', e => {
|
||||
if (!carriesFiles(e)) return
|
||||
e.preventDefault()
|
||||
hide()
|
||||
queueFiles(Array.from(e.dataTransfer?.files || []))
|
||||
})
|
||||
}
|
||||
|
||||
const input = document.getElementById('upload-input') as HTMLInputElement
|
||||
document.getElementById('upload-choose')?.addEventListener('click', () => input.click())
|
||||
input.addEventListener('change', () => {
|
||||
queueFiles(Array.from(input.files || []))
|
||||
// So picking the same file again still fires `change`
|
||||
input.value = ''
|
||||
})
|
||||
setupDragAndDrop()
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
Material Design Icons (https://pictogrammers.com/library/mdi/), the set
|
||||
Immich uses, as SVG path data. Shared by the page view and the client.
|
||||
*/
|
||||
|
||||
export const ICON_ARROW_LEFT = 'M20,11V13H8L13.5,18.5L12.08,19.92L4.16,12L12.08,4.08L13.5,5.5L8,11H20Z'
|
||||
export const ICON_CLOUD_UPLOAD = 'M6.5 20Q4.22 20 2.61 18.43 1 16.85 1 14.58 1 12.63 2.17 11.1 3.35 9.57 5.25 9.15 5.88 6.85 7.75 5.43 9.63 4 12 4 14.93 4 16.96 6.04 19 8.07 19 11 20.73 11.2 21.86 12.5 23 13.78 23 15.5 23 17.38 21.69 18.69 20.38 20 18.5 20H13Q12.18 20 11.59 19.41 11 18.83 11 18V12.85L9.4 14.4L8 13L12 9L16 13L14.6 14.4L13 12.85V18H18.5Q19.55 18 20.27 17.27 21 16.55 21 15.5 21 14.45 20.27 13.73 19.55 13 18.5 13H17V11Q17 8.93 15.54 7.46 14.08 6 12 6 9.93 6 8.46 7.46 7 8.93 7 11H6.5Q5.05 11 4.03 12.03 3 13.05 3 14.5 3 15.95 4.03 17 5.05 18 6.5 18H9V20M12 13Z'
|
||||
export const ICON_CIRCLE_OUTLINE = 'M12,20A8,8 0 0,1 4,12A8,8 0 0,1 12,4A8,8 0 0,1 20,12A8,8 0 0,1 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z'
|
||||
export const ICON_LOADING = 'M12,4V2A10,10 0 0,0 2,12H4A8,8 0 0,1 12,4Z'
|
||||
export const ICON_CHECK_CIRCLE = 'M12 2C6.5 2 2 6.5 2 12S6.5 22 12 22 22 17.5 22 12 17.5 2 12 2M10 17L5 12L6.41 10.59L10 14.17L17.59 6.58L19 8L10 17Z'
|
||||
export const ICON_ALERT_CIRCLE = 'M13,13H11V7H13M13,17H11V15H13M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z'
|
||||
export const ICON_CLOSE = 'M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z'
|
||||
export const ICON_RESTART = 'M12,4C14.1,4 16.1,4.8 17.6,6.3C20.7,9.4 20.7,14.5 17.6,17.6C15.8,19.5 13.3,20.2 10.9,19.9L11.4,17.9C13.1,18.1 14.9,17.5 16.2,16.2C18.5,13.9 18.5,10.1 16.2,7.7C15.1,6.6 13.5,6 12,6V10.6L7,5.6L12,0.6V4M6.3,17.6C3.7,15 3.3,11 5.1,7.9L6.6,9.4C5.5,11.6 5.9,14.4 7.8,16.2C8.3,16.7 8.9,17.1 9.6,17.4L9,19.4C8,19 7.1,18.4 6.3,17.6Z'
|
||||
|
||||
/** An inline SVG element's markup for one icon path. */
|
||||
export function iconSvg (path: string, className = ''): string {
|
||||
return `<svg viewBox="0 0 24 24" aria-hidden="true"${className ? ` class="${className}"` : ''}><path fill="currentColor" d="${path}"/></svg>`
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { jsonForInlineScript, ThemeScript } from '@ipp/core'
|
||||
import { ICON_ARROW_LEFT, ICON_CLOUD_UPLOAD } from '../shared/icons'
|
||||
import { UPLOAD_ACCEPT, UploadPageConfig } from '../shared/upload'
|
||||
import { ASSET_VERSION } from '../version'
|
||||
|
||||
@@ -10,7 +11,19 @@ export interface UploadPageProps {
|
||||
galleryLink?: string
|
||||
}
|
||||
|
||||
/** The upload page for one share: the share title, the picker, and the list the client fills. */
|
||||
function Icon ({ path, class: className }: { path: string, class?: string }) {
|
||||
return (
|
||||
<svg viewBox="0 0 24 24" aria-hidden="true" class={className}>
|
||||
<path fill="currentColor" d={path}/>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The upload page for one share, in the look of Immich's own upload UI: the
|
||||
* gallery's header, a placeholder to click or drop on, and an upload panel
|
||||
* the client fills.
|
||||
*/
|
||||
export function UploadPage ({ config, basePath, galleryLink }: UploadPageProps) {
|
||||
const staticPath = `${basePath}/share/static/${ASSET_VERSION}`
|
||||
return (
|
||||
@@ -19,25 +32,40 @@ export function UploadPage ({ config, basePath, galleryLink }: UploadPageProps)
|
||||
<ThemeScript/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<meta name="robots" content="noindex"/>
|
||||
<title>{'Send photos to ' + config.title}</title>
|
||||
<title>{'Add photos to ' + config.title}</title>
|
||||
<link rel="icon" href={basePath + '/share/static/favicon.ico'} type="image/x-icon"/>
|
||||
<link type="text/css" rel="stylesheet" href={staticPath + '/pico.min.css'}/>
|
||||
<link type="text/css" rel="stylesheet" href={staticPath + '/theme.css'}/>
|
||||
<link type="text/css" rel="stylesheet" href={staticPath + '/style.css'}/>
|
||||
</head>
|
||||
<body>
|
||||
<main class="container upload-page">
|
||||
<h1>Send photos to <em>{config.title}</em></h1>
|
||||
<button id="upload-choose" type="button" class="upload-choose">Choose photos</button>
|
||||
<header id="header">
|
||||
{galleryLink && (
|
||||
<a class="header-action" href={galleryLink} title="Back to gallery" aria-label="Back to gallery">
|
||||
<Icon path={ICON_ARROW_LEFT}/>
|
||||
</a>
|
||||
)}
|
||||
<div class="header-text">
|
||||
<h1>{config.title}</h1>
|
||||
<p class="subtitle">Add photos and videos</p>
|
||||
</div>
|
||||
</header>
|
||||
<main id="upload-main">
|
||||
<button id="upload-choose" type="button" class="upload-placeholder">
|
||||
<Icon path={ICON_CLOUD_UPLOAD} class="upload-placeholder-icon"/>
|
||||
<span class="upload-placeholder-title">Add photos</span>
|
||||
<span class="upload-placeholder-text">Choose photos and videos<span class="upload-drop-hint">, or drop them here</span></span>
|
||||
</button>
|
||||
<input id="upload-input" type="file" multiple accept={UPLOAD_ACCEPT} hidden/>
|
||||
{galleryLink && <p><a class="upload-back" href={galleryLink}>Back to gallery</a></p>}
|
||||
<section id="upload-batch" hidden aria-live="polite">
|
||||
<h2 id="upload-status"></h2>
|
||||
<section id="upload-panel" hidden aria-live="polite" aria-label="Uploads">
|
||||
<p id="upload-progress"></p>
|
||||
<p id="upload-counts"></p>
|
||||
<p id="upload-note" hidden>New photos appear in the gallery once Immich has processed them.</p>
|
||||
<ul id="upload-list"></ul>
|
||||
</section>
|
||||
</main>
|
||||
<div id="upload-drop" hidden>
|
||||
<p>Drop to send photos to <em>{config.title}</em></p>
|
||||
<Icon path={ICON_CLOUD_UPLOAD} class="upload-drop-icon"/>
|
||||
<p>Drop files to upload</p>
|
||||
</div>
|
||||
<script type="application/json" id="upload-init" dangerouslySetInnerHTML={{ __html: jsonForInlineScript(config) }}/>
|
||||
<script type="module" src={staticPath + '/js/client/upload.js'}></script>
|
||||
|
||||
Reference in New Issue
Block a user