Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: removed util function #1323

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/lib/components/Channel/Chat/DrawerEditChannel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { category_list } from '$lib/stores/channelStore'
import { emitChannelUpdate } from '$lib/websocket'
import IconChatScreenshot from '$lib/assets/icons/chat/IconChatScreenshot.svelte'
import { captureScreenShot, convertFileToStringOrDel } from '$lib/utils'
import { captureScreenShot, fileToBase64 } from '$lib/utils'

export let channel: any, showDrawer: boolean, isLive: boolean

Expand Down Expand Up @@ -106,9 +106,22 @@
class="flex h-full p-5"
action="?/edit-channel"
method="post"
use:enhance={({ formData }) => {
use:enhance={async ({ formData }) => {
let thumbnail = formData.get('thumbnail') || null
if (thumbnail != null && thumbnail instanceof File && thumbnail.size > 0) {
await fileToBase64(thumbnail)
.then((base64String) => {
if (base64String) {
formData.set('thumbnail', base64String)
}
})
.catch((error) => {
console.error('Error converting file to base64:', error)
})
} else {
formData.delete('thumbnail')
}
return ({ result }) => {
convertFileToStringOrDel(formData, 'thumbnail')
if (result.type === 'success') {
console.log('channel', channel)
emitChannelUpdate({ channelSocket: channel.socket, channel })
Expand Down
38 changes: 29 additions & 9 deletions src/lib/components/Profile/DrawerEditProfile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
import { page } from '$app/stores'
import DrawerAddCategory from '$lib/components/Browse/DrawerAddCategory.svelte'
import { category_list } from '$lib/stores/channelStore'
import {
createEffect,
objectMonitor,
isValidUrl,
fileToBase64,
convertFileToStringOrDel
} from '$lib/utils'
import { createEffect, objectMonitor, isValidUrl, fileToBase64 } from '$lib/utils'

export let showDrawer: boolean
export let profile: any
Expand Down Expand Up @@ -116,8 +110,34 @@
action="?/update-profile"
method="post"
use:enhance={async ({ formData }) => {
convertFileToStringOrDel(formData, 'avatar')
convertFileToStringOrDel(formData, 'banner')
let avatar = formData.get('avatar') || null
if (avatar != null && avatar instanceof File && avatar.size > 0) {
await fileToBase64(avatar)
.then((base64String) => {
if (base64String) {
formData.set('avatar', base64String)
}
})
.catch((error) => {
console.error('Error converting file to base64:', error)
})
} else {
formData.delete('avatar')
}
let banner = formData.get('banner') || null
if (banner != null && banner instanceof File && banner.size > 0) {
await fileToBase64(banner)
.then((base64String) => {
if (base64String) {
formData.set('banner', base64String)
}
})
.catch((error) => {
console.error('Error converting file to base64:', error)
})
} else {
formData.delete('banner')
}
if (profile?.category?.length) {
formData.append('category', JSON.stringify(profile?.category))
}
Expand Down
32 changes: 16 additions & 16 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,19 +477,19 @@ export const base64ToFile = (base64: string): File => {
return new File([ab], filename, { type: mimeType })
}

export const convertFileToStringOrDel = async (formData: any, inputName: string) => {
let file = formData.get(inputName) || null
if (file != null && file instanceof File && file.size > 0) {
await fileToBase64(file)
.then((base64String) => {
if (base64String) {
formData.set(inputName, base64String)
}
})
.catch((error) => {
console.error('Error converting file to base64:', error)
})
} else {
formData.delete(inputName)
}
}
// export const convertFileToStringOrDel = async (formData: any, inputName: string) => {
// let file = formData.get(inputName) || null
// if (file != null && file instanceof File && file.size > 0) {
// await fileToBase64(file)
// .then((base64String) => {
// if (base64String) {
// formData.set(inputName, base64String)
// }
// })
// .catch((error) => {
// console.error('Error converting file to base64:', error)
// })
// } else {
// formData.delete(inputName)
// }
// }