-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-icns-backend-code.txt
More file actions
122 lines (107 loc) · 3.63 KB
/
generate-icns-backend-code.txt
File metadata and controls
122 lines (107 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// app/api/generate-icns/route.ts
import { NextRequest, NextResponse } from 'next/server';
import * as png2icons from 'png2icons';
import * as UZIP from 'uzip-module';
// Make UZIP available globally for png2icons
if (typeof global !== 'undefined') {
(global as any).UZIP = UZIP;
}
if (typeof globalThis !== 'undefined') {
(globalThis as any).UZIP = UZIP;
}
const ALLOWED_ORIGINS = [
'https://app.iconify.roboticela.com',
'http://app.iconify.roboticela.com',
'http://localhost:3000',
'http://127.0.0.1:3000',
];
function getCorsHeaders(request: NextRequest): Record<string, string> {
const origin = request.headers.get('origin') ?? '';
const allowOrigin = ALLOWED_ORIGINS.includes(origin) ? origin : ALLOWED_ORIGINS[0];
return {
'Access-Control-Allow-Origin': allowOrigin,
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Max-Age': '86400',
};
}
export async function OPTIONS(request: NextRequest) {
return new NextResponse(null, {
status: 204,
headers: getCorsHeaders(request),
});
}
interface IconGenerationSettings {
backgroundColor: string;
scale: number; // percentage (0-500)
positionX: number; // percentage (0-100)
positionY: number; // percentage (0-100)
borderRoundness: number; // pixels
}
interface GenerateICNSRequest {
image: string; // Base64-encoded PNG string
settings: IconGenerationSettings;
}
export async function POST(request: NextRequest) {
try {
// Parse request body
const body: GenerateICNSRequest = await request.json();
if (!body.image) {
return NextResponse.json(
{ error: 'Image is required' },
{ status: 400, headers: getCorsHeaders(request) }
);
}
// Convert base64 string to Buffer
// Remove data URL prefix if present
const base64Data = body.image.includes(',')
? body.image.split(',')[1]
: body.image;
const pngBuffer = Buffer.from(base64Data, 'base64');
// Validate PNG signature
const pngSignature = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
if (!pngBuffer.subarray(0, 8).equals(pngSignature)) {
return NextResponse.json(
{ error: 'Invalid PNG format' },
{ status: 400, headers: getCorsHeaders(request) }
);
}
// Generate ICNS file using png2icons
// createICNS(input, scalingAlgorithm, numOfColors)
// - input: Buffer containing PNG data
// - scalingAlgorithm: 2 = BICUBIC for good quality
// Constants: 0=NEAREST_NEIGHBOR, 1=BILINEAR, 2=BICUBIC, 3=BEZIER, 4=HERMITE, 5=BICUBIC2
// - numOfColors: 0 for lossless (no color reduction)
const icnsBuffer = png2icons.createICNS(
pngBuffer,
2, // BICUBIC interpolation for good quality
0 // Lossless, no color reduction
);
if (!icnsBuffer) {
return NextResponse.json(
{ error: 'Failed to generate ICNS file' },
{ status: 500, headers: getCorsHeaders(request) }
);
}
// Convert Buffer to Uint8Array for response
const icnsArray = new Uint8Array(icnsBuffer);
// Return ICNS file as binary response
return new NextResponse(icnsArray, {
status: 200,
headers: {
...getCorsHeaders(request),
'Content-Type': 'image/icns',
'Content-Disposition': 'attachment; filename="icon.icns"',
},
});
} catch (error) {
console.error('Error generating ICNS:', error);
return NextResponse.json(
{
error: 'Internal server error',
message: error instanceof Error ? error.message : 'Unknown error'
},
{ status: 500, headers: getCorsHeaders(request) }
);
}
}