Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #440 - Crash on non-utf8 text file #441

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions elodie/media/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,21 @@ def parse_metadata_line(self):
if source is None:
return None

# FIXME / TODO document why this is being done issue https://github.com/jmathai/elodie/issues/424
# Read first line of text file, check if it is valid json (i.e. utf-8)
with open(source, 'r') as f:
first_line = f.readline().strip()
try:
first_line = f.readline().strip()
except UnicodeDecodeError:
log.error('Could not load JSON from first line of file %r' % source)
return None # no valid meta data

try:
parsed_json = loads(first_line)
if isinstance(parsed_json, dict):
self.metadata_line = parsed_json
except ValueError:
log.error('Could not parse JSON from first line: %s' % first_line)
log.error('Could not parse JSON from file %r first line: %s' % (source, first_line))
pass

def write_metadata(self, **kwargs):
Expand Down Expand Up @@ -191,13 +197,13 @@ def write_metadata(self, **kwargs):
copyfileobj(f_read, f_write)
else:
# Prepend the metadata to the file
with open(source, 'r') as f_read:
with open(source, 'rb') as f_read:
original_contents = f_read.read()
with open(source, 'w') as f_write:
f_write.write("{}\n{}".format(
metadata_as_json,
original_contents)
)
with open(source, 'wb') as f_write:
f_write.write(metadata_as_json.encode('utf8')) # write first line json (utf-8 encoded) header
f_write.write(b'\n')
f_write.write(original_contents) # what ever format was already there


self.reset_cache()
return True