feat: support other type file upload

This commit is contained in:
Hk-Gosuto
2024-03-31 23:07:17 +08:00
parent aea5bedb68
commit 80a077a3db
9 changed files with 223 additions and 33 deletions

View File

@@ -2,6 +2,7 @@ import { getServerSideConfig } from "@/app/config/server";
import LocalFileStorage from "@/app/utils/local_file_storage";
import S3FileStorage from "@/app/utils/s3_file_storage";
import { NextRequest, NextResponse } from "next/server";
import mime from "mime";
async function handle(
req: NextRequest,
@@ -13,19 +14,27 @@ async function handle(
try {
const serverConfig = getServerSideConfig();
const fileName = params.path[0];
const contentType = mime.getType(fileName);
if (serverConfig.isStoreFileToLocal) {
var fileBuffer = await LocalFileStorage.get(params.path[0]);
var fileBuffer = await LocalFileStorage.get(fileName);
return new Response(fileBuffer, {
headers: {
"Content-Type": "image/png",
"Content-Type": contentType ?? "application/octet-stream",
},
});
} else {
var file = await S3FileStorage.get(params.path[0]);
return new Response(file?.transformToWebStream(), {
headers: {
"Content-Type": "image/png",
},
var file = await S3FileStorage.get(fileName);
if (file) {
return new Response(file?.transformToWebStream(), {
headers: {
"Content-Type": contentType ?? "application/octet-stream",
},
});
}
return new Response("not found", {
status: 404,
});
}
} catch (e) {