mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Adding some methods in order to support the serving files images dynamically in production.
This commit is contained in:
@@ -12,7 +12,7 @@ export async function GET(
|
|||||||
const expires = searchParams.get('expires');
|
const expires = searchParams.get('expires');
|
||||||
const fileName = (await params).fileName
|
const fileName = (await params).fileName
|
||||||
|
|
||||||
const privateLocalDir = "private/uploads/";
|
const privateLocalDir = "private/uploads/files/";
|
||||||
const filePath = path.join(privateLocalDir, fileName);
|
const filePath = path.join(privateLocalDir, fileName);
|
||||||
|
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import path from "path";
|
||||||
|
import fs from "fs/promises";
|
||||||
|
import {NextResponse} from "next/server";
|
||||||
|
|
||||||
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{params}: { params: Promise<{ fileName: string }> }
|
||||||
|
) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
const fileName = (await params).fileName
|
||||||
|
const filePath = path.join(process.cwd(), "private/uploads/images", fileName);
|
||||||
|
|
||||||
|
// Check if the file exists
|
||||||
|
try {
|
||||||
|
await fs.access(filePath); // Ensures the file exists
|
||||||
|
} catch {
|
||||||
|
return NextResponse.json({error: "File not found"}, {status: 404});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the file
|
||||||
|
const fileContent = await fs.readFile(filePath); // Returns a Buffer
|
||||||
|
|
||||||
|
return new NextResponse(fileContent, {
|
||||||
|
headers: {
|
||||||
|
"Content-Disposition": `attachment; filename="${fileName}"`,
|
||||||
|
"Content-Type": "application/octet-stream", // Adjust MIME type as needed
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error reading file:", error);
|
||||||
|
return NextResponse.json({error: "Internal Server Error"}, {status: 500});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,6 +41,7 @@ function checkRouteExists(pathname) {
|
|||||||
/^\/api\/agent\/[^/]+\/backup\/?$/,
|
/^\/api\/agent\/[^/]+\/backup\/?$/,
|
||||||
/^\/api\/agent\/[^/]+\/restore\/?$/,
|
/^\/api\/agent\/[^/]+\/restore\/?$/,
|
||||||
/^\/api\/files\/[^/]+\/?$/,
|
/^\/api\/files\/[^/]+\/?$/,
|
||||||
|
/^\/api\/images\/[^/]+\/?$/,
|
||||||
/^\/api\/events\/?$/,
|
/^\/api\/events\/?$/,
|
||||||
/^\/api\/init\/?$/,
|
/^\/api\/init\/?$/,
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import path from "path";
|
|||||||
import * as fs from "node:fs";
|
import * as fs from "node:fs";
|
||||||
import {getServerUrl} from "@/utils/get-server-url";
|
import {getServerUrl} from "@/utils/get-server-url";
|
||||||
|
|
||||||
const privateLocalDir = "private/uploads/";
|
const privateLocalDir = "private/uploads/files/";
|
||||||
|
|
||||||
|
|
||||||
export async function uploadLocalPrivate(fileName: string, buffer: any) {
|
export async function uploadLocalPrivate(fileName: string, buffer: any) {
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ function getUrl(fileName:string, settings: Settings, bucketName: string):string
|
|||||||
return `https://${env.S3_ENDPOINT}/${bucketName}/${fileName}`
|
return `https://${env.S3_ENDPOINT}/${bucketName}/${fileName}`
|
||||||
}else if(settings.storage === "local"){
|
}else if(settings.storage === "local"){
|
||||||
const url = getServerUrl()
|
const url = getServerUrl()
|
||||||
return `${url}/uploads/${fileName}`
|
return `${url}/api/images/${fileName}`
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -63,13 +63,13 @@ function getUrl(fileName:string, settings: Settings, bucketName: string):string
|
|||||||
return `http://localhost:${env.S3_PORT}/${bucketName}/${fileName}`
|
return `http://localhost:${env.S3_PORT}/${bucketName}/${fileName}`
|
||||||
}else if(settings.storage === "local"){
|
}else if(settings.storage === "local"){
|
||||||
const url = getServerUrl()
|
const url = getServerUrl()
|
||||||
return `${url}/uploads/${fileName}`
|
return `${url}/api/images/${fileName}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadLocal(fileName: string, buffer: any) {
|
async function uploadLocal(fileName: string, buffer: any) {
|
||||||
const localDir = "public/uploads/"
|
const localDir = "private/uploads/images/"
|
||||||
try {
|
try {
|
||||||
await mkdir(path.join(process.cwd(), localDir), { recursive: true });
|
await mkdir(path.join(process.cwd(), localDir), { recursive: true });
|
||||||
return await writeFile(
|
return await writeFile(
|
||||||
|
|||||||
Reference in New Issue
Block a user