From be1372539353cb404b6fdfc0d50765057ccc0c44 Mon Sep 17 00:00:00 2001 From: ideas Date: Tue, 19 Jan 2021 07:27:40 +0800 Subject: [PATCH] fix image upload failed (#35) * fix images upload failed, beacuse the variable "content" not update on outer namespace of "if" block * fix images upload failed. beacuse escape unicode path ( file.go) and image path not found for relative path( image.go ) * modify the variable name for url Co-authored-by: Ideas --- lib/file.go | 34 +++++++++++++++++++--------------- lib/renderer/image.go | 12 +++++++++--- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/file.go b/lib/file.go index 77fa7ea..6a449de 100644 --- a/lib/file.go +++ b/lib/file.go @@ -3,6 +3,7 @@ package lib import ( "fmt" "io/ioutil" + "net/url" "strings" "github.com/justmiles/go-confluence" @@ -16,7 +17,7 @@ type MarkdownFile struct { Ancestor string } -func (f *MarkdownFile) String() (url string) { +func (f *MarkdownFile) String() (urlPath string) { return fmt.Sprintf("Path: %s, Title: %s, Parent: %s, Ancestor: %s", f.Path, f.Title, f.Parents, f.Ancestor) } @@ -29,24 +30,25 @@ func (f *MarkdownFile) FormattedPath() (s string) { } // Upload a markdown file -func (f *MarkdownFile) Upload(m *Markdown2Confluence) (url string, err error) { +func (f *MarkdownFile) Upload(m *Markdown2Confluence) (urlPath string, err error) { var ancestorID string // Content of Wiki dat, err := ioutil.ReadFile(f.Path) if err != nil { - return url, fmt.Errorf("Could not open file %s:\n\t%s", f.Path, err) + return urlPath, fmt.Errorf("Could not open file %s:\n\t%s", f.Path, err) } if m.Debug { fmt.Println(f.Path) } - wikiContent := string(dat) + //unescape unicode path %xx -> unicode word for some markdown software make escape + wikiContent, _ := url.QueryUnescape(string(dat)) var images []string wikiContent, images, err = renderContent(f.Path, wikiContent, m.WithHardWraps) if err != nil { - return url, fmt.Errorf("unable to render content from %s: %s", f.Path, err) + return urlPath, fmt.Errorf("unable to render content from %s: %s", f.Path, err) } if m.Debug { @@ -68,18 +70,18 @@ func (f *MarkdownFile) Upload(m *Markdown2Confluence) (url string, err error) { Expand: []string{"version", "body.storage"}, }) if err != nil { - return url, fmt.Errorf("Error checking for existing page: %s", err) + return urlPath, fmt.Errorf("Error checking for existing page: %s", err) } if len(f.Parents) > 0 { ancestorID, err = f.FindOrCreateAncestors(m) if err != nil { - return url, err + return urlPath, err } } var content confluence.Content - + var currContentID string // if page exists, update it if len(contentResults) > 0 { content = contentResults[0] @@ -95,9 +97,10 @@ func (f *MarkdownFile) Upload(m *Markdown2Confluence) (url string, err error) { content, err = m.client.UpdateContent(&content, nil) if err != nil { - return url, fmt.Errorf("Error updating content: %s", err) + return urlPath, fmt.Errorf("Error updating content: %s", err) } - url = m.client.Endpoint + content.Links.Tinyui + urlPath = m.client.Endpoint + content.Links.Tinyui + currContentID = content.ID // if page does not exist, create it } else { @@ -117,20 +120,21 @@ func (f *MarkdownFile) Upload(m *Markdown2Confluence) (url string, err error) { content, err := m.client.CreateContent(&bp, nil) if err != nil { - return url, fmt.Errorf("Error creating page: %s", err) + return urlPath, fmt.Errorf("Error creating page: %s", err) } - url = m.client.Endpoint + content.Links.Tinyui + urlPath = m.client.Endpoint + content.Links.Tinyui + currContentID = content.ID } - // fmt.Println(content) + fmt.Println("content.ID = " + content.ID + " content_id = " + currContentID) - _, errors := m.client.AddUpdateAttachments(content.ID, images) + _, errors := m.client.AddUpdateAttachments(currContentID, images) if len(errors) > 0 { fmt.Println(errors) err = errors[0] } - return url, err + return urlPath, err } // FindOrCreateAncestors creates an empty page to represent a local "folder" name diff --git a/lib/renderer/image.go b/lib/renderer/image.go index 1365eff..0dc7f2b 100644 --- a/lib/renderer/image.go +++ b/lib/renderer/image.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "path/filepath" "github.com/yuin/goldmark/ast" "github.com/yuin/goldmark/renderer" @@ -101,12 +102,17 @@ func RenderImageAttributes(w util.BufWriter, node ast.Node, filter util.BytesFil func localFile(filePath string, destination []byte) (string, error) { localizedPath := string(destination) - if _, err := os.Stat(localizedPath); err == nil { + _, err := os.Stat(localizedPath) + if err == nil { return localizedPath, nil } - localizedPath = path.Join(path.Dir(filePath), string(destination)) - if _, err := os.Stat(localizedPath); err == nil { + //path.Dir currDir is workpath so "path.Dir is '.'" + //And so make a absolute path for check file + localizedAbsPath, _ := filepath.Abs(filePath) + localizedPath = path.Join(filepath.Dir(localizedAbsPath), string(destination)) + _, err = os.Stat(localizedPath) + if err == nil { return localizedPath, nil }