diff --git a/.dockerignore b/.dockerignore index 14dd9db..f49ee86 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,3 +5,7 @@ /app/.env /shared/node_modules/ /shared/dist/ +/upload-app/node_modules/ +/upload-app/dist/ +/upload-app/public/js/ +/upload-app/.env diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f6fafa7..da1580f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -84,3 +84,46 @@ jobs: subject-name: docker.io/${{ github.repository }} subject-digest: ${{ steps.build.outputs.digest }} push-to-registry: true + + - name: Extract upload image metadata + id: upload-metadata + uses: docker/metadata-action@v5 + with: + images: | + ${{ github.repository }}-upload + ghcr.io/${{ github.repository }}-upload + tags: | + type=semver,pattern={{version}},value=v${{ env.PACKAGE_VERSION }} + type=semver,pattern={{major}}.{{minor}},value=v${{ env.PACKAGE_VERSION }} + type=semver,pattern={{major}},value=v${{ env.PACKAGE_VERSION }} + type=raw,value=latest,enable=${{ !contains(env.PACKAGE_VERSION, '-') }} + + - name: Build and push upload image + id: upload-build + uses: docker/build-push-action@v5 + with: + context: . + file: Dockerfile.upload + push: true + platforms: linux/amd64,linux/arm64 + build-args: | + PACKAGE_VERSION=${{ env.PACKAGE_VERSION }} + tags: ${{ steps.upload-metadata.outputs.tags }} + labels: ${{ steps.upload-metadata.outputs.labels }} + annotations: ${{ steps.upload-metadata.outputs.annotations }} + cache-from: type=gha,scope=upload + cache-to: type=gha,mode=max,scope=upload + + - name: Attest GHCR upload image + uses: actions/attest-build-provenance@v2 + with: + subject-name: ghcr.io/${{ github.repository }}-upload + subject-digest: ${{ steps.upload-build.outputs.digest }} + push-to-registry: true + + - name: Attest Docker Hub upload image + uses: actions/attest-build-provenance@v2 + with: + subject-name: docker.io/${{ github.repository }}-upload + subject-digest: ${{ steps.upload-build.outputs.digest }} + push-to-registry: true diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ee80583..300bef2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -97,6 +97,7 @@ upload-app/ immich-public-proxy-upload - one page per share; strea 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 +Dockerfile.upload The same for the upload service; it also runs from /app docker-compose.yml Reference deployment ``` @@ -193,7 +194,7 @@ The root `README.md` is a front door only: pitch, demo, quick start and links. N ## Release process -Releases are triggered by pushing a `v*` tag. The `.github/workflows/ci.yaml` workflow builds a multi-arch (`linux/amd64`, `linux/arm64`) image, pushes to both GHCR and Docker Hub, and attaches a build-provenance attestation to each registry. +Releases are triggered by pushing a `v*` tag. The `.github/workflows/ci.yaml` workflow builds both multi-arch (`linux/amd64`, `linux/arm64`) images, `immich-public-proxy` and `immich-public-proxy-upload`, with the same version tags, pushes them to both GHCR and Docker Hub, and attaches a build-provenance attestation to each image in each registry. Maintainer workflow for a release: diff --git a/Dockerfile b/Dockerfile index 1a1e992..e4dc37b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ WORKDIR /ipp COPY --chown=node:node package.json package-lock.json ./ COPY --chown=node:node shared/package.json shared/ COPY --chown=node:node app/package.json app/ +COPY --chown=node:node upload-app/package.json upload-app/ RUN npm ci --workspace=app COPY --chown=node:node shared/ shared/ @@ -17,11 +18,12 @@ COPY --chown=node:node app/ app/ RUN npm run build --workspace=shared --workspace=app # Production tree with @ipp/core dereferenced from its workspace symlink into a -# real folder (cp -L), so the runner's /app is self-contained. IPP's own -# workspace link goes first: the runner has IPP at /app, not in node_modules. +# real folder (cp -L), so the runner's /app is self-contained. The apps' +# workspace links go first: the runner has IPP at /app, and the upload app +# has no place in this image. RUN rm -rf node_modules \ && npm ci --workspace=app --omit=dev \ - && rm -rf node_modules/immich-public-proxy node_modules/.bin/immich-public-proxy \ + && rm -rf node_modules/immich-public-proxy node_modules/immich-public-proxy-upload node_modules/.bin/immich-public-proxy* \ && cp -rL node_modules /ipp/prod_modules \ && cd /ipp/prod_modules/@ipp/core \ && rm -rf src tests tsconfig.json vitest.config.ts dist/tsconfig.tsbuildinfo \ diff --git a/Dockerfile.upload b/Dockerfile.upload new file mode 100644 index 0000000..30bf0d6 --- /dev/null +++ b/Dockerfile.upload @@ -0,0 +1,50 @@ +FROM node:lts-alpine AS builder + +# /ipp must be node-owned for npm ci; WORKDIR leaves it root-owned on some builders +RUN mkdir /ipp && chown node:node /ipp + +USER node +WORKDIR /ipp + +# npm ci needs the manifest of every workspace in the root `workspaces` array +COPY --chown=node:node package.json package-lock.json ./ +COPY --chown=node:node shared/package.json shared/ +COPY --chown=node:node app/package.json app/ +COPY --chown=node:node upload-app/package.json upload-app/ +RUN npm ci --workspace=upload-app + +COPY --chown=node:node shared/ shared/ +COPY --chown=node:node upload-app/ upload-app/ +RUN npm run build --workspace=shared --workspace=upload-app + +# Production tree with @ipp/core dereferenced from its workspace symlink into a +# real folder (cp -L), so the runner's /app is self-contained. The apps' +# workspace links go first: the runner has the upload app at /app, and IPP +# has no place in this image. +RUN rm -rf node_modules \ + && npm ci --workspace=upload-app --omit=dev \ + && rm -rf node_modules/immich-public-proxy node_modules/immich-public-proxy-upload node_modules/.bin/immich-public-proxy* \ + && cp -rL node_modules /ipp/prod_modules \ + && cd /ipp/prod_modules/@ipp/core \ + && rm -rf src tests tsconfig.json vitest.config.ts dist/tsconfig.tsbuildinfo \ + && rm -f /ipp/upload-app/dist/tsconfig.tsbuildinfo + +FROM node:lts-alpine AS runner + +RUN apk --no-cache add curl \ + && mkdir /app && chown node:node /app + +# At /app, as IPP, so a config.json mounts at the same path in both images +USER node +WORKDIR /app +COPY --from=builder --chown=node:node /ipp/upload-app/package.json ./ +COPY --from=builder --chown=node:node /ipp/prod_modules ./node_modules +COPY --from=builder --chown=node:node /ipp/upload-app/dist ./dist +COPY --from=builder --chown=node:node /ipp/upload-app/public ./public +COPY --from=builder --chown=node:node /ipp/upload-app/config.json ./ + +ARG PACKAGE_VERSION +ENV APP_VERSION=${PACKAGE_VERSION} +ENV NODE_ENV=production + +CMD ["node", "dist/index.js" ] diff --git a/app/config.json b/app/config.json index fdf6bcf..52f0f2a 100644 --- a/app/config.json +++ b/app/config.json @@ -9,6 +9,7 @@ "motionPhotos": true, "downloadedFilename": 0, "allowDownload": 0, + "uploadUrl": "", "allowSlugLinks": true, "showHomePage": true, "showMetadata": { diff --git a/app/src/gallery/builder.ts b/app/src/gallery/builder.ts index d12977d..44f5213 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 } from '../share' +import { canDownload, expiryDate, uploadLink } from '../share' import { h } from 'preact' import { Gallery, GalleryItem, GalleryProps } from '../view/gallery' import type { GroupByDateMode } from '../shared/types' @@ -155,7 +155,8 @@ export async function gallery (res: Response, share: SharedLink, openItem?: numb locationWebLink: !!getConfigOption('ipp.showMetadata.location.webLink', true) }, groupByDate, - metaBase + metaBase, + uploadLink: uploadLink(share) } // HTML gallery page cache time. A password-protected gallery keeps the diff --git a/app/src/share.ts b/app/src/share.ts index 8f575cc..fe26b78 100644 --- a/app/src/share.ts +++ b/app/src/share.ts @@ -1,4 +1,4 @@ -import { Asset, getConfigOption, log, SharedLink, TtlLruCache } from '@ipp/core' +import { Asset, getConfigOption, KeyType, log, SharedLink, TtlLruCache } from '@ipp/core' import { DownloadAll } from './types' import dayjs from 'dayjs' @@ -22,6 +22,21 @@ export function canDownload (share: SharedLink): boolean { let warnedInvalidDownloadPolicy = false +/** + * 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. + */ +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(/\/+$/, '') + return share.keyType === KeyType.slug && share.slug + ? base + '/s/' + encodeURIComponent(share.slug) + : base + '/share/' + encodeURIComponent(share.key) +} + // Asset ids already warned about, so each warns at most once a day const warnedUnprocessed = new TtlLruCache({ ttlMs: 24 * 60 * 60_000, max: 1000 }) diff --git a/app/src/view/gallery.tsx b/app/src/view/gallery.tsx index 7497e26..4d701bf 100644 --- a/app/src/view/gallery.tsx +++ b/app/src/view/gallery.tsx @@ -21,6 +21,8 @@ export interface GalleryProps { metadataConfig: MetadataConfig groupByDate: GroupByDateMode | false metaBase?: string + // The upload service's page for this share, when it accepts uploads + uploadLink?: string } export function Gallery (props: GalleryProps) { @@ -69,7 +71,7 @@ export function Gallery (props: GalleryProps) { - {(showHeaderText || props.showDownloadZip) && ( + {(showHeaderText || props.showDownloadZip || props.uploadLink) && ( )} {props.description && ( diff --git a/app/tests/uploadLink.test.ts b/app/tests/uploadLink.test.ts new file mode 100644 index 0000000..5702862 --- /dev/null +++ b/app/tests/uploadLink.test.ts @@ -0,0 +1,52 @@ +import { describe, it, expect, beforeEach } from 'vitest' +import { KeyType, loadConfig, SharedLink } from '@ipp/core' +import { uploadLink } from '../src/share' + +function setConfig (config: unknown) { + process.env.CONFIG = JSON.stringify(config) + loadConfig() +} + +const share = (overrides: Partial = {}): SharedLink => ({ + key: 'the-key', + keyType: KeyType.key, + type: 'ALBUM', + assets: [], + expiresAt: null, + allowUpload: true, + ...overrides +}) + +describe('uploadLink', () => { + beforeEach(() => { + delete process.env.CONFIG + loadConfig() + }) + + it('is absent while ipp.uploadUrl is unset', () => { + expect(uploadLink(share())).toBeUndefined() + }) + + it('links a key share by its key, without a doubled slash', () => { + setConfig({ ipp: { uploadUrl: 'https://upload.example.com/' } }) + expect(uploadLink(share())).toBe('https://upload.example.com/share/the-key') + }) + + it('links a share opened by its slug by that slug', () => { + setConfig({ ipp: { uploadUrl: 'https://photos.example.com/upload' } }) + expect(uploadLink(share({ keyType: KeyType.slug, slug: 'holiday 2025' }))).toBe('https://photos.example.com/upload/s/holiday%202025') + }) + + it('is absent when the owner has not allowed uploads', () => { + setConfig({ ipp: { uploadUrl: 'https://upload.example.com' } }) + expect(uploadLink(share({ allowUpload: false }))).toBeUndefined() + expect(uploadLink(share({ allowUpload: undefined }))).toBeUndefined() + }) + + it('ignores a value that is not an http(s) URL', () => { + setConfig({ ipp: { uploadUrl: 'javascript:alert(1)' } }) + expect(uploadLink(share())).toBeUndefined() + setConfig({ ipp: { uploadUrl: 42 } }) + expect(uploadLink(share())).toBeUndefined() + }) +}) diff --git a/docs/config/ipp-options.md b/docs/config/ipp-options.md index 615cdcf..cdd4c2d 100644 --- a/docs/config/ipp-options.md +++ b/docs/config/ipp-options.md @@ -104,6 +104,12 @@ The bulk-zip and per-asset buttons can be toggled independently once downloads a > > To guarantee full-quality downloads of everything, leave the share's download permission on in Immich. +## `uploadUrl` + +**Type:** `string` · **Default:** `""` + +The public URL of the optional upload service (the `immich-public-proxy-upload` container), for example `https://upload.example.com`. 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. + ## `allowSlugLinks` **Type:** `bool` · **Default:** `true` diff --git a/shared/public/theme.css b/shared/public/theme.css index 03e5cb3..9b5a3be 100644 --- a/shared/public/theme.css +++ b/shared/public/theme.css @@ -114,6 +114,13 @@ html, body { opacity: 0.55; } +/* The round icon buttons at the end of the header */ +.header-actions { + display: flex; + flex: 0 0 auto; + gap: 4px; +} + /* Round icon button in the header: an anchor or a button */ .header-action { flex: 0 0 auto; diff --git a/upload-app/package.json b/upload-app/package.json index 0590ac0..5aa27f9 100644 --- a/upload-app/package.json +++ b/upload-app/package.json @@ -8,6 +8,7 @@ "build": "tsc -b && tsc -p tsconfig.client.json", "test": "vitest run", "test:watch": "vitest", + "test:container": "podman build --build-arg PACKAGE_VERSION=$(npm pkg get version | xargs) --format docker -f ../Dockerfile.upload -t immich-proxy-upload-test .. && podman run --init -it -p=3001:3000 --env-file .env -e IPP_PORT=3000 --restart=always immich-proxy-upload-test", "start": "node dist/index.js" }, "author": "Alan Grainger", diff --git a/upload-app/src/shared/upload.ts b/upload-app/src/shared/upload.ts index 91c1ced..fb2ae8e 100644 --- a/upload-app/src/shared/upload.ts +++ b/upload-app/src/shared/upload.ts @@ -19,7 +19,7 @@ export function isUploadableType (mimeType: string): boolean { } /* - Types for files a desktop browser reports with an empty `File.type`: HEIC, + Types for files a desktop browser reports with an empty or generic `File.type`: HEIC, HEIF, camera RAW and some video containers. Immich types the asset by its extension, so these only need to pass `isUploadableType`. */ @@ -46,11 +46,15 @@ const TYPE_BY_EXTENSION: Record = { insv: 'video/mp4' } -/** The browser's type for a file, or one derived from its extension when the browser gives none. */ +/** + * The browser's type for a file, or one derived from its extension when the + * browser gives none or a generic one (Chrome reports HEIC from the file + * picker as `application/octet-stream`). + */ export function fileType (name: string, browserType: string): string { - if (browserType) return browserType + if (isUploadableType(browserType)) return browserType const extension = name.split('.').pop()?.toLowerCase() || '' - return TYPE_BY_EXTENSION[extension] || '' + return TYPE_BY_EXTENSION[extension] || browserType } /** Bytes as a short human-readable string. */ diff --git a/upload-app/tests/uploadRules.test.ts b/upload-app/tests/uploadRules.test.ts index 147f8df..dfc06af 100644 --- a/upload-app/tests/uploadRules.test.ts +++ b/upload-app/tests/uploadRules.test.ts @@ -27,6 +27,14 @@ describe('fileType', () => { expect(fileType('IMG_0042.HEIC', 'image/heic')).toBe('image/heic') }) + it('derives a type from the extension when the browser gives a generic one', () => { + expect(fileType('IMG_0042.HEIC', 'application/octet-stream')).toBe('image/heic') + }) + + it('keeps a generic type it cannot improve on, so the client refuses the file', () => { + expect(fileType('notes.txt', 'application/octet-stream')).toBe('application/octet-stream') + }) + it('derives a type from the extension when the browser gives none', () => { expect(fileType('IMG_0042.HEIC', '')).toBe('image/heic') expect(fileType('DSC_0001.NEF', '')).toBe('image/x-nikon-nef')