Commit 3d8fc829 authored by Jérome Perrin's avatar Jérome Perrin Committed by Kazuhiko Shiozaki

File: treat data as bytes, not str

parent 8ee1faca
...@@ -589,8 +589,8 @@ class TestDocument(TestDocumentMixin): ...@@ -589,8 +589,8 @@ class TestDocument(TestDocumentMixin):
def testTempOOoDocument_get_size(self): def testTempOOoDocument_get_size(self):
# test get_size on temporary OOoDocument # test get_size on temporary OOoDocument
doc = self.portal.newContent(temp_object=True, portal_type='OOo Document', id='tmp') doc = self.portal.newContent(temp_object=True, portal_type='OOo Document', id='tmp')
doc.edit(data='OOo') doc.edit(data=b'OOo')
self.assertEqual(len('OOo'), doc.get_size()) self.assertEqual(len(b'OOo'), doc.get_size())
def testOOoDocument_hasData(self): def testOOoDocument_hasData(self):
# test hasData on OOoDocument # test hasData on OOoDocument
...@@ -1279,7 +1279,7 @@ class TestDocument(TestDocumentMixin): ...@@ -1279,7 +1279,7 @@ class TestDocument(TestDocumentMixin):
display='thumbnail') display='thumbnail')
self.assertEqual(mime, 'image/png') self.assertEqual(mime, 'image/png')
# it's a valid PNG # it's a valid PNG
self.assertEqual(image_data[1:4], 'PNG') self.assertEqual(image_data[1:4], b'PNG')
def test_PDFToJpg(self): def test_PDFToJpg(self):
upload_file = makeFileUpload('REF-en-001.pdf') upload_file = makeFileUpload('REF-en-001.pdf')
...@@ -1290,7 +1290,7 @@ class TestDocument(TestDocumentMixin): ...@@ -1290,7 +1290,7 @@ class TestDocument(TestDocumentMixin):
frame=0, frame=0,
display='thumbnail') display='thumbnail')
self.assertEqual(mime, 'image/jpeg') self.assertEqual(mime, 'image/jpeg')
self.assertEqual(image_data[6:10], 'JFIF') self.assertEqual(image_data[6:10], b'JFIF')
def test_PDFToGif(self): def test_PDFToGif(self):
upload_file = makeFileUpload('REF-en-001.pdf') upload_file = makeFileUpload('REF-en-001.pdf')
...@@ -1301,7 +1301,7 @@ class TestDocument(TestDocumentMixin): ...@@ -1301,7 +1301,7 @@ class TestDocument(TestDocumentMixin):
frame=0, frame=0,
display='thumbnail') display='thumbnail')
self.assertEqual(mime, 'image/gif') self.assertEqual(mime, 'image/gif')
self.assertEqual(image_data[0:4], 'GIF8') self.assertEqual(image_data[0:4], b'GIF8')
def test_PDFToTiff(self): def test_PDFToTiff(self):
upload_file = makeFileUpload('REF-en-001.pdf') upload_file = makeFileUpload('REF-en-001.pdf')
...@@ -1312,7 +1312,7 @@ class TestDocument(TestDocumentMixin): ...@@ -1312,7 +1312,7 @@ class TestDocument(TestDocumentMixin):
frame=0, frame=0,
display='thumbnail') display='thumbnail')
self.assertEqual(mime, 'image/tiff') self.assertEqual(mime, 'image/tiff')
self.assertIn(image_data[0:2], ('II', 'MM')) self.assertIn(image_data[0:2], (b'II', b'MM'))
def test_PDF_content_information(self): def test_PDF_content_information(self):
......
...@@ -38,13 +38,6 @@ from OFS.Image import File as OFS_File ...@@ -38,13 +38,6 @@ from OFS.Image import File as OFS_File
from Products.ERP5Type.Utils import deprecated from Products.ERP5Type.Utils import deprecated
def _unpackData(data):
"""
Unpack Pdata into string
OBSOLETED. use str(data) instead, because Pdata.__str__ is defined.
"""
return str(data)
_MARKER = object() _MARKER = object()
class File(Document, OFS_File): class File(Document, OFS_File):
...@@ -147,7 +140,7 @@ class File(Document, OFS_File): ...@@ -147,7 +140,7 @@ class File(Document, OFS_File):
if data is None: if data is None:
return return
if self.hasData(): if self.hasData():
if str(data.read()) == str(self.getData()): if bytes(data.read()) == bytes(self.getData()):
# Same data as previous, no need to change its content # Same data as previous, no need to change its content
return return
else: else:
...@@ -180,13 +173,13 @@ class File(Document, OFS_File): ...@@ -180,13 +173,13 @@ class File(Document, OFS_File):
security.declareProtected(Permissions.AccessContentsInformation, 'getData') security.declareProtected(Permissions.AccessContentsInformation, 'getData')
def getData(self, default=None): def getData(self, default=None):
"""return Data as str.""" """return Data as bytes."""
self._checkConversionFormatPermission(None) self._checkConversionFormatPermission(None)
data = self._baseGetData() data = self._baseGetData()
if data is None: if data is None:
return None return None
else: else:
return str(data) return bytes(data)
# DAV Support # DAV Support
security.declareProtected(Permissions.ModifyPortalContent, 'PUT') security.declareProtected(Permissions.ModifyPortalContent, 'PUT')
...@@ -226,8 +219,8 @@ class File(Document, OFS_File): ...@@ -226,8 +219,8 @@ class File(Document, OFS_File):
elif getattr(self, 'getBaseData', None) is not None: elif getattr(self, 'getBaseData', None) is not None:
content = self.getBaseData() content = self.getBaseData()
if content and not isinstance(content, str): if content and not isinstance(content, bytes):
content = str(content) content = bytes(content)
return (mime_type, content) return (mime_type, content)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment