Skip to content

Commit

Permalink
fix upload endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
asoltys committed Sep 6, 2024
1 parent d21f764 commit 1d5aa72
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 46 deletions.
Binary file modified bun.lockb
Binary file not shown.
57 changes: 12 additions & 45 deletions lib/upload.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,37 @@
import { fileTypeFromStream } from "file-type";
import pump from "pump";
import Clone from "readable-stream-clone";
import fs from "fs";
import path from "path";
import { pipeline } from "stream";
import { g, s, db } from "$lib/db";
import { fileTypeFromBuffer } from "file-type";
import { writeFileSync } from "fs";
import { bail, fail } from "$lib/utils";
import crypto from "crypto";
import { createHash } from "crypto";
import { err } from "$lib/logging";

async function getSha256Hash(stream) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash("sha256");
stream.pipe(hash);

let hashValue;
hash.on("data", (data) => {
hashValue = data.toString("hex");
});

hash.on("end", () => {
resolve(hashValue);
});

hash.on("error", reject);
});
}
const sharp = require("sharp");

export default async (req, res) => {
try {
let {
params: { type },
} = req;

let width = type === "banner" ? 1920 : 240;

let data = await req.file();
let s1 = new Clone(data.file);
let s2 = new Clone(data.file);
let buf = await data.toBuffer();

let [format, ext] = (await fileTypeFromStream(s1)).mime.split("/");
let [format, ext] = (await fileTypeFromBuffer(buf)).mime.split("/");

if (format !== "image" && !["jpg", "jpeg", "png"].includes(ext))
fail("unsupported file type");

let processedBuffer = await streamToBuffer(s2.pipe(t));
let hash = crypto
.createHash("sha256")
.update(processedBuffer)
.digest("hex");
let w = type === "banner" ? 1920 : 240;
buf = await sharp(buf, { failOnError: false }).rotate().resize(w).webp().toBuffer();

let hash = createHash("sha256").update(buf).digest("hex");

let filePath = `/home/bun/app/data/uploads/${hash}.webp`;
fs.writeFileSync(filePath, processedBuffer);
writeFileSync(filePath, buf);

res.send({ hash });
} catch (e) {
console.log(e);
err("problem uploading", e.message);
bail(res, e.message);
}
};

function streamToBuffer(stream) {
return new Promise((resolve, reject) => {
const chunks = [];
stream.on("data", (chunk) => chunks.push(chunk));
stream.on("end", () => resolve(Buffer.concat(chunks)));
stream.on("error", reject);
});
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"@fastify/secure-session": "^7.5.1",
"@fastify/static": "^6.5.0",
"@fastify/websocket": "^10.0.1",
"@jimp/core": "^1.3.0",
"@jimp/wasm-webp": "^1.3.0",
"@noble/hashes": "^1.4.0",
"bech32": "^2.0.0",
"buffer-crc32": "^0.2.13",
Expand All @@ -28,6 +30,7 @@
"fluent-ffmpeg": "^2.1.2",
"got": "^13.0.0",
"handlebars": "^4.7.8",
"jimp": "^1.3.0",
"jsonwebtoken": "^9.0.2",
"lnurl": "^0.24.2",
"mqtt": "^5.0.2",
Expand All @@ -41,7 +44,7 @@
"pump": "^3.0.0",
"readable-stream-clone": "^0.0.7",
"redis": "^4.6.10",
"sharp": "^0.31.1",
"sharp": "^0.33.5",
"uuid": "8",
"web-push": "^3.4.4",
"ws": "^7.4.6"
Expand Down

0 comments on commit 1d5aa72

Please sign in to comment.