mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat(api): wire AI route handlers to SSE progress via clientJobId
Extract clientJobId from multipart form data in all 5 AI route handlers (remove-background, upscale, blur-faces, erase-object, ocr) and forward progress callbacks to the SSE system via updateSingleFileProgress.
This commit is contained in:
@@ -4,6 +4,7 @@ import { writeFile } from "node:fs/promises";
|
||||
import { join, basename } from "node:path";
|
||||
import { upscale } from "@stirling-image/ai";
|
||||
import { createWorkspace } from "../../lib/workspace.js";
|
||||
import { updateSingleFileProgress } from "../progress.js";
|
||||
|
||||
/**
|
||||
* AI image upscaling route.
|
||||
@@ -16,6 +17,7 @@ export function registerUpscale(app: FastifyInstance) {
|
||||
let fileBuffer: Buffer | null = null;
|
||||
let filename = "image";
|
||||
let settingsRaw: string | null = null;
|
||||
let clientJobId: string | null = null;
|
||||
|
||||
try {
|
||||
const parts = request.parts();
|
||||
@@ -29,6 +31,8 @@ export function registerUpscale(app: FastifyInstance) {
|
||||
filename = basename(part.filename ?? "image");
|
||||
} else if (part.fieldname === "settings") {
|
||||
settingsRaw = part.value as string;
|
||||
} else if (part.fieldname === "clientJobId") {
|
||||
clientJobId = part.value as string;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -54,10 +58,22 @@ export function registerUpscale(app: FastifyInstance) {
|
||||
await writeFile(inputPath, fileBuffer);
|
||||
|
||||
// Process
|
||||
const onProgress = clientJobId
|
||||
? (percent: number, stage: string) => {
|
||||
updateSingleFileProgress({
|
||||
jobId: clientJobId!,
|
||||
phase: "processing",
|
||||
stage,
|
||||
percent,
|
||||
});
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const result = await upscale(
|
||||
fileBuffer,
|
||||
join(workspacePath, "output"),
|
||||
{ scale },
|
||||
onProgress,
|
||||
);
|
||||
|
||||
// Save output
|
||||
@@ -66,6 +82,14 @@ export function registerUpscale(app: FastifyInstance) {
|
||||
const outputPath = join(workspacePath, "output", outputFilename);
|
||||
await writeFile(outputPath, result.buffer);
|
||||
|
||||
if (clientJobId) {
|
||||
updateSingleFileProgress({
|
||||
jobId: clientJobId,
|
||||
phase: "complete",
|
||||
percent: 100,
|
||||
});
|
||||
}
|
||||
|
||||
return reply.send({
|
||||
jobId,
|
||||
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(outputFilename)}`,
|
||||
|
||||
Reference in New Issue
Block a user