diff --git a/bun.lock b/bun.lock index 82d64a9..9e17343 100644 --- a/bun.lock +++ b/bun.lock @@ -48,6 +48,7 @@ "date-fns": "^3.6.0", "date-fns-tz": "^3.2.0", "drizzle-orm": "^0.45.2", + "fast-xml-parser": "^5.5.10", "jsonwebtoken": "^9.0.3", "leaflet": "^1.9.4", "leaflet.heat": "^0.2.0", @@ -1002,6 +1003,10 @@ "fast-uri": ["fast-uri@3.1.0", "", {}, "sha512-iPeeDKJSWf4IEOasVVrknXpaBV0IApz/gp7S2bb7Z4Lljbl2MGJRqInZiUrQwV16cpzw/D3S5j5Julj/gT52AA=="], + "fast-xml-builder": ["fast-xml-builder@1.1.4", "", { "dependencies": { "path-expression-matcher": "^1.1.3" } }, "sha512-f2jhpN4Eccy0/Uz9csxh3Nu6q4ErKxf0XIsasomfOihuSUa3/xw6w8dnOtCDgEItQFJG8KyXPzQXzcODDrrbOg=="], + + "fast-xml-parser": ["fast-xml-parser@5.5.10", "", { "dependencies": { "fast-xml-builder": "^1.1.4", "path-expression-matcher": "^1.2.1", "strnum": "^2.2.2" }, "bin": { "fxparser": "src/cli/cli.js" } }, "sha512-go2J2xODMc32hT+4Xr/bBGXMaIoiCwrwp2mMtAvKyvEFW6S/v5Gn2pBmE4nvbwNjGhpcAiOwEv7R6/GZ6XRa9w=="], + "fastq": ["fastq@1.20.1", "", { "dependencies": { "reusify": "^1.0.4" } }, "sha512-GGToxJ/w1x32s/D2EKND7kTil4n8OVk/9mycTc4VDza13lOvpUZTGX3mFSCtV9ksdGBVzvsyAVLM6mHFThxXxw=="], "fd-slicer": ["fd-slicer@1.1.0", "", { "dependencies": { "pend": "~1.2.0" } }, "sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g=="], @@ -1342,6 +1347,8 @@ "path-exists": ["path-exists@4.0.0", "", {}, "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w=="], + "path-expression-matcher": ["path-expression-matcher@1.4.0", "", {}, "sha512-s4DQMxIdhj3jLFWd9LxHOplj4p9yQ4ffMGowFf3cpEgrrJjEhN0V5nxw4Ye1EViAGDoL4/1AeO6qHpqYPOzE4Q=="], + "path-key": ["path-key@3.1.1", "", {}, "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q=="], "path-parse": ["path-parse@1.0.7", "", {}, "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw=="], @@ -1564,6 +1571,8 @@ "strip-json-comments": ["strip-json-comments@3.1.1", "", {}, "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig=="], + "strnum": ["strnum@2.2.3", "", {}, "sha512-oKx6RUCuHfT3oyVjtnrmn19H1SiCqgJSg+54XqURKp5aCMbrXrhLjRN9TjuwMjiYstZ0MzDrHqkGZ5dFTKd+zg=="], + "style-loader": ["style-loader@2.0.0", "", { "dependencies": { "loader-utils": "^2.0.0", "schema-utils": "^3.0.0" }, "peerDependencies": { "webpack": "^4.0.0 || ^5.0.0" } }, "sha512-Z0gYUJmzZ6ZdRUqpg1r8GsaFKypE+3xAzuFeMuoHgjc9KZv3wMyCRjQIWEbhoFSq7+7yoHXySDJyyWQaPajeiQ=="], "styled-jsx": ["styled-jsx@5.1.6", "", { "dependencies": { "client-only": "0.0.1" }, "peerDependencies": { "react": ">= 16.8.0 || 17.x.x || ^18.0.0-0 || ^19.0.0-0" } }, "sha512-qSVyDTeMotdvQYoHWLNGwRFJHC+i+ZvdBRYosOFgC+Wg1vx4frN2/RG/NA7SYqqvKNLf39P2LSRA2pu6n0XYZA=="], diff --git a/package.json b/package.json index e38090e..757748a 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "date-fns": "^3.6.0", "date-fns-tz": "^3.2.0", "drizzle-orm": "^0.45.2", + "fast-xml-parser": "^5.5.10", "jsonwebtoken": "^9.0.3", "leaflet": "^1.9.4", "leaflet.heat": "^0.2.0", diff --git a/src/pages/api/import-shared.ts b/src/pages/api/import-shared.ts index 831d02e..c2238a3 100644 --- a/src/pages/api/import-shared.ts +++ b/src/pages/api/import-shared.ts @@ -1,4 +1,5 @@ import type { NextApiRequest, NextApiResponse } from "next"; +import { detectNextcloud, checkPasswordProtection, listNextcloudFiles } from "./import-shared/nextcloud"; interface IAlbumContributorCount { userId: string; @@ -83,6 +84,7 @@ interface IImportSharedAlbum { } interface IImportSharedResponse { + platform: "immich" | "nextcloud"; link: string; origin: string; key: string; @@ -165,9 +167,89 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) return respondWithError(res, 400, "A shared album link is required"); } + // Detect platform from the URL + const nextcloud = detectNextcloud(link); + + if (nextcloud) { + // ── Nextcloud flow ── + try { + if (!password) { + const isProtected = await checkPasswordProtection(nextcloud.baseUrl, nextcloud.token); + if (isProtected) { + return res.status(401).json({ error: "PASSWORD_REQUIRED" }); + } + } + + const { metadata, files } = await listNextcloudFiles(nextcloud.baseUrl, nextcloud.token, password ?? ""); + + const assets: IImportSharedAsset[] = files.map((file) => ({ + id: file.relativePath, + originalFileName: file.fileName, + type: file.type, + fileCreatedAt: file.lastModified ?? null, + localDateTime: null, + description: null, + location: null, + thumbhash: file.blurhash ?? null, + fileSizeInByte: file.size || null, + })); + + // Derive date range from file modification dates + const dates = files + .map((f) => f.lastModified ? new Date(f.lastModified).getTime() : NaN) + .filter((t) => !Number.isNaN(t)); + const startDate = dates.length > 0 ? new Date(Math.min(...dates)).toISOString() : null; + const endDate = dates.length > 0 ? new Date(Math.max(...dates)).toISOString() : null; + + const albumName = metadata.displayName || "Nextcloud Share"; + const owner = metadata.ownerId + ? { name: metadata.ownerId, email: null } + : null; + + const responseBody: IImportSharedResponse = { + platform: "nextcloud", + link: link.trim(), + origin: nextcloud.baseUrl, + key: nextcloud.token, + sharedLink: { + id: nextcloud.token, + type: "FOLDER", + createdAt: new Date().toISOString(), + expiresAt: null, + allowUpload: false, + allowDownload: true, + showMetadata: true, + }, + album: { + albumName, + assetCount: assets.length, + owner, + description: null, + startDate, + endDate, + shared: true, + hasSharedLink: true, + lastModifiedAssetTimestamp: endDate, + order: null, + contributorCounts: [], + assets, + }, + }; + + return res.status(200).json(responseBody); + } catch (error: any) { + if (error?.code === "PASSWORD_REQUIRED") { + return res.status(401).json({ error: "PASSWORD_REQUIRED" }); + } + console.error("Nextcloud import shared error", error); + return respondWithError(res, 500, error?.message ?? "Failed to fetch Nextcloud share"); + } + } + + // ── Immich flow ── const parsed = parseSharedLink(link); if (!parsed) { - return respondWithError(res, 400, "Invalid Immich share link"); + return respondWithError(res, 400, "Invalid share link. Supported: Immich or Nextcloud share URLs."); } const authQuery = password @@ -216,6 +298,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) } const responseBody: IImportSharedResponse = { + platform: "immich", link: parsed.original, origin: parsed.origin, key: parsed.key, diff --git a/src/pages/api/import-shared/helpers.ts b/src/pages/api/import-shared/helpers.ts index 48fca46..8f4968f 100644 --- a/src/pages/api/import-shared/helpers.ts +++ b/src/pages/api/import-shared/helpers.ts @@ -1,6 +1,7 @@ import { ENV } from "@/config/environment"; export const POWER_TOOLS_TAG_NAME = "immich-power-tools"; +export const DEVICE_ID = "immich-power-tools"; export type HeadersRecord = Record; @@ -40,6 +41,187 @@ export const ensurePowerToolsTag = async (headers: HeadersRecord) => { return (await createResponse.json()) as ImmichTagResponse; }; +export 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"; + } +}; + +export 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; + } +}; + +export 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"; + } +}; + +export 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; +}; + +export 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"}` + ); + } +}; + +export interface SharedAssetPayload { + id: string; + originalFileName?: string; + type: string; + fileCreatedAt?: string | null; + localDateTime?: string | null; + duration?: string | null; + isFavorite?: boolean; + isArchived?: boolean; +} + +export interface DownloadedAssetPayload { + buffer: Buffer; + fileName: string; + contentType: string | null; +} + +export const uploadAssetBuffer = async ( + asset: SharedAssetPayload, + payload: DownloadedAssetPayload, + uploadHeaders: HeadersRecord, + jsonHeaders: HeadersRecord, + deviceAssetId: string, + 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", deviceAssetId); + 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) { + await tagAssetWithPowerTools(tagId, uploadedId, jsonHeaders); + } + return uploadedId; +}; + export const tagAssetWithPowerTools = async ( tagId: string, assetId: string, diff --git a/src/pages/api/import-shared/nextcloud.ts b/src/pages/api/import-shared/nextcloud.ts new file mode 100644 index 0000000..35b7bbb --- /dev/null +++ b/src/pages/api/import-shared/nextcloud.ts @@ -0,0 +1,260 @@ +import { XMLParser } from "fast-xml-parser"; +import { inferAssetTypeFromName } from "./helpers"; + +const NEXTCLOUD_SHARE_PATTERN = + /^(https?:\/\/[^/]+?)(?:\/index\.php)?\/s\/([a-zA-Z0-9]{10,32})\/?$/; + +export interface NextcloudDetection { + baseUrl: string; + token: string; +} + +export interface NextcloudShareMetadata { + displayName: string | null; + ownerId: string | null; + totalSize: number; +} + +export interface NextcloudFileEntry { + relativePath: string; + fileName: string; + contentType: string | null; + size: number; + lastModified: string | null; + type: "IMAGE" | "VIDEO"; + blurhash: string | null; + fileId: string | null; +} + +export interface NextcloudListResult { + metadata: NextcloudShareMetadata; + files: NextcloudFileEntry[]; +} + +/** + * Detect if a URL is a Nextcloud public share link. + * Returns { baseUrl, token } or null. + */ +export const detectNextcloud = (url: string): NextcloudDetection | null => { + const match = url.trim().match(NEXTCLOUD_SHARE_PATTERN); + if (!match) return null; + return { baseUrl: match[1], token: match[2] }; +}; + +const buildBasicAuth = (username: string, password: string): string => { + const encoded = Buffer.from(`${username}:${password}`).toString("base64"); + return `Basic ${encoded}`; +}; + +const PROPFIND_BODY = ` + + + + + + + + + + + + +`; + +/** + * Check if a Nextcloud share is password-protected. + * Returns true if password is required. + */ +export const checkPasswordProtection = async ( + baseUrl: string, + token: string +): Promise => { + const webdavUrl = `${baseUrl}/public.php/dav/files/${token}/`; + const response = await fetch(webdavUrl, { + method: "PROPFIND", + headers: { + Authorization: buildBasicAuth(token, ""), + Depth: "0", + "Content-Type": "application/xml", + }, + body: "", + }); + + if (response.status === 207) return false; + if (response.status === 401) return true; + + throw new Error( + `Unexpected response from Nextcloud (status ${response.status})` + ); +}; + +const IMAGE_EXTENSIONS = new Set([ + "jpg", "jpeg", "jfif", "png", "heic", "heif", "webp", + "gif", "tif", "tiff", "bmp", "svg", "avif", "jxl", + "cr2", "nef", "arw", "dng", "raf", "orf", "rw2", +]); + +const VIDEO_EXTENSIONS = new Set([ + "mp4", "mov", "m4v", "avi", "mkv", "webm", + "3gp", "3g2", "mts", "m2ts", +]); + +const isMediaFile = (fileName: string): boolean => { + const ext = fileName.split(".").pop()?.toLowerCase(); + if (!ext) return false; + return IMAGE_EXTENSIONS.has(ext) || VIDEO_EXTENSIONS.has(ext); +}; + +/** + * List all files in a Nextcloud public share, flattened (no directories). + * Only returns image and video files. Also extracts share-level metadata. + */ +export const listNextcloudFiles = async ( + baseUrl: string, + token: string, + password: string = "" +): Promise => { + const webdavUrl = `${baseUrl}/public.php/dav/files/${token}/`; + const response = await fetch(webdavUrl, { + method: "PROPFIND", + headers: { + Authorization: buildBasicAuth(token, password), + Depth: "infinity", + "Content-Type": "application/xml", + }, + body: PROPFIND_BODY, + }); + + if (response.status === 401) { + throw Object.assign(new Error("PASSWORD_REQUIRED"), { code: "PASSWORD_REQUIRED" }); + } + + if (!response.ok && response.status !== 207) { + throw new Error(`Failed to list Nextcloud share (status ${response.status})`); + } + + const xml = await response.text(); + const parser = new XMLParser({ + ignoreAttributes: false, + removeNSPrefix: true, + }); + const parsed = parser.parse(xml); + + const multistatus = parsed.multistatus; + if (!multistatus) { + throw new Error("Invalid PROPFIND response from Nextcloud"); + } + + // Normalize responses to array + let responses = multistatus.response; + if (!Array.isArray(responses)) { + responses = responses ? [responses] : []; + } + + const prefix = `/public.php/dav/files/${token}/`; + const files: NextcloudFileEntry[] = []; + const metadata: NextcloudShareMetadata = { + displayName: null, + ownerId: null, + totalSize: 0, + }; + + for (const resp of responses) { + const href = typeof resp.href === "string" ? decodeURIComponent(resp.href) : ""; + // Get the first propstat with 200 status (skip 404 propstats) + const propstats = Array.isArray(resp.propstat) ? resp.propstat : [resp.propstat]; + const okPropstat = propstats.find( + (ps: any) => ps?.status?.includes("200") ?? false + ) ?? propstats[0]; + const prop = okPropstat?.prop; + if (!prop) continue; + + // Check if it's a directory (collection) + const resourceType = prop.resourcetype; + const isCollection = + resourceType != null && + typeof resourceType === "object" && + "collection" in resourceType; + + // Extract relative path + const prefixIdx = href.indexOf(prefix); + const relativePath = + prefixIdx >= 0 ? href.slice(prefixIdx + prefix.length) : href; + + // Root collection — extract share metadata + if (isCollection && !relativePath) { + metadata.displayName = + typeof prop.displayname === "string" ? prop.displayname : null; + metadata.ownerId = + typeof prop["owner-id"] === "string" ? prop["owner-id"] : null; + const totalSize = + typeof prop.size === "number" + ? prop.size + : parseInt(prop.size || "0", 10) || 0; + metadata.totalSize = totalSize; + continue; + } + + if (isCollection) continue; // skip subdirectories + if (!relativePath) continue; + + const fileName = relativePath.split("/").pop() || relativePath; + + // Filter to media files only + if (!isMediaFile(fileName)) continue; + + const contentType = + typeof prop.getcontenttype === "string" ? prop.getcontenttype : null; + const size = + typeof prop.getcontentlength === "number" + ? prop.getcontentlength + : parseInt(prop.getcontentlength || "0", 10) || 0; + // Convert RFC 2822 date to ISO 8601 (Immich requires ISO format for fileCreatedAt) + const rawLastModified = + typeof prop.getlastmodified === "string" ? prop.getlastmodified : null; + let lastModified: string | null = null; + if (rawLastModified) { + const parsed = new Date(rawLastModified); + lastModified = Number.isNaN(parsed.getTime()) ? rawLastModified : parsed.toISOString(); + } + const blurhash = + typeof prop["metadata-blurhash"] === "string" + ? prop["metadata-blurhash"] + : null; + const fileId = + prop.fileid != null ? String(prop.fileid) : null; + + files.push({ + relativePath, + fileName, + contentType, + size, + lastModified, + type: inferAssetTypeFromName(fileName), + blurhash, + fileId, + }); + } + + return { metadata, files }; +}; + +/** + * Build a Nextcloud public preview URL for a shared file. + */ +export const buildNextcloudPreviewUrl = ( + baseUrl: string, + token: string, + relativePath: string, + width: number, + height: number +): string => { + const params = new URLSearchParams({ + file: `/${relativePath}`, + x: String(width), + y: String(height), + mimeFallback: "true", + a: "0", + }); + return `${baseUrl}/apps/files_sharing/publicpreview/${token}?${params.toString()}`; +}; diff --git a/src/pages/api/import-shared/thumbnail.ts b/src/pages/api/import-shared/thumbnail.ts index 101a7c6..ae120c6 100644 --- a/src/pages/api/import-shared/thumbnail.ts +++ b/src/pages/api/import-shared/thumbnail.ts @@ -7,7 +7,7 @@ const respondWithError = (res: NextApiResponse, status: number, message: string) const ALLOWED_SIZES = new Set(["thumbnail", "preview"]); const validateParams = (req: NextApiRequest) => { - const { origin, assetId, key, thumbhash, size } = req.query; + const { origin, assetId, key, thumbhash, size, platform } = req.query; if (!origin || Array.isArray(origin)) { return { error: "Query parameter 'origin' is required" }; @@ -30,6 +30,8 @@ const validateParams = (req: NextApiRequest) => { return { error: "Query parameter 'size' must be either 'thumbnail' or 'preview'" }; } + const resolvedPlatform = typeof platform === "string" ? platform : "immich"; + try { const parsedOrigin = new URL(origin); return { @@ -37,6 +39,7 @@ const validateParams = (req: NextApiRequest) => { assetId, key, size: resolvedSize, + platform: resolvedPlatform, thumbhash: typeof thumbhash === "string" ? thumbhash : undefined, }; } catch (_err) { @@ -55,20 +58,34 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) return respondWithError(res, 400, params.error as string); } - const search = new URLSearchParams({ key: params.key, size: params.size }); - if (params.thumbhash) { - search.set("c", params.thumbhash); - } - try { - const response = await fetch( - `${params.origin}/api/assets/${params.assetId}/thumbnail?${search.toString()}`, - { - headers: { - accept: "image/*", - }, + let targetUrl: string; + + if (params.platform === "nextcloud") { + // Nextcloud public preview endpoint + const dimensions = params.size === "preview" ? { x: 1080, y: 1080 } : { x: 250, y: 250 }; + const previewParams = new URLSearchParams({ + file: `/${params.assetId}`, + x: String(dimensions.x), + y: String(dimensions.y), + mimeFallback: "true", + a: "0", + }); + targetUrl = `${params.origin}/apps/files_sharing/publicpreview/${params.key}?${previewParams.toString()}`; + } else { + // Immich thumbnail endpoint + const search = new URLSearchParams({ key: params.key, size: params.size }); + if (params.thumbhash) { + search.set("c", params.thumbhash); } - ); + targetUrl = `${params.origin}/api/assets/${params.assetId}/thumbnail?${search.toString()}`; + } + + const response = await fetch(targetUrl, { + headers: { + accept: "image/*", + }, + }); if (!response.ok) { return respondWithError(res, response.status, "Failed to fetch thumbnail"); diff --git a/src/pages/api/import-shared/video.ts b/src/pages/api/import-shared/video.ts index 6745749..876db05 100644 --- a/src/pages/api/import-shared/video.ts +++ b/src/pages/api/import-shared/video.ts @@ -6,7 +6,7 @@ const respondWithError = (res: NextApiResponse, status: number, message: string) }; const validateParams = (req: NextApiRequest) => { - const { origin, assetId, key, thumbhash } = req.query; + const { origin, assetId, key, thumbhash, platform, password } = req.query; if (!origin || Array.isArray(origin)) { return { error: "Query parameter 'origin' is required" }; @@ -18,12 +18,17 @@ const validateParams = (req: NextApiRequest) => { return { error: "Query parameter 'key' is required" }; } + const resolvedPlatform = typeof platform === "string" ? platform : "immich"; + const resolvedPassword = typeof password === "string" ? password : ""; + try { const parsedOrigin = new URL(origin); return { origin: parsedOrigin.origin, assetId, key, + platform: resolvedPlatform, + password: resolvedPassword, thumbhash: typeof thumbhash === "string" ? thumbhash : undefined, }; } catch (_err) { @@ -50,21 +55,36 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) return respondWithError(res, 400, params.error as string); } - const search = new URLSearchParams({ key: params.key }); - if (params.thumbhash) { - search.set("c", params.thumbhash); - } - - const targetUrl = `${params.origin}/api/assets/${params.assetId}/video/playback?${search.toString()}`; - try { - const upstream = await fetch(targetUrl, { - headers: { + let targetUrl: string; + let fetchHeaders: Record; + + if (params.platform === "nextcloud") { + // Nextcloud: stream via WebDAV + targetUrl = `${params.origin}/public.php/dav/files/${params.key}/${params.assetId}`; + const basicAuth = Buffer.from(`${params.key}:${params.password}`).toString("base64"); + fetchHeaders = { + Authorization: `Basic ${basicAuth}`, + accept: "*/*", + ...(req.headers.range ? { range: req.headers.range } : {}), + }; + } else { + // Immich: video playback endpoint + const search = new URLSearchParams({ key: params.key }); + if (params.thumbhash) { + search.set("c", params.thumbhash); + } + targetUrl = `${params.origin}/api/assets/${params.assetId}/video/playback?${search.toString()}`; + fetchHeaders = { accept: "video/*", ...(req.headers.range ? { range: req.headers.range } : {}), referer: req.headers.referer?.toString() ?? params.origin, "sec-fetch-dest": "video", - }, + }; + } + + const upstream = await fetch(targetUrl, { + headers: fetchHeaders, }); if (!upstream.ok && upstream.status !== 206) { diff --git a/src/pages/assets/import-shared.tsx b/src/pages/assets/import-shared.tsx index 3fb6054..e6ddb98 100644 --- a/src/pages/assets/import-shared.tsx +++ b/src/pages/assets/import-shared.tsx @@ -84,6 +84,7 @@ interface IImportSharedAlbum { } interface IImportSharedResponse { + platform: "immich" | "nextcloud"; link: string; origin: string; key: string; @@ -295,7 +296,10 @@ export default function ImportSharedPage() { setImportAllLoading(false); setAlbumImportMode("album"); setNeedsPassword(false); - setShareLinkPassword(""); + // Keep password for Nextcloud (needed for import job and video proxy) + if (payload.platform !== "nextcloud") { + setShareLinkPassword(""); + } } catch (err: any) { const errorCode = err?.error ?? err?.message ?? "Unexpected error"; if (errorCode === "PASSWORD_REQUIRED") { @@ -321,6 +325,7 @@ export default function ImportSharedPage() { origin: sharedData.origin, key: sharedData.key, size: "thumbnail", + platform: sharedData.platform, }); if (asset.thumbhash) params.set("thumbhash", asset.thumbhash); return `/api/import-shared/thumbnail?${params.toString()}`; @@ -332,8 +337,12 @@ export default function ImportSharedPage() { assetId: asset.id, origin: sharedData.origin, key: sharedData.key, + platform: sharedData.platform, }); if (asset.thumbhash) params.set("thumbhash", asset.thumbhash); + if (sharedData.platform === "nextcloud" && shareLinkPassword) { + params.set("password", shareLinkPassword); + } return `/api/import-shared/video?${params.toString()}`; }; @@ -344,6 +353,7 @@ export default function ImportSharedPage() { origin: sharedData.origin, key: sharedData.key, size: "preview", + platform: sharedData.platform, }); if (asset.thumbhash) params.set("thumbhash", asset.thumbhash); return `/api/import-shared/thumbnail?${params.toString()}`; @@ -377,10 +387,16 @@ export default function ImportSharedPage() { setUploadBanner(null); setImportAllLoading(true); try { + const isNextcloud = sharedData.platform === "nextcloud"; + const urlConfig: Record = { key: sharedData.key }; + if (isNextcloud && shareLinkPassword) { + urlConfig.password = shareLinkPassword; + } + const job = await createImportJob({ - platform: "immich", + platform: sharedData.platform, url: sharedData.origin, - urlConfig: { key: sharedData.key }, + urlConfig, importData: { albumOptions, tagAssets }, assets: uploadableAssets.map((asset) => ({ id: asset.id, @@ -391,6 +407,7 @@ export default function ImportSharedPage() { duration: asset.duration ?? null, isFavorite: asset.isFavorite ?? false, isArchived: asset.isArchived ?? false, + ...(isNextcloud ? { relativePath: asset.id } : {}), })), }); setActiveJobId(job.jobId); @@ -454,7 +471,7 @@ export default function ImportSharedPage() {

