Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions packages/api/api/uploadFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import formidable from "formidable";
import fs from "fs";
import Jimp from "jimp";
import sharp from "sharp";
import { VercelRequest, VercelResponse } from "@vercel/node";

import { uploadToPinata } from "../lib/fileStorage";
Expand Down Expand Up @@ -29,15 +28,6 @@ export default async function uploadFile(
const fileName = req.query.name as string;
const form = formidable({});

let isSvg = false;
form.on("file", (_, file) => {
const mimeType = file.mimetype;

if (mimeType === "image/svg+xml") {
isSvg = true;
}
});

try {
const [, files] = await form.parse(req);
const formFile = files[fileName] as [FormFile] | undefined;
Expand All @@ -46,16 +36,10 @@ export default async function uploadFile(
return res.status(400).json({ error: "No file provided" });
}

let fileContents: Buffer;

if (isSvg) {
fileContents = fs.readFileSync(formFile[0]._writeStream.path);
} else {
const image = await Jimp.read(formFile[0]._writeStream.path);
fileContents = await image
.resize(700, Jimp.AUTO)
.getBufferAsync(Jimp.MIME_PNG);
}
const fileContents = await sharp(formFile[0]._writeStream.path)
.resize(700)
.png()
.toBuffer();

const cid = await uploadToPinata(fileContents, `${fileName}.png`);
if (!cid) {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dotenv": "^16.4.5",
"express": "^4.19.2",
"formidable": "^3.5.1",
"jimp": "^0.22.12",
"sharp": "^0.33.4",
"vercel": "^34.2.7"
},
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion packages/client/src/pages/CharacterCreation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,11 @@ export const CharacterCreation = (): JSX.Element => {
</FormControl>
<FormControl isInvalid={showError && !avatar}>
<Input
accept=".png, .jpg, .jpeg, .webp, .svg"
id="avatarInput"
onChange={e => setAvatar(e.target.files?.[0] ?? null)}
onChange={e =>
e.target.files?.[0] && setAvatar(e.target.files?.[0])
}
style={{ display: 'none' }}
type="file"
/>
Expand Down
Loading