diff --git a/.claude/settings.json b/.claude/settings.json new file mode 100644 index 0000000..671a084 --- /dev/null +++ b/.claude/settings.json @@ -0,0 +1,6 @@ +{ + "attribution": { + "commit": "", + "pr": "" + } +} diff --git a/.env.sample b/.env.sample index 071d49f..6207dbc 100644 --- a/.env.sample +++ b/.env.sample @@ -15,6 +15,8 @@ AI_BASE_URL="https://api.openai.com/v1" # Base URL for OpenAI-compatible API AI_MODEL="gpt-4o-mini" # Model used for parsing search queries in "Find" # Immich Share Link -IMMICH_SHARE_LINK_KEY="" # Share link key for Immich POWER_TOOLS_ENDPOINT_URL="" # URL of the Power Tools endpoint (Used for share links) JWT_SECRET="" # JWT Secret for authentication + +# App Database (SQLite) +APP_DB_PATH="" # Path to the SQLite database file (default: ./data/app.db) diff --git a/.gitignore b/.gitignore index b9f8d23..fcdd3c9 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,11 @@ yarn-error.log* # local env files .env*.local +# SQLite database files +/data/*.db +/data/*.db-shm +/data/*.db-wal + # vercel .vercel @@ -35,4 +40,7 @@ yarn-error.log* *.tsbuildinfo next-env.d.ts +# plans +/docs/plans + .env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index a6992f3..712cfed 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,13 +1,10 @@ # Source: https://github.com/vercel/next.js/blob/canary/examples/with-docker/README.md # Install dependencies only when needed -FROM node:22-alpine AS deps -# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. - -RUN apk add --no-cache libc6-compat +FROM oven/bun:1-alpine AS deps WORKDIR /app -COPY package.json ./ -RUN npm install --frozen-lockfile +COPY package.json bun.lockb ./ +RUN bun install --frozen-lockfile # Rebuild the source code only when needed FROM node:22-alpine AS builder @@ -38,10 +35,15 @@ RUN adduser -S nextjs -u 1001 COPY --from=builder /app/public ./public COPY --from=builder /app/package.json ./package.json -# Automatically leverage output traces to reduce image size +# Automatically leverage output traces to reduce image size # https://nextjs.org/docs/advanced-features/output-file-tracing COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static +COPY --from=builder --chown=nextjs:nodejs /app/src/db/migrations ./src/db/migrations + +# Copy native libsql binaries — Next.js standalone tracing misses dynamically required native modules +COPY --from=deps --chown=nextjs:nodejs /app/node_modules/@libsql ./node_modules/@libsql +COPY --from=deps --chown=nextjs:nodejs /app/node_modules/libsql ./node_modules/libsql USER nextjs @@ -58,4 +60,4 @@ ENV HOSTNAME=0.0.0.0 CMD ["node", "server.js"] -HEALTHCHECK CMD curl --fail http://localhost:3000/api/health || exit 1 \ No newline at end of file +HEALTHCHECK CMD curl --fail http://localhost:3000/api/health || exit 1 diff --git a/bun.lockb b/bun.lockb index f6133ad..f521ce9 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/drizzle.app.config.ts b/drizzle.app.config.ts new file mode 100644 index 0000000..3e3ede5 --- /dev/null +++ b/drizzle.app.config.ts @@ -0,0 +1,12 @@ +import { defineConfig } from "drizzle-kit"; + +export default defineConfig({ + dialect: "sqlite", + schema: "./src/db/schema/index.ts", + out: "./src/db/migrations", + dbCredentials: { + url: process.env.APP_DB_PATH + ? `file:${process.env.APP_DB_PATH}` + : "file:./data/app.db", + }, +}); diff --git a/next.config.mjs b/next.config.mjs index 65ab7b9..3f1b21a 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -3,6 +3,9 @@ const nextConfig = { reactStrictMode: false, output: 'standalone', + experimental: { + instrumentationHook: true, + }, images: { unoptimized: true, }, diff --git a/package.json b/package.json index 75dd287..88b0984 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,10 @@ "dev": "next dev", "build": "next build", "start": "next start -p ${PORT:=3000} -H ${HOST:=0.0.0.0}", - "lint": "next lint" + "lint": "next lint", + "db:generate": "drizzle-kit generate --config=drizzle.app.config.ts", + "db:migrate": "drizzle-kit migrate --config=drizzle.app.config.ts", + "db:studio": "drizzle-kit studio --config=drizzle.app.config.ts" }, "author": { "name": "Varun Raj Manoharan", @@ -17,6 +20,7 @@ "@ai-sdk/openai": "^3.0.29", "@ai-sdk/openai-compatible": "^2.0.30", "@ai-sdk/react": "^3.0.88", + "@libsql/client": "^0.17.2", "@radix-ui/react-accordion": "^1.2.1", "@radix-ui/react-alert-dialog": "^1.1.4", "@radix-ui/react-avatar": "^1.1.0", @@ -54,7 +58,7 @@ "cookie": "^0.6.0", "date-fns": "^3.6.0", "date-fns-tz": "^3.2.0", - "drizzle-orm": "^0.33.0", + "drizzle-orm": "^0.45.1", "jsonwebtoken": "^9.0.2", "leaflet": "^1.9.4", "leaflet-defaulticon-compatibility": "0.1.2", @@ -92,7 +96,7 @@ "@types/react-dom": "^18", "@types/react-mentions": "^4.4.1", "autoprefixer": "^10.4.20", - "drizzle-kit": "^0.24.0", + "drizzle-kit": "^0.31.10", "eslint": "^8", "eslint-config-next": "14.2.5", "postcss": "^8.4.41", diff --git a/src/components/import/ImportJobDetailDialog.tsx b/src/components/import/ImportJobDetailDialog.tsx new file mode 100644 index 0000000..e1abd74 --- /dev/null +++ b/src/components/import/ImportJobDetailDialog.tsx @@ -0,0 +1,164 @@ +import React from "react"; +import { useQuery } from "@tanstack/react-query"; +import { ExternalLink, Loader2 } from "lucide-react"; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { getImportJob } from "@/handlers/api/import-jobs.handler"; +import { useConfig } from "@/contexts/ConfigContext"; + +const InfoRow = ({ label, value }: { label: string; value: React.ReactNode }) => ( +
+

{label}

+

{value ?? "—"}

