Merge pull request #221 from bipproduction/amalia/11-september-24

upd: upload file
This commit is contained in:
Amalia
2024-09-11 16:47:52 +08:00
committed by GitHub
7 changed files with 80 additions and 43 deletions

View File

@@ -1,4 +1,4 @@
import { prisma } from "@/module/_global"; import { funDeleteFile, funUploadFile, prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth"; import { funGetUserByCookies } from "@/module/auth";
import _ from "lodash"; import _ from "lodash";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
@@ -118,26 +118,35 @@ export async function PUT(request: Request) {
}) })
if (String(file) != "undefined" && String(file) != "null") { if (String(file) != "undefined" && String(file) != "null") {
fs.unlink(`./public/image/user/${update.img}`, (err) => { })
const root = path.join(process.cwd(), "./public/image/user/");
const fExt = file.name.split(".").pop() const fExt = file.name.split(".").pop()
const fileName = user.id + '.' + fExt; // const fileName = user.id + '.' + fExt;
const filePath = path.join(root, fileName); const fileName = 'COBAAYAA.' + fExt;
const newFile = new File([file], fileName, { type: file.type });
console.log(fileName, newFile.name)
await funDeleteFile({ name: fileName, dirId: "cm0x8dbwn0005bp5tgmfcthzw" })
await funUploadFile({ file: newFile, dirId: "cm0x8dbwn0005bp5tgmfcthzw" })
// Konversi ArrayBuffer ke Buffer
const buffer = Buffer.from(await file.arrayBuffer());
// Tulis file ke sistem // fs.unlink(`./public/image/user/${update.img}`, (err) => { })
fs.writeFileSync(filePath, buffer); // const root = path.join(process.cwd(), "./public/image/user/");
// const fExt = file.name.split(".").pop()
// const fileName = user.id + '.' + fExt;
// const filePath = path.join(root, fileName);
await prisma.user.update({ // // Konversi ArrayBuffer ke Buffer
where: { // const buffer = Buffer.from(await file.arrayBuffer());
id: user.id
}, // // Tulis file ke sistem
data: { // fs.writeFileSync(filePath, buffer);
img: fileName
} // await prisma.user.update({
}) // where: {
// id: user.id
// },
// data: {
// img: fileName
// }
// })
} }
const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data profile', table: 'user', data: user.id }) const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data profile', table: 'user', data: user.id })

View File

@@ -1,4 +1,4 @@
import { prisma } from "@/module/_global"; import { funUploadFile, prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth"; import { funGetUserByCookies } from "@/module/auth";
import { createLogUser } from "@/module/user"; import { createLogUser } from "@/module/user";
import _ from "lodash"; import _ from "lodash";
@@ -137,25 +137,31 @@ export async function POST(request: Request) {
}); });
if (String(file) != "undefined" && String(file) != "null") { if (String(file) != "undefined" && String(file) != "null") {
const root = path.join(process.cwd(), "./public/image/user/");
const fExt = file.name.split(".").pop() const fExt = file.name.split(".").pop()
const fileName = users.id + '.' + fExt; const fileName = user.id + '.' + fExt;
const filePath = path.join(root, fileName); const newFile = new File([file], fileName, { type: file.type });
await funUploadFile({ file: newFile, dirId: "cm0x8dbwn0005bp5tgmfcthzw" })
// Konversi ArrayBuffer ke Buffer // const root = path.join(process.cwd(), "./public/image/user/");
const buffer = Buffer.from(await file.arrayBuffer()); // const fExt = file.name.split(".").pop()
// const fileName = users.id + '.' + fExt;
// const filePath = path.join(root, fileName);
// Tulis file ke sistem // // Konversi ArrayBuffer ke Buffer
fs.writeFileSync(filePath, buffer); // const buffer = Buffer.from(await file.arrayBuffer());
await prisma.user.update({ // // Tulis file ke sistem
where: { // fs.writeFileSync(filePath, buffer);
id: users.id
}, // await prisma.user.update({
data: { // where: {
img: fileName // id: users.id
} // },
}) // data: {
// img: fileName
// }
// })
} }
// create log user // create log user

View File

