2025-01-05 21:27:31 +01:00
|
|
|
import { FreshContext, Handlers } from "$fresh/server.ts";
|
2025-01-05 21:58:07 +01:00
|
|
|
import { getImageContent } from "@lib/image.ts";
|
2023-08-01 17:50:00 +02:00
|
|
|
import { SILVERBULLET_SERVER } from "@lib/env.ts";
|
2023-08-05 22:16:14 +02:00
|
|
|
import { createLogger } from "@lib/log.ts";
|
2025-01-05 21:27:31 +01:00
|
|
|
import { isLocalImage } from "@lib/string.ts";
|
2023-08-02 02:24:08 +02:00
|
|
|
|
2023-08-05 22:16:14 +02:00
|
|
|
const log = createLogger("api/image");
|
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
// Constants for image processing
|
|
|
|
const CONFIG = {
|
|
|
|
maxDimension: 2048,
|
|
|
|
minDimension: 1,
|
|
|
|
acceptedMimeTypes: new Set([
|
|
|
|
"image/jpeg",
|
|
|
|
"image/png",
|
|
|
|
"image/webp",
|
|
|
|
"image/gif",
|
|
|
|
]),
|
|
|
|
};
|
2023-07-26 15:48:03 +02:00
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
interface ImageParams {
|
|
|
|
image: string;
|
|
|
|
height: number;
|
|
|
|
width: number;
|
2023-07-26 15:48:03 +02:00
|
|
|
}
|
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
/**
|
|
|
|
* Validates and parses URL parameters for image processing
|
|
|
|
*/
|
|
|
|
function parseParams(reqUrl: URL): ImageParams | string {
|
|
|
|
try {
|
|
|
|
const image = reqUrl.searchParams.get("image")?.replace(/^\//, "");
|
|
|
|
if (!image) {
|
|
|
|
return "Missing 'image' query parameter.";
|
2023-08-09 13:32:28 +02:00
|
|
|
}
|
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
// Parse dimensions with defaults
|
|
|
|
const height = Math.floor(Number(reqUrl.searchParams.get("height")) || 0);
|
|
|
|
const width = Math.floor(Number(reqUrl.searchParams.get("width")) || 0);
|
2023-07-26 15:48:03 +02:00
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
// Validate dimensions
|
|
|
|
if (height < 0 || width < 0) {
|
|
|
|
return "Negative height or width is not supported.";
|
|
|
|
}
|
2023-07-30 18:27:45 +02:00
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
if (height > CONFIG.maxDimension || width > CONFIG.maxDimension) {
|
|
|
|
return `Width and height cannot exceed ${CONFIG.maxDimension}.`;
|
|
|
|
}
|
2023-07-26 15:48:03 +02:00
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
// If dimensions are provided, ensure they're not too small
|
|
|
|
if (
|
|
|
|
(height > 0 && height < CONFIG.minDimension) ||
|
|
|
|
(width > 0 && width < CONFIG.minDimension)
|
|
|
|
) {
|
|
|
|
return `Dimensions must be at least ${CONFIG.minDimension} pixel.`;
|
|
|
|
}
|
2023-08-02 02:24:08 +02:00
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
return { image, height, width };
|
|
|
|
} catch (error) {
|
|
|
|
log.error("Error parsing parameters:", error);
|
|
|
|
return "Invalid parameters provided.";
|
2023-08-02 02:24:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 17:50:00 +02:00
|
|
|
const GET = async (
|
2025-01-05 21:27:31 +01:00
|
|
|
req: Request,
|
|
|
|
_ctx: FreshContext,
|
2023-07-26 13:47:01 +02:00
|
|
|
): Promise<Response> => {
|
2025-01-05 21:27:31 +01:00
|
|
|
try {
|
|
|
|
const url = new URL(req.url);
|
|
|
|
const params = parseParams(url);
|
|
|
|
|
|
|
|
if (typeof params === "string") {
|
|
|
|
return new Response(params, {
|
|
|
|
status: 400,
|
|
|
|
headers: { "Content-Type": "text/plain" },
|
|
|
|
});
|
|
|
|
}
|
2023-07-26 15:48:03 +02:00
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
const imageUrl = isLocalImage(params.image)
|
|
|
|
? `${SILVERBULLET_SERVER}/${params.image.replace(/^\//, "")}`
|
|
|
|
: params.image;
|
2023-07-26 15:48:03 +02:00
|
|
|
|
2025-01-05 21:27:31 +01:00
|
|
|
log.debug("Processing image request:", { imageUrl, params });
|
2023-07-30 18:27:45 +02:00
|
|
|
|
2025-01-05 21:58:07 +01:00
|
|
|
const image = await getImageContent(imageUrl, params);
|
2025-01-05 21:27:31 +01:00
|
|
|
|
|
|
|
return new Response(image.content, {
|
2023-07-30 18:27:45 +02:00
|
|
|
headers: {
|
2025-01-05 21:27:31 +01:00
|
|
|
"Content-Type": image.mimeType,
|
2023-07-30 18:27:45 +02:00
|
|
|
},
|
|
|
|
});
|
2025-01-05 21:27:31 +01:00
|
|
|
} catch (error) {
|
|
|
|
log.error("Error processing image:", error);
|
|
|
|
return new Response("Internal server error", {
|
|
|
|
status: 500,
|
|
|
|
headers: { "Content-Type": "text/plain" },
|
2023-08-11 13:03:42 +02:00
|
|
|
});
|
2025-01-05 21:27:31 +01:00
|
|
|
}
|
2023-07-26 13:47:01 +02:00
|
|
|
};
|
2023-08-01 17:50:00 +02:00
|
|
|
|
|
|
|
export const handler: Handlers = {
|
|
|
|
GET,
|
|
|
|
};
|