feat(17-02): refactor image service and routes to use S3 storage service

- Replace Bun.write/mkdir with uploadImage() from storage.service
- Remove uploadsDir parameter from fetchImageFromUrl
- Update tests to mock storage service instead of checking filesystem
This commit is contained in:
2026-04-05 12:20:31 +02:00
parent 544dd5bcd9
commit 5ce3f92a78
4 changed files with 117 additions and 39 deletions

View File

@@ -1,10 +1,9 @@
import { randomUUID } from "node:crypto";
import { mkdir } from "node:fs/promises";
import { join } from "node:path";
import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { z } from "zod";
import { fetchImageFromUrl } from "../services/image.service";
import { uploadImage } from "../services/storage.service";
const ALLOWED_TYPES = ["image/jpeg", "image/png", "image/webp"];
const MAX_SIZE = 5 * 1024 * 1024; // 5MB
@@ -56,12 +55,9 @@ app.post("/", async (c) => {
file.type.split("/")[1] === "jpeg" ? "jpg" : file.type.split("/")[1];
const filename = `${Date.now()}-${randomUUID()}.${ext}`;
// Ensure uploads directory exists
await mkdir("uploads", { recursive: true });
// Write file
// Upload to object storage
const buffer = await file.arrayBuffer();
await Bun.write(join("uploads", filename), buffer);
await uploadImage(Buffer.from(buffer), filename, file.type);
return c.json({ filename }, 201);
});

View File

@@ -1,6 +1,5 @@
import { randomUUID } from "node:crypto";
import { mkdir } from "node:fs/promises";
import { join } from "node:path";
import { uploadImage } from "./storage.service";
const ALLOWED_TYPES = ["image/jpeg", "image/png", "image/webp"];
const MAX_SIZE = 5 * 1024 * 1024; // 5MB
@@ -13,7 +12,6 @@ interface FetchImageResult {
export async function fetchImageFromUrl(
url: string,
uploadsDir = "uploads",
): Promise<FetchImageResult> {
// Validate URL format
let parsedUrl: URL;
@@ -74,9 +72,8 @@ export async function fetchImageFromUrl(
const ext = contentType === "image/jpeg" ? "jpg" : contentType.split("/")[1];
const filename = `${Date.now()}-${randomUUID()}.${ext}`;
// Ensure directory exists and write
await mkdir(uploadsDir, { recursive: true });
await Bun.write(join(uploadsDir, filename), buffer);
// Upload to object storage
await uploadImage(Buffer.from(buffer), filename, contentType);
return { filename, sourceUrl: url };
}