Code

Opened 5 years ago

Closed 4 years ago

Last modified 3 years ago

#10258 closed (fixed)

many files with the same file name cannot be saved correctly

Reported by: liangent Owned by: duncan
Component: File uploads/storage Version: 1.0
Severity: Keywords:
Cc: ianschenck Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description

The current Django code add '_' to file name in order to avoid overwrite saved files.
However, if I save many files with the same file name, Django will add many '_'s.
If the file name is too long, file will be saved correctly, but path in database will be truncated.

/var/lib/python-support/python2.5/django/db/backends/mysql/base.py:83: Warning: Data truncated for column 'output' at row 1
  return self.cursor.execute(query, args)

Then when I try to get that file according to the path saved in database, it will fail...

Attachments (3)

10258.diff (3.3 KB) - added by ianschenck 5 years ago.
patch
10258.2.diff (3.9 KB) - added by Thilo 5 years ago.
Added one more doctest which uploads 10 files and checks for their names.
10258-on-9160.diff (4.7 KB) - added by Thilo 5 years ago.
Patch that applies when patch from #9610 has been applied before. Apart from that its identical to 10258.2.diff

Download all attachments as: .zip

Change History (16)

comment:1 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:2 Changed 5 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by marcelor

Is adding the creation timestamp to the filename a good solution for this?

comment:4 Changed 5 years ago by marcelor

I mean, that way multiple files with the same filename can be saved with different names keeping them short.

comment:5 Changed 5 years ago by bthomas

Timestamps could still be long. Appending a number to the end of a filename would be much better than more underscores, though.

Something like:

name
name_0
name_1
name_2
...etc

Changed 5 years ago by ianschenck

patch

comment:6 Changed 5 years ago by ianschenck

  • Cc ianschenck added
  • Has patch set

comment:7 Changed 5 years ago by duncan

  • Owner changed from nobody to duncan

comment:8 Changed 5 years ago by duncan

  • Needs tests set

Patch to the code looks fairly sensible to me. I think we could do with some more tests though.

comment:9 Changed 5 years ago by adurdin

Fixing this may affect the patch or tests for #9610

Changed 5 years ago by Thilo

Added one more doctest which uploads 10 files and checks for their names.

comment:10 Changed 5 years ago by Thilo

#9610 indeed conflicts with this issue: both of the patches change the same lines in django/core/files/storage.py. I merged them locally and created a diff for 10258 which will apply on top of #9610. Attachment follows.

Changed 5 years ago by Thilo

Patch that applies when patch from #9610 has been applied before. Apart from that its identical to 10258.2.diff

comment:11 Changed 5 years ago by jacob

  • milestone changed from 1.1 to 1.2

Punting to 1.2. For now, use a custom upload_to and/or override the storage engine's get_available_filename function.

comment:12 Changed 4 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

(In [12552]) Fixed #10258: handle duplicate file names better.

Instead of just continually appending "_" to duplicate file names, Django's
default storage now appends _1, _2, _3, etc.

Thanks to ianschenck and Thilo.

comment:13 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.