Skip to content

Commit 09e7a2f

Browse files
committed
Refactor file handling to send raw binary data and combine encrypted data with IV for uploads
1 parent 7021832 commit 09e7a2f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

server.mjs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ app.get("/download/:docId", async (req, res) => {
4141
const { docId } = req.params;
4242
const filePath = join(__dirname, UPLOAD_DIR, `${docId}.bin`);
4343
try {
44-
const encryptedData = await fs.readFile(filePath);
45-
const parsedData = JSON.parse(encryptedData.toString());
46-
if (!parsedData.encryptedData || !parsedData.iv) throw new Error("Invalid file format");
47-
const encryptedArray = Uint8Array.from(Object.values(parsedData.encryptedData));
48-
const ivArray = Uint8Array.from(Object.values(parsedData.iv));
49-
res.json({ encryptedArray: Array.from(encryptedArray), ivArray: Array.from(ivArray) });
44+
const data = await fs.readFile(filePath);
45+
res.setHeader("Content-Type", "application/octet-stream");
46+
res.send(data);
5047
} catch (error) {
5148
console.error("Error loading file:", error);
5249
res.status(500).json({ error: "Error retrieving file" });
@@ -79,7 +76,12 @@ async function processUploadQueue() {
7976
const iv = new Uint8Array(data.iv);
8077
const id = randomUUID();
8178
const filePath = join(__dirname, UPLOAD_DIR, `${id}.bin`);
82-
await fs.writeFile(filePath, JSON.stringify({ encryptedData, iv }));
79+
// await fs.writeFile(filePath, JSON.stringify({ encryptedData, iv }));
80+
const combined = Buffer.concat([
81+
Buffer.from(iv),
82+
Buffer.from(encryptedData)
83+
]);
84+
await fs.writeFile(filePath, combined);
8385
socket.emit("saved", { id, page: data.page });
8486
} catch (err) {
8587
console.error("Upload error:", err);

0 commit comments

Comments
 (0)