Skip to content

Commit

Permalink
Merge pull request #19 from tengju/bugfix/fix-early-promise-resolve
Browse files Browse the repository at this point in the history
Fix conflicting File name and Promise resolve issue
  • Loading branch information
NyllRE authored Oct 9, 2024
2 parents b2c4407 + e0ad3a7 commit 095930b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 44 deletions.
Binary file modified bun.lockb
Binary file not shown.
62 changes: 28 additions & 34 deletions src/runtime/composables/useFileStorage.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
import { ref } from "vue";
import { ref } from 'vue'

export default function () {
const files = ref<File[]>([])
const serializeFile = (file: File) => {
const reader = new FileReader()
reader.onload = (e: any) => {
files.value.push({
...file,
name: file.name,
size: file.size,
type: file.type,
lastModified: file.lastModified,
content: e.target.result,
})
}
reader.readAsDataURL(file)
}

const handleFileInput = (event: any): Promise<void> => {
const files = ref<ClientFile[]>([])
const serializeFile = (file: ClientFile): Promise<void> => {
return new Promise<void>((resolve, reject) => {
files.value.splice(0)
// console.log('handleFileInput event: ' + event)

const promises = []
for (const file of event.target.files) {
promises.push(serializeFile(file))
const reader = new FileReader()
reader.onload = (e: ProgressEvent<FileReader>) => {
files.value.push({
...file,
name: file.name,
size: file.size,
type: file.type,
lastModified: file.lastModified,
content: e.target?.result,
})
resolve()
}

Promise.all(promises)
.then(() => resolve())
.catch((error) => reject(error))
reader.onerror = (error) => {
reject(error)
}
reader.readAsDataURL(file)
})
}

const handleFileInput = async (event: any) => {
files.value.splice(0)

const promises = []
for (const file of event.target.files) {
promises.push(serializeFile(file))
}

await Promise.all(promises)
}

return {
files,
handleFileInput,
}
}

interface File extends Blob {
content: any
name: string
lastModified: string
}
11 changes: 1 addition & 10 deletions src/runtime/server/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useRuntimeConfig } from '#imports'
* @prop filelocation: provide the folder you wish to locate the file in
*/
export const storeFileLocally = async (
file: File,
file: ServerFile,
fileNameOrIdLength: string | number,
filelocation: string = '',
): Promise<string> => {
Expand Down Expand Up @@ -38,15 +38,6 @@ export const deleteFile = async (filename: string, filelocation: string = '') =>
await rm(`${location}${filelocation}/${filename}`)
}

interface File {
name: string
content: string
size: string
type: string
lastModified: string
}


const generateRandomId = (length: number) => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let randomId = ''
Expand Down
13 changes: 13 additions & 0 deletions types/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface ServerFile {
name: string
content: string
size: string
type: string
lastModified: string
}

interface ClientFile extends Blob {
content: string | ArrayBuffer
name: string
lastModified: number
}

0 comments on commit 095930b

Please sign in to comment.