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

Clean up frontend file upload code #2308

Open
leblowl opened this issue Feb 20, 2024 · 0 comments
Open

Clean up frontend file upload code #2308

leblowl opened this issue Feb 20, 2024 · 0 comments

Comments

@leblowl
Copy link
Collaborator

leblowl commented Feb 20, 2024

With #2306 it looks like we might be able to remove this logic:

if (name === 'image') {
id = `${Date.now()}_${Math.random().toString(36).substring(0, 20)}`
} else {
id = name
}
or at least move it all into the writeTempFile handler so the logic is altogether.

Other ideas:

  • Move some of this logic out of the component and into handleClipboardFiles:
    const fileExt = path.extname(files[i].name).toLowerCase()
    const fileName = path.basename(files[i].name, fileExt)
    const arrayBuffer = await files[i].arrayBuffer()
    handleClipboardFiles(arrayBuffer, fileExt, fileName)
  • Unify the two methods for selecting a file to upload (copy/paste and file manager):
    export const getFileData = (filePath: string, isTmpPath = false): FilePreviewData => {
    const fileContent: FileContent = {
    path: filePath,
    tmpPath: isTmpPath ? filePath : undefined,
    name: path.basename(filePath, path.extname(filePath)),
    ext: path.extname(filePath).toLowerCase(),
    }
    const id = `${Date.now()}_${Math.random().toString(36).substring(0, 20)}`
    return { [id]: fileContent }
    }

I haven't looked too deeply at this code, so you might want to check my assumptions before making changes. But in general it looks like we can clean things up a bit. Feel free to get creative with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Backlog - Desktop & Backend
Development

No branches or pull requests

2 participants