forked from jokull/markdown-ig-story
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
118 lines (96 loc) · 2.99 KB
/
server.ts
File metadata and controls
118 lines (96 loc) · 2.99 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
import { serve } from "bun";
import { MarkdownRenderer } from "./src/renderer";
import index from "./index.html";
// Simple zip creation using adm-zip
async function createZip(images: Buffer[]): Promise<Buffer> {
const AdmZip = (await import("adm-zip")).default;
const zip = new AdmZip();
for (let i = 0; i < images.length; i++) {
const paddedIndex = String(i + 1).padStart(2, "0");
const filename = `story-${paddedIndex}.png`;
const buffer = images[i];
// Validate buffer exists and has data
if (!buffer || buffer.length === 0) {
console.error(`Invalid buffer at index ${i}`);
throw new Error(`Image ${i} has invalid buffer data`);
}
// Add buffer directly to zip
zip.addFile(filename, buffer);
}
// Get the zip as a Buffer
return zip.toBuffer();
}
const server = serve({
routes: {
"/": index,
"/api/convert": {
async POST(req) {
try {
const { markdown } = await req.json();
if (!markdown || !markdown.trim()) {
return Response.json({ error: "No markdown content provided" }, { status: 400 });
}
console.log("Converting markdown to images...");
const renderer = new MarkdownRenderer();
const images = await renderer.renderMarkdown(markdown);
console.log(`Generated ${images.length} image(s)`);
// Create zip file
const zipData = await createZip(images);
// Return zip file
return new Response(zipData, {
headers: {
"Content-Type": "application/zip",
"Content-Disposition": "attachment; filename=instagram-stories.zip",
},
});
} catch (error) {
console.error("Error converting markdown:", error);
return Response.json(
{
error: "Failed to convert markdown",
details: error instanceof Error ? error.message : String(error),
},
{ status: 500 }
);
}
},
},
"/api/preview": {
async POST(req) {
try {
const { markdown } = await req.json();
if (!markdown || !markdown.trim()) {
return Response.json({ error: "No markdown content provided" }, { status: 400 });
}
console.log("Converting markdown to images for preview...");
const renderer = new MarkdownRenderer();
const images = await renderer.renderMarkdown(markdown);
console.log(`Generated ${images.length} image(s) for preview`);
// Convert images to base64 data URLs
const imageUrls = images.map((buffer, index) => ({
index: index + 1,
dataUrl: `data:image/png;base64,${buffer.toString("base64")}`,
}));
return Response.json({ images: imageUrls });
} catch (error) {
console.error("Error converting markdown:", error);
return Response.json(
{
error: "Failed to convert markdown",
details: error instanceof Error ? error.message : String(error),
},
{ status: 500 }
);
}
},
},
},
development: {
hmr: true,
console: true,
},
fetch(req) {
return new Response("Not Found", { status: 404 });
},
});
console.log(`🚀 Server running on ${server.url}`);