Working on presigned urls in local.

This commit is contained in:
charles-gauthereau
2024-11-28 20:28:00 +01:00
parent 319662e39c
commit feb10603e9
9 changed files with 134 additions and 7 deletions
@@ -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";
@@ -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;
}
}
+3
View File
@@ -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}`;
}