mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working on presigned urls in local.
This commit is contained in:
@@ -3,7 +3,7 @@ import {Avatar, AvatarFallback, AvatarImage} from "@/components/ui/avatar";
|
||||
import {User} from "@prisma/client";
|
||||
import {UploadIcon} from "lucide-react";
|
||||
import {toast} from "sonner";
|
||||
import {uploadImageAction} from "@/features/upload/upload.action";
|
||||
import {uploadImageAction} from "@/features/upload/public/upload.action";
|
||||
import {useMutation} from "@tanstack/react-query";
|
||||
import {prisma} from "@/prisma";
|
||||
import {updateImageUserAction} from "@/components/wrappers/Dashboard/Profile/Avatar/avatar.action";
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import {useState} from "react";
|
||||
import {Settings} from "@prisma/client";
|
||||
import {ButtonWithLoading} from "@/components/wrappers/Button/ButtonWithLoading/ButtonWithLoading";
|
||||
import {useMutation} from "@tanstack/react-query";
|
||||
import {checkConnexionToS3} from "@/features/upload/upload.action";
|
||||
import {checkConnexionToS3} from "@/features/upload/public/upload.action";
|
||||
import {toast} from "sonner";
|
||||
import {updateUserAction} from "@/components/wrappers/Dashboard/Profile/UserForm/user-form.action";
|
||||
import {useRouter} from "next/navigation";
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
"use server"
|
||||
import {mkdir} from "fs/promises";
|
||||
import path from "path";
|
||||
import * as fs from "node:fs";
|
||||
import {getServerUrl} from "@/utils/get-server-url";
|
||||
|
||||
|
||||
// async function uploadLocalPrivate(fileName: string, buffer: any) {
|
||||
// const localDir = "public/uploads/"
|
||||
// try {
|
||||
// await mkdir(path.join(process.cwd(), localDir), { recursive: true });
|
||||
// return await writeFile(
|
||||
// path.join(process.cwd(), localDir + fileName),
|
||||
// buffer
|
||||
// )
|
||||
//
|
||||
// } catch (error) {
|
||||
// console.log("Error occured ", error);
|
||||
// throw new Error('An error occured while importing image');
|
||||
//
|
||||
// }
|
||||
// }
|
||||
|
||||
const privateLocalDir = "private/uploads/";
|
||||
|
||||
export async function getFileUrlPresignedLocal(fileName: string) {
|
||||
try {
|
||||
const filePath = path.join(privateLocalDir, fileName);
|
||||
await mkdir(path.join(process.cwd(), privateLocalDir), { recursive: true });
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error('File not found at:', filePath);
|
||||
return `File not found at: ${filePath}`
|
||||
}
|
||||
const crypto = require('crypto');
|
||||
const baseUrl = getServerUrl();
|
||||
|
||||
const expiresAt = Date.now() + 60 * 1000; // expires in 1 minute
|
||||
const token = crypto.createHash('sha256').update(`${fileName}${expiresAt}`).digest('hex');
|
||||
return `${baseUrl}/api/files/${fileName}?token=${token}&expires=${expiresAt}`;
|
||||
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,8 @@ export const getServerUrl = () => {
|
||||
if (typeof window !== 'undefined') {
|
||||
return window.location.origin;
|
||||
}
|
||||
if(env.NODE_ENV === 'development') {
|
||||
return `http://${env.NEXT_PUBLIC_DOMAIN_NAME}`;
|
||||
}
|
||||
return `https://${env.NEXT_PUBLIC_DOMAIN_NAME}`;
|
||||
}
|
||||
Reference in New Issue
Block a user