How Uploadcare sanitizes filenames?

We convert filenames to a valid format. If your filename contains unacceptable symbols they will be stripped. Valid symbols include ASCII letters, digits, and underscore _ symbol. Both the original and sanitized versions can be found in file metadata under the original_filename and filename keys correspondingly. 

If your backend needs to know the new filename after uploading, here's how our sanitization function works:

import os.path
import string

VALID_CHARS = '_%s%s' % (string.ascii_letters, string.digits)

def clean_filename_part(part):
    return ''.join(c for c in part if c in VALID_CHARS)

def clean_filename(fname):
    root, ext = os.path.splitext(fname)

    root = clean_filename_part(root)
    ext = clean_filename_part(ext)

    fmt = '{root}.{ext}'
    if not ext:
        fmt = '{root}'
    if not root:
        root = 'noroot'

    return fmt.format(root=root, ext=ext)

A sanitized filename version is used when you copy files from Uploadcare to your S3 bucket (backup or custom storage).

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.