fix: reset stuck items on resume, validate platform, move ensurePowerToolsTag to setup, safe JSON.parse

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
VarunandClaude Sonnet 4.6 committed 2026-04-03 20:26:56 +05:30
1 parent a640fe6555
commit 440b50340f
4 files changed
+41 -10

No files matched your search

+7
View File
@@ -4,6 +4,7 @@ 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 !== "POST") {
@@ -24,6 +25,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
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({
+6 -1
View File
@@ -209,7 +209,12 @@ export default function ImportSharedPage() {
setImportAllLoading(false);
setImportAllDialogOpen(false);
const importData = JSON.parse(job.importData);
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;
@@ -285,11 +285,12 @@ export class ImmichSharedLinkProcessor implements ImportProcessor {
albumId = albumOptions.addToAlbumId.trim();
}
return {
skipAssetIds,
albumId,
importDataPatch: albumId ? { albumId } : undefined,
};
const tag = await ensurePowerToolsTag({ ...context.headers, "Content-Type": "application/json" } as HeadersRecord);
const importDataPatch: Record<string, unknown> = {};
if (albumId) importDataPatch.albumId = albumId;
importDataPatch.tagId = tag.id;
return { skipAssetIds, albumId, importDataPatch };
}
async processItem(
@@ -332,9 +333,12 @@ export class ImmichSharedLinkProcessor implements ImportProcessor {
// Strip Content-Type for multipart upload (same pattern as upload-all.ts)
const { ["Content-Type"]: _omit, ...uploadHeaders } = headers;
// Ensure the power-tools tag exists and upload
const tag = await ensurePowerToolsTag(headers);
const immichId = await uploadAssetBuffer(asset, downloaded, uploadHeaders, headers, tag.id);
// Read tagId from importData set during setup()
const importData = JSON.parse(job.importData) as { albumId?: string; tagId?: string };
const tagId = importData.tagId;
if (!tagId) throw new Error("tagId missing from job importData — setup() may not have completed");
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) {
+16 -1
View File
@@ -21,10 +21,25 @@ export async function getJobItems(jobId: string) {
}
export async function resumePendingJobs() {
// Reset jobs that were mid-flight when the server last crashed.
// 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" })