#102 Support binary attachment like images and pdf
Merged 7 years ago by cverna. Opened 7 years ago by cverna.
cverna/pagure-importer fix_dump_attachments  into  master

@@ -267,8 +267,14 @@ 

          for key in attachments.keys():

              filename = get_secure_filename(attachments[key], key)

              attach_path = os.path.join(folder, 'files', filename)

-             with open(attach_path, 'w') as stream:

-                 stream.write(str(attachments[key]))

+             # Try decoding Bytes to UTF-8

+             try:

+                 with open(attach_path, 'w') as stream:

+                     stream.write(attachments[key].decode())

+             # If it fails write the data as binary

+             except UnicodeDecodeError:

+                 with open(attach_path, 'wb') as stream:

+                     stream.write(attachments[key])

              files.append('files/' + filename)

  

      # Write down what changed

If we cannot decode the attachment then dump the content in bytes. This is needed for images (jpg, png, etc) and pdf.

Signed-off-by: Clement Verna cverna@tutanota.com

Looks fine, you may want to consider the binaryornot module to detect if the file is binary as the current approach relies on the assumption the attachment is always UTF-8

Thanks I'll merge this one, and add binary or not on my todo list :smile:

Pull-Request has been merged by cverna

7 years ago
Metadata