Skip to content

Rename File On Upload? Not Overwriting Other Uploads #1153

Discussion options

You must be logged in to vote

I found this thread because I want to sanitize filenames to not include spaces.
Since uploads always have a URL attached, should this maybe be a core functionality?

something like sanitizeFilename: true?

Solved it like this if anyone cares:

import { CollectionBeforeOperationHook } from 'payload/types'

/**
 * Sanitizes the filename for upload collections to not include spaces or special characters
 */
export const sanitizeFileName: CollectionBeforeOperationHook = async ({
  args,
}) => {
  const files = args.req?.files
  if (
    files !== undefined &&
    files !== null &&
    typeof files === 'object' &&
    files.file !== undefined &&
    files.file !== null &&
    typeof files.file === 

Replies: 5 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@jmikrut
Comment options

@andwrobs
Comment options

Comment options

You must be logged in to vote
2 replies
@ccssmnn
Comment options

@linobino1
Comment options

Answer selected by zubricks
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
10 participants