Import a shared album

- Paste a public shared link from another Immich instance to import its assets into yours. + Paste a public shared link from Immich or Nextcloud to import its assets into your Immich instance.

@@ -471,7 +488,7 @@ export default function ImportSharedPage() { setShareLinkPassword(""); } }} - placeholder="https://demo.immich.app/share/..." + placeholder="Immich or Nextcloud share URL" type="url" required className="pl-9" @@ -507,24 +524,31 @@ export default function ImportSharedPage() {

Previous imports

-
+
{importHistory.jobs.map((job) => { let jobImportData: Record = {}; try { jobImportData = JSON.parse(job.importData); } catch { /* ignore */ } const albumOpts = jobImportData.albumOptions as { albumName?: string } | undefined; const albumName = albumOpts?.albumName; + + const borderColor = job.status === "completed" + ? "border-l-emerald-500" + : job.status === "failed" + ? "border-l-destructive" + : "border-l-amber-400"; + const statusIcon = job.status === "completed" ? ( ) : job.status === "failed" ? ( ) : ( - + ); return (
setDetailJobId(job.id)} > {statusIcon} @@ -532,11 +556,21 @@ export default function ImportSharedPage() {

{albumName || job.url}

-

- {job.uploadedCount} imported - {job.skippedCount > 0 && ` · ${job.skippedCount} skipped`} - {job.failedCount > 0 && ` · ${job.failedCount} failed`} -

+
+ + {job.uploadedCount} imported + + {job.skippedCount > 0 && ( + + {job.skippedCount} skipped + + )} + {job.failedCount > 0 && ( + + {job.failedCount} failed + + )} +
{formatDateOnly(job.createdAt)} diff --git a/src/workers/import/processors/immich-shared-link.ts b/src/workers/import/processors/immich-shared-link.ts index 3784d3a..ed6b1aa 100644 --- a/src/workers/import/processors/immich-shared-link.ts +++ b/src/workers/import/processors/immich-shared-link.ts @@ -5,142 +5,22 @@ import { ENV } from "@/config/environment"; import { ensurePowerToolsTag, HeadersRecord, + inferAssetTypeFromName, + parseFileNameFromDisposition, + guessContentType, + createImmichAlbum, + addAssetToAlbum, + uploadAssetBuffer, + SharedAssetPayload, + DownloadedAssetPayload, + DEVICE_ID, } 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, + asset: SharedAssetPayload, origin: string, key: string ): Promise => { @@ -157,59 +37,6 @@ const downloadSharedAsset = async ( 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; @@ -315,7 +142,7 @@ export class ImmichSharedLinkProcessor implements ImportProcessor { const headers = context.headers as HeadersRecord; // Parse item metadata - let asset: ISharedAssetPayload; + let asset: SharedAssetPayload; try { const parsed = JSON.parse(item.itemData); asset = { @@ -351,7 +178,14 @@ export class ImmichSharedLinkProcessor implements ImportProcessor { 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); + const immichId = await uploadAssetBuffer( + asset, + downloaded, + uploadHeaders as HeadersRecord, + headers as HeadersRecord, + makeDeviceAssetId(asset.id), + tagId + ); // Add to album if one was resolved during setup if (context.albumId) { diff --git a/src/workers/import/processors/index.ts b/src/workers/import/processors/index.ts index 00cc5d7..19c1d3d 100644 --- a/src/workers/import/processors/index.ts +++ b/src/workers/import/processors/index.ts @@ -1,4 +1,6 @@ import { registerProcessor } from "../registry"; import { ImmichSharedLinkProcessor } from "./immich-shared-link"; +import { NextcloudSharedLinkProcessor } from "./nextcloud-shared-link"; registerProcessor("immich", new ImmichSharedLinkProcessor()); +registerProcessor("nextcloud", new NextcloudSharedLinkProcessor()); diff --git a/src/workers/import/processors/nextcloud-shared-link.ts b/src/workers/import/processors/nextcloud-shared-link.ts new file mode 100644 index 0000000..6cdd62f --- /dev/null +++ b/src/workers/import/processors/nextcloud-shared-link.ts @@ -0,0 +1,214 @@ +import { eq } from "drizzle-orm"; +import { appDb } from "@/db"; +import { importJobItems } from "@/db/schema"; +import { ENV } from "@/config/environment"; +import { + ensurePowerToolsTag, + HeadersRecord, + createImmichAlbum, + addAssetToAlbum, + uploadAssetBuffer, + SharedAssetPayload, + DownloadedAssetPayload, + DEVICE_ID, + parseFileNameFromDisposition, +} from "@/pages/api/import-shared/helpers"; +import type { ImportJob, ImportJobItem, ImportProcessor, ProcessorContext, SetupResult } from "../types"; + +const makeDeviceAssetId = (relativePath: string) => `shared-nc-${relativePath}`; + +const buildBasicAuth = (username: string, password: string): string => { + const encoded = Buffer.from(`${username}:${password}`).toString("base64"); + return `Basic ${encoded}`; +}; + +const downloadNextcloudAsset = async ( + baseUrl: string, + token: string, + relativePath: string, + password: string +): Promise => { + const encodedPath = relativePath + .split("/") + .map((segment) => encodeURIComponent(segment)) + .join("/"); + const url = `${baseUrl}/public.php/dav/files/${token}/${encodedPath}`; + const response = await fetch(url, { + method: "GET", + headers: { + Authorization: buildBasicAuth(token, password), + accept: "*/*", + }, + }); + + if (!response.ok) { + throw new Error(`Failed to download ${relativePath} from Nextcloud (status ${response.status})`); + } + + const buffer = Buffer.from(await response.arrayBuffer()); + const disposition = response.headers.get("content-disposition"); + const inferredName = parseFileNameFromDisposition(disposition); + const fileName = inferredName ?? relativePath.split("/").pop() ?? "unknown"; + + return { + buffer, + fileName, + contentType: response.headers.get("content-type"), + }; +}; + +export class NextcloudSharedLinkProcessor implements ImportProcessor { + async setup(job: ImportJob, context: ProcessorContext): Promise { + const headers = context.headers as HeadersRecord; + + 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 jsonHeaders: HeadersRecord = { ...headers, "Content-Type": "application/json" }; + const checkResponse = await fetch(`${ENV.IMMICH_URL}/api/assets/exist`, { + method: "POST", + headers: jsonHeaders, + 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(`[NextcloudProcessor] Failed to check existing assets (status ${checkResponse.status})`); + } + } catch (error) { + console.warn("[NextcloudProcessor] Unable to check existing assets", error); + } + } + + // Parse importData for album options + let importData: Record = {}; + try { + importData = JSON.parse(job.importData); + } catch { + // ignore + } + + const albumOptions = importData.albumOptions as { + createAlbum?: boolean; + albumName?: string; + addToAlbumId?: string; + } | undefined; + + let albumId: string | undefined; + const jsonHeaders: HeadersRecord = { ...headers, "Content-Type": "application/json" }; + + if (albumOptions?.createAlbum) { + const desiredAlbumName = + albumOptions.albumName?.trim() || `Nextcloud import ${new Date().toISOString()}`; + albumId = await createImmichAlbum(desiredAlbumName, jsonHeaders); + console.log(`[NextcloudProcessor] 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; + + const tagAssets = importData.tagAssets !== false; + if (tagAssets) { + try { + const tag = await ensurePowerToolsTag(jsonHeaders); + importDataPatch.tagId = tag.id; + } catch (err) { + console.warn("[NextcloudProcessor] 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: SharedAssetPayload; + let relativePath: string; + try { + const parsed = JSON.parse(item.itemData); + relativePath = parsed.relativePath ?? item.assetId; + asset = { + id: item.assetId, + originalFileName: parsed.originalFileName, + type: parsed.type ?? "IMAGE", + fileCreatedAt: parsed.fileCreatedAt ?? null, + localDateTime: parsed.localDateTime ?? null, + duration: parsed.duration ?? null, + isFavorite: false, + isArchived: false, + }; + } catch { + relativePath = item.assetId; + asset = { id: item.assetId, type: "IMAGE" }; + } + + // Parse urlConfig for token and password + let urlConfig: Record = {}; + try { + urlConfig = JSON.parse(job.urlConfig); + } catch { + // ignore + } + const token = typeof urlConfig.key === "string" ? urlConfig.key : ""; + const password = typeof urlConfig.password === "string" ? urlConfig.password : ""; + + // Download the asset from Nextcloud + const downloaded = await downloadNextcloudAsset(job.url, token, relativePath, password); + + // Strip Content-Type for multipart upload + const { ["Content-Type"]: _omit, ...uploadHeaders } = headers; + + // Read tagId from importData set during setup() + 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, + makeDeviceAssetId(relativePath), + tagId + ); + + // Add to album if one was resolved during setup + if (context.albumId) { + const jsonHeaders: HeadersRecord = { ...headers, "Content-Type": "application/json" }; + await addAssetToAlbum(context.albumId, immichId, jsonHeaders); + } + + return { immichId }; + } +}