Skip to content

Commit

Permalink
fix: Fileupload with non ascii filename does not work #86
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Kühnlein committed Mar 4, 2024
1 parent d3c9328 commit a7f8da4
Showing 1 changed file with 44 additions and 50 deletions.
94 changes: 44 additions & 50 deletions app/src/app/api/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,51 @@ import { getImporterManager } from "@/lib/ImporterManager";
import { getMinioClient } from "../../../lib/minioClient";

export async function POST(req: NextRequest) {
const { importerId, bucket, destFileName, fileFormat } =
await handleFileUpload(req);
const importerManager = await getImporterManager();
await importerManager.addFile(importerId, destFileName, fileFormat, bucket);
return new Response(undefined, { status: 201 });
const { importerId, bucket, destFileName, fileFormat } = await handleFileUpload(req);
const importerManager = await getImporterManager();
await importerManager.addFile(importerId, destFileName, fileFormat, bucket);
return new Response(undefined, { status: 201 });
}

async function handleFileUpload(req: NextRequest) {
const formData = await req.formData();
const file: File | null = formData.get("file") as unknown as File;
const importerId = formData.get("importerId") as unknown as string;
const fileBuffer = Buffer.from(await file.arrayBuffer());

if (!importerId) {
throw new Error("importerId missing");
}
if (!fileBuffer) {
throw new Error("file missing");
}

const bucket = importerId;
const bucketExists = await getMinioClient().bucketExists(bucket);

if (bucketExists === false) {
await getMinioClient().makeBucket(bucket);
}

const metadata = {
"Content-Type": file.type,
FileName: file.name,
ImporterId: importerId,
};

const destFileName = `${randomUUID()}${extname(file.name)}`;
try {
await getMinioClient().putObject(
bucket,
destFileName,
fileBuffer,
metadata
);
} catch (error) {
console.error(error);
throw new Error("Error uploading file");
}
const fileFormat = extname(file.name) === ".csv" ? "csv" : "xlsx";

return {
importerId,
bucket,
destFileName,
fileFormat,
};
const formData = await req.formData();
const file: File | null = formData.get("file") as unknown as File;
const importerId = formData.get("importerId") as unknown as string;
const fileBuffer = Buffer.from(await file.arrayBuffer());

if (!importerId) {
throw new Error("importerId missing");
}
if (!fileBuffer) {
throw new Error("file missing");
}

const bucket = importerId;
const bucketExists = await getMinioClient().bucketExists(bucket);

if (bucketExists === false) {
await getMinioClient().makeBucket(bucket);
}

const metadata = {
"Content-Type": file.type,
FileName: encodeURIComponent(file.name),
ImporterId: importerId,
};

const destFileName = `${randomUUID()}${extname(file.name)}`;
try {
await getMinioClient().putObject(bucket, destFileName, fileBuffer, metadata);
} catch (error) {
console.error(error);
throw new Error("Error uploading file");
}
const fileFormat = extname(file.name) === ".csv" ? "csv" : "xlsx";

return {
importerId,
bucket,
destFileName,
fileFormat,
};
}

0 comments on commit a7f8da4

Please sign in to comment.