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 conflicting File name and Promise resolve issue #19

Merged
merged 1 commit into from
Oct 9, 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
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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Align types between ServerFile and ClientFile

The types for 'size' and 'lastModified' in ServerFile are strings, while in ClientFile they are more specific (number). Consider aligning these types for consistency and to prevent potential type-related issues.

interface ServerFile {
  name: string;
  content: string;
  size: number;
  lastModified: number;
}

name: string
content: string
size: string
type: string
lastModified: string
}

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