Skip to content

Commit 91c96e4

Browse files
committed
feat: added zip functionality
1 parent 4b66e95 commit 91c96e4

File tree

5 files changed

+80
-40
lines changed

5 files changed

+80
-40
lines changed

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
# million
88
.million/
99

10-
# tmp
11-
tmp/
12-
13-
# site assets in public folder (yes its still publicly accessible)
10+
# other stuff
1411
ads.txt
15-
BingSiteAuth.xml
1612

1713
# private testing stuff :p
1814
h.ts

bun.lock

Lines changed: 26 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"@million/lint": "^1.0.14",
1515
"bun-types": "^1.1.39",
16+
"jszip": "^3.10.1",
1617
"next": "15.1.0",
1718
"react": "^19.0.0",
1819
"react-dom": "^19.0.0",

pages/convert.tsx

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React, { useState, useEffect } from "react";
44
import { FaCloudUploadAlt, FaDatabase } from "react-icons/fa";
55
import initSqlJs from "sql.js";
6+
import JSZip from "jszip";
67

78
import Navbar from "@/components/Navbar";
89
import Questions from "@/components/Questions";
@@ -55,9 +56,10 @@ export default function Home() {
5556

5657
const tableNames = tablesResult[0].values.flat() as string[];
5758

58-
// Fetch data for each table
59-
const dbData: { [key: string]: any[] } = {};
60-
let jsonContent: { [key: string]: any[] } = {};
59+
// Store files for later download
60+
const blobs: { name: string; blob: Blob }[] = [];
61+
const zip = new JSZip();
62+
const jsonContent: { [key: string]: any[] } = {};
6163

6264
for (const table of tableNames) {
6365
const result = database.exec(`SELECT * FROM ${table}`);
@@ -70,55 +72,67 @@ export default function Home() {
7072
),
7173
);
7274

73-
dbData[table] = rows;
75+
if (selectedKeys.has("JSON")) {
76+
jsonContent[table] = rows;
77+
}
7478

7579
if (selectedKeys.has("CSV")) {
76-
// Convert table data to CSV
7780
const csvContent = [
78-
columns.join(","),
81+
columns.join(","), // Header
7982
...rows.map((row) =>
8083
columns.map((col) => row[col]).join(","),
81-
),
84+
), // Rows
8285
].join("\n");
8386

84-
// Create and download CSV file
85-
const blob = new Blob([csvContent], {
87+
const csvBlob = new Blob([csvContent], {
8688
type: "text/csv;charset=utf-8;",
8789
});
8890

89-
const link = document.createElement("a");
90-
91-
link.href = URL.createObjectURL(blob);
92-
link.download = `${table}.csv`;
93-
document.body.appendChild(link);
94-
link.click();
95-
document.body.removeChild(link);
96-
}
97-
98-
if (selectedKeys.has("JSON")) {
99-
jsonContent[table] = rows;
91+
blobs.push({ name: `${table}.csv`, blob: csvBlob });
10092
}
101-
} else {
102-
dbData[table] = [];
10393
}
10494
}
10595

106-
// Export combined JSON file if selected
10796
if (selectedKeys.has("JSON")) {
108-
const blob = new Blob([JSON.stringify(jsonContent, null, 2)], {
109-
type: "application/json;charset=utf-8;",
110-
});
111-
112-
const link = document.createElement("a");
97+
const jsonBlob = new Blob(
98+
[JSON.stringify(jsonContent, null, 2)],
99+
{
100+
type: "application/json;charset=utf-8;",
101+
},
102+
);
103+
104+
blobs.push({ name: "database.json", blob: jsonBlob });
105+
}
113106

114-
link.href = URL.createObjectURL(blob);
115-
link.download = `database.json`;
116-
document.body.appendChild(link);
117-
link.click();
118-
document.body.removeChild(link);
107+
if (blobs.length === 1) {
108+
// Download single file directly
109+
const { name, blob } = blobs[0];
110+
const url = URL.createObjectURL(blob);
111+
const a = document.createElement("a");
112+
113+
a.href = url;
114+
a.download = name;
115+
document.body.appendChild(a);
116+
a.click();
117+
document.body.removeChild(a);
118+
URL.revokeObjectURL(url);
119+
} else if (blobs.length > 1) {
120+
// Create ZIP archive for multiple files
121+
blobs.forEach(({ name, blob }) => zip.file(name, blob));
122+
zip.generateAsync({ type: "blob" }).then((zipBlob) => {
123+
const zipUrl = URL.createObjectURL(zipBlob);
124+
const a = document.createElement("a");
125+
126+
a.href = zipUrl;
127+
a.download = "database_export.zip";
128+
document.body.appendChild(a);
129+
a.click();
130+
document.body.removeChild(a);
131+
URL.revokeObjectURL(zipUrl);
132+
});
119133
}
120134

121-
console.debug(dbData);
135+
console.debug("Exported blobs:", blobs);
122136
};
123137

124138
reader.readAsArrayBuffer(file);

public/BingSiteAuth.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<users>
3+
<user>BA7C85D67344474D7785F2AAD744AEC2</user>
4+
</users>

0 commit comments

Comments
 (0)