Skip to content

Commit

Permalink
Handle decoding notes with missing content
Browse files Browse the repository at this point in the history
  • Loading branch information
wormi4ok committed Mar 19, 2021
1 parent 7c133b9 commit 56af658
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
33 changes: 18 additions & 15 deletions encoding/enex/enex.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package enex
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"log"
)

type (
Expand Down Expand Up @@ -79,20 +80,17 @@ type (
// Decode will return an Export from evernote
func Decode(data io.Reader) (*Export, error) {
var e Export

decoder := xml.NewDecoder(data)
decoder.Strict = false
err := decoder.Decode(&e)
err := newDecoder(data).Decode(&e)

for i := range e.Notes {
var c Content
decoder := xml.NewDecoder(bytes.NewReader(e.Notes[i].Content))
decoder.Strict = false
var reader = bytes.NewReader(e.Notes[i].Content)

log.Printf("[DEBUG] Decoding a note: %s", e.Notes[i].Title)
err = decoder.Decode(&c)
if err != nil {
log.Fatal(err)
if err := newDecoder(reader).Decode(&c); err != nil {
// EOF is a known case when the content is empty
if !errors.Is(err, io.EOF) {
return nil, fmt.Errorf("decoding note %s: %w", e.Notes[i].Title, err)
}
}
e.Notes[i].Content = c.Text

Expand All @@ -101,16 +99,21 @@ func Decode(data io.Reader) (*Export, error) {
if len(e.Notes[i].Resources[j].Recognition) == 0 {
continue
}
decoder := xml.NewDecoder(bytes.NewReader(e.Notes[i].Resources[j].Recognition))
decoder.Strict = false
decoder := newDecoder(bytes.NewReader(e.Notes[i].Resources[j].Recognition))
err = decoder.Decode(&r)
if err != nil {
log.Fatal(err)
return nil, fmt.Errorf("decoding resource %s: %w", e.Notes[i].Resources[j].Attributes.Filename, err)
}
e.Notes[i].Resources[j].ID = r.ObjID
e.Notes[i].Resources[j].Type = r.ObjType
}

}

return &e, err
}

func newDecoder(r io.Reader) *xml.Decoder {
d := xml.NewDecoder(r)
d.Strict = false
return d
}
10 changes: 10 additions & 0 deletions encoding/enex/enex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ func TestDecode(t *testing.T) {
if !reflect.DeepEqual(got, expect) {
t.Errorf("Decode() = %+v,\nwant %+v", got, expect)
}
}

func TestDecodeEmptyNote(t *testing.T) {
enexContent, err := os.Open("testdata/empty.enex")
if err != nil {
t.Error(err)
}
_, err = enex.Decode(enexContent)
if err != nil {
t.Errorf("Error while Decoding = %v", err)
}
}

func readFile(filename string) []byte {
Expand Down
16 changes: 16 additions & 0 deletions encoding/enex/testdata/empty.enex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export3.dtd">
<en-export export-date="20210418T174314Z" application="Evernote" version="10.9.10">
<note>
<title>Empty</title>
<created>20210318T174301Z</created>
<updated>20210318T174304Z</updated>
<note-attributes>
<author>me</author>
</note-attributes>
<content>
<![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
]]>
</content>
</note>
</en-export>

0 comments on commit 56af658

Please sign in to comment.