Skip to content

Commit

Permalink
Merge pull request #230 from collective/thet-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pbauer authored Nov 19, 2023
2 parents d3946d8 + 5c25ec7 commit 56bcb0e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog
1.11 (unreleased)
-----------------

- Try to restore broken blobs when exporting content.
[thet]

- When exporting into separate JSON files write also the error in a separate errors.json file.
This fixes an error at the end of the export and no errors being written.
[thet]
Expand Down
53 changes: 26 additions & 27 deletions src/collective/exportimport/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,6 @@ def get_blob_path(blob):

# Custom Serializers for Dexterity

@adapter(INamedImageField, IDexterityContent, IBase64BlobsMarker)
class ImageFieldSerializerWithBlobs(DefaultFieldSerializer):
def __call__(self):
try:
image = self.field.get(self.context)
except AttributeError:
image = None
if not image:
return None

if "built-in function id" in image.filename:
filename = self.context.id
else:
filename = image.filename

result = {
"filename": filename,
"content-type": image.contentType,
"data": base64.b64encode(image.data),
"encoding": "base64",
}
return json_compatible(result)


@adapter(INamedFileField, IDexterityContent, IBase64BlobsMarker)
class FileFieldSerializerWithBlobs(DefaultFieldSerializer):
Expand All @@ -109,10 +86,27 @@ def __call__(self):
if namedfile is None:
return None

if "built-in function id" in namedfile.filename:
filename = self.context.id
else:
filename = namedfile.filename
try:
if "built-in function id" in namedfile.filename:
filename = self.context.id
else:
filename = namedfile.filename
except AttributeError:
# Try to recover broken namedfile
# Related to: WARNING OFS.Uninstalled Could not import class 'NamedBlobFile' from module 'zope.app.file.file'
from ZODB.broken import Broken

if isinstance(namedfile, Broken):
broken_namedfile = namedfile.__Broken_state__
file = broken_namedfile["_blob"].open()
result = {
"filename": broken_namedfile["filename"],
"content-type": broken_namedfile["contentType"],
"data": base64.b64encode(file.read()),
"encoding": "base64",
}
file.close()
return result

result = {
"filename": filename,
Expand All @@ -123,6 +117,11 @@ def __call__(self):
return json_compatible(result)


@adapter(INamedImageField, IDexterityContent, IBase64BlobsMarker)
class ImageFieldSerializerWithBlobs(FileFieldSerializerWithBlobs):
pass


@adapter(IRichText, IDexterityContent, IRawRichTextMarker)
class RichttextFieldSerializerWithRawText(DefaultFieldSerializer):
def __call__(self):
Expand Down

0 comments on commit 56bcb0e

Please sign in to comment.