+
+); + +const statusBadge = (status: string) => { + const styles = + status === "uploaded" || status === "completed" + ? "bg-emerald-500/10 text-emerald-600" + : status === "skipped" + ? "bg-amber-500/10 text-amber-600" + : status === "failed" + ? "bg-destructive/10 text-destructive" + : "bg-muted text-muted-foreground"; + + return ( + + {status} + + ); +}; + +interface ImportJobDetailDialogProps { + jobId: string | null; + onClose: () => void; +} + +export default function ImportJobDetailDialog({ jobId, onClose }: ImportJobDetailDialogProps) { + const { exImmichUrl } = useConfig(); + + const { data: detailJob, isLoading } = useQuery({ + queryKey: ["import-job-detail", jobId], + queryFn: () => getImportJob(jobId!), + enabled: !!jobId, + }); + + let dImportData: Record = {}; + let dAlbumOpts: { createAlbum?: boolean; albumName?: string; addToAlbumId?: string } | undefined; + let dError: string | null = null; + let albumId: string | undefined; + + if (detailJob) { + try { + dImportData = JSON.parse(detailJob.job.importData); + } catch { /* ignore */ } + dAlbumOpts = dImportData.albumOptions as typeof dAlbumOpts; + dError = typeof dImportData.error === "string" ? dImportData.error : null; + albumId = typeof dImportData.albumId === "string" ? dImportData.albumId : undefined; + } + + return ( + { if (!open) onClose(); }}> + + {isLoading && ( +
+ +
+ )} + {detailJob && (() => { + const dJob = detailJob.job; + + return ( + <> + + {dAlbumOpts?.albumName || "Import details"} + + +
+ + + + + + + {albumId && ( + + {dAlbumOpts?.albumName || albumId} + + + } /> + )} + {!albumId && dAlbumOpts?.albumName && ( + + )} +
+ + {dError && ( +
+ {dError} +
+ )} + +
+ + + + + + + + + + {detailJob.items.map((item) => { + let itemData: Record = {}; + try { itemData = JSON.parse(item.itemData ?? "{}"); } catch { /* ignore */ } + const fileName = (itemData.originalFileName as string) || item.assetId; + + return ( + + + + + + ); + })} + +
AssetStatusDetails
+ {item.immichId ? ( + e.stopPropagation()} + > + {fileName} + + + ) : ( + fileName + )} + + {statusBadge(item.status)} + + {item.error || "—"} +
+
+ + ); + })()} +
+
+ ); +} diff --git a/src/components/shared/ShareAssetsTrigger.tsx b/src/components/shared/ShareAssetsTrigger.tsx index 31e05db..0e3756a 100644 --- a/src/components/shared/ShareAssetsTrigger.tsx +++ b/src/components/shared/ShareAssetsTrigger.tsx @@ -3,6 +3,7 @@ import { Dialog, DialogTitle, DialogContent, DialogHeader, DialogTrigger, Dialog import { Button, ButtonProps } from '../ui/button'; import { ShareLinkFilters } from '@/types/shareLink'; import { generateShareLink } from '@/handlers/api/shareLink.handler'; +import { createShareKey, getShareKey } from '@/handlers/api/share-key.handler'; import { Input } from '../ui/input'; import { Label } from '@radix-ui/react-label'; @@ -22,6 +23,28 @@ export default function ShareAssetsTrigger({ filters, buttonProps }: ShareAssets const [config, setConfig] = useState>({ expiresIn: "never" }); + const [endpointConfigured, setEndpointConfigured] = useState(null); + const [shareKeyExists, setShareKeyExists] = useState(null); + const [creatingKey, setCreatingKey] = useState(false); + const [keyError, setKeyError] = useState(null); + + const checkShareKey = async () => { + const data = await getShareKey(); + setEndpointConfigured(data.endpointConfigured); + setShareKeyExists(data.exists); + }; + + const handleCreateKey = async () => { + setCreatingKey(true); + setKeyError(null); + try { + await createShareKey(); + setShareKeyExists(true); + } catch (err: any) { + setKeyError(err.message ?? 'Failed to create share key'); + } + setCreatingKey(false); + }; const handleReset = () => { setConfig({ expiresIn: "never" }); @@ -52,7 +75,7 @@ export default function ShareAssetsTrigger({ filters, buttonProps }: ShareAssets return ( - + { if (open) checkShareKey(); }}> @@ -63,6 +86,24 @@ export default function ShareAssetsTrigger({ filters, buttonProps }: ShareAssets Generate a share link for the selected assets (either bunch of albums or bunch of people) and share it with your friends and family. + {endpointConfigured === false && ( +
+

+ POWER_TOOLS_ENDPOINT_URL is not set. Add it to your environment variables to enable share links. +

+
+ )} + {endpointConfigured && shareKeyExists === false && ( +
+

+ Serving thumbnails on share links requires a dedicated API key. Create one now with minimal permissions. +

+ {keyError &&

{keyError}

} + +
+ )} {errorMessage &&
{errorMessage}
} {generatedLink ?
@@ -106,7 +147,7 @@ export default function ShareAssetsTrigger({ filters, buttonProps }: ShareAssets
- + )} diff --git a/src/config/constants/sidebarNavs.tsx b/src/config/constants/sidebarNavs.tsx index 27c1834..78d8655 100644 --- a/src/config/constants/sidebarNavs.tsx +++ b/src/config/constants/sidebarNavs.tsx @@ -1,4 +1,4 @@ -import { Copy, GalleryHorizontal, GalleryVerticalEnd, Image as ImageIcon, MapPin, MapPinX, Rewind, Search, Share2, User, Video } from "lucide-react"; +import { Copy, GalleryHorizontal, GalleryVerticalEnd, Image as ImageIcon, MapPin, MapPinX, Rewind, Search, Settings, Share2, User, Video } from "lucide-react"; export const sidebarNavs = [ // { @@ -59,5 +59,9 @@ export const sidebarNavs = [ link: "/assets/bulk-duplicate-finder", icon: , }, - + { + title: "Settings", + link: "/settings", + icon: , + }, ]; diff --git a/src/config/environment.ts b/src/config/environment.ts index f3daf06..bc0c5c3 100644 --- a/src/config/environment.ts +++ b/src/config/environment.ts @@ -15,8 +15,9 @@ export const ENV = { AI_API_KEY: process.env.AI_API_KEY as string, AI_BASE_URL: (process.env.AI_BASE_URL || 'https://api.openai.com/v1') as string, AI_MODEL: process.env.AI_MODEL as string, - IMMICH_SHARE_LINK_KEY: process.env.IMMICH_SHARE_LINK_KEY as string, POWER_TOOLS_ENDPOINT_URL: process.env.POWER_TOOLS_ENDPOINT_URL as string, + // App SQLite database path (defaults to ./data/app.db) + APP_DB_PATH: process.env.APP_DB_PATH as string | undefined, // OAuth configuration OAUTH_ENABLED: process.env.OAUTH_ENABLED === 'true', OAUTH_BUTTON_TEXT: (process.env.OAUTH_BUTTON_TEXT || 'Login with OAuth') as string, diff --git a/src/config/routes.ts b/src/config/routes.ts index 5011619..9067511 100644 --- a/src/config/routes.ts +++ b/src/config/routes.ts @@ -83,4 +83,22 @@ export const SHARE_LINK_PEOPLE_PATH = (token: string) => BASE_API_ENDPOINT + "/s export const SHARE_LINK_DOWNLOAD_PATH = (token: string) => BASE_API_ENDPOINT + "/share-link/" + token + "/download"; export const DOWNLOAD_ASSETS_PATH = BASE_PROXY_ENDPOINT + "/download/archive"; -export const LIST_DUPLICATES_PATH = BASE_PROXY_ENDPOINT + "/duplicates"; \ No newline at end of file +export const LIST_DUPLICATES_PATH = BASE_PROXY_ENDPOINT + "/duplicates"; + +// Settings +export const SETTINGS_API_KEYS_PATH = BASE_API_ENDPOINT + "/settings/api-keys"; +export const SETTINGS_API_KEY_PATH = (purpose: string) => BASE_API_ENDPOINT + "/settings/api-keys/" + purpose; + +// Share Key +export const SHARE_KEY_PATH = BASE_API_ENDPOINT + "/share-key"; + +// Import Shared +export const IMPORT_SHARED_PATH = BASE_API_ENDPOINT + "/import-shared"; +export const IMPORT_SHARED_UPLOAD_ALL_PATH = BASE_API_ENDPOINT + "/import-shared/upload-all"; + +// Permissions +export const VALIDATE_PERMISSIONS_PATH = BASE_API_ENDPOINT + "/validate-permissions"; + +// Import Jobs (queue) +export const IMPORT_JOBS_PATH = BASE_API_ENDPOINT + "/import-jobs"; +export const IMPORT_JOB_PATH = (jobId: string) => BASE_API_ENDPOINT + "/import-jobs/" + jobId; \ No newline at end of file diff --git a/src/db/index.ts b/src/db/index.ts new file mode 100644 index 0000000..993fd62 --- /dev/null +++ b/src/db/index.ts @@ -0,0 +1,23 @@ +import { createClient } from "@libsql/client"; +import { drizzle } from "drizzle-orm/libsql"; +import { migrate } from "drizzle-orm/libsql/migrator"; +import fs from "fs"; +import path from "path"; +import * as schema from "./schema"; + +const DB_PATH = + process.env.APP_DB_PATH || path.join(process.cwd(), "data", "app.db"); + +fs.mkdirSync(path.dirname(DB_PATH), { recursive: true }); + +const client = createClient({ url: `file:${DB_PATH}` }); + +export const appDb = drizzle(client, { schema }); + +// Run pending migrations automatically on startup. +// migrate() is idempotent — safe to call on every boot. +export async function runMigrations() { + await migrate(appDb, { + migrationsFolder: path.join(process.cwd(), "src", "db", "migrations"), + }); +} diff --git a/src/db/migrations/0000_vengeful_kylun.sql b/src/db/migrations/0000_vengeful_kylun.sql new file mode 100644 index 0000000..b60b53d --- /dev/null +++ b/src/db/migrations/0000_vengeful_kylun.sql @@ -0,0 +1,17 @@ +CREATE TABLE `api_keys` ( + `id` text PRIMARY KEY NOT NULL, + `user_id` text NOT NULL, + `purpose` text NOT NULL, + `key_name` text NOT NULL, + `secret` text NOT NULL, + `immich_key_id` text NOT NULL, + `created_at` integer +); +--> statement-breakpoint +CREATE TABLE `settings` ( + `key` text PRIMARY KEY NOT NULL, + `value` text NOT NULL, + `updated_at` integer +); +--> statement-breakpoint +CREATE UNIQUE INDEX `api_keys_user_id_purpose_unique` ON `api_keys` (`user_id`,`purpose`); \ No newline at end of file diff --git a/src/db/migrations/0001_rainy_jamie_braddock.sql b/src/db/migrations/0001_rainy_jamie_braddock.sql new file mode 100644 index 0000000..507a47f --- /dev/null +++ b/src/db/migrations/0001_rainy_jamie_braddock.sql @@ -0,0 +1,28 @@ +CREATE TABLE `import_job_items` ( + `id` text PRIMARY KEY NOT NULL, + `job_id` text NOT NULL, + `asset_id` text NOT NULL, + `status` text DEFAULT 'pending' NOT NULL, + `item_data` text DEFAULT '{}' NOT NULL, + `immich_id` text, + `error` text, + `created_at` integer, + `updated_at` integer, + FOREIGN KEY (`job_id`) REFERENCES `import_jobs`(`id`) ON UPDATE no action ON DELETE no action +); +--> statement-breakpoint +CREATE TABLE `import_jobs` ( + `id` text PRIMARY KEY NOT NULL, + `user_id` text NOT NULL, + `platform` text NOT NULL, + `status` text DEFAULT 'pending' NOT NULL, + `url` text NOT NULL, + `url_config` text DEFAULT '{}' NOT NULL, + `import_data` text DEFAULT '{}' NOT NULL, + `total_count` integer DEFAULT 0 NOT NULL, + `uploaded_count` integer DEFAULT 0 NOT NULL, + `skipped_count` integer DEFAULT 0 NOT NULL, + `failed_count` integer DEFAULT 0 NOT NULL, + `created_at` integer, + `updated_at` integer +); diff --git a/src/db/migrations/meta/0000_snapshot.json b/src/db/migrations/meta/0000_snapshot.json new file mode 100644 index 0000000..f17cfa1 --- /dev/null +++ b/src/db/migrations/meta/0000_snapshot.json @@ -0,0 +1,114 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "8b257fc8-972b-47da-ae98-7a83316d6df6", + "prevId": "00000000-0000-0000-0000-000000000000", + "tables": { + "api_keys": { + "name": "api_keys", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "purpose": { + "name": "purpose", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "key_name": { + "name": "key_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "secret": { + "name": "secret", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "immich_key_id": { + "name": "immich_key_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "api_keys_user_id_purpose_unique": { + "name": "api_keys_user_id_purpose_unique", + "columns": [ + "user_id", + "purpose" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "settings": { + "name": "settings", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + } + }, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/src/db/migrations/meta/0001_snapshot.json b/src/db/migrations/meta/0001_snapshot.json new file mode 100644 index 0000000..026a6bf --- /dev/null +++ b/src/db/migrations/meta/0001_snapshot.json @@ -0,0 +1,314 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "bec76260-a0eb-4895-a32e-244df56b6c2e", + "prevId": "8b257fc8-972b-47da-ae98-7a83316d6df6", + "tables": { + "settings": { + "name": "settings", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "api_keys": { + "name": "api_keys", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "purpose": { + "name": "purpose", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "key_name": { + "name": "key_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "secret": { + "name": "secret", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "immich_key_id": { + "name": "immich_key_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "api_keys_user_id_purpose_unique": { + "name": "api_keys_user_id_purpose_unique", + "columns": [ + "user_id", + "purpose" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "import_job_items": { + "name": "import_job_items", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "job_id": { + "name": "job_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "asset_id": { + "name": "asset_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'pending'" + }, + "item_data": { + "name": "item_data", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'{}'" + }, + "immich_id": { + "name": "immich_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "import_job_items_job_id_import_jobs_id_fk": { + "name": "import_job_items_job_id_import_jobs_id_fk", + "tableFrom": "import_job_items", + "tableTo": "import_jobs", + "columnsFrom": [ + "job_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "import_jobs": { + "name": "import_jobs", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "platform": { + "name": "platform", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'pending'" + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "url_config": { + "name": "url_config", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'{}'" + }, + "import_data": { + "name": "import_data", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'{}'" + }, + "total_count": { + "name": "total_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "uploaded_count": { + "name": "uploaded_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "skipped_count": { + "name": "skipped_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "failed_count": { + "name": "failed_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/src/db/migrations/meta/_journal.json b/src/db/migrations/meta/_journal.json new file mode 100644 index 0000000..05662f3 --- /dev/null +++ b/src/db/migrations/meta/_journal.json @@ -0,0 +1,20 @@ +{ + "version": "5", + "dialect": "sqlite", + "entries": [ + { + "idx": 0, + "version": "6", + "when": 1774242821856, + "tag": "0000_vengeful_kylun", + "breakpoints": true + }, + { + "idx": 1, + "version": "6", + "when": 1775227109706, + "tag": "0001_rainy_jamie_braddock", + "breakpoints": true + } + ] +} \ No newline at end of file diff --git a/src/db/schema/apiKeys.schema.ts b/src/db/schema/apiKeys.schema.ts new file mode 100644 index 0000000..e931daa --- /dev/null +++ b/src/db/schema/apiKeys.schema.ts @@ -0,0 +1,14 @@ +import { sqliteTable, text, integer, unique } from "drizzle-orm/sqlite-core"; +import { randomUUID } from "crypto"; + +export const apiKeys = sqliteTable("api_keys", { + id: text("id").primaryKey().$defaultFn(() => randomUUID()), + userId: text("user_id").notNull(), + purpose: text("purpose").notNull(), + keyName: text("key_name").notNull(), + secret: text("secret").notNull(), + immichKeyId: text("immich_key_id").notNull(), + createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(() => new Date()), +}, (t) => [ + unique().on(t.userId, t.purpose), +]); diff --git a/src/db/schema/importJobs.schema.ts b/src/db/schema/importJobs.schema.ts new file mode 100644 index 0000000..259f16d --- /dev/null +++ b/src/db/schema/importJobs.schema.ts @@ -0,0 +1,34 @@ +import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core"; +import { randomUUID } from "crypto"; + +export const importJobs = sqliteTable("import_jobs", { + id: text("id").primaryKey().$defaultFn(() => randomUUID()), + userId: text("user_id").notNull(), + platform: text("platform").notNull(), // 'immich' | 'nextcloud' | 'photoprism' + status: text("status").notNull().default("pending"), // pending | processing | completed | failed + url: text("url").notNull(), // base URL of source + urlConfig: text("url_config").notNull().default("{}"), // JSON: auth/connection (key, token, etc.) + importData: text("import_data").notNull().default("{}"), // JSON: album options, resolved albumId, etc. + totalCount: integer("total_count").notNull().default(0), + uploadedCount: integer("uploaded_count").notNull().default(0), + skippedCount: integer("skipped_count").notNull().default(0), + failedCount: integer("failed_count").notNull().default(0), + createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(() => new Date()), + updatedAt: integer("updated_at", { mode: "timestamp" }) + .$defaultFn(() => new Date()) + .$onUpdate(() => new Date()), +}); + +export const importJobItems = sqliteTable("import_job_items", { + id: text("id").primaryKey().$defaultFn(() => randomUUID()), + jobId: text("job_id").notNull().references(() => importJobs.id), + assetId: text("asset_id").notNull(), // source platform's asset ID + status: text("status").notNull().default("pending"), // pending | processing | uploaded | skipped | failed + itemData: text("item_data").notNull().default("{}"), // JSON: filename, type, dates, isFavorite, etc. + immichId: text("immich_id"), // Immich asset ID after successful upload + error: text("error"), + createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(() => new Date()), + updatedAt: integer("updated_at", { mode: "timestamp" }) + .$defaultFn(() => new Date()) + .$onUpdate(() => new Date()), +}); diff --git a/src/db/schema/index.ts b/src/db/schema/index.ts new file mode 100644 index 0000000..df0931d --- /dev/null +++ b/src/db/schema/index.ts @@ -0,0 +1,3 @@ +export * from "./settings.schema"; +export * from "./apiKeys.schema"; +export * from "./importJobs.schema"; diff --git a/src/db/schema/settings.schema.ts b/src/db/schema/settings.schema.ts new file mode 100644 index 0000000..463a2ee --- /dev/null +++ b/src/db/schema/settings.schema.ts @@ -0,0 +1,9 @@ +import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core"; + +export const settings = sqliteTable("settings", { + key: text("key").primaryKey(), + value: text("value").notNull(), + updatedAt: integer("updated_at", { mode: "timestamp" }).$onUpdate( + () => new Date() + ), +}); diff --git a/src/handlers/api/import-jobs.handler.ts b/src/handlers/api/import-jobs.handler.ts new file mode 100644 index 0000000..3f27132 --- /dev/null +++ b/src/handlers/api/import-jobs.handler.ts @@ -0,0 +1,56 @@ +import { IMPORT_JOBS_PATH, IMPORT_JOB_PATH } from "@/config/routes"; +import API from "@/lib/api"; + +export interface CreateImportJobParams { + platform: string; + url: string; + urlConfig: Record; + importData: Record; + assets: { id: string; [key: string]: unknown }[]; +} + +export const createImportJob = async (params: CreateImportJobParams): Promise<{ jobId: string }> => { + return API.post(IMPORT_JOBS_PATH, params); +}; + +export interface ImportJobSummary { + id: string; + platform: string; + status: string; + url: string; + importData: string; + totalCount: number; + uploadedCount: number; + skippedCount: number; + failedCount: number; + createdAt: string; +} + +export const listImportJobs = async (): Promise<{ jobs: ImportJobSummary[] }> => { + return API.get(IMPORT_JOBS_PATH); +}; + +export interface ImportJobStatus { + job: { + id: string; + status: string; + platform: string; + totalCount: number; + uploadedCount: number; + skippedCount: number; + failedCount: number; + importData: string; + }; + items: { + id: string; + assetId: string; + status: string; + itemData: string; + immichId: string | null; + error: string | null; + }[]; +} + +export const getImportJob = async (jobId: string): Promise => { + return API.get(IMPORT_JOB_PATH(jobId)); +}; diff --git a/src/handlers/api/import-shared.handler.ts b/src/handlers/api/import-shared.handler.ts new file mode 100644 index 0000000..e6fee34 --- /dev/null +++ b/src/handlers/api/import-shared.handler.ts @@ -0,0 +1,6 @@ +import { IMPORT_SHARED_PATH } from "@/config/routes"; +import API from "@/lib/api"; + +export const importShared = async (link: string) => { + return API.post(IMPORT_SHARED_PATH, { link }); +}; diff --git a/src/handlers/api/settings.handler.ts b/src/handlers/api/settings.handler.ts new file mode 100644 index 0000000..494f818 --- /dev/null +++ b/src/handlers/api/settings.handler.ts @@ -0,0 +1,14 @@ +import { SETTINGS_API_KEYS_PATH, SETTINGS_API_KEY_PATH } from "@/config/routes"; +import API from "@/lib/api"; + +export const getApiKeys = async () => { + return API.get(SETTINGS_API_KEYS_PATH); +}; + +export const deleteApiKey = async (purpose: string) => { + return API.delete(SETTINGS_API_KEY_PATH(purpose)); +}; + +export const regenerateApiKey = async (purpose: string) => { + return API.put(SETTINGS_API_KEY_PATH(purpose)); +}; diff --git a/src/handlers/api/share-key.handler.ts b/src/handlers/api/share-key.handler.ts new file mode 100644 index 0000000..2fb8b8c --- /dev/null +++ b/src/handlers/api/share-key.handler.ts @@ -0,0 +1,10 @@ +import { SHARE_KEY_PATH } from "@/config/routes"; +import API from "@/lib/api"; + +export const getShareKey = async () => { + return API.get(SHARE_KEY_PATH); +}; + +export const createShareKey = async () => { + return API.post(SHARE_KEY_PATH); +}; diff --git a/src/handlers/api/validate-permissions.handler.ts b/src/handlers/api/validate-permissions.handler.ts new file mode 100644 index 0000000..c37f59c --- /dev/null +++ b/src/handlers/api/validate-permissions.handler.ts @@ -0,0 +1,10 @@ +import { VALIDATE_PERMISSIONS_PATH } from "@/config/routes"; +import API from "@/lib/api"; + +export interface PermissionsResult { + canUpload: boolean; +} + +export const validatePermissions = async (): Promise => { + return API.get(VALIDATE_PERMISSIONS_PATH); +}; diff --git a/src/helpers/user.helper.ts b/src/helpers/user.helper.ts index 6713b45..b34e6cb 100644 --- a/src/helpers/user.helper.ts +++ b/src/helpers/user.helper.ts @@ -3,7 +3,6 @@ import { IUser } from "@/types/user" export const getUserHeaders = (user: { isUsingAPIKey?: boolean, - isUsingShareKey?: boolean, accessToken?: string }, otherHeaders?: { 'Content-Type': string @@ -15,9 +14,7 @@ export const getUserHeaders = (user: { } = { 'Content-Type': 'application/json', } - if (user.isUsingShareKey) { - headers['x-api-key'] = ENV.IMMICH_SHARE_LINK_KEY - } else if (user.isUsingAPIKey) { + if (user.isUsingAPIKey) { headers['x-api-key'] = ENV.IMMICH_API_KEY } else { headers['Authorization'] = `Bearer ${user.accessToken}` diff --git a/src/instrumentation.ts b/src/instrumentation.ts new file mode 100644 index 0000000..3a733b4 --- /dev/null +++ b/src/instrumentation.ts @@ -0,0 +1,10 @@ +export async function register() { + // Only run on the Node.js server, not in the Edge runtime or client + if (process.env.NEXT_RUNTIME === "nodejs") { + const { runMigrations } = await import("./db"); + await runMigrations(); + + const { resumePendingJobs } = await import("./workers/import/runner"); + await resumePendingJobs(); + } +} diff --git a/src/pages/api/immich-proxy/asset/share-thumbnail/[id].ts b/src/pages/api/immich-proxy/asset/share-thumbnail/[id].ts index 4fa6c77..2d3526a 100644 --- a/src/pages/api/immich-proxy/asset/share-thumbnail/[id].ts +++ b/src/pages/api/immich-proxy/asset/share-thumbnail/[id].ts @@ -1,7 +1,9 @@ // pages/api/proxy.js import { ENV } from '@/config/environment'; -import { getUserHeaders } from '@/helpers/user.helper'; +import { appDb } from '@/db'; +import { apiKeys } from '@/db/schema'; +import { and, eq } from 'drizzle-orm'; import { verify } from 'jsonwebtoken'; import { NextApiRequest, NextApiResponse } from 'next' @@ -16,18 +18,31 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) if (req.method !== 'GET') { return res.status(405).json({ message: 'Method Not Allowed' }) } - + const { id, size, token, p } = req.query; if (!token) { return res.status(401).json({ message: 'Unauthorized' }) } + let userId: string; try { - verify(token as string, ENV.JWT_SECRET); + const previewDecoded = verify(token as string, ENV.JWT_SECRET) as { token: string }; + const mainDecoded = verify(previewDecoded.token, ENV.JWT_SECRET) as { userId: string }; + userId = mainDecoded.userId; } catch (error) { return res.status(401).json({ message: 'Token is invalid' }) } + const [keyRow] = await appDb + .select({ secret: apiKeys.secret }) + .from(apiKeys) + .where(and(eq(apiKeys.userId, userId), eq(apiKeys.purpose, "share"))) + .limit(1); + + if (!keyRow) { + return res.status(403).json({ message: 'Share key not configured. Please set up sharing in Power Tools.' }); + } + const resource = p === "true" ? "people" : "assets"; const baseURL = `${ENV.IMMICH_URL}/api/${resource}/${id}`; const version = size === "original" ? "original" : "thumbnail"; @@ -37,9 +52,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) // Forward the request to the target API const response = await fetch(targetUrl, { method: 'GET', - headers: getUserHeaders({ isUsingShareKey: true }, { + headers: { 'Content-Type': 'application/octet-stream', - }), + 'x-api-key': keyRow.secret, + }, }) if (!response.ok) { diff --git a/src/pages/api/import-jobs/[jobId].ts b/src/pages/api/import-jobs/[jobId].ts new file mode 100644 index 0000000..1289028 --- /dev/null +++ b/src/pages/api/import-jobs/[jobId].ts @@ -0,0 +1,38 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import { eq } from "drizzle-orm"; +import { appDb } from "@/db"; +import { importJobs, importJobItems } from "@/db/schema"; +import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; +import { getUserHeaders } from "@/helpers/user.helper"; +import { isJobActive, runImportJob } from "@/workers/import/runner"; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method !== "GET") { + res.setHeader("Allow", "GET"); + return res.status(405).json({ error: "Method Not Allowed" }); + } + + const currentUser = await getCurrentUser(req); + if (!currentUser) return res.status(401).json({ error: "Unauthorized" }); + + const { jobId } = req.query as { jobId: string }; + + const [job] = await appDb.select().from(importJobs).where(eq(importJobs.id, jobId)); + if (!job || job.userId !== currentUser.id) { + return res.status(404).json({ error: "Job not found" }); + } + + if (job.status === "pending" && !isJobActive(jobId)) { + const headers = getUserHeaders(currentUser) as Record; + runImportJob(jobId, headers).catch((err) => + console.error("Unhandled error in runImportJob (resume)", err) + ); + } + + const items = await appDb + .select() + .from(importJobItems) + .where(eq(importJobItems.jobId, jobId)); + + return res.status(200).json({ job, items }); +} diff --git a/src/pages/api/import-jobs/index.ts b/src/pages/api/import-jobs/index.ts new file mode 100644 index 0000000..b0c6590 --- /dev/null +++ b/src/pages/api/import-jobs/index.ts @@ -0,0 +1,75 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import { desc, eq } from "drizzle-orm"; +import { appDb } from "@/db"; +import { importJobs, importJobItems } from "@/db/schema"; +import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; +import { getUserHeaders } from "@/helpers/user.helper"; +import { runImportJob } from "@/workers/import/runner"; +import { getProcessor } from "@/workers/import/registry"; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method === "GET") { + const currentUser = await getCurrentUser(req); + if (!currentUser) return res.status(401).json({ error: "Unauthorized" }); + + const jobs = await appDb + .select() + .from(importJobs) + .where(eq(importJobs.userId, currentUser.id)) + .orderBy(desc(importJobs.createdAt)) + .limit(20); + + return res.status(200).json({ jobs }); + } + + if (req.method !== "POST") { + res.setHeader("Allow", "GET, POST"); + return res.status(405).json({ error: "Method Not Allowed" }); + } + + const currentUser = await getCurrentUser(req); + if (!currentUser) return res.status(401).json({ error: "Unauthorized" }); + + const { platform, url, urlConfig, importData, assets } = req.body; + + if (!platform || !url || !Array.isArray(assets) || assets.length === 0) { + return res.status(400).json({ error: "platform, url, and assets are required" }); + } + + if (assets.some((a: unknown) => typeof (a as any).id !== "string")) { + return res.status(400).json({ error: "Each asset must have a string id" }); + } + + try { + getProcessor(platform); + } catch { + return res.status(400).json({ error: `Unknown platform: ${platform}` }); + } + + const [job] = await appDb + .insert(importJobs) + .values({ + userId: currentUser.id, + platform, + url, + urlConfig: JSON.stringify(urlConfig ?? {}), + importData: JSON.stringify(importData ?? {}), + totalCount: assets.length, + }) + .returning(); + + await appDb.insert(importJobItems).values( + assets.map((asset: { id: string; [key: string]: unknown }) => ({ + jobId: job.id, + assetId: asset.id, + itemData: JSON.stringify(asset), + })) + ); + + const headers = getUserHeaders(currentUser) as Record; + runImportJob(job.id, headers).catch((err) => + console.error("Unhandled error in runImportJob", err) + ); + + return res.status(201).json({ jobId: job.id }); +} diff --git a/src/pages/api/import-shared/helpers.ts b/src/pages/api/import-shared/helpers.ts index 09aee47..48fca46 100644 --- a/src/pages/api/import-shared/helpers.ts +++ b/src/pages/api/import-shared/helpers.ts @@ -26,7 +26,10 @@ export const ensurePowerToolsTag = async (headers: HeadersRecord) => { const createResponse = await fetch(`${ENV.IMMICH_URL}/api/tags`, { method: "POST", - headers, + headers: { + ...headers, + "Content-Type": "application/json", + }, body: JSON.stringify({ name: POWER_TOOLS_TAG_NAME }), }); diff --git a/src/pages/api/settings/api-keys/[purpose].ts b/src/pages/api/settings/api-keys/[purpose].ts new file mode 100644 index 0000000..aeb60a0 --- /dev/null +++ b/src/pages/api/settings/api-keys/[purpose].ts @@ -0,0 +1,93 @@ +import { ENV } from "@/config/environment"; +import { appDb } from "@/db"; +import { apiKeys } from "@/db/schema"; +import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; +import { getUserHeaders } from "@/helpers/user.helper"; +import { and, eq } from "drizzle-orm"; +import { NextApiRequest, NextApiResponse } from "next"; + +const PURPOSE_CONFIG: Record = { + share: { + keyName: "Power Tools Share Key", + permissions: ["asset.view", "asset.download"], + }, +}; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const currentUser = await getCurrentUser(req); + if (!currentUser) { + return res.status(401).json({ message: "Unauthorized" }); + } + + const { purpose } = req.query as { purpose: string }; + + if (req.method === "DELETE") { + const [row] = await appDb + .select() + .from(apiKeys) + .where(and(eq(apiKeys.userId, currentUser.id), eq(apiKeys.purpose, purpose))) + .limit(1); + + if (!row) { + return res.status(404).json({ message: "API key not found" }); + } + + await fetch(`${ENV.IMMICH_URL}/api/api-keys/${row.immichKeyId}`, { + method: "DELETE", + headers: getUserHeaders(currentUser, { "Content-Type": "application/json" }), + }); + + await appDb + .delete(apiKeys) + .where(and(eq(apiKeys.userId, currentUser.id), eq(apiKeys.purpose, purpose))); + + return res.status(200).json({ success: true }); + } + + if (req.method === "PUT") { + const config = PURPOSE_CONFIG[purpose]; + if (!config) { + return res.status(400).json({ message: `Unknown purpose: ${purpose}` }); + } + + // Delete old key from Immich if it exists + const [existing] = await appDb + .select() + .from(apiKeys) + .where(and(eq(apiKeys.userId, currentUser.id), eq(apiKeys.purpose, purpose))) + .limit(1); + + if (existing) { + await fetch(`${ENV.IMMICH_URL}/api/api-keys/${existing.immichKeyId}`, { + method: "DELETE", + headers: getUserHeaders(currentUser, { "Content-Type": "application/json" }), + }); + } + + // Create new key in Immich + const immichRes = await fetch(`${ENV.IMMICH_URL}/api/api-keys`, { + method: "POST", + headers: getUserHeaders(currentUser, { "Content-Type": "application/json" }), + body: JSON.stringify({ name: config.keyName, permissions: config.permissions }), + }); + + if (!immichRes.ok) { + const err = await immichRes.json().catch(() => ({})); + return res.status(immichRes.status).json({ message: err.message ?? "Failed to create API key in Immich" }); + } + + const { secret, apiKey } = await immichRes.json(); + + await appDb + .insert(apiKeys) + .values({ userId: currentUser.id, purpose, keyName: config.keyName, secret, immichKeyId: apiKey.id }) + .onConflictDoUpdate({ + target: [apiKeys.userId, apiKeys.purpose], + set: { secret, immichKeyId: apiKey.id, keyName: config.keyName }, + }); + + return res.status(200).json({ success: true }); + } + + return res.status(405).json({ message: "Method not allowed" }); +} diff --git a/src/pages/api/settings/api-keys/index.ts b/src/pages/api/settings/api-keys/index.ts new file mode 100644 index 0000000..af8b783 --- /dev/null +++ b/src/pages/api/settings/api-keys/index.ts @@ -0,0 +1,28 @@ +import { appDb } from "@/db"; +import { apiKeys } from "@/db/schema"; +import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; +import { eq } from "drizzle-orm"; +import { NextApiRequest, NextApiResponse } from "next"; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method !== "GET") { + return res.status(405).json({ message: "Method not allowed" }); + } + + const currentUser = await getCurrentUser(req); + if (!currentUser) { + return res.status(401).json({ message: "Unauthorized" }); + } + + const rows = await appDb + .select({ + id: apiKeys.id, + purpose: apiKeys.purpose, + keyName: apiKeys.keyName, + createdAt: apiKeys.createdAt, + }) + .from(apiKeys) + .where(eq(apiKeys.userId, currentUser.id)); + + return res.status(200).json(rows); +} diff --git a/src/pages/api/share-key/index.ts b/src/pages/api/share-key/index.ts new file mode 100644 index 0000000..8c55719 --- /dev/null +++ b/src/pages/api/share-key/index.ts @@ -0,0 +1,91 @@ +import { ENV } from "@/config/environment"; +import { appDb } from "@/db"; +import { apiKeys } from "@/db/schema"; +import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; +import { getUserHeaders } from "@/helpers/user.helper"; +import { and, eq } from "drizzle-orm"; +import { NextApiRequest, NextApiResponse } from "next"; + +const PURPOSE = "share"; +const KEY_NAME = "Power Tools Share Key"; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const currentUser = await getCurrentUser(req); + if (!currentUser) { + return res.status(401).json({ message: "Unauthorized" }); + } + + if (req.method === "GET") { + const [row] = await appDb + .select({ keyName: apiKeys.keyName, createdAt: apiKeys.createdAt }) + .from(apiKeys) + .where(and(eq(apiKeys.userId, currentUser.id), eq(apiKeys.purpose, PURPOSE))) + .limit(1); + + return res.status(200).json({ + endpointConfigured: !!ENV.POWER_TOOLS_ENDPOINT_URL, + exists: !!row, + keyName: row?.keyName ?? null, + createdAt: row?.createdAt ?? null, + }); + } + + if (req.method === "POST") { + const immichRes = await fetch(`${ENV.IMMICH_URL}/api/api-keys`, { + method: "POST", + headers: getUserHeaders(currentUser, { "Content-Type": "application/json" }), + body: JSON.stringify({ + name: KEY_NAME, + permissions: ["asset.view", "asset.download"], + }), + }); + + if (!immichRes.ok) { + const err = await immichRes.json().catch(() => ({})); + return res.status(immichRes.status).json({ message: err.message ?? "Failed to create API key in Immich" }); + } + + const { secret, apiKey } = await immichRes.json(); + + await appDb + .insert(apiKeys) + .values({ + userId: currentUser.id, + purpose: PURPOSE, + keyName: KEY_NAME, + secret, + immichKeyId: apiKey.id, + }) + .onConflictDoUpdate({ + target: [apiKeys.userId, apiKeys.purpose], + set: { secret, immichKeyId: apiKey.id, keyName: KEY_NAME }, + }); + + return res.status(200).json({ success: true }); + } + + if (req.method === "DELETE") { + const [row] = await appDb + .select() + .from(apiKeys) + .where(and(eq(apiKeys.userId, currentUser.id), eq(apiKeys.purpose, PURPOSE))) + .limit(1); + + if (!row) { + return res.status(404).json({ message: "No share key found" }); + } + + await fetch(`${ENV.IMMICH_URL}/api/api-keys/${row.immichKeyId}`, { + method: "DELETE", + headers: getUserHeaders(currentUser, { "Content-Type": "application/json" }), + }); + + await appDb + .delete(apiKeys) + .where(and(eq(apiKeys.userId, currentUser.id), eq(apiKeys.purpose, PURPOSE))); + + return res.status(200).json({ success: true }); + } + + return res.status(405).json({ message: "Method not allowed" }); +} diff --git a/src/pages/api/share-link/[token]/download.ts b/src/pages/api/share-link/[token]/download.ts index 9915a03..2fb3b20 100644 --- a/src/pages/api/share-link/[token]/download.ts +++ b/src/pages/api/share-link/[token]/download.ts @@ -1,9 +1,11 @@ import { ENV } from "@/config/environment"; +import { appDb } from "@/db"; +import { apiKeys } from "@/db/schema"; +import { and, eq } from "drizzle-orm"; import { JsonWebTokenError, verify } from "jsonwebtoken"; import { NextApiResponse } from "next"; import { NextApiRequest } from "next"; -import { getUserHeaders } from "@/helpers/user.helper"; interface ShareLinkFilters { personIds: string[]; @@ -23,20 +25,31 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) return res.status(405).json({ message: "Method not allowed" }); } - const decoded = verify(token as string, ENV.JWT_SECRET); + const decoded = verify(token as string, ENV.JWT_SECRET) as { userId: string }; if (!decoded) { return res.status(401).json({ message: "Unauthorized" }); } + const [keyRow] = await appDb + .select({ secret: apiKeys.secret }) + .from(apiKeys) + .where(and(eq(apiKeys.userId, decoded.userId), eq(apiKeys.purpose, "share"))) + .limit(1); + + if (!keyRow) { + return res.status(403).json({ message: "Share key not configured. Please set up sharing in Power Tools." }); + } + const targetUrl = `${ENV.IMMICH_URL}/api/download/archive`; const response = await fetch(targetUrl, { method: 'POST', body: JSON.stringify({ assetIds }), - headers: getUserHeaders({ isUsingShareKey: true }, { + headers: { 'Content-Type': 'application/json', - }), + 'x-api-key': keyRow.secret, + }, }) if (!response.ok) { diff --git a/src/pages/api/share-link/[token]/index.ts b/src/pages/api/share-link/[token]/index.ts index d2a1d11..5c1a1ee 100644 --- a/src/pages/api/share-link/[token]/index.ts +++ b/src/pages/api/share-link/[token]/index.ts @@ -1,7 +1,6 @@ import { NextApiResponse } from "next"; import { ENV } from "@/config/environment"; -import { ShareLinkFilters } from "@/types/shareLink"; import { JsonWebTokenError, verify } from "jsonwebtoken"; import { NextApiRequest } from "next"; @@ -13,10 +12,6 @@ export default async function GET(req: NextApiRequest, res: NextApiResponse) { return res.status(404).json({ message: "Token not found" }); } - if (!ENV.IMMICH_SHARE_LINK_KEY) { - return res.status(401).json({ message: "Please check your link and try again. If you're the admin, check if you've enabled all the configurations in the Immich Power Tools in your environment variables" }); - } - const decoded = verify(token as string, ENV.JWT_SECRET); diff --git a/src/pages/api/share-link/generate.ts b/src/pages/api/share-link/generate.ts index 139f7e9..b355354 100644 --- a/src/pages/api/share-link/generate.ts +++ b/src/pages/api/share-link/generate.ts @@ -19,15 +19,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) return res.status(401).json({ message: "The JWT_SECRET is missing in the .env file. Please add it to the .env file for generating share links" }); } - if (!ENV.IMMICH_SHARE_LINK_KEY) { - return res.status(401).json({ message: "The IMMICH_SHARE_LINK_KEY is missing in the .env file. Please add it to the .env file for generating share links" }); - } - if (!ENV.POWER_TOOLS_ENDPOINT_URL) { return res.status(401).json({ message: "The POWER_TOOLS_ENDPOINT_URL is missing in the .env file. Please add it to the .env file for generating share links" }); } - const token = sign(filters, ENV.JWT_SECRET, expiresIn !== "never" ? { + const token = sign({ ...filters, userId: currentUser.id }, ENV.JWT_SECRET, expiresIn !== "never" ? { expiresIn: expiresIn } : {}); diff --git a/src/pages/api/validate-permissions.ts b/src/pages/api/validate-permissions.ts new file mode 100644 index 0000000..37959f9 --- /dev/null +++ b/src/pages/api/validate-permissions.ts @@ -0,0 +1,33 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import { ENV } from "@/config/environment"; +import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; +import { getUserHeaders } from "@/helpers/user.helper"; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method !== "GET") { + return res.status(405).json({ message: "Method not allowed" }); + } + + const currentUser = await getCurrentUser(req); + if (!currentUser) { + return res.status(401).json({ message: "Unauthorized" }); + } + + const headers = getUserHeaders(currentUser, { "Content-Type": "application/json" }); + + const result = { canUpload: false }; + + // Test asset upload permission by calling a no-op endpoint + try { + const response = await fetch(`${ENV.IMMICH_URL}/api/assets/exist`, { + method: "POST", + headers, + body: JSON.stringify({ deviceAssetIds: ["permission-check"], deviceId: "immich-power-tools-check" }), + }); + result.canUpload = response.ok; + } catch { + result.canUpload = false; + } + + return res.status(200).json(result); +} diff --git a/src/pages/assets/import-shared.tsx b/src/pages/assets/import-shared.tsx index 86843f4..95289c2 100644 --- a/src/pages/assets/import-shared.tsx +++ b/src/pages/assets/import-shared.tsx @@ -1,5 +1,6 @@ -import React, { FormEvent, useEffect, useState } from "react"; -import { Loader2 } from "lucide-react"; +import React, { FormEvent, useEffect, useRef, useState } from "react"; +import { useQuery } from "@tanstack/react-query"; +import { Calendar, Loader2, Images, User, Clock, CheckCircle2, XCircle, Clock3 } from "lucide-react"; import PageLayout from "@/components/layouts/PageLayout"; import Header from "@/components/shared/Header"; import { Button } from "@/components/ui/button"; @@ -17,12 +18,16 @@ import { import { Dialog, DialogContent, - DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { listAlbums } from "@/handlers/api/album.handler"; +import { importShared } from "@/handlers/api/import-shared.handler"; +import { createImportJob, getImportJob, listImportJobs } from "@/handlers/api/import-jobs.handler"; +import ImportJobDetailDialog from "@/components/import/ImportJobDetailDialog"; +import { validatePermissions } from "@/handlers/api/validate-permissions.handler"; import { IAlbum } from "@/types/album"; +import { AlertTriangle } from "lucide-react"; interface IAlbumContributorCount { userId: string; @@ -157,18 +162,41 @@ export default function ImportSharedPage() { const [previewLoading, setPreviewLoading] = useState(false); const [uploadBanner, setUploadBanner] = useState<{ type: "success" | "error"; message: string } | null>(null); const [importAllLoading, setImportAllLoading] = useState(false); - const [importAllDialogOpen, setImportAllDialogOpen] = useState(false); const [albumImportMode, setAlbumImportMode] = useState<"album" | "no-album" | "existing-album">("album"); const [albumNameInput, setAlbumNameInput] = useState(""); const [selectedAssetIds, setSelectedAssetIds] = useState>(new Set()); const [existingAlbums, setExistingAlbums] = useState([]); const [selectedAlbumId, setSelectedAlbumId] = useState(null); + const [activeJobId, setActiveJobId] = useState(null); + const [jobProgress, setJobProgress] = useState<{ uploaded: number; skipped: number; failed: number; total: number } | null>(null); + const [tagAssets, setTagAssets] = useState(true); + const [detailJobId, setDetailJobId] = useState(null); + + const { data: permissions } = useQuery({ + queryKey: ["validate-permissions"], + queryFn: validatePermissions, + staleTime: 5 * 60 * 1000, + retry: false, + }); + + const { data: importHistory, refetch: refetchHistory } = useQuery({ + queryKey: ["import-jobs"], + queryFn: listImportJobs, + staleTime: 30 * 1000, + }); + + + const albumNameRef = useRef(albumNameInput); useEffect(() => { - if (importAllDialogOpen) { + albumNameRef.current = albumNameInput; + }, [albumNameInput]); + + useEffect(() => { + if (sharedData) { listAlbums().then(setExistingAlbums).catch(console.error); } - }, [importAllDialogOpen]); + }, [sharedData]); useEffect(() => { if (sharedData?.album?.albumName) { @@ -180,6 +208,64 @@ export default function ImportSharedPage() { } }, [sharedData]); + useEffect(() => { + if (!activeJobId) return; + + const interval = setInterval(async () => { + try { + const { job } = await getImportJob(activeJobId); + setJobProgress({ + uploaded: job.uploadedCount, + skipped: job.skippedCount, + failed: job.failedCount, + total: job.totalCount, + }); + + if (job.status === "completed" || job.status === "failed") { + clearInterval(interval); + setActiveJobId(null); + setImportAllLoading(false); + + let importData: Record = {}; + try { + importData = JSON.parse(job.importData); + } catch { + // malformed importData — skip album name append + } + const failedCount = job.failedCount; + const skippedCount = job.skippedCount; + const uploadedCount = job.uploadedCount; + const bannerType: "success" | "error" = (failedCount > 0 || job.status === "failed") ? "error" : "success"; + let message: string; + + if (job.status === "failed" && importData.error && typeof importData.error === "string") { + message = importData.error; + } else if (!uploadedCount && skippedCount && !failedCount) { + message = `All ${skippedCount} assets already exist on Immich.`; + } else if (failedCount) { + const skippedSuffix = skippedCount ? `, ${skippedCount} skipped` : ""; + message = `Imported ${uploadedCount} assets, ${failedCount} failed${skippedSuffix}.`; + } else if (skippedCount) { + message = `Imported ${uploadedCount} assets, skipped ${skippedCount} already on Immich.`; + } else { + message = `Imported ${uploadedCount} assets successfully.`; + } + + if (importData.albumId && uploadedCount > 0) { + message = `${message} Added to album "${albumNameRef.current || "selected album"}".`; + } + + setUploadBanner({ type: bannerType, message }); + refetchHistory(); + } + } catch (err) { + console.error("Polling error", err); + } + }, 2000); + + return () => clearInterval(interval); + }, [activeJobId]); + const handleSubmit = async (event: FormEvent) => { event.preventDefault(); setError(null); @@ -187,7 +273,6 @@ export default function ImportSharedPage() { setSubmittedLink(null); setUploadBanner(null); setImportAllLoading(false); - setImportAllDialogOpen(false); setAlbumImportMode("album"); setAlbumNameInput(""); setSelectedAssetIds(new Set()); @@ -200,27 +285,14 @@ export default function ImportSharedPage() { setLoading(true); try { - const response = await fetch("/api/import-shared", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ link: shareLink }), - }); - - if (!response.ok) { - const errorPayload = await response.json().catch(() => ({})); - throw new Error(errorPayload.error || "Failed to import shared album"); - } - - const payload: IImportSharedResponse = await response.json(); + const payload: IImportSharedResponse = await importShared(shareLink); setSharedData(payload); setSubmittedLink(payload.link); setUploadBanner(null); setImportAllLoading(false); setAlbumImportMode("album"); - } catch (err) { - const message = err instanceof Error ? err.message : "Unexpected error"; + } catch (err: any) { + const message = err.message ?? "Unexpected error"; setError(message); } finally { setLoading(false); @@ -309,63 +381,27 @@ export default function ImportSharedPage() { setUploadBanner(null); setImportAllLoading(true); try { - const response = await fetch("/api/import-shared/upload-all", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - origin: sharedData.origin, - key: sharedData.key, - assets: uploadableAssets.map((asset) => ({ - id: asset.id, - originalFileName: asset.originalFileName, - type: asset.type, - fileCreatedAt: asset.fileCreatedAt ?? null, - localDateTime: asset.localDateTime ?? null, - duration: asset.duration ?? null, - isFavorite: asset.isFavorite ?? false, - isArchived: asset.isArchived ?? false, - })), - albumOptions, - }), + const job = await createImportJob({ + platform: "immich", + url: sharedData.origin, + urlConfig: { key: sharedData.key }, + importData: { albumOptions, tagAssets }, + assets: uploadableAssets.map((asset) => ({ + id: asset.id, + originalFileName: asset.originalFileName, + type: asset.type, + fileCreatedAt: asset.fileCreatedAt ?? null, + localDateTime: asset.localDateTime ?? null, + duration: asset.duration ?? null, + isFavorite: asset.isFavorite ?? false, + isArchived: asset.isArchived ?? false, + })), }); - - const result = await response.json().catch(() => ({})); - if (!response.ok) { - throw new Error(result.error || "Failed to import shared archive"); - } - - const processedAssetIds: string[] = result.processedAssetIds ?? []; - const skippedAssetIds: string[] = result.skippedAssetIds ?? []; - const failed: { assetId: string; reason?: string }[] = result.failed ?? []; - - const failedCount = failed.length; - const skippedCount = skippedAssetIds.length; - const uploadedCount = result.uploadedCount ?? processedAssetIds.length; - let bannerType: "success" | "error" = failedCount ? "error" : "success"; - let message: string; - if (!uploadedCount && skippedCount && !failedCount) { - message = `All ${skippedCount} assets already exist on Immich.`; - } else if (failedCount) { - const skippedSuffix = skippedCount ? `, ${skippedCount} skipped` : ""; - message = `Imported ${uploadedCount} assets, ${failedCount} failed${skippedSuffix}.`; - } else if (skippedCount) { - message = `Imported ${uploadedCount} assets, skipped ${skippedCount} already on Immich.`; - } else { - message = `Imported ${uploadedCount} assets successfully.`; - } - if (options.createAlbum && uploadedCount > 0 && normalizedAlbumName) { - message = `${message} Added to album "${normalizedAlbumName}".`; - } else if (options.addToAlbumId && uploadedCount > 0) { - const album = existingAlbums.find((a) => a.id === options.addToAlbumId); - message = `${message} Added to album "${album?.albumName || "existing album"}".`; - } - setUploadBanner({ type: bannerType, message }); - } catch (err) { - const message = err instanceof Error ? err.message : "Failed to import shared archive"; - setUploadBanner({ type: "error", message }); - } finally { + setActiveJobId(job.jobId); + setJobProgress({ uploaded: 0, skipped: 0, failed: 0, total: uploadableAssets.length }); + // importAllLoading stays true — cleared when polling detects completion + } catch (err: any) { + setUploadBanner({ type: "error", message: err.message ?? "Failed to start import" }); setImportAllLoading(false); } }; @@ -385,7 +421,6 @@ export default function ImportSharedPage() { setPreviewLoading(false); setUploadBanner(null); setImportAllLoading(false); - setImportAllDialogOpen(false); setAlbumImportMode("album"); setAlbumNameInput(""); setSelectedAssetIds(new Set()); @@ -413,16 +448,23 @@ export default function ImportSharedPage() { + ))} + + + + {albumImportMode === "album" && ( + setAlbumNameInput(e.target.value)} + placeholder="Album name" + disabled={importAllLoading} + /> + )} + + {albumImportMode === "existing-album" && ( + + )} + +
+ setTagAssets(!!checked)} + disabled={importAllLoading} + /> + +
+ + {importAllLoading && jobProgress && ( +
+ +

+ {jobProgress.uploaded + jobProgress.skipped} / {jobProgress.total} processed + {jobProgress.failed > 0 && ( + · {jobProgress.failed} failed + )} +

+
)} @@ -631,121 +833,6 @@ export default function ImportSharedPage() { )} - { - if (importAllLoading) { - return; - } - setImportAllDialogOpen(open); - }} - > - - - {selectedAssetIds.size > 0 ? "Import selected assets" : "Import all assets"} - - Decide whether to create a new Immich album for these imported assets. - - -
-
- -
- - - -
-
- {albumImportMode === "album" && ( -
- - setAlbumNameInput(event.target.value)} - placeholder="Imported shared album" - disabled={importAllLoading} - /> -
- )} - {albumImportMode === "existing-album" && ( -
- - -
- )} -
-
- - -
-
-
- { @@ -809,6 +896,7 @@ export default function ImportSharedPage() { )} + setDetailJobId(null)} /> ); } diff --git a/src/pages/settings/index.tsx b/src/pages/settings/index.tsx new file mode 100644 index 0000000..1e165d8 --- /dev/null +++ b/src/pages/settings/index.tsx @@ -0,0 +1,133 @@ +import PageLayout from "@/components/layouts/PageLayout"; +import Header from "@/components/shared/Header"; +import { deleteApiKey, getApiKeys, regenerateApiKey } from "@/handlers/api/settings.handler"; +import { AlertDialog } from "@/components/ui/alert-dialog"; +import { Badge } from "@/components/ui/badge"; +import { Button } from "@/components/ui/button"; +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; +import { RefreshCw, Trash2 } from "lucide-react"; +import { useEffect, useState } from "react"; +import toast from "react-hot-toast"; + +interface ApiKey { + id: string; + purpose: string; + keyName: string; + createdAt: string; +} + +export default function SettingsPage() { + const [keys, setKeys] = useState([]); + const [loading, setLoading] = useState(true); + const [deleting, setDeleting] = useState(null); + const [regenerating, setRegenerating] = useState(null); + + const fetchKeys = async () => { + setLoading(true); + try { + const data = await getApiKeys(); + setKeys(data); + } catch { + // silently fail — keys list stays empty + } + setLoading(false); + }; + + useEffect(() => { + fetchKeys(); + }, []); + + const handleDelete = async (purpose: string) => { + setDeleting(purpose); + try { + await deleteApiKey(purpose); + setKeys((prev) => prev.filter((k) => k.purpose !== purpose)); + toast.success("API key deleted"); + } catch (err: any) { + toast.error(err.message ?? "Failed to delete API key"); + } + setDeleting(null); + }; + + const handleRegenerate = async (purpose: string) => { + setRegenerating(purpose); + try { + await regenerateApiKey(purpose); + await fetchKeys(); + toast.success("API key regenerated"); + } catch (err: any) { + toast.error(err.message ?? "Failed to regenerate API key"); + } + setRegenerating(null); + }; + + return ( + +
+
+ + + API Keys + + Managed API keys created by Power Tools in your Immich instance. + These are used internally for features that require unauthenticated access (e.g. share links). + + + + {loading && ( +

Loading...

+ )} + {!loading && keys.length === 0 && ( +

No API keys created yet.

+ )} + {!loading && keys.length > 0 && ( +
    + {keys.map((key) => ( +
  • +
    +
    + {key.keyName} + {key.purpose} +
    + + Created {new Date(key.createdAt).toLocaleDateString()} + +
    +
    + handleRegenerate(key.purpose)} + > + + + handleDelete(key.purpose)} + > + + +
    +
  • + ))} +
+ )} +
+
+
+ + ); +} diff --git a/src/workers/import/processors/immich-shared-link.ts b/src/workers/import/processors/immich-shared-link.ts new file mode 100644 index 0000000..3784d3a --- /dev/null +++ b/src/workers/import/processors/immich-shared-link.ts @@ -0,0 +1,367 @@ +import { eq } from "drizzle-orm"; +import { appDb } from "@/db"; +import { importJobItems } from "@/db/schema"; +import { ENV } from "@/config/environment"; +import { + ensurePowerToolsTag, + HeadersRecord, +} from "@/pages/api/import-shared/helpers"; +import type { ImportJob, ImportJobItem, ImportProcessor, ProcessorContext, SetupResult } from "../types"; + +const DEVICE_ID = "immich-power-tools"; +const makeDeviceAssetId = (assetId: string) => `shared-${assetId}`; + +interface ISharedAssetPayload { + id: string; + originalFileName?: string; + type: string; + fileCreatedAt?: string | null; + localDateTime?: string | null; + duration?: string | null; + isFavorite?: boolean; + isArchived?: boolean; +} + +interface DownloadedAssetPayload { + buffer: Buffer; + fileName: string; + contentType: string | null; +} + +const inferAssetTypeFromName = (fileName: string): "IMAGE" | "VIDEO" => { + const extension = fileName.split(".").pop()?.toLowerCase(); + if (!extension) return "IMAGE"; + switch (extension) { + case "mp4": + case "mov": + case "m4v": + case "avi": + case "mkv": + case "webm": + case "3gp": + case "3g2": + case "mts": + case "m2ts": + return "VIDEO"; + default: + return "IMAGE"; + } +}; + +const parseFileNameFromDisposition = (disposition?: string | null): string | null => { + if (!disposition) return null; + const match = disposition.match(/filename\*?=([^;]+)/i); + if (!match || !match[1]) return null; + const value = match[1].trim().replace(/^UTF-8''/i, "").replace(/^"|"$/g, "").replace(/^'|'$/g, ""); + try { + return decodeURIComponent(value); + } catch { + return value; + } +}; + +const guessContentType = (fileName: string): string => { + const extension = fileName.toLowerCase().split(".").pop(); + switch (extension) { + case "jpg": + case "jpeg": + case "jfif": + return "image/jpeg"; + case "png": + return "image/png"; + case "heic": + return "image/heic"; + case "heif": + return "image/heif"; + case "webp": + return "image/webp"; + case "gif": + return "image/gif"; + case "tif": + case "tiff": + return "image/tiff"; + case "bmp": + return "image/bmp"; + case "mp4": + case "m4v": + return "video/mp4"; + case "mov": + return "video/quicktime"; + case "avi": + return "video/x-msvideo"; + case "mkv": + return "video/x-matroska"; + case "webm": + return "video/webm"; + case "3gp": + case "3g2": + return "video/3gpp"; + case "mts": + case "m2ts": + return "video/MP2T"; + default: + return "application/octet-stream"; + } +}; + +const createImmichAlbum = async (albumName: string, headers: HeadersRecord): Promise => { + const response = await fetch(`${ENV.IMMICH_URL}/api/albums`, { + method: "POST", + headers, + body: JSON.stringify({ albumName }), + }); + if (!response.ok) { + if (response.status === 403) { + throw new Error(`Your API key does not have permission to create albums. Try importing without creating an album, or use an API key with album.create permission.`); + } + const body = await response.text().catch(() => ""); + throw new Error(`Failed to create album "${albumName}" (status ${response.status}): ${body || "Unknown error"}`); + } + const album = await response.json().catch(() => null); + const albumId = album?.id; + if (!albumId || typeof albumId !== "string") { + throw new Error("Immich album creation response did not include an id"); + } + return albumId; +}; + +const addAssetToAlbum = async (albumId: string, assetId: string, headers: HeadersRecord): Promise => { + const response = await fetch(`${ENV.IMMICH_URL}/api/albums/${albumId}/assets`, { + method: "PUT", + headers, + body: JSON.stringify({ ids: [assetId] }), + }); + if (!response.ok) { + const body = await response.text().catch(() => ""); + throw new Error( + `Failed to attach asset ${assetId} to album ${albumId} (status ${response.status}): ${body || "Unknown error"}` + ); + } +}; + +const downloadSharedAsset = async ( + asset: ISharedAssetPayload, + origin: string, + key: string +): Promise => { + const url = `${origin}/api/assets/${asset.id}/original?key=${key}`; + const response = await fetch(url, { method: "GET", headers: { accept: "*/*" } }); + if (!response.ok) { + throw new Error(`Failed to download ${asset.originalFileName ?? asset.id} (status ${response.status})`); + } + const buffer = Buffer.from(await response.arrayBuffer()); + const disposition = response.headers.get("content-disposition"); + const inferredName = parseFileNameFromDisposition(disposition); + const fallbackExtension = asset.type === "VIDEO" ? "mp4" : "jpg"; + const fileName = inferredName ?? asset.originalFileName ?? `${asset.id}.${fallbackExtension}`; + return { buffer, fileName, contentType: response.headers.get("content-type") }; +}; + +const uploadAssetBuffer = async ( + asset: ISharedAssetPayload, + payload: DownloadedAssetPayload, + uploadHeaders: HeadersRecord, + jsonHeaders: HeadersRecord, + tagId?: string +): Promise => { + const fileType = asset.type ?? inferAssetTypeFromName(payload.fileName); + const resolvedFileName = payload.fileName || asset.originalFileName || `${asset.id}.bin`; + const contentType = payload.contentType ?? guessContentType(resolvedFileName); + const uint8Array = new Uint8Array(payload.buffer.byteLength); + uint8Array.set(payload.buffer); + const blob = new Blob([uint8Array.buffer], { type: contentType }); + const createdAt = asset.fileCreatedAt ?? asset.localDateTime ?? new Date().toISOString(); + const modifiedAt = asset.localDateTime ?? asset.fileCreatedAt ?? createdAt; + + const formData = new FormData(); + formData.set("deviceAssetId", makeDeviceAssetId(asset.id)); + formData.set("deviceId", DEVICE_ID); + formData.set("fileCreatedAt", createdAt); + formData.set("fileModifiedAt", modifiedAt); + formData.set("fileType", fileType); + formData.set("duration", asset.duration ?? "0:00:00.000000"); + formData.set("assetData", blob, resolvedFileName); + if (asset.isArchived) formData.set("isArchived", String(asset.isArchived)); + if (asset.isFavorite) formData.set("isFavorite", String(asset.isFavorite)); + + const uploadResponse = await fetch(`${ENV.IMMICH_URL}/api/assets`, { + method: "POST", + headers: uploadHeaders, + body: formData, + }); + + if (!uploadResponse.ok) { + const body = await uploadResponse.text().catch(() => ""); + throw new Error( + `Upload failed for ${asset.originalFileName ?? asset.id} (status ${uploadResponse.status}): ${body || "Unknown error"}` + ); + } + + const uploaded = await uploadResponse.json(); + const uploadedId = uploaded?.id; + if (!uploadedId) { + throw new Error("Immich upload response did not include an asset id"); + } + + if (tagId) { + const { tagAssetWithPowerTools } = await import("@/pages/api/import-shared/helpers"); + await tagAssetWithPowerTools(tagId, uploadedId, jsonHeaders); + } + return uploadedId; +}; + +export class ImmichSharedLinkProcessor implements ImportProcessor { + async setup(job: ImportJob, context: ProcessorContext): Promise { + const headers = context.headers as HeadersRecord; + + // Load all items for this job from the db + const items = await appDb + .select() + .from(importJobItems) + .where(eq(importJobItems.jobId, job.id)); + + // Build deviceAssetIds for dedup check + const deviceAssetIds: string[] = []; + const deviceAssetIdLookup = new Map(); + for (const item of items) { + const deviceAssetId = makeDeviceAssetId(item.assetId); + deviceAssetIds.push(deviceAssetId); + deviceAssetIdLookup.set(deviceAssetId.toLowerCase(), item.assetId); + } + + // Check which assets already exist in Immich + const skipAssetIds: string[] = []; + if (deviceAssetIds.length > 0) { + try { + const jsonHeadersWithContentType: HeadersRecord = { + ...headers, + "Content-Type": "application/json", + }; + const checkResponse = await fetch(`${ENV.IMMICH_URL}/api/assets/exist`, { + method: "POST", + headers: jsonHeadersWithContentType, + body: JSON.stringify({ deviceAssetIds, deviceId: DEVICE_ID }), + }); + + if (checkResponse.ok) { + const existPayload = await checkResponse.json().catch(() => ({})); + const existingIds: string[] = existPayload?.existingIds ?? []; + for (const deviceAssetId of existingIds) { + if (typeof deviceAssetId !== "string") continue; + const matchedAssetId = deviceAssetIdLookup.get(deviceAssetId.toLowerCase()); + if (matchedAssetId) { + skipAssetIds.push(matchedAssetId); + } + } + } else { + console.warn(`[ImmichSharedLinkProcessor] Failed to check existing assets (status ${checkResponse.status})`); + } + } catch (error) { + console.warn("[ImmichSharedLinkProcessor] Unable to check existing assets", error); + } + } + + // Parse importData for album options + let importData: Record = {}; + try { + importData = JSON.parse(job.importData); + } catch { + // ignore parse errors, treat as empty + } + + const albumOptions = importData.albumOptions as { + createAlbum?: boolean; + albumName?: string; + addToAlbumId?: string; + } | undefined; + + let albumId: string | undefined; + const jsonHeadersWithContentType: HeadersRecord = { + ...headers, + "Content-Type": "application/json", + }; + + if (albumOptions?.createAlbum) { + const desiredAlbumName = + albumOptions.albumName?.trim() || `Shared import ${new Date().toISOString()}`; + albumId = await createImmichAlbum(desiredAlbumName, jsonHeadersWithContentType); + console.log(`[ImmichSharedLinkProcessor] Created album ${albumId} (${desiredAlbumName})`); + } else if (typeof albumOptions?.addToAlbumId === "string" && albumOptions.addToAlbumId.trim()) { + albumId = albumOptions.addToAlbumId.trim(); + } + + const importDataPatch: Record = {}; + if (albumId) importDataPatch.albumId = albumId; + + // Tag assets with "immich-power-tools" if enabled + const tagAssets = importData.tagAssets !== false; // default true for backwards compat + if (tagAssets) { + try { + const tag = await ensurePowerToolsTag({ ...context.headers, "Content-Type": "application/json" } as HeadersRecord); + importDataPatch.tagId = tag.id; + } catch (err) { + console.warn("[ImmichSharedLinkProcessor] Failed to create power-tools tag, skipping tagging", err); + } + } + + return { skipAssetIds, albumId, importDataPatch }; + } + + async processItem( + job: ImportJob, + item: ImportJobItem, + context: ProcessorContext + ): Promise<{ immichId: string }> { + const headers = context.headers as HeadersRecord; + + // Parse item metadata + let asset: ISharedAssetPayload; + try { + const parsed = JSON.parse(item.itemData); + asset = { + id: item.assetId, + originalFileName: parsed.originalFileName, + type: parsed.type ?? "IMAGE", + fileCreatedAt: parsed.fileCreatedAt ?? null, + localDateTime: parsed.localDateTime ?? null, + duration: parsed.duration ?? null, + isFavorite: parsed.isFavorite ?? false, + isArchived: parsed.isArchived ?? false, + }; + } catch { + asset = { id: item.assetId, type: "IMAGE" }; + } + + // Parse urlConfig for the share key + let urlConfig: Record = {}; + try { + urlConfig = JSON.parse(job.urlConfig); + } catch { + // ignore + } + const key = typeof urlConfig.key === "string" ? urlConfig.key : ""; + + // Download the asset from the shared link + const downloaded = await downloadSharedAsset(asset, job.url, key); + + // Strip Content-Type for multipart upload (same pattern as upload-all.ts) + const { ["Content-Type"]: _omit, ...uploadHeaders } = headers; + + // Read tagId from importData set during setup() (optional) + const importData = JSON.parse(job.importData) as { albumId?: string; tagId?: string }; + const tagId = importData.tagId; + + const immichId = await uploadAssetBuffer(asset, downloaded, uploadHeaders as HeadersRecord, headers as HeadersRecord, tagId); + + // Add to album if one was resolved during setup + if (context.albumId) { + const jsonHeadersWithContentType: HeadersRecord = { + ...headers, + "Content-Type": "application/json", + }; + await addAssetToAlbum(context.albumId, immichId, jsonHeadersWithContentType); + } + + return { immichId }; + } +} diff --git a/src/workers/import/processors/index.ts b/src/workers/import/processors/index.ts new file mode 100644 index 0000000..00cc5d7 --- /dev/null +++ b/src/workers/import/processors/index.ts @@ -0,0 +1,4 @@ +import { registerProcessor } from "../registry"; +import { ImmichSharedLinkProcessor } from "./immich-shared-link"; + +registerProcessor("immich", new ImmichSharedLinkProcessor()); diff --git a/src/workers/import/registry.ts b/src/workers/import/registry.ts new file mode 100644 index 0000000..1292413 --- /dev/null +++ b/src/workers/import/registry.ts @@ -0,0 +1,15 @@ +import type { ImportProcessor } from "./types"; + +const processors = new Map(); + +export function registerProcessor(platform: string, processor: ImportProcessor) { + processors.set(platform, processor); +} + +export function getProcessor(platform: string): ImportProcessor { + const processor = processors.get(platform); + if (!processor) { + throw new Error(`No import processor registered for platform: ${platform}`); + } + return processor; +} diff --git a/src/workers/import/runner.ts b/src/workers/import/runner.ts new file mode 100644 index 0000000..c3b7dcd --- /dev/null +++ b/src/workers/import/runner.ts @@ -0,0 +1,170 @@ +import { and, eq, inArray, sql } from "drizzle-orm"; +import { appDb } from "@/db"; +import { importJobs, importJobItems } from "@/db/schema"; +import { getProcessor } from "./registry"; +import "./processors/index"; // side-effect: registers all processors + +export type { ImportJob, ImportJobItem } from "./types"; + +const activeJobs = new Set(); +const CONCURRENCY = 4; + +export function isJobActive(jobId: string) { + return activeJobs.has(jobId); +} + +export async function getJobItems(jobId: string) { + return appDb + .select() + .from(importJobItems) + .where(eq(importJobItems.jobId, jobId)); +} + +export async function resumePendingJobs() { + // Reset any jobs stuck in 'processing' from a previous crashed run. + // We cannot restart workers here — no auth headers are available at boot. + // Workers restart automatically when the polling endpoint (GET /api/import-jobs/[jobId]) + // detects a pending job with no active worker. + + // First reset stuck items (must happen before the job status reset) + const stuckJobs = await appDb + .select({ id: importJobs.id }) + .from(importJobs) + .where(eq(importJobs.status, "processing")); + + if (stuckJobs.length > 0) { + const stuckJobIds = stuckJobs.map((j) => j.id); + await appDb + .update(importJobItems) + .set({ status: "pending" }) + .where(and(inArray(importJobItems.jobId, stuckJobIds), eq(importJobItems.status, "processing"))); + } + + await appDb + .update(importJobs) + .set({ status: "pending" }) + .where(eq(importJobs.status, "processing")); +} + +export async function runImportJob( + jobId: string, + headers: Record +): Promise { + if (activeJobs.has(jobId)) return; + activeJobs.add(jobId); + + try { + const [job] = await appDb + .select() + .from(importJobs) + .where(eq(importJobs.id, jobId)); + if (!job || job.status === "completed" || job.status === "failed") return; + + await appDb + .update(importJobs) + .set({ status: "processing" }) + .where(eq(importJobs.id, jobId)); + + const processor = getProcessor(job.platform); + const context = { headers }; + + // Setup: dedup + album creation + const { skipAssetIds, albumId, importDataPatch } = await processor.setup( + job, + context + ); + + if (importDataPatch) { + const merged = { ...JSON.parse(job.importData), ...importDataPatch }; + const mergedStr = JSON.stringify(merged); + await appDb + .update(importJobs) + .set({ importData: mergedStr }) + .where(eq(importJobs.id, jobId)); + job.importData = mergedStr; + } + + if (skipAssetIds.length > 0) { + await appDb + .update(importJobItems) + .set({ status: "skipped" }) + .where(and(eq(importJobItems.jobId, jobId), inArray(importJobItems.assetId, skipAssetIds))); + await appDb + .update(importJobs) + .set({ skippedCount: sql`${importJobs.skippedCount} + ${skipAssetIds.length}` }) + .where(eq(importJobs.id, jobId)); + } + + // Fetch only the items that are still pending for this job (after skip updates) + const pending = await appDb + .select() + .from(importJobItems) + .where(and(eq(importJobItems.jobId, jobId), eq(importJobItems.status, "pending"))); + + const processorContext = { ...context, albumId }; + + let currentIndex = 0; + const worker = async () => { + while (true) { + const index = currentIndex++; + if (index >= pending.length) break; + const item = pending[index]; + + await appDb + .update(importJobItems) + .set({ status: "processing" }) + .where(eq(importJobItems.id, item.id)); + + try { + const { immichId } = await processor.processItem( + job, + item, + processorContext + ); + await appDb + .update(importJobItems) + .set({ status: "uploaded", immichId }) + .where(eq(importJobItems.id, item.id)); + await appDb + .update(importJobs) + .set({ uploadedCount: sql`${importJobs.uploadedCount} + 1` }) + .where(eq(importJobs.id, jobId)); + } catch (err: any) { + await appDb + .update(importJobItems) + .set({ status: "failed", error: err?.message ?? "Unknown error" }) + .where(eq(importJobItems.id, item.id)); + await appDb + .update(importJobs) + .set({ failedCount: sql`${importJobs.failedCount} + 1` }) + .where(eq(importJobs.id, jobId)); + } + } + }; + + const concurrency = Math.min(CONCURRENCY, pending.length); + if (concurrency > 0) { + await Promise.all(Array.from({ length: concurrency }, worker)); + } + + await appDb + .update(importJobs) + .set({ status: "completed" }) + .where(eq(importJobs.id, jobId)); + } catch (err: any) { + console.error(`Import job ${jobId} failed fatally:`, err); + // Store error message in importData so the UI can display it + let existingData: Record = {}; + try { + const [current] = await appDb.select({ importData: importJobs.importData }).from(importJobs).where(eq(importJobs.id, jobId)); + if (current) existingData = JSON.parse(current.importData); + } catch { /* ignore */ } + existingData.error = err?.message ?? "Unknown error"; + await appDb + .update(importJobs) + .set({ status: "failed", importData: JSON.stringify(existingData) }) + .where(eq(importJobs.id, jobId)); + } finally { + activeJobs.delete(jobId); + } +} diff --git a/src/workers/import/types.ts b/src/workers/import/types.ts new file mode 100644 index 0000000..0b98aaa --- /dev/null +++ b/src/workers/import/types.ts @@ -0,0 +1,22 @@ +import type { importJobs, importJobItems } from "@/db/schema"; + +export type ImportJob = typeof importJobs.$inferSelect; +export type ImportJobItem = typeof importJobItems.$inferSelect; + +export interface ProcessorContext { + headers: Record; // user auth headers for Immich API calls + albumId?: string; // resolved after setup() +} + +export interface SetupResult { + skipAssetIds: string[]; // source asset IDs already in Immich → mark skipped + albumId?: string; // created or resolved album ID + importDataPatch?: Record; // merged into job.importData in db +} + +export interface ImportProcessor { + /** Called once per job before items are processed. Dedup + album creation. */ + setup(job: ImportJob, context: ProcessorContext): Promise; + /** Called once per item. Returns the Immich asset ID on success, throws on failure. */ + processItem(job: ImportJob, item: ImportJobItem, context: ProcessorContext): Promise<{ immichId: string }>; +}