mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-11-13 20:53:45 +08:00
feat: Cloudflare R2 对象存储支持
This commit is contained in:
36
app/api/file/[...path]/route.ts
Normal file
36
app/api/file/[...path]/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "../../auth";
|
||||
import S3FileStorage from "../../../utils/r2_file_storage";
|
||||
|
||||
async function handle(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { path: string[] } },
|
||||
) {
|
||||
if (req.method === "OPTIONS") {
|
||||
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||
}
|
||||
|
||||
const authResult = auth(req);
|
||||
if (authResult.error) {
|
||||
return NextResponse.json(authResult, {
|
||||
status: 401,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const GET = handle;
|
||||
|
||||
export const runtime = "edge";
|
||||
@@ -1,9 +1,6 @@
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import axios from "axios";
|
||||
|
||||
import { Tool } from "langchain/tools";
|
||||
import OpenAI from "openai";
|
||||
import S3FileStorage from "../../utils/r2_file_storage";
|
||||
|
||||
export class DallEAPIWrapper extends Tool {
|
||||
name = "dalle_image_generator";
|
||||
@@ -22,20 +19,10 @@ export class DallEAPIWrapper extends Tool {
|
||||
}
|
||||
|
||||
async saveImageFromUrl(url: string) {
|
||||
const response = await axios.get(url, { responseType: "arraybuffer" });
|
||||
|
||||
const uploadsDir = "public/uploads";
|
||||
console.log("[fileUpload]", { uploadsDir });
|
||||
if (!fs.existsSync(uploadsDir)) {
|
||||
fs.mkdirSync(uploadsDir, { recursive: true });
|
||||
}
|
||||
|
||||
const filename = `${Date.now()}.png`;
|
||||
const filePath = path.join(uploadsDir, filename);
|
||||
|
||||
fs.writeFileSync(filePath, Buffer.from(response.data, "binary"));
|
||||
|
||||
return `uploads/${filename}`;
|
||||
const response = await fetch(url);
|
||||
const content = await response.arrayBuffer();
|
||||
const buffer = Buffer.from(content);
|
||||
return await S3FileStorage.put(`${Date.now()}.png`, buffer);
|
||||
}
|
||||
|
||||
/** @ignore */
|
||||
@@ -61,5 +48,5 @@ export class DallEAPIWrapper extends Tool {
|
||||
description = `openai's dall-e image generator.
|
||||
input must be a english prompt.
|
||||
output will be the image link url.
|
||||
use markdown to display images.`;
|
||||
use markdown to display images. like: `;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user