Skip to content

Commit 64e9694

Browse files
committed
fix: some more bugs fixed on the last commit
1 parent 5dac2c7 commit 64e9694

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/lib/server/supabase.server.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ export async function uploadFile(supabase: SupabaseClient, bucket: string, path:
4242
}
4343
}
4444

45+
export async function reuseFile(supabase: SupabaseClient, bucket: string, oldPath: string, newPath: string) {
46+
const { error: err } = await supabase.storage.from("scripts").copy(oldPath, newPath)
47+
if (err) {
48+
console.error(err)
49+
return (
50+
"storage " +
51+
bucket +
52+
" COPY " +
53+
oldPath +
54+
" TO " +
55+
newPath +
56+
" failed with the following error: " +
57+
JSON.stringify(err)
58+
)
59+
}
60+
}
61+
4562
export async function updateImgFile(supabase: SupabaseClient, bucket: string, path: string, file: File) {
4663
const { error: err } = await supabase.storage
4764
.from(bucket)

src/routes/scripts/[slug]/edit/files/+page.server.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { superValidate, setError, message } from "sveltekit-superforms/server"
22
import { error } from "@sveltejs/kit"
33
import { scriptFilesServerSchema } from "$lib/server/schemas.server"
44
import { canEdit } from "$lib/client/supabase"
5-
import { doLogin, supabaseAdmin, updateImgFile, uploadFile } from "$lib/server/supabase.server"
5+
import { doLogin, reuseFile, supabaseAdmin, updateImgFile, uploadFile } from "$lib/server/supabase.server"
66
import { formatError, UUID_V4_REGEX } from "$lib/utils"
77
import { zod } from "sveltekit-superforms/adapters"
88
import { getScriptByID, getScriptByURL, updateScript } from "$lib/server/scripts.server"
@@ -100,7 +100,8 @@ export const actions = {
100100
const fileName =
101101
form.data.script[i].name == form.data.main ? "script.simba" : form.data.script[i].name
102102
fileNames.push(fileName)
103-
storagePromises.push(uploadFile(supabaseServer, "scripts", path + fileName, form.data.script[i]))
103+
const promise = uploadFile(supabaseServer, "scripts", path + fileName, form.data.script[i])
104+
storagePromises.push(promise)
104105
}
105106
} else {
106107
const { data: filesData, error: filesError } = await supabaseServer
@@ -121,7 +122,8 @@ export const actions = {
121122

122123
filesData.files.forEach((name) => {
123124
fileNames.push(name)
124-
storagePromises.push(supabaseServer.storage.from("scripts").copy(old_path + name, new_path + name))
125+
const promise = reuseFile(supabaseServer, "scripts", old_path + name, new_path + name)
126+
storagePromises.push(promise)
125127
})
126128
}
127129

0 commit comments

Comments
 (0)