randomize file name
This commit is contained in:
@@ -211,9 +211,22 @@
|
||||
|
||||
if (selectedFile) {
|
||||
// Upload file to Supabase Storage
|
||||
const oldFilePath = newUser.profile_picture
|
||||
? newUser.profile_picture.split("/").pop()
|
||||
: null;
|
||||
if (oldFilePath) {
|
||||
await supabase.storage
|
||||
.from("villabugis")
|
||||
.remove([`profile_pictures/${oldFilePath}`]);
|
||||
}
|
||||
|
||||
const fileName = selectedFile.name;
|
||||
const fileExtension = fileName.split(".").pop();
|
||||
const randomFileName = `${crypto.randomUUID()}.${fileExtension}`;
|
||||
|
||||
const { data, error } = await supabase.storage
|
||||
.from("villabugis")
|
||||
.upload(`profile_pictures/${selectedFile.name}`, selectedFile);
|
||||
.upload(`profile_pictures/${randomFileName}`, selectedFile);
|
||||
|
||||
if (error) {
|
||||
console.error("Error uploading file:", error);
|
||||
|
||||
@@ -340,12 +340,26 @@
|
||||
|
||||
//upload image if selected
|
||||
if (selectedFile) {
|
||||
//delete previous image
|
||||
const oldFilePath = newIssue.issue_related_image
|
||||
? newIssue.issue_related_image.split("/").pop()
|
||||
: null;
|
||||
if (oldFilePath) {
|
||||
const { error: deleteError } = await supabase.storage
|
||||
.from("villabugis")
|
||||
.remove([`issues/${oldFilePath}`]);
|
||||
if (deleteError) {
|
||||
console.error("Error deleting old image:", deleteError);
|
||||
}
|
||||
}
|
||||
|
||||
const fileName = selectedFile.name;
|
||||
const fileExtension = fileName.split(".").pop();
|
||||
const randomFileName = `${crypto.randomUUID()}.${fileExtension}`;
|
||||
|
||||
const { data, error } = await supabase.storage
|
||||
.from("villabugis")
|
||||
.upload(
|
||||
`issues/${Date.now()}_${selectedFile.name}`,
|
||||
selectedFile,
|
||||
);
|
||||
.upload(`issues/${randomFileName}`, selectedFile);
|
||||
|
||||
console.log("Image upload data:", data);
|
||||
|
||||
@@ -389,7 +403,9 @@
|
||||
"description_of_the_issue",
|
||||
) as string,
|
||||
reported_date: formData.get("reported_date") as string,
|
||||
issue_related_image: imagePreviewUrl || "",
|
||||
issue_related_image: newIssue.issue_related_image
|
||||
? newIssue.issue_related_image
|
||||
: (formData.get("issue_related_image") as string),
|
||||
issue_source: formData.get("issue_source") as string,
|
||||
reported_by: formData.get("reported_by") as string,
|
||||
input_by: formData.get("input_by") as string,
|
||||
@@ -419,6 +435,11 @@
|
||||
|
||||
await fetchIssues();
|
||||
showModal = false;
|
||||
|
||||
//clear form data
|
||||
newIssue = {};
|
||||
|
||||
selectedFile = null;
|
||||
}
|
||||
|
||||
// function get public URL for image supabase
|
||||
@@ -1165,7 +1186,12 @@
|
||||
<div class="flex justify-end gap-2 mt-4">
|
||||
<button
|
||||
class="px-4 py-2 text-sm rounded bg-gray-200 hover:bg-gray-300"
|
||||
on:click={() => (showModal = false)}
|
||||
on:click={() => (
|
||||
(showModal = false),
|
||||
(newIssue = {}),
|
||||
(selectedFile = null),
|
||||
(imagePreviewUrl = null)
|
||||
)}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte";
|
||||
import { supabase } from "$lib/supabaseClient";
|
||||
import { getSessionAuthId } from "$lib/utils/authUtil";
|
||||
|
||||
type User = {
|
||||
id: string;
|
||||
@@ -83,14 +84,28 @@
|
||||
async function uploadProfilePicture() {
|
||||
if (!file) return;
|
||||
|
||||
const filePath = `profile_pictures/${user.id}-${file.name}`;
|
||||
// delete file lama if exists
|
||||
if (user.profile_picture) {
|
||||
const oldFilePath = user.profile_picture.split("/").pop();
|
||||
if (oldFilePath) {
|
||||
await supabase.storage
|
||||
.from("villabugis")
|
||||
.remove([`profile_pictures/${oldFilePath}`]);
|
||||
}
|
||||
}
|
||||
|
||||
const fileName = file.name;
|
||||
const fileExtension = fileName.split(".").pop();
|
||||
const randomFileName = `${crypto.randomUUID()}.${fileExtension}`;
|
||||
|
||||
const filePath = `profile_pictures/${user.id}-${randomFileName}`;
|
||||
const { error } = await supabase.storage
|
||||
.from("profile")
|
||||
.from("villabugis")
|
||||
.upload(filePath, file, { upsert: true });
|
||||
|
||||
if (!error) {
|
||||
const { data: publicUrlData } = supabase.storage
|
||||
.from("profile")
|
||||
.from("villabugis")
|
||||
.getPublicUrl(filePath);
|
||||
user.profile_picture = publicUrlData.publicUrl;
|
||||
} else {
|
||||
@@ -100,11 +115,19 @@
|
||||
|
||||
async function updateProfile() {
|
||||
loading = true;
|
||||
|
||||
const sessionId = await getSessionAuthId();
|
||||
if (!sessionId) {
|
||||
message = "Anda harus login terlebih dahulu";
|
||||
loading = false;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (file) await uploadProfilePicture();
|
||||
|
||||
user.last_updated = new Date().toISOString();
|
||||
user.last_updated_by = user.email;
|
||||
user.last_updated_by = user.id;
|
||||
|
||||
const { error } = await supabase
|
||||
.from("vb_users")
|
||||
|
||||
@@ -349,9 +349,26 @@
|
||||
|
||||
// Upload image if selected
|
||||
if (selectedFile) {
|
||||
const oldFilePath = formData.get("picture_link") as string;
|
||||
if (oldFilePath) {
|
||||
// Delete old file if it exists
|
||||
const { error: deleteError } = await supabase.storage
|
||||
.from("villabugis")
|
||||
.remove([`project/${oldFilePath}`]);
|
||||
|
||||
if (deleteError) {
|
||||
console.error("Error deleting old image:", deleteError);
|
||||
return;
|
||||
}
|
||||
}
|
||||
//randomize file name uuid
|
||||
const fileName = selectedFile.name;
|
||||
const fileExtension = fileName.split(".").pop();
|
||||
const randomFileName = `${crypto.randomUUID()}.${fileExtension}`;
|
||||
|
||||
const { data, error } = await supabase.storage
|
||||
.from("villabugis")
|
||||
.upload(`project/${selectedFile.name}`, selectedFile);
|
||||
.upload(`project/${randomFileName}`, selectedFile);
|
||||
|
||||
if (error) {
|
||||
console.error("Error uploading image:", error);
|
||||
@@ -403,6 +420,12 @@
|
||||
|
||||
await fetchProjects();
|
||||
showModal = false;
|
||||
|
||||
isEditing = false;
|
||||
currentEditingId = null;
|
||||
newProjects = {};
|
||||
imagePreviewUrl = null;
|
||||
selectedFile = null;
|
||||
}
|
||||
|
||||
async function deleteProject(id: string) {
|
||||
|
||||
@@ -284,6 +284,9 @@
|
||||
"updated_by",
|
||||
"updated_name",
|
||||
"inputed_name",
|
||||
"approved_name",
|
||||
"acknowledged_name",
|
||||
"received_name",
|
||||
];
|
||||
const formColumns = columns.filter(
|
||||
(col) => !excludedKeys.includes(col.key),
|
||||
|
||||
Reference in New Issue
Block a user