Ticket #5 (assigned defect)

Opened 2 years ago

Last modified 2 years ago

Should PhotoField automatically rename uploaded files?

Reported by: 235 Assigned to: bryan (accepted)
Priority: minor Milestone:
Component: PhotoField Version:
Keywords: file name image name Cc:

Description

I think it's not a great idea to change image file name. Search engines looks onto them to index.

I've changed it a little (3 lines marked with comments):

    def save_file(self, new_data, new_object, original_object, change, rel):
        field_names = self.get_manipulator_field_names('')
        upload_field_name = field_names[0]
        #filename = '%s-%s.jpg' % (self.parent_pk, self.get_attname())      #235 - fix names
        # If there is no DeleteCheckbox or the DeleteCheckbox was not checked
        if len(field_names) < 3 or not new_data.get(field_names[2], False):
            if new_data.get(upload_field_name, False):
                if rel:
                    #new_data[upload_field_name][0]["filename"] = filename  #235 - fix names
                    filename = new_data[upload_field_name][0]["filename"]
                    new_data[upload_field_name][0]["content"] = PhotoField.resize(new_data[upload_field_name][0]["content"], self.width, self.height, self.mode, self.quality)
                else:
                    #new_data[upload_field_name]["filename"] = filename     #235- fix names
                    filename = new_data[upload_field_name]["filename"]
                    new_data[upload_field_name]["content"] = PhotoField.resize(new_data[upload_field_name]["content"], self.width, self.height, self.mode, self.quality)
                # If file exists, delete it
                filepath = os.path.join(settings.MEDIA_ROOT, self.upload_to, filename)
                if os.path.exists(filepath):
                    os.remove(filepath)
        super(PhotoField, self).save_file(new_data, new_object, original_object, change, rel)

Change History

09/07/06 19:32:30 changed by bryan

  • status changed from new to assigned.
  • owner changed from somebody to bryan.
  • summary changed from PhotoField changes image file name after upload to Should PhotoField automatically rename uploaded files?.

Renaming the filenames of uploaded photos was an intentional design decision. The idea was to assign unique filenames to reduce the number of orphaned files, as is oft to occur with Django's core FileField and ImageField.

However, I understand why some developers might not want the automatic renaming to occur. I think a better way to address this issue would be to pass a configuration parameter into the constructor that determines whether or not the uploaded filenames get renamed. What do you think?

09/09/06 17:35:03 changed by 235

I've catched that u've done that intentionaly. But sometimes, like in my situation, better to have meaningful file names for better site indexing.

It's good idea to move out this like a configuration parameter