Opened 6 weeks ago

Last modified 3 days ago

#35326 assigned Bug

OverwritingStorageTests fail if a TemporaryUploadedFile is used

Reported by: bcail Owned by: bcail
Component: File uploads/storage Version: dev
Severity: Normal Keywords:
Cc: bcail, Tim Graham Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Ticket #28144 added the option of using custom flags on a storage object to allow overwriting files in storage. However, this doesn't seem to work for temporary uploaded files, since the alternate path is taken in the _save method.

Here is an example test that fails for me - it loops forever:

diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index 420314573d..d404300708 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -648,6 +648,34 @@ class OverwritingStorageTests(FileStorageTests):
         finally:
             self.storage.delete(name)
 
+    def test_save_overwrite_behavior_temp_file(self):
+        """Saving to same file name twice overwrites the first file."""
+        name = "test.file"
+        self.assertFalse(self.storage.exists(name))
+        content_1 = b"content one"
+        content_2 = b"second content"
+        f_1 = TemporaryUploadedFile('tmp1', 'text/plain', 11, 'utf8')
+        f_1.write(content_1)
+        f_1.seek(0)
+        f_2 = TemporaryUploadedFile('tmp2', 'text/plain', 14, 'utf8')
+        f_2.write(content_2)
+        f_2.seek(0)
+        stored_name_1 = self.storage.save(name, f_1)
+        try:
+            self.assertEqual(stored_name_1, name)
+            self.assertTrue(self.storage.exists(name))
+            self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name)))
+            with self.storage.open(name) as fp:
+                self.assertEqual(fp.read(), content_1)
+            stored_name_2 = self.storage.save(name, f_2)
+            self.assertEqual(stored_name_2, name)
+            self.assertTrue(self.storage.exists(name))
+            self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name)))
+            with self.storage.open(name) as fp:
+                self.assertEqual(fp.read(), content_2)
+        finally:
+            self.storage.delete(name)

Change History (10)

comment:1 by alexialg05, 6 weeks ago

can i be assigned on this issue?

comment:2 by bcail, 6 weeks ago

@alexialg05, yes, you can assign yourself to this issue, but we should probably see if others confirm this as an issue. If it is confirmed as an issue, there may be design questions to figure out.

comment:3 by Natalia Bidart, 6 weeks ago

Cc: Tim Graham added
Triage Stage: UnreviewedAccepted

Hello bcail, thank you for your report!

This definitely seems like a valid issue, file_move_safe (from django.core.files.move) requires the allow_overwrite flag to be passed to perform overwrites. I don't have clarity on the possible fix, I agree that it requires a design decision. A forum post seeking advice would be the best next step, I think.

comment:4 by bcail, 6 weeks ago

Thanks, @Natalia. I opened a new forum post.

comment:5 by bcail, 5 weeks ago

I opened a draft PR with a possible direction we could go.

comment:6 by Natalia Bidart, 5 weeks ago

Has patch: set
Owner: changed from nobody to bcail
Status: newassigned

comment:7 by Sarah Boyce, 9 days ago

Patch needs improvement: set

comment:8 by bcail, 3 days ago

Patch needs improvement: unset

comment:9 by Sarah Boyce, 3 days ago

Patch needs improvement: set

comment:10 by bcail, 3 days ago

Patch needs improvement: unset
Note: See TracTickets for help on using tickets.
Back to Top