Opened 16 years ago

Closed 15 years ago

Last modified 10 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 by Jacob, 16 years ago

milestone: 1.21.1
Triage Stage: UnreviewedAccepted

comment:2 by Russell Keith-Magee, 16 years ago

Component: UncategorizedFile uploads/storage

comment:3 by Peter Bengtsson, 16 years ago

Owner: changed from nobody to Peter Bengtsson
Status: newassigned

comment:4 by Peter Bengtsson, 16 years ago

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 by Peter Bengtsson, 16 years ago

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 by anonymous, 15 years ago

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 by Russell Keith-Magee, 15 years ago

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 by rent, 15 years ago

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 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

comment:10 by m1ndpixel, 10 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset
Version 0, edited 10 years ago by m1ndpixel (next)

comment:11 by Collin Anderson, 10 years ago

Is this still an issue now that upload_to is optional?

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