feat: support local storage

This commit is contained in:
Hk-Gosuto
2024-01-05 18:41:16 +08:00
parent 8e10354109
commit 296df592e0
18 changed files with 181 additions and 56 deletions

View File

@@ -1,5 +1,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 S3FileStorage from "../../../utils/s3_file_storage";
async function handle(
req: NextRequest,
@@ -10,12 +12,22 @@ async function handle(
}
try {
var file = await S3FileStorage.get(params.path[0]);
return new Response(file?.transformToWebStream(), {
headers: {
"Content-Type": "image/png",
},
});
const serverConfig = getServerSideConfig();
if (serverConfig.isStoreFileToLocal) {
var fileBuffer = await LocalFileStorage.get(params.path[0]);
return new Response(fileBuffer, {
headers: {
"Content-Type": "image/png",
},
});
} else {
var file = await S3FileStorage.get(params.path[0]);
return new Response(file?.transformToWebStream(), {
headers: {
"Content-Type": "image/png",
},
});
}
} catch (e) {
return new Response("not found", {
status: 404,
@@ -25,5 +37,5 @@ async function handle(
export const GET = handle;
export const runtime = "edge";
export const runtime = "nodejs";
export const revalidate = 0;

View File

@@ -1,7 +1,9 @@
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import S3FileStorage from "../../../utils/s3_file_storage";
import { ModelProvider } from "@/app/constant";
import { auth } from "@/app/api/auth";
import LocalFileStorage from "@/app/utils/local_file_storage";
import { getServerSideConfig } from "@/app/config/server";
import S3FileStorage from "@/app/utils/s3_file_storage";
async function handle(req: NextRequest) {
if (req.method === "OPTIONS") {
@@ -31,10 +33,17 @@ async function handle(req: NextRequest) {
const buffer = Buffer.from(imageData);
var fileName = `${Date.now()}.png`;
await S3FileStorage.put(fileName, buffer);
var filePath = "";
const serverConfig = getServerSideConfig();
if (serverConfig.isStoreFileToLocal) {
filePath = await LocalFileStorage.put(fileName, buffer);
} else {
filePath = await S3FileStorage.put(fileName, buffer);
}
return NextResponse.json(
{
fileName: fileName,
filePath: filePath,
},
{
status: 200,
@@ -55,4 +64,4 @@ async function handle(req: NextRequest) {
export const POST = handle;
export const runtime = "edge";
export const runtime = "nodejs";