|
18 | 18 | from OFS.Image import Pdata
|
19 | 19 | from plone.namedfile.file import FileChunk
|
20 | 20 | from plone.namedfile.file import NamedBlobImage
|
| 21 | +from plone.namedfile.file import NamedBlobFile |
| 22 | +from plone.namedfile import field |
21 | 23 | from plone.namedfile.testing import PLONE_NAMEDFILE_FUNCTIONAL_TESTING
|
22 | 24 | from plone.namedfile.tests import getFile
|
23 | 25 |
|
@@ -56,3 +58,44 @@ def test_opened_file_storable(self):
|
56 | 58 | if os.path.exists(path):
|
57 | 59 | os.remove(path)
|
58 | 60 | self.assertEqual(303, fi.getSize())
|
| 61 | + |
| 62 | + def test_upload_no_read(self): |
| 63 | + # ensure we don't read the whole file into memory |
| 64 | + |
| 65 | + import ZODB.blob |
| 66 | + |
| 67 | + old_open = ZODB.blob.Blob.open |
| 68 | + blob_read = 0 |
| 69 | + blob_write = 0 |
| 70 | + |
| 71 | + def count_open(self, mode="r"): |
| 72 | + nonlocal blob_read, blob_write |
| 73 | + blob_read += 1 if "r" in mode else 0 |
| 74 | + blob_write += 1 if "w" in mode else 0 |
| 75 | + return old_open(self, mode) |
| 76 | + |
| 77 | + with unittest.mock.patch.object(ZODB.blob.Blob, 'open', count_open): |
| 78 | + data = getFile("image.gif") |
| 79 | + f = tempfile.NamedTemporaryFile(delete=False) |
| 80 | + try: |
| 81 | + path = f.name |
| 82 | + f.write(data) |
| 83 | + f.close() |
| 84 | + with open(path, "rb") as f: |
| 85 | + fi = NamedBlobFile(f, filename="image.gif") |
| 86 | + finally: |
| 87 | + if os.path.exists(path): |
| 88 | + os.remove(path) |
| 89 | + self.assertEqual(303, fi.getSize()) |
| 90 | + self.assertEqual(blob_read, 1, "blob should have only been opened to get size") |
| 91 | + self.assertEqual( |
| 92 | + blob_write, |
| 93 | + 1, |
| 94 | + "Slow write to blob instead of os rename. Should be only 1 on init", |
| 95 | + ) |
| 96 | + blob_read = 0 |
| 97 | + |
| 98 | + blob_field = field.NamedBlobFile() |
| 99 | + blob_field.validate(fi) |
| 100 | + |
| 101 | + self.assertEqual(blob_read, 0, "Validation is reading the whole blob in memory") |
0 commit comments