diff roundup/backends/back_anydbm.py @ 5725:6923225fd781

Handle UnicodeDecodeError in file class when file contents are not text (e.g. jpg). This shows up in python3 due to conversion from bytes to string. This allows binary file retrieval via the rest interface to complete.
author John Rouillard <rouilj@ieee.org>
date Wed, 22 May 2019 20:56:59 -0400
parents aa13a517cc63
children 936275dfe1fa
line wrap: on
line diff
--- a/roundup/backends/back_anydbm.py	Wed May 22 17:35:47 2019 -0400
+++ b/roundup/backends/back_anydbm.py	Wed May 22 20:56:59 2019 -0400
@@ -36,6 +36,8 @@
 
 from roundup.backends.indexer_common import get_indexer
 
+from hashlib import md5
+
 def db_exists(config):
     # check for the user db
     for db in 'nodes.user nodes.user.db'.split():
@@ -2195,6 +2197,15 @@
                 # XXX by catching this we don't see an error in the log.
                 return 'ERROR reading file: %s%s\n%s\n%s'%(
                         self.classname, nodeid, poss_msg, strerror)
+            except UnicodeDecodeError as e:
+                # if content is not text (e.g. jpeg file) we get
+                # unicode error trying to convert to string in python 3.
+                # trap it and supply an error message. Include md5sum
+                # of content as this string is included in the etag
+                # calculation of the object.
+                return ('%s%s is not text, retrieve using '
+                        'binary_content property. mdsum: %s')%(self.classname,
+                   nodeid, md5(self.db.getfile(self.classname, nodeid, None)).hexdigest())
         elif propname == 'binary_content':
             return self.db.getfile(self.classname, nodeid, None)
 

Roundup Issue Tracker: http://roundup-tracker.org/