merge: resolve conflicts with prerelease branch

This commit is contained in:
Varun committed 2026-04-06 20:18:11 +05:30
commit e814793190
48 files changed
+2382 -258

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
{
"attribution": {
"commit": "",
"pr": ""
}
}
+3 -1
View File
@@ -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)
+8
View File
@@ -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
+10 -8
View File
@@ -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
HEALTHCHECK CMD curl --fail http://localhost:3000/api/health || exit 1
BIN
View File
Binary file not shown.
+12
View File
@@ -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",
},
});
+3
View File
@@ -3,6 +3,9 @@
const nextConfig = {
reactStrictMode: false,
output: 'standalone',
experimental: {
instrumentationHook: true,
},
images: {
unoptimized: true,
},
+7 -3
View File
@@ -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",
@@ -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 }) => (
<div className="flex flex-col gap-1 rounded-md border border-border bg-muted/30 p-3">
<p className="text-xs uppercase tracking-wide text-muted-foreground">{label}</p>
<p className="text-sm font-medium text-foreground break-words">{value ?? "—"}</p>
</div>
);
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 (
<span className={`text-xs px-1.5 py-0.5 rounded ${styles}`}>
{status}
</span>
);
};
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<string, unknown> = {};
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 (
<Dialog open={!!jobId} onOpenChange={(open) => { if (!open) onClose(); }}>
<DialogContent className="max-w-2xl max-h-[80vh] flex flex-col">
{isLoading && (
<div className="flex items-center justify-center py-12">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
)}
{detailJob && (() => {
const dJob = detailJob.job;
return (
<>
<DialogHeader>
<DialogTitle>{dAlbumOpts?.albumName || "Import details"}</DialogTitle>
</DialogHeader>
<div className="grid grid-cols-2 gap-3 text-sm">
<InfoRow label="Status" value={statusBadge(dJob.status)} />
<InfoRow label="Platform" value={dJob.platform} />
<InfoRow label="Total" value={dJob.totalCount} />
<InfoRow label="Uploaded" value={dJob.uploadedCount} />
<InfoRow label="Skipped" value={dJob.skippedCount} />
<InfoRow label="Failed" value={dJob.failedCount} />
{albumId && (
<InfoRow label="Album" value={
<a
href={`${exImmichUrl}/albums/${albumId}`}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-primary hover:underline"
>
{dAlbumOpts?.albumName || albumId}
<ExternalLink className="h-3 w-3" />
</a>
} />
)}
{!albumId && dAlbumOpts?.albumName && (
<InfoRow label="Album" value={dAlbumOpts.albumName} />
)}
</div>
{dError && (
<div className="rounded-md border border-destructive/50 bg-destructive/10 px-4 py-3 text-sm text-destructive">
{dError}
</div>
)}
<div className="flex-1 overflow-auto min-h-0 mt-2">
<table className="w-full text-sm">
<thead className="sticky top-0 bg-background">
<tr className="border-b text-left text-xs text-muted-foreground">
<th className="pb-2 font-medium">Asset</th>
<th className="pb-2 font-medium">Status</th>
<th className="pb-2 font-medium">Details</th>
</tr>
</thead>
<tbody className="divide-y divide-border">
{detailJob.items.map((item) => {
let itemData: Record<string, unknown> = {};
try { itemData = JSON.parse(item.itemData ?? "{}"); } catch { /* ignore */ }
const fileName = (itemData.originalFileName as string) || item.assetId;
return (
<tr key={item.id}>
<td className="py-2 pr-2 max-w-[200px] truncate" title={fileName}>
{item.immichId ? (
<a
href={`${exImmichUrl}/photos/${item.immichId}`}
target="_blank"
rel="noopener noreferrer"
className="inline-flex items-center gap-1 text-primary hover:underline"
onClick={(e) => e.stopPropagation()}
>
{fileName}
<ExternalLink className="h-3 w-3 shrink-0" />
</a>
) : (
fileName
)}
</td>
<td className="py-2 pr-2">
{statusBadge(item.status)}
</td>
<td className="py-2 text-xs text-muted-foreground max-w-[200px] truncate" title={item.error || ""}>
{item.error || "—"}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</>
);
})()}
</DialogContent>
</Dialog>
);
}
+43 -2
View File
@@ -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<Partial<ShareLinkFilters>>({
expiresIn: "never"
});
const [endpointConfigured, setEndpointConfigured] = useState<boolean | null>(null);
const [shareKeyExists, setShareKeyExists] = useState<boolean | null>(null);
const [creatingKey, setCreatingKey] = useState(false);
const [keyError, setKeyError] = useState<string | null>(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" });
@@ -66,7 +89,7 @@ export default function ShareAssetsTrigger({ filters, buttonProps }: ShareAssets
return (
<Dialog>
<Dialog onOpenChange={(open) => { if (open) checkShareKey(); }}>
<DialogTrigger asChild>
<Button {...buttonProps}>Share</Button>
</DialogTrigger>
@@ -77,6 +100,24 @@ export default function ShareAssetsTrigger({ filters, buttonProps }: ShareAssets
<DialogDescription>
Generate a share link for the selected assets (either bunch of albums or bunch of people) and share it with your friends and family.
</DialogDescription>
{endpointConfigured === false && (
<div className="rounded-md border border-red-400 bg-red-50 dark:bg-red-950 p-3">
<p className="text-sm text-red-800 dark:text-red-200">
<strong>POWER_TOOLS_ENDPOINT_URL</strong> is not set. Add it to your environment variables to enable share links.
</p>
</div>
)}
{endpointConfigured && shareKeyExists === false && (
<div className="rounded-md border border-yellow-400 bg-yellow-50 dark:bg-yellow-950 p-3 flex flex-col gap-2">
<p className="text-sm text-yellow-800 dark:text-yellow-200">
Serving thumbnails on share links requires a dedicated API key. Create one now with minimal permissions.
</p>
{keyError && <p className="text-xs text-red-500">{keyError}</p>}
<Button size="sm" onClick={handleCreateKey} disabled={creatingKey} className="self-start">
{creatingKey ? 'Creating...' : 'Create API Key'}
</Button>
</div>
)}
{errorMessage && <div className="text-red-500">{errorMessage}</div>}
{generatedLink ? <div className="flex flex-col gap-2">
<Label className='text-sm'>Share Link</Label>
@@ -120,7 +161,7 @@ export default function ShareAssetsTrigger({ filters, buttonProps }: ShareAssets
</SelectContent>
</Select>
</div>
<Button onClick={handleGenerate} disabled={loading}>Generate Share Link</Button>
<Button onClick={handleGenerate} disabled={loading || !endpointConfigured || !shareKeyExists}>Generate Share Link</Button>
</div>
)}
</DialogContent>
+7 -3
View File
@@ -1,4 +1,4 @@
import { Copy, GalleryHorizontal, GalleryVerticalEnd, Image as ImageIcon, MapPin, MapPinX, PackageSearch, Rewind, Search, Share2, User, Video } from "lucide-react";
import { Copy, GalleryHorizontal, GalleryVerticalEnd, Image as ImageIcon, MapPin, MapPinX, PackageSearch, Rewind, Search, Settings, Share2, User, Video } from "lucide-react";
export const sidebarNavs = [
// {
@@ -50,7 +50,7 @@ export const sidebarNavs = [
// Empty Videos
{
title: "Empty Videos",
link: "/assets/empty-videos",
link: "/assets/empty-videos",
icon: <Video className="h-4 w-4" />,
},
// Bulk Duplicate
@@ -64,5 +64,9 @@ export const sidebarNavs = [
link: "/assets/orphan-finder",
icon: <PackageSearch className="h-4 w-4" />,
},
{
title: "Settings",
link: "/settings",
icon: <Settings className="h-4 w-4" />,
},
];
+2 -1
View File
@@ -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,
+19 -1
View File
@@ -84,4 +84,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";
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;
+23
View File
@@ -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"),
});
}
+17
View File
@@ -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`);
@@ -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
);
+114
View File
@@ -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": {}
}
}
+314
View File
@@ -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": {}
}
}
+20
View File
@@ -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
}
]
}
+14
View File
@@ -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),
]);
+34
View File
@@ -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()),
});
+3
View File
@@ -0,0 +1,3 @@
export * from "./settings.schema";
export * from "./apiKeys.schema";
export * from "./importJobs.schema";
+9
View File
@@ -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()
),
});
+56
View File
@@ -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<string, unknown>;
importData: Record<string, unknown>;
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<ImportJobStatus> => {
return API.get(IMPORT_JOB_PATH(jobId));
};
@@ -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 });
};
+14
View File
@@ -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));
};
+10
View File
@@ -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);
};
@@ -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<PermissionsResult> => {
return API.get(VALIDATE_PERMISSIONS_PATH);
};
+1 -4
View File
@@ -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}`
+10
View File
@@ -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();
}
}
@@ -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) {
+38
View File
@@ -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<string, string>;
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 });
}
+75
View File
@@ -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<string, string>;
runImportJob(job.id, headers).catch((err) =>
console.error("Unhandled error in runImportJob", err)
);
return res.status(201).json({ jobId: job.id });
}
+4 -1
View File
@@ -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 }),
});
@@ -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<string, { keyName: string; permissions: string[] }> = {
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" });
}
+28
View File
@@ -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);
}
+91
View File
@@ -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" });
}
+17 -4
View File
@@ -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) {
@@ -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);
+1 -5
View File
@@ -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
} : {});
+33
View File
@@ -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);
}
+303 -215
View File
@@ -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<Set<string>>(new Set());
const [existingAlbums, setExistingAlbums] = useState<IAlbum[]>([]);
const [selectedAlbumId, setSelectedAlbumId] = useState<string | null>(null);
const [activeJobId, setActiveJobId] = useState<string | null>(null);
const [jobProgress, setJobProgress] = useState<{ uploaded: number; skipped: number; failed: number; total: number } | null>(null);
const [tagAssets, setTagAssets] = useState(true);
const [detailJobId, setDetailJobId] = useState<string | null>(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<string, unknown> = {};
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<HTMLFormElement>) => {
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() {
<Button
variant="default"
size="sm"
disabled={importAllLoading || importableAssetCount === 0}
disabled={
importAllLoading ||
importableAssetCount === 0 ||
(albumImportMode === "existing-album" && !selectedAlbumId) ||
(permissions && !permissions.canUpload)
}
onClick={() => {
if (importAllLoading) {
return;
}
if (!albumNameInput.trim()) {
setAlbumNameInput(albumDetails?.albumName || "Imported shared album");
}
setAlbumImportMode("album");
setImportAllDialogOpen(true);
if (importAllLoading) return;
const shouldCreateAlbum = albumImportMode === "album";
handleImportAll({
createAlbum: shouldCreateAlbum,
albumName: shouldCreateAlbum ? albumNameInput.trim() : undefined,
addToAlbumId:
albumImportMode === "existing-album" && selectedAlbumId
? selectedAlbumId
: undefined,
});
}}
>
{importAllLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
@@ -434,6 +476,12 @@ export default function ImportSharedPage() {
/>
<div className={!sharedData ? "flex flex-1 justify-center px-4 py-6" : "flex flex-col gap-6 p-4"}>
<section className={!sharedData ? "w-full max-w-6xl flex flex-col gap-6" : "w-full flex flex-col gap-6"}>
{permissions?.canUpload === false && (
<div className="flex items-center gap-2 rounded-md border border-amber-500/40 bg-amber-500/10 px-4 py-3 text-sm text-amber-600 dark:text-amber-400">
<AlertTriangle className="h-4 w-4 shrink-0" />
Your API key does not have permission to upload assets. Import will not work until this is fixed.
</div>
)}
{!sharedData && (
<Card>
<CardHeader className="space-y-2">
@@ -470,19 +518,173 @@ export default function ImportSharedPage() {
</Card>
)}
{sharedLinkDetails && sharedData && albumDetails && (
{!sharedData && importHistory?.jobs && importHistory.jobs.length > 0 && (
<Card>
<CardHeader className="space-y-1">
<CardTitle className="text-xl">{albumDetails.albumName}</CardTitle>
<CardHeader className="pb-3">
<CardTitle className="text-lg">Previous imports</CardTitle>
</CardHeader>
<CardContent className="grid gap-4 md:grid-cols-3">
<InfoRow label="Expires" value={sharedLinkDetails.expiresAt ? formatDate(sharedLinkDetails.expiresAt) : "No expiry"} />
{albumDetails && (
<>
<InfoRow label="Owner" value={albumDetails.owner?.name ?? "Unknown"} />
<InfoRow label="Assets" value={albumDetails.assetCount} />
<InfoRow label="Date range" value={`${formatDateOnly(albumDetails.startDate)} → ${formatDateOnly(albumDetails.endDate)}`} />
</>
<CardContent>
<div className="divide-y divide-border">
{importHistory.jobs.map((job) => {
let jobImportData: Record<string, unknown> = {};
try { jobImportData = JSON.parse(job.importData); } catch { /* ignore */ }
const albumOpts = jobImportData.albumOptions as { albumName?: string } | undefined;
const albumName = albumOpts?.albumName;
const statusIcon = job.status === "completed" ? (
<CheckCircle2 className="h-4 w-4 text-emerald-500 shrink-0" />
) : job.status === "failed" ? (
<XCircle className="h-4 w-4 text-destructive shrink-0" />
) : (
<Clock3 className="h-4 w-4 text-muted-foreground shrink-0" />
);
return (
<div
key={job.id}
className="flex items-center gap-3 py-3 first:pt-0 last:pb-0 cursor-pointer hover:bg-muted/50 -mx-2 px-2 rounded-md transition-colors"
onClick={() => setDetailJobId(job.id)}
>
{statusIcon}
<div className="flex-1 min-w-0">
<p className="text-sm font-medium truncate">
{albumName || job.url}
</p>
<p className="text-xs text-muted-foreground">
{job.uploadedCount} uploaded
{job.skippedCount > 0 && `, ${job.skippedCount} skipped`}
{job.failedCount > 0 && `, ${job.failedCount} failed`}
{" · "}
{formatDate(job.createdAt)}
</p>
</div>
<span className={`text-xs px-2 py-0.5 rounded-full ${
job.status === "completed" ? "bg-emerald-500/10 text-emerald-600" :
job.status === "failed" ? "bg-destructive/10 text-destructive" :
"bg-muted text-muted-foreground"
}`}>
{job.status}
</span>
</div>
);
})}
</div>
</CardContent>
</Card>
)}
{sharedLinkDetails && sharedData && albumDetails && (
<Card className="overflow-hidden">
{/* Album identity */}
<CardHeader className="pb-4">
<CardTitle className="text-xl leading-tight">{albumDetails.albumName}</CardTitle>
<div className="flex flex-wrap gap-x-4 gap-y-1 pt-1">
<span className="flex items-center gap-1.5 text-xs text-muted-foreground">
<Images className="h-3.5 w-3.5 shrink-0" />
{albumDetails.assetCount} assets
</span>
{albumDetails.owner?.name && (
<span className="flex items-center gap-1.5 text-xs text-muted-foreground">
<User className="h-3.5 w-3.5 shrink-0" />
{albumDetails.owner.name}
</span>
)}
{albumDetails.startDate && (
<span className="flex items-center gap-1.5 text-xs text-muted-foreground">
<Calendar className="h-3.5 w-3.5 shrink-0" />
{formatDateOnly(albumDetails.startDate)}
{albumDetails.endDate && albumDetails.endDate !== albumDetails.startDate && (
<> → {formatDateOnly(albumDetails.endDate)}</>
)}
</span>
)}
{sharedLinkDetails.expiresAt && (
<span className="flex items-center gap-1.5 text-xs text-muted-foreground">
<Clock className="h-3.5 w-3.5 shrink-0" />
Expires {formatDate(sharedLinkDetails.expiresAt)}
</span>
)}
</div>
</CardHeader>
{/* Divider */}
<div className="mx-6 border-t border-border" />
{/* Destination */}
<CardContent className="flex flex-col gap-4 pt-4">
<div className="flex items-center justify-between gap-4">
<Label className="text-sm font-medium shrink-0">Import to</Label>
<div className="flex rounded-md border border-border overflow-hidden">
{(["album", "existing-album", "no-album"] as const).map((mode, i) => (
<button
key={mode}
type="button"
onClick={() => setAlbumImportMode(mode)}
disabled={importAllLoading}
className={[
"px-3 py-1.5 text-xs font-medium transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-50",
i > 0 ? "border-l border-border" : "",
albumImportMode === mode
? "bg-primary text-primary-foreground"
: "bg-background text-muted-foreground hover:bg-muted hover:text-foreground",
].join(" ")}
>
{mode === "album" ? "New album" : mode === "existing-album" ? "Existing" : "None"}
</button>
))}
</div>
</div>
{albumImportMode === "album" && (
<Input
id="destination-album-name"
value={albumNameInput}
onChange={(e) => setAlbumNameInput(e.target.value)}
placeholder="Album name"
disabled={importAllLoading}
/>
)}
{albumImportMode === "existing-album" && (
<Select
onValueChange={setSelectedAlbumId}
value={selectedAlbumId || undefined}
disabled={importAllLoading}
>
<SelectTrigger id="destination-existing-album">
<SelectValue placeholder="Select an album" />
</SelectTrigger>
<SelectContent>
{existingAlbums.map((album) => (
<SelectItem key={album.id} value={album.id}>
{album.albumName}
</SelectItem>
))}
</SelectContent>
</Select>
)}
<div className="flex items-center gap-2">
<Checkbox
id="tag-assets"
checked={tagAssets}
onCheckedChange={(checked) => setTagAssets(!!checked)}
disabled={importAllLoading}
/>
<Label htmlFor="tag-assets" className="text-sm text-muted-foreground cursor-pointer">
Tag imported assets with &quot;immich-power-tools&quot;
</Label>
</div>
{importAllLoading && jobProgress && (
<div className="flex items-center gap-2 rounded-md bg-muted px-3 py-2">
<Loader2 className="h-3.5 w-3.5 animate-spin text-muted-foreground shrink-0" />
<p className="text-xs text-muted-foreground">
{jobProgress.uploaded + jobProgress.skipped} / {jobProgress.total} processed
{jobProgress.failed > 0 && (
<span className="text-destructive"> · {jobProgress.failed} failed</span>
)}
</p>
</div>
)}
</CardContent>
</Card>
@@ -631,121 +833,6 @@ export default function ImportSharedPage() {
)}
</section>
</div>
<Dialog
open={importAllDialogOpen}
onOpenChange={(open) => {
if (importAllLoading) {
return;
}
setImportAllDialogOpen(open);
}}
>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>{selectedAssetIds.size > 0 ? "Import selected assets" : "Import all assets"}</DialogTitle>
<DialogDescription>
Decide whether to create a new Immich album for these imported assets.
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
<div className="flex flex-col gap-2">
<Label className="text-sm font-medium text-foreground">Destination</Label>
<div className="flex flex-col gap-2 sm:flex-row">
<Button
type="button"
variant={albumImportMode === "album" ? "default" : "outline"}
className="flex-1"
onClick={() => setAlbumImportMode("album")}
disabled={importAllLoading}
>
Create a new album
</Button>
<Button
type="button"
variant={albumImportMode === "existing-album" ? "default" : "outline"}
className="flex-1"
onClick={() => setAlbumImportMode("existing-album")}
disabled={importAllLoading}
>
Existing Album
</Button>
<Button
type="button"
variant={albumImportMode === "no-album" ? "default" : "outline"}
className="flex-1"
onClick={() => setAlbumImportMode("no-album")}
disabled={importAllLoading}
>
No Album
</Button>
</div>
</div>
{albumImportMode === "album" && (
<div className="flex flex-col gap-2">
<Label htmlFor="import-album-name">Album name</Label>
<Input
id="import-album-name"
value={albumNameInput}
onChange={(event) => setAlbumNameInput(event.target.value)}
placeholder="Imported shared album"
disabled={importAllLoading}
/>
</div>
)}
{albumImportMode === "existing-album" && (
<div className="flex flex-col gap-2">
<Label htmlFor="existing-album">Select Album</Label>
<Select onValueChange={setSelectedAlbumId} value={selectedAlbumId || undefined}>
<SelectTrigger>
<SelectValue placeholder="Select an album" />
</SelectTrigger>
<SelectContent>
{existingAlbums.map((album) => (
<SelectItem key={album.id} value={album.id}>
{album.albumName}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)}
</div>
<div className="flex justify-end gap-2 pt-4">
<Button
type="button"
variant="ghost"
onClick={() => setImportAllDialogOpen(false)}
disabled={importAllLoading}
>
Cancel
</Button>
<Button
type="button"
onClick={() => {
if (importAllLoading) {
return;
}
const shouldCreateAlbum = albumImportMode === "album";
const trimmedAlbumName = albumNameInput.trim();
setImportAllDialogOpen(false);
handleImportAll({
createAlbum: shouldCreateAlbum,
albumName: shouldCreateAlbum ? trimmedAlbumName : undefined,
addToAlbumId: albumImportMode === "existing-album" && selectedAlbumId ? selectedAlbumId : undefined,
});
}}
disabled={
importAllLoading ||
(albumImportMode === "album" && albumNameInput.trim().length === 0) ||
(albumImportMode === "existing-album" && !selectedAlbumId)
}
>
{importAllLoading ? "Importing..." : "Start import"}
</Button>
</div>
</DialogContent>
</Dialog>
<Dialog
open={!!activeVideoAsset}
onOpenChange={(open) => {
@@ -809,6 +896,7 @@ export default function ImportSharedPage() {
)}
</DialogContent>
</Dialog>
<ImportJobDetailDialog jobId={detailJobId} onClose={() => setDetailJobId(null)} />
</PageLayout>
);
}
+133
View File
@@ -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<ApiKey[]>([]);
const [loading, setLoading] = useState(true);
const [deleting, setDeleting] = useState<string | null>(null);
const [regenerating, setRegenerating] = useState<string | null>(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 (
<PageLayout>
<Header leftComponent="Settings" />
<div className="p-6 flex flex-col gap-6 max-w-2xl">
<Card>
<CardHeader>
<CardTitle>API Keys</CardTitle>
<CardDescription>
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).
</CardDescription>
</CardHeader>
<CardContent>
{loading && (
<p className="text-sm text-muted-foreground">Loading...</p>
)}
{!loading && keys.length === 0 && (
<p className="text-sm text-muted-foreground">No API keys created yet.</p>
)}
{!loading && keys.length > 0 && (
<ul className="flex flex-col divide-y">
{keys.map((key) => (
<li key={key.id} className="flex items-center justify-between py-3 gap-4">
<div className="flex flex-col gap-1">
<div className="flex items-center gap-2">
<span className="text-sm font-medium">{key.keyName}</span>
<Badge variant="secondary" className="capitalize">{key.purpose}</Badge>
</div>
<span className="text-xs text-muted-foreground">
Created {new Date(key.createdAt).toLocaleDateString()}
</span>
</div>
<div className="flex items-center gap-1">
<AlertDialog
title="Regenerate API Key"
description={`This will delete the current "${key.keyName}" key from Immich and create a new one. Existing share links will continue to work.`}
onConfirm={() => handleRegenerate(key.purpose)}
>
<Button
variant="ghost"
size="icon"
disabled={regenerating === key.purpose || deleting === key.purpose}
>
<RefreshCw className={`h-4 w-4 ${regenerating === key.purpose ? "animate-spin" : ""}`} />
</Button>
</AlertDialog>
<AlertDialog
title="Delete API Key"
description={`This will remove the "${key.keyName}" key from Immich and Power Tools. Share links that depend on it will stop serving thumbnails.`}
onConfirm={() => handleDelete(key.purpose)}
>
<Button
variant="ghost"
size="icon"
disabled={deleting === key.purpose || regenerating === key.purpose}
>
<Trash2 className="h-4 w-4 text-destructive" />
</Button>
</AlertDialog>
</div>
</li>
))}
</ul>
)}
</CardContent>
</Card>
</div>
</PageLayout>
);
}
@@ -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<string> => {
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<void> => {
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<DownloadedAssetPayload> => {
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<string> => {
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<SetupResult> {
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<string, string>();
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<string, unknown> = {};
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<string, unknown> = {};
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<string, unknown> = {};
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 };
}
}
+4
View File
@@ -0,0 +1,4 @@
import { registerProcessor } from "../registry";
import { ImmichSharedLinkProcessor } from "./immich-shared-link";
registerProcessor("immich", new ImmichSharedLinkProcessor());
+15
View File
@@ -0,0 +1,15 @@
import type { ImportProcessor } from "./types";
const processors = new Map<string, ImportProcessor>();
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;
}
+170
View File
@@ -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<string>();
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<string, string>
): Promise<void> {
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<string, unknown> = {};
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);
}
}
+22
View File
@@ -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<string, string>; // 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<string, unknown>; // 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<SetupResult>;
/** Called once per item. Returns the Immich asset ID on success, throws on failure. */
processItem(job: ImportJob, item: ImportJobItem, context: ProcessorContext): Promise<{ immichId: string }>;
}