Skip to content

Commit

Permalink
Pass bytes for file content.
Browse files Browse the repository at this point in the history
  • Loading branch information
tseaver authored and hannosch committed May 13, 2017
1 parent f3e158d commit 832b6ae
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/OFS/Image.py
Expand Up @@ -53,7 +53,7 @@
'dtml/imageAdd', globals(), Kind='File', kind='file')


def manage_addFile(self, id, file='', title='', precondition='',
def manage_addFile(self, id, file=b'', title='', precondition='',
content_type='', REQUEST=None):
"""Add a new File object.
Expand All @@ -69,7 +69,7 @@ def manage_addFile(self, id, file='', title='', precondition='',
self = self.this()

# First, we create the file without data:
self._setObject(id, File(id, title, '', content_type, precondition))
self._setObject(id, File(id, title, b'', content_type, precondition))

newFile = self._getOb(id)

Expand Down Expand Up @@ -508,7 +508,7 @@ def _get_content_type(self, file, body, id, content_type=None):
if headers and 'content-type' in headers:
content_type = headers['content-type']
else:
if not isinstance(body, str):
if not isinstance(body, bytes):
body = body.data
content_type, enc = guess_content_type(
getattr(file, 'filename', id), body, content_type)
Expand All @@ -520,6 +520,9 @@ def _read_data(self, file):
n = 1 << 16

if isinstance(file, str):
raise ValueError("Must be bytes")

if isinstance(file, bytes):
size = len(file)
if size < n:
return (file, size)
Expand Down
26 changes: 13 additions & 13 deletions src/OFS/tests/testCopySupport.py
Expand Up @@ -70,7 +70,7 @@ def _initFolders(self):
folder1 = getattr(self.app, 'folder1')

manage_addFile(
folder1, 'file', file='', content_type='text/plain')
folder1, 'file', file=b'', content_type='text/plain')

# Hack, we need a _p_mtime for the file, so we make sure that it
# has one. We use a subtransaction, which means we can rollback
Expand Down Expand Up @@ -152,7 +152,7 @@ def testCut(self):
def testCopyNewObject(self):
self.assertFalse('newfile' in self.folder1.objectIds())
manage_addFile(self.folder1, 'newfile',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
cookie = self.folder1.manage_copyObjects(ids=('newfile',))
self.folder2.manage_pasteObjects(cookie)
self.assertTrue('newfile' in self.folder1.objectIds())
Expand All @@ -171,7 +171,7 @@ def testPasteSingleSameID(self):
self.assertTrue('file' in self.folder1.objectIds())
self.assertFalse('file' in self.folder2.objectIds())
manage_addFile(self.folder2, 'file',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
cookie = self.folder1.manage_copyObjects(ids=('file',))
result = self.folder2.manage_pasteObjects(cookie)
self.assertTrue('file' in self.folder1.objectIds())
Expand Down Expand Up @@ -209,7 +209,7 @@ def testPasteSingleSameIDMultipleTimes(self):

def testPasteSpecialName(self):
manage_addFile(self.folder1, 'copy_of_',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
cookie = self.folder1.manage_copyObjects(ids=('copy_of_',))
result = self.folder1.manage_pasteObjects(cookie)
self.assertEqual(self.folder1.objectIds(),
Expand All @@ -220,10 +220,10 @@ def testPasteMultiNotSameID(self):
self.assertTrue('file' in self.folder1.objectIds())
self.assertFalse('file1' in self.folder1.objectIds())
manage_addFile(self.folder1, 'file1',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
self.assertFalse('file2' in self.folder1.objectIds())
manage_addFile(self.folder1, 'file2',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
self.assertFalse('file' in self.folder2.objectIds())
self.assertFalse('file1' in self.folder2.objectIds())
self.assertFalse('file2' in self.folder2.objectIds())
Expand All @@ -246,19 +246,19 @@ def testPasteMultiSameID(self):
self.assertTrue('file' in self.folder1.objectIds())
self.assertFalse('file1' in self.folder1.objectIds())
manage_addFile(self.folder1, 'file1',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
self.assertFalse('file2' in self.folder1.objectIds())
manage_addFile(self.folder1, 'file2',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
self.assertFalse('file' in self.folder2.objectIds())
manage_addFile(self.folder2, 'file',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
self.assertFalse('file1' in self.folder2.objectIds())
manage_addFile(self.folder2, 'file1',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
self.assertFalse('file2' in self.folder2.objectIds())
manage_addFile(self.folder2, 'file2',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
cookie = self.folder1.manage_copyObjects(
ids=('file', 'file1', 'file2',))
result = self.folder2.manage_pasteObjects(cookie)
Expand Down Expand Up @@ -417,9 +417,9 @@ def test_copy_cant_copy_invisible_items(self):

folder1, folder2 = self._initFolders()
manage_addFile(folder1, 'private',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
manage_addFile(folder1, 'public',
file='', content_type='text/plain')
file=b'', content_type='text/plain')
folder1.private.manage_permission(view, roles=(), acquire=0)
folder2.manage_permission(add_folders, roles=('Anonymous',), acquire=1)

Expand Down
2 changes: 1 addition & 1 deletion src/OFS/tests/testRanges.py
Expand Up @@ -67,7 +67,7 @@ def setUp(self):
manage_addFolder(self.app, TESTFOLDER_NAME)
folder = getattr(self.app, TESTFOLDER_NAME)

data = string.ascii_letters
data = string.ascii_letters.encode('ascii')
manage_addFile(
folder, 'file', file=data, content_type='text/plain')

Expand Down
2 changes: 1 addition & 1 deletion src/OFS/tests/testTraverse.py
Expand Up @@ -97,7 +97,7 @@ def setUp(self):
)

manage_addFile(folder1, 'file',
file='', content_type='text/plain')
file=b'', content_type='text/plain')

# Hack, we need a _p_mtime for the file, so we make sure that it
# has one. We use a subtransaction, which means we can rollback
Expand Down

0 comments on commit 832b6ae

Please sign in to comment.