|
1 | 1 | import { v2 as cloudinary, UploadApiResponse } from "cloudinary"; |
2 | | -// @ts-expect-error: imagetracerjs has no TypeScript types |
3 | | -import ImageTracer from "imagetracerjs"; |
4 | 2 | import { NextResponse } from "next/server"; |
5 | | -import fetch from "node-fetch"; |
6 | | -import sharp from "sharp"; |
| 3 | + |
| 4 | +import { createRequire } from "module"; |
| 5 | +const require = createRequire(import.meta.url); |
| 6 | +const potrace = require("potrace"); |
7 | 7 |
|
8 | 8 | cloudinary.config({ |
9 | 9 | cloud_name: process.env.CLOUDINARY_CLOUD_NAME!, |
10 | 10 | api_key: process.env.CLOUDINARY_API_KEY!, |
11 | 11 | api_secret: process.env.CLOUDINARY_API_SECRET!, |
12 | 12 | }); |
13 | 13 |
|
14 | | -async function cloudinaryToSVG(imageUrl: string, options = { scale: 5 }) { |
15 | | - const response = await fetch(imageUrl); |
16 | | - if (!response.ok) |
17 | | - throw new Error(`Failed to fetch image: ${response.statusText}`); |
18 | | - const buffer = Buffer.from(await response.arrayBuffer()); |
19 | | - |
20 | | - const image = sharp(buffer).ensureAlpha(); // ensures RGBA format |
21 | | - const { width, height } = await image.metadata(); |
22 | | - |
23 | | - if (!width || !height) { |
24 | | - throw new Error("Unable to read image metadata."); |
25 | | - } |
26 | | - |
27 | | - const raw = await image.raw().toBuffer(); |
28 | | - |
29 | | - const myImageData = { |
30 | | - width, |
31 | | - height, |
32 | | - data: raw, |
33 | | - }; |
| 14 | +async function cloudinaryToSVG(imageUrl: string) { |
| 15 | + const svgString: string = await new Promise((resolve, reject) => { |
| 16 | + potrace.posterize(imageUrl, (err: unknown, svg: string) => { |
| 17 | + if (err) |
| 18 | + return reject( |
| 19 | + new Error( |
| 20 | + `Failed to convert image to SVG: ${(err as Error)?.message}`, |
| 21 | + ), |
| 22 | + ); |
| 23 | + resolve(svg); |
| 24 | + }); |
| 25 | + }); |
34 | 26 |
|
35 | | - const svgstring = ImageTracer.imagedataToSVG(myImageData, options); |
36 | | - return svgstring; |
| 27 | + return svgString; |
37 | 28 | } |
38 | 29 |
|
39 | 30 | export async function POST(req: Request) { |
@@ -73,8 +64,7 @@ export async function POST(req: Request) { |
73 | 64 | ); |
74 | 65 | } catch (err: unknown) { |
75 | 66 | console.error("Upload error:", err); |
76 | | - const errorMessage = |
77 | | - err instanceof Error ? err.message : "Internal Server Error"; |
| 67 | + const errorMessage = (err as Error)?.message || "Internal Server Error"; |
78 | 68 | return NextResponse.json( |
79 | 69 | { success: false, error: errorMessage }, |
80 | 70 | { status: 500 }, |
|
0 commit comments