From 7bcf2d58263f1fa69ce9c40b613cdd37181d861f Mon Sep 17 00:00:00 2001 From: Alex Disney Date: Sun, 4 Dec 2022 12:22:31 -0600 Subject: [PATCH 1/2] Add file and page tags to the cache file --- api/sync15/blobdoc.go | 44 +++++++++++++++++++++++++++++++++++++ archive/file.go | 50 +++++++++++++++++++++++++++---------------- 2 files changed, 76 insertions(+), 18 deletions(-) diff --git a/api/sync15/blobdoc.go b/api/sync15/blobdoc.go index d20d19b..ea4253d 100644 --- a/api/sync15/blobdoc.go +++ b/api/sync15/blobdoc.go @@ -109,6 +109,42 @@ func (d *BlobDoc) IndexReader() (io.ReadCloser, error) { return pipeReader, nil } +func (d *BlobDoc) ReadContentTags(fileEntry *Entry, r RemoteStorage) error { + if strings.HasSuffix(fileEntry.DocumentID, ".content") { + contentFile := archive.Content{} + + meta, err := r.GetReader(fileEntry.Hash) + if err != nil { + return err + } + defer meta.Close() + content, err := ioutil.ReadAll(meta) + if err != nil { + return err + } + err = json.Unmarshal(content, &contentFile) + if err != nil { + log.Error.Printf("cannot read content %s %v", fileEntry.DocumentID, err) + } + if contentFile.FileTags != nil { + fileTags := []string{} + for _, t := range contentFile.FileTags { + fileTags = append(fileTags, t.Name) + } + d.MetadataFile.FileTags = fileTags + } + if contentFile.Tags != nil { + pageTags := []string{} + for _, t := range contentFile.Tags { + pageTags = append(pageTags, t.Name) + } + d.MetadataFile.PageTags = pageTags + } + } + + return nil +} + // ReadMetadata the document metadata from remote blob func (d *BlobDoc) ReadMetadata(fileEntry *Entry, r RemoteStorage) error { if strings.HasSuffix(fileEntry.DocumentID, ".metadata") { @@ -184,6 +220,10 @@ func (d *BlobDoc) Mirror(e *Entry, r RemoteStorage) error { if err != nil { return err } + err = d.ReadContentTags(newEntry, r) + if err != nil { + return err + } currentEntry.Hash = newEntry.Hash } head = append(head, currentEntry) @@ -198,6 +238,10 @@ func (d *BlobDoc) Mirror(e *Entry, r RemoteStorage) error { if err != nil { return err } + err = d.ReadContentTags(newEntry, r) + if err != nil { + return err + } head = append(head, newEntry) } } diff --git a/archive/file.go b/archive/file.go index 08062ea..b319b35 100644 --- a/archive/file.go +++ b/archive/file.go @@ -87,25 +87,37 @@ type Layer struct { Name string `json:"name"` } +type FileTag struct { + Name string `json:"name"` + Timestamp int `json:"timestamp"` +} + +type PageTag struct { + Name string `json:"name"` + PageId string `json:"pageId"` + Timestamp int `json:"timestamp"` +} + // Content represents the structure of a .content json file. type Content struct { DummyDocument bool `json:"dummyDocument"` ExtraMetadata ExtraMetadata `json:"extraMetadata"` // FileType is "pdf", "epub" or empty for a simple note - FileType string `json:"fileType"` - FontName string `json:"fontName"` - LastOpenedPage int `json:"lastOpenedPage"` - LineHeight int `json:"lineHeight"` - Margins int `json:"margins"` + FileType string `json:"fileType"` + FileTags []FileTag `json:"fileTags"` + FontName string `json:"fontName"` + LastOpenedPage int `json:"lastOpenedPage"` + LineHeight int `json:"lineHeight"` + Margins int `json:"margins"` // Orientation can take "portrait" or "landscape". Orientation string `json:"orientation"` PageCount int `json:"pageCount"` // Pages is a list of page IDs - Pages []string `json:"pages"` - Tags []string `json:"pageTags"` - RedirectionMap []int `json:"redirectionPageMap"` - TextScale int `json:"textScale"` + Pages []string `json:"pages"` + Tags []PageTag `json:"pageTags"` + RedirectionMap []int `json:"redirectionPageMap"` + TextScale int `json:"textScale"` Transform Transform `json:"transform"` } @@ -147,13 +159,15 @@ type MetadataFile struct { CollectionType string `json:"type"` Parent string `json:"parent"` //LastModified in milliseconds - LastModified string `json:"lastModified"` - LastOpened string `json:"lastOpened"` - LastOpenedPage int `json:"lastOpenedPage"` - Version int `json:"version"` - Pinned bool `json:"pinned"` - Synced bool `json:"synced"` - Modified bool `json:"modified"` - Deleted bool `json:"deleted"` - MetadataModified bool `json:"metadatamodified"` + LastModified string `json:"lastModified"` + LastOpened string `json:"lastOpened"` + LastOpenedPage int `json:"lastOpenedPage"` + Version int `json:"version"` + Pinned bool `json:"pinned"` + Synced bool `json:"synced"` + Modified bool `json:"modified"` + Deleted bool `json:"deleted"` + MetadataModified bool `json:"metadatamodified"` + FileTags []string `json:"fileTags"` + PageTags []string `json:"pageTags"` } From 17068f64ae2991352973b12c3c225702fb8029db Mon Sep 17 00:00:00 2001 From: Alex Disney Date: Tue, 3 Jan 2023 15:36:59 -0600 Subject: [PATCH 2/2] Return error when trying to unmarshal tag content --- api/sync15/blobdoc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/sync15/blobdoc.go b/api/sync15/blobdoc.go index ea4253d..232ce88 100644 --- a/api/sync15/blobdoc.go +++ b/api/sync15/blobdoc.go @@ -124,7 +124,7 @@ func (d *BlobDoc) ReadContentTags(fileEntry *Entry, r RemoteStorage) error { } err = json.Unmarshal(content, &contentFile) if err != nil { - log.Error.Printf("cannot read content %s %v", fileEntry.DocumentID, err) + return err } if contentFile.FileTags != nil { fileTags := []string{}