fix(upscale): overhaul UI, fix AI pipeline bugs, add format support

- Replace Auto/AI/Fast buttons with Fast/Balanced/Best (consistent with other tools)
- Rename "Denoise" to "Noise Reduction" with explanatory subtitle
- Change output format from 3 buttons to dropdown with all formats (PNG, JPG, WebP, AVIF, TIFF, GIF, HEIC, HEIF)
- Add HEIC/HEIF input decoding (was missing unlike other tools)
- Add HEIC/HEIF/AVIF output conversion via Sharp and heif-enc
- Generate browser-compatible WebP preview for non-previewable output formats
- Fix torchvision compatibility shim so Real-ESRGAN actually loads (was silently falling back to Lanczos)
- Fix denoise crash: Image.fromarray() instead of type(img).fromarray()
- Redirect stdout for entire AI pipeline to prevent library messages corrupting JSON output
- Add GFPGAN model download for face enhancement
- Use batch endpoint for multi-file uploads (enables Download All ZIP)
This commit is contained in:
Siddharth Kumar Sah
2026-04-12 21:22:55 +08:00
parent ed5f71e2fc
commit f2e17d2d44
4 changed files with 304 additions and 216 deletions
+61 -4
View File
@@ -3,9 +3,11 @@ import { writeFile } from "node:fs/promises";
import { basename, join } from "node:path";
import { upscale } from "@stirling-image/ai";
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import sharp from "sharp";
import { z } from "zod";
import { autoOrient } from "../../lib/auto-orient.js";
import { validateImageBuffer } from "../../lib/file-validation.js";
import { decodeHeic, encodeHeic } from "../../lib/heic-converter.js";
import { createWorkspace } from "../../lib/workspace.js";
import { updateSingleFileProgress } from "../progress.js";
import { registerToolProcessFn } from "../tool-factory.js";
@@ -66,6 +68,11 @@ export function registerUpscale(app: FastifyInstance) {
"Starting upscale",
);
// Decode HEIC/HEIF input via system decoder
if (validation.format === "heif") {
fileBuffer = await decodeHeic(fileBuffer);
}
// Auto-orient to fix EXIF rotation before upscaling
fileBuffer = await autoOrient(fileBuffer);
@@ -76,6 +83,12 @@ export function registerUpscale(app: FastifyInstance) {
const inputPath = join(workspacePath, "input", filename);
await writeFile(inputPath, fileBuffer);
// Determine which format the Python sidecar should produce.
// Formats that need Node.js-side conversion (HEIC/HEIF via heif-enc,
// AVIF via Sharp) are produced as PNG first, then converted below.
const needsNodeConversion = ["heic", "heif", "avif"].includes(format);
const pythonFormat = needsNodeConversion ? "png" : format;
// Process
const jobIdForProgress = clientJobId;
const onProgress = jobIdForProgress
@@ -92,15 +105,58 @@ export function registerUpscale(app: FastifyInstance) {
const result = await upscale(
fileBuffer,
join(workspacePath, "output"),
{ scale, model, faceEnhance, denoise, format, quality: outputQuality },
{ scale, model, faceEnhance, denoise, format: pythonFormat, quality: outputQuality },
onProgress,
);
// Convert to final format if needed (HEIC/HEIF/AVIF)
let outputBuffer = result.buffer;
let finalFormat = result.format;
if (needsNodeConversion) {
if (format === "heic" || format === "heif") {
outputBuffer = await encodeHeic(result.buffer, outputQuality);
finalFormat = format;
} else if (format === "avif") {
outputBuffer = await sharp(result.buffer).avif({ quality: outputQuality }).toBuffer();
finalFormat = "avif";
}
}
// Save output with correct extension for the chosen format
const ext = result.format === "jpeg" ? "jpg" : result.format === "webp" ? "webp" : "png";
const EXT_MAP: Record<string, string> = {
jpeg: "jpg",
jpg: "jpg",
png: "png",
webp: "webp",
tiff: "tiff",
gif: "gif",
avif: "avif",
heic: "heic",
heif: "heif",
};
const ext = EXT_MAP[finalFormat] || "png";
const outputFilename = `${filename.replace(/\.[^.]+$/, "")}_${scale}x.${ext}`;
const outputPath = join(workspacePath, "output", outputFilename);
await writeFile(outputPath, result.buffer);
await writeFile(outputPath, outputBuffer);
// Generate browser-compatible preview for non-previewable formats
const BROWSER_PREVIEWABLE = new Set(["png", "jpg", "jpeg", "webp", "gif", "avif", "bmp"]);
let previewUrl: string | undefined;
if (!BROWSER_PREVIEWABLE.has(finalFormat)) {
try {
// For HEIC/HEIF, decode first since Sharp can't read HEVC
const previewInput =
finalFormat === "heic" || finalFormat === "heif"
? await decodeHeic(outputBuffer)
: outputBuffer;
const previewBuffer = await sharp(previewInput).webp({ quality: 80 }).toBuffer();
const previewPath = join(workspacePath, "output", "preview.webp");
await writeFile(previewPath, previewBuffer);
previewUrl = `/api/v1/download/${jobId}/preview.webp`;
} catch {
// Non-fatal - frontend will show fallback
}
}
if (clientJobId) {
updateSingleFileProgress({
@@ -113,8 +169,9 @@ export function registerUpscale(app: FastifyInstance) {
return reply.send({
jobId,
downloadUrl: `/api/v1/download/${jobId}/${encodeURIComponent(outputFilename)}`,
previewUrl,
originalSize: fileBuffer.length,
processedSize: result.buffer.length,
processedSize: outputBuffer.length,
width: result.width,
height: result.height,
method: result.method,