diff --git a/bun.lockb b/bun.lockb index 2fc343c..d389668 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/runtime/composables/useFileStorage.ts b/src/runtime/composables/useFileStorage.ts index 5f4a26a..6f10dd6 100644 --- a/src/runtime/composables/useFileStorage.ts +++ b/src/runtime/composables/useFileStorage.ts @@ -1,47 +1,41 @@ -import { ref } from "vue"; +import { ref } from 'vue' export default function () { - const files = ref([]) - 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 => { + const files = ref([]) + const serializeFile = (file: ClientFile): Promise => { return new Promise((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) => { + 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 -} diff --git a/src/runtime/server/utils/storage.ts b/src/runtime/server/utils/storage.ts index 35e20ff..1e9c58b 100644 --- a/src/runtime/server/utils/storage.ts +++ b/src/runtime/server/utils/storage.ts @@ -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 => { @@ -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 = '' diff --git a/types/types.d.ts b/types/types.d.ts new file mode 100644 index 0000000..558a53f --- /dev/null +++ b/types/types.d.ts @@ -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 +}