Skip to content

Commit

Permalink
Fix for subdirectories related attachments (issue kovetskiy#31) (kove…
Browse files Browse the repository at this point in the history
  • Loading branch information
klysunkin authored Jan 18, 2022
1 parent 8d58ff2 commit 9a7146c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
33 changes: 22 additions & 11 deletions pkg/mark/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -55,8 +52,6 @@ func ResolveAttachments(
}

attach.Checksum = checksum

attaches = append(attaches, attach)
}

remotes, err := api.GetAttachments(page.ID)
Expand Down Expand Up @@ -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{}
Expand Down
55 changes: 55 additions & 0 deletions pkg/mark/attachment_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 2 additions & 3 deletions pkg/mark/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Meta struct {
Title string
Layout string
Sidebar string
Attachments map[string]string
Attachments []string
Labels []string
}

Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9a7146c

Please sign in to comment.