2

like django's upload_to

def upload_to(instance, filename):
    filename = time.strftime('%Y%m%d%H%M%S')
    ym = time.strftime('%Y%m')
    return 'uploads/%s/%s.jpg' % (ym,filename)

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    photo = models.ImageField(u"Image (Upload)",upload_to=upload_to)

file saved 'uploads/%s/%s.jpg'

but change for

photo = FileBrowseField("Image", max_length=200, directory="uploads/", extensions=[".jpg"], blank=True, null=True)

How to rename a file before uploading it to a folder like django's upload_to

1

2 Answers 2

1

In filebrowser/sites.py you can create a hook for this when uploading / handling uploads:

def _upload_file(self, request):
    """
    Upload file to the server.
    """
    if request.method == "POST":
        folder = request.GET.get('folder', '')

        if len(request.FILES) == 0:
            return HttpResponseBadRequest('Invalid request! No files included.')
        if len(request.FILES) > 1:
            return HttpResponseBadRequest('Invalid request! Multiple files included.')

        filedata = list(request.FILES.values())[0]

        fb_uploadurl_re = re.compile(r'^.*(%s)' % reverse("filebrowser:fb_upload", current_app=self.name))
        folder = fb_uploadurl_re.sub('', folder)

        path = os.path.join(self.directory, folder)
        # we convert the filename before uploading in order
        # to check for existing files/folders
        file_name = convert_filename(filedata.name)
        filedata.name = file_name
        file_path = os.path.join(path, file_name
        ....

You can modify file_path here to whatever you like, or modify the file name.

For those of you that just want to ensure that files are not overwritten, you can set the FILEBROWSER_OVERWRITE_EXISTING flag in your settings.py as such:

FILEBROWSER_OVERWRITE_EXISTING = False

This will ensure that when you edit your files you give them a unique name, and it also ensures new uploads get their filename converted to something unique using filebrowsers convert_filename method defined in filebrowser/utils.py

More on filebrowser settings here. Hope this helps :)

0

For all these many years of this question and similar questions on other sites, I have not found a ready-made solution, but only hints of it. It's time to post the solution: signals.py:

from filebrowser.signals import filebrowser_pre_upload
import os
import time

def pre_upload_callback(sender, **kwargs):
    fullname = kwargs['file'].name
    filename, extension = os.path.splitext(fullname)
    new_filename = time.strftime('%Y%m%d%H%M%S')

    kwargs['file'].name = new_filename + extension


filebrowser_pre_upload.connect(pre_upload_callback)

I hope I helped someone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.