Opened 8 years ago

Closed 7 years ago

Last modified 2 years ago

#10410 closed Uncategorized (worksforme)

FileField saves one filename on disk and another on DB

Reported by: Elland Owned by: Peter Bengtsson
Component: File uploads/storage Version:
Severity: Normal Keywords: FileField valid filename
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When saving a file with spaces on the name, it will be saved with underscores for spaces on disk, but the database will point to it without underscores,
but with the regular spaces, leading to a File Not Found error.

Change History (11)

comment:1 Changed 8 years ago by Jacob

milestone: 1.21.1
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

comment:2 Changed 8 years ago by Russell Keith-Magee

Component: UncategorizedFile uploads/storage

comment:3 Changed 8 years ago by Peter Bengtsson

Owner: changed from nobody to Peter Bengtsson
Status: newassigned

comment:4 Changed 8 years ago by Peter Bengtsson

Unable to reproduce. Currently on revision 10189.

When uploading a file called "my file.txt" it gets saved on the filesystem as "my_file.txt" and in SQL it's stored as "./my_file.txt".
Thus, there's no reference back to "my file.txt".

Also I don't know how to actually reproduce the "leading to a File Not Found error". What does that mean? How are you *viewing* the FileField

comment:5 Changed 8 years ago by Peter Bengtsson

Resolution: invalid
Status: assignedclosed

This test proves that the bug is not reproducable

models.py

from django.db import models
class MyModel(models.Model):
    my_file = models.FileField(upload_to='.')

tests.py

import os
from django.test import TestCase
from django.conf import settings
from models import MyModel
                              
class SimpleTest(TestCase):

    def test_file_upload(self):

        # create a fake file to upload
        instance = MyModel.objects.create()
        try:
            open('upload me.txt','w').write('any junk\n')
            from django.core.files import File
            f = File(open('upload me.txt'))
            settings.MEDIA_URL = ''
            instance.my_file.save('with spaces.txt', f)

            # django replaces all the spaces with underscores
            assert os.path.isfile('with_spaces.txt')
            # but in the database it will be 'with spaces.txt'
            self.assertEqual(instance.my_file, u'./with_spaces.txt')
            self.assertEqual(instance.my_file.url, './with_spaces.txt')
        finally:
            os.remove('upload me.txt')
            if os.path.isfile('with_spaces.txt'):
                os.remove('with_spaces.txt')

I think what this proves is that there's no problem with "with spaces.txt" once the file has been uploaded.

comment:6 Changed 7 years ago by anonymous

Resolution: invalid
Status: closedreopened

I can reproduce this bug. It also exists if there is a file with the same name. If you upload two images with the same name, then when you try to show the first image, the second image is shown. (because the second image get a '_' appended to it, but the name in the database does not)

I wrote a bug report here:
http://code.djangoproject.com/ticket/11504
I originally thought it was specific to django-storages, so I submitted a bug report to django-storages:
http://code.welldev.org/django-storages/issue/13/the-database-doesnt-store-the-correct-file-name

comment:7 Changed 7 years ago by Russell Keith-Magee

Resolution: worksforme
Status: reopenedclosed

peterbe has contributed an executable test case that implements the problem case described in the original ticket, and that test case passes on Django Trunk under SQLite, Postgres and MySQL.

I don't doubt that you are experiencing a problem, but at this point, the onus is on you to establish how your error case differs from the working test case that we have. We can't fix any bug that we can't reproduce.

comment:8 Changed 7 years ago by rent

Where in the code-base does Django write to the database? I want to see if I can trace is myself, to see what I have done wrong.

comment:9 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

comment:10 Changed 2 years ago by m1ndpixel

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset
Last edited 2 years ago by m1ndpixel (previous) (diff)

comment:11 Changed 2 years ago by Collin Anderson

Is this still an issue now that upload_to is optional?

Note: See TracTickets for help on using tickets.
Back to Top