Skip to content

Commit

Permalink
fix image upload failed (#35)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
iamideas and Ideas authored Jan 18, 2021
1 parent 2af6fa8 commit be13725
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
34 changes: 19 additions & 15 deletions lib/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lib
import (
"fmt"
"io/ioutil"
"net/url"
"strings"

"github.com/justmiles/go-confluence"
Expand All @@ -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)
}

Expand All @@ -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 {
Expand All @@ -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]
Expand All @@ -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 {
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions lib/renderer/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"path/filepath"

"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit be13725

Please sign in to comment.