Spaces:
Build error
Build error
Create src/routes/api/download-zip.ts
#3
by launch-calcium - opened
src/routes/api/download-zip.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { createRouteHandler, json } from '@tanstack/start';
|
| 2 |
+
import fetch from 'node-fetch';
|
| 3 |
+
import { unzip, zip } from 'fflate';
|
| 4 |
+
import { NormalizedPost } from '../../lib/grabber/sites';
|
| 5 |
+
|
| 6 |
+
export const POST = async ({ request }) => {
|
| 7 |
+
const { siteId, posts } = await request.json();
|
| 8 |
+
if (!Array.isArray(posts) || posts.length > 200) return json({ error: 'Too many posts' }, { status: 400 });
|
| 9 |
+
|
| 10 |
+
const buffer = unzip(zip(posts.map((post, i) => ({
|
| 11 |
+
filename: `${siteId}_${post.id}.${post.ext}`,
|
| 12 |
+
data: fetch(post.fileUrl).then(r => r.arrayBuffer()).then(b => b),
|
| 13 |
+
}))));
|
| 14 |
+
|
| 15 |
+
return new Response(buffer, {
|
| 16 |
+
headers: {
|
| 17 |
+
'Content-Type': 'application/zip',
|
| 18 |
+
'Content-Disposition': `attachment; filename="grabber-${Date.now()}.zip"`,
|
| 19 |
+
},
|
| 20 |
+
});
|
| 21 |
+
};
|