Skip to content

Commit

Permalink
fix: prevent resources from having empty extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhd1701 committed Jan 12, 2022
1 parent 8fbc393 commit 9a30823
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
5 changes: 4 additions & 1 deletion enex2notion/enex_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ def _convert_resource(resource_raw):
file_name = res_attr.get("file-name")

if not file_name:
ext = mimetypes.guess_extension(resource_raw["mime"]) or ""
ext = mimetypes.guess_extension(resource_raw["mime"]) or ".bin"
file_name = f"{uuid.uuid4()}{ext}"
elif "." not in file_name:
ext = mimetypes.guess_extension(resource_raw["mime"]) or ".bin"
file_name = f"{file_name}{ext}"

if resource_raw["data"].get("#text"):
data_bin = base64.b64decode(resource_raw["data"]["#text"])
Expand Down
106 changes: 106 additions & 0 deletions tests/test_enex_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,112 @@ def test_iter_notes_single_with_resource(fs):
assert notes[0].resource_by_md5("000") is None


def test_iter_notes_single_with_noext_resource(fs):
test_enex = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">
<en-export export-date="20211218T085932Z" application="Evernote" version="10.25.6">
<note>
<title>test1</title>
<created>20211118T085332Z</created>
<updated>20211118T085920Z</updated>
<note-attributes>
</note-attributes>
<content>test</content>
<resource>
<data encoding="base64">
R0lGODlhAQABAAAAACwAAAAAAQABAAAC
</data>
<mime>image/gif</mime>
<resource-attributes>
<file-name>smallest</file-name>
</resource-attributes>
</resource>
</note>
</en-export>
"""
fs.create_file("test.enex", contents=test_enex)

notes = list(iter_notes(Path("test.enex")))

assert notes == [
EvernoteNote(
title="test1",
created=datetime.datetime(2021, 11, 18, 8, 53, 32, tzinfo=tzutc()),
updated=datetime.datetime(2021, 11, 18, 8, 59, 20, tzinfo=tzutc()),
content="test",
tags=[],
author="",
url="",
is_webclip=False,
resources=[
EvernoteResource(
data_bin=(
b"GIF89a\x01\x00\x01\x00\x00\x00\x00,"
b"\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02"
),
size=24,
md5="bc32ed98d624acb4008f986349a20d26",
mime="image/gif",
file_name="smallest.gif",
)
],
),
]


def test_iter_notes_single_with_noext_badmime_resource(fs):
test_enex = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">
<en-export export-date="20211218T085932Z" application="Evernote" version="10.25.6">
<note>
<title>test1</title>
<created>20211118T085332Z</created>
<updated>20211118T085920Z</updated>
<note-attributes>
</note-attributes>
<content>test</content>
<resource>
<data encoding="base64">
R0lGODlhAQABAAAAACwAAAAAAQABAAAC
</data>
<mime>application/unknown</mime>
<resource-attributes>
<file-name>smallest</file-name>
</resource-attributes>
</resource>
</note>
</en-export>
"""
fs.create_file("test.enex", contents=test_enex)

notes = list(iter_notes(Path("test.enex")))

assert notes == [
EvernoteNote(
title="test1",
created=datetime.datetime(2021, 11, 18, 8, 53, 32, tzinfo=tzutc()),
updated=datetime.datetime(2021, 11, 18, 8, 59, 20, tzinfo=tzutc()),
content="test",
tags=[],
author="",
url="",
is_webclip=False,
resources=[
EvernoteResource(
data_bin=(
b"GIF89a\x01\x00\x01\x00\x00\x00\x00,"
b"\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02"
),
size=24,
md5="bc32ed98d624acb4008f986349a20d26",
mime="application/unknown",
file_name="smallest.bin",
)
],
),
]


def test_iter_notes_single_with_noname_resource(fs, mocker):
test_enex = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">
Expand Down

0 comments on commit 9a30823

Please sign in to comment.