@@ -0,0 +1,22 @@
export async function funDeleteFile({ name, dirId }: { name: String, dirId: string }) {
try {
const res = await fetch(`https://wibu-storage.wibudev.com/api/dir/${dirId}/${name}`, {
method: "GET",
headers: {
Authorization: `Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7ImlkIjoiY20wdnQ4bzFrMDAwMDEyenE1eXl1emd5YiIsIm5hbWUiOiJhbWFsaWEiLCJlbWFpbCI6ImFtYWxpYUBiaXAuY29tIiwiQXBpS2V5IjpbeyJpZCI6ImNtMHZ0OG8xcjAwMDIxMnpxZDVzejd3eTgiLCJuYW1lIjoiZGVmYXVsdCJ9XX0sImlhdCI6MTcyNTkzNTE5MiwiZXhwIjo0ODgxNjk1MTkyfQ.7U-HUnNBDmeq_6XXohiFZjFnh2rSzUPMHDdrUKOd7G4`
}
});
if (res.ok) {
console.log("Berhasil dapat");
const hasil = await res.json()
console.log('berhasilAmalia', hasil)
} else {
const errorText = await res.json();
console.log('errorAmalia', errorText)
}
} catch (error) {
console.error("Upload error:", error);
}
}

View File

@@ -1,7 +1,7 @@
export async function fileUpload({ file }: { file: File}) { export async function funUploadFile({ file, dirId }: { file: File, dirId: string }) {
const formData = new FormData(); const formData = new FormData();
formData.append("file", file); formData.append("file", file);
formData.append("dirId", "cm0x8dbwn0005bp5tgmfcthzw"); formData.append("dirId", dirId);
try { try {
const res = await fetch("https://wibu-storage.wibudev.com/api/upload", { const res = await fetch("https://wibu-storage.wibudev.com/api/upload", {
@@ -13,13 +13,10 @@ export async function fileUpload({ file }: { file: File}) {
}); });
if (res.ok) { if (res.ok) {
console.log("File uploaded successfullyAmalia"); console.log("File uploaded successfully");
const hasil = await res.text()
console.log('berhasilAmalia',hasil)
} else { } else {
const errorText = await res.text(); const errorText = await res.text();
console.log('errorAmalia',errorText) console.log('errorAmalia', errorText)
} }
} catch (error) { } catch (error) {
console.error("Upload error:", error); console.error("Upload error:", error);

View File

@@ -6,6 +6,8 @@ import SkeletonDetailListTugasTask from "./components/skeleton_detail_list_tugas
import SkeletonDetailProfile from "./components/skeleton_detail_profile"; import SkeletonDetailProfile from "./components/skeleton_detail_profile";
import SkeletonSingle from "./components/skeleton_single"; import SkeletonSingle from "./components/skeleton_single";
import WrapLayout from "./components/wrap_layout"; import WrapLayout from "./components/wrap_layout";
import { funDeleteFile } from "./fun/delete_file";
import { funUploadFile } from "./fun/upload_file";
import { WARNA } from "./fun/WARNA"; import { WARNA } from "./fun/WARNA";
import LayoutDrawer from "./layout/layout_drawer"; import LayoutDrawer from "./layout/layout_drawer";
import LayoutIconBack from "./layout/layout_icon_back"; import LayoutIconBack from "./layout/layout_icon_back";
@@ -36,3 +38,5 @@ export { LayoutModalViewFile }
export { globalRole } export { globalRole }
export { WrapLayout } export { WrapLayout }
export { NoZoom } export { NoZoom }
export { funUploadFile }
export { funDeleteFile }

View File

@@ -3,11 +3,9 @@ import createLogUser from "./log/fun/createLogUser";
import { funGetAllmember } from './member/lib/api_member'; import { funGetAllmember } from './member/lib/api_member';
import Profile from './profile/ui/profile'; import Profile from './profile/ui/profile';
import EditProfile from './profile/ui/edit_profile'; import EditProfile from './profile/ui/edit_profile';
import { fileUpload } from './profile/lib/upload_img_profile';
export { createLogUser }; export { createLogUser };
export type { TypeUser } export type { TypeUser }
export { funGetAllmember } export { funGetAllmember }
export { Profile } export { Profile }
export { EditProfile } export { EditProfile }
export { fileUpload }

View File

@@ -28,6 +28,7 @@ export default function Profile() {
const res = await funGetProfileByCookies() const res = await funGetProfileByCookies()
setData(res.data) setData(res.data)
setIMG(`/api/file/img?jenis=image&cat=user&file=${res.data.img}`) setIMG(`/api/file/img?jenis=image&cat=user&file=${res.data.img}`)
// setIMG(`https://wibu-storage.wibudev.com/api/files/view/cm0x8dbwn0005bp5tgmfcthzw/${res.data.img}`)
setLoading(false) setLoading(false)
} catch (error) { } catch (error) {
console.error(error); console.error(error);