From 9a7146c7d7d7ec07079407c187c9e4ebcfb77063 Mon Sep 17 00:00:00 2001 From: klysunkin Date: Tue, 18 Jan 2022 09:07:36 +0300 Subject: [PATCH] Fix for subdirectories related attachments (issue #31) (#151) --- main.go | 2 +- pkg/mark/attachment.go | 33 ++++++++++++++-------- pkg/mark/attachment_test.go | 55 +++++++++++++++++++++++++++++++++++++ pkg/mark/meta.go | 5 ++-- 4 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 pkg/mark/attachment_test.go diff --git a/main.go b/main.go index 84a3ff9..92f14ac 100644 --- a/main.go +++ b/main.go @@ -313,7 +313,7 @@ func processFile( target = page } - attaches, err := mark.ResolveAttachments(api, target, ".", meta.Attachments) + attaches, err := mark.ResolveAttachments(api, target, filepath.Dir(file), meta.Attachments) if err != nil { log.Fatalf(err, "unable to create/update attachments") } diff --git a/pkg/mark/attachment.go b/pkg/mark/attachment.go index 44f8185..b67efd5 100644 --- a/pkg/mark/attachment.go +++ b/pkg/mark/attachment.go @@ -35,17 +35,14 @@ func ResolveAttachments( api *confluence.API, page *confluence.PageInfo, base string, - replacements map[string]string, + replacements []string, ) ([]Attachment, error) { - attaches := []Attachment{} - for replace, name := range replacements { - attach := Attachment{ - Name: name, - Filename: strings.ReplaceAll(name, "/", "_"), - Path: filepath.Join(base, name), - Replace: replace, - } + attaches, err := prepareAttachments(base, replacements) + if err != nil { + return nil, err + } + for _, attach := range attaches { checksum, err := getChecksum(attach.Path) if err != nil { return nil, karma.Format( @@ -55,8 +52,6 @@ func ResolveAttachments( } attach.Checksum = checksum - - attaches = append(attaches, attach) } remotes, err := api.GetAttachments(page.ID) @@ -160,6 +155,22 @@ func ResolveAttachments( return attaches, nil } +func prepareAttachments(base string, replacements []string) ([]Attachment, error) { + attaches := []Attachment{} + for _, name := range replacements { + attach := Attachment{ + Name: name, + Filename: strings.ReplaceAll(name, "/", "_"), + Path: filepath.Join(base, name), + Replace: name, + } + + attaches = append(attaches, attach) + } + + return attaches, nil +} + func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte { links := map[string]string{} replaces := []string{} diff --git a/pkg/mark/attachment_test.go b/pkg/mark/attachment_test.go new file mode 100644 index 0000000..594cfa6 --- /dev/null +++ b/pkg/mark/attachment_test.go @@ -0,0 +1,55 @@ +package mark + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +var ( + replacements = []string{ + "image1.jpg", + "images/image2.jpg", + "../image3.jpg", + } +) + +func TestPrepareAttachmentsWithWorkDirBase(t *testing.T) { + + attaches, err := prepareAttachments(".", replacements) + if err != nil { + println(err.Error()) + } + + assert.Equal(t, "image1.jpg", attaches[0].Name) + assert.Equal(t, "image1.jpg", attaches[0].Replace) + assert.Equal(t, "image1.jpg", attaches[0].Path) + + assert.Equal(t, "images/image2.jpg", attaches[1].Name) + assert.Equal(t, "images/image2.jpg", attaches[1].Replace) + assert.Equal(t, "images/image2.jpg", attaches[1].Path) + + assert.Equal(t, "../image3.jpg", attaches[2].Name) + assert.Equal(t, "../image3.jpg", attaches[2].Replace) + assert.Equal(t, "../image3.jpg", attaches[2].Path) + + assert.Equal(t, len(attaches), 3) +} + +func TestPrepareAttachmentsWithSubDirBase(t *testing.T) { + + attaches, _ := prepareAttachments("a/b", replacements) + + assert.Equal(t, "image1.jpg", attaches[0].Name) + assert.Equal(t, "image1.jpg", attaches[0].Replace) + assert.Equal(t, "a/b/image1.jpg", attaches[0].Path) + + assert.Equal(t, "images/image2.jpg", attaches[1].Name) + assert.Equal(t, "images/image2.jpg", attaches[1].Replace) + assert.Equal(t, "a/b/images/image2.jpg", attaches[1].Path) + + assert.Equal(t, "../image3.jpg", attaches[2].Name) + assert.Equal(t, "../image3.jpg", attaches[2].Replace) + assert.Equal(t, "a/image3.jpg", attaches[2].Path) + + assert.Equal(t, len(attaches), 3) +} diff --git a/pkg/mark/meta.go b/pkg/mark/meta.go index 3e9e8e4..0f01e95 100644 --- a/pkg/mark/meta.go +++ b/pkg/mark/meta.go @@ -29,7 +29,7 @@ type Meta struct { Title string Layout string Sidebar string - Attachments map[string]string + Attachments []string Labels []string } @@ -72,7 +72,6 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) { if meta == nil { meta = &Meta{} meta.Type = "page" //Default if not specified - meta.Attachments = make(map[string]string) } header := strings.Title(matches[1]) @@ -103,7 +102,7 @@ func ExtractMeta(data []byte) (*Meta, []byte, error) { meta.Sidebar = strings.TrimSpace(value) case HeaderAttachment: - meta.Attachments[value] = value + meta.Attachments = append(meta.Attachments, value) case HeaderLabel: meta.Labels = append(meta.Labels, value)