fix image path

use /upload/filename/media/imagename.ext
pull/770/head
Augists 2022-02-15 10:08:01 +08:00
parent 63c0ac0a1d
commit 247dbe8a2d
1 changed files with 13 additions and 4 deletions

View File

@ -12,7 +12,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
_ "log" "log"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -123,6 +123,7 @@ type file struct {
r *zip.ReadCloser r *zip.ReadCloser
embed bool embed bool
list map[string]int list map[string]int
name string
} }
// Node is // Node is
@ -151,7 +152,7 @@ func escape(s, set string) string {
} }
func (zf *file) extract(rel *Relationship, w io.Writer) error { func (zf *file) extract(rel *Relationship, w io.Writer) error {
err := os.MkdirAll(filepath.Dir(rel.Target), 0755) err := os.MkdirAll(filepath.Join("uploads", zf.name, filepath.Dir(rel.Target)), 0755)
if err != nil { if err != nil {
return err return err
} }
@ -174,11 +175,11 @@ func (zf *file) extract(rel *Relationship, w io.Writer) error {
fmt.Fprintf(w, "![](data:image/png;base64,%s)", fmt.Fprintf(w, "![](data:image/png;base64,%s)",
base64.StdEncoding.EncodeToString(b[:n])) base64.StdEncoding.EncodeToString(b[:n]))
} else { } else {
err = ioutil.WriteFile(rel.Target, b, 0644) err = ioutil.WriteFile(filepath.Join("uploads", zf.name, rel.Target), b, 0644)
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(w, "![](%s)", escape(rel.Target, "()")) fmt.Fprintf(w, "![](%s)", "/"+filepath.Join("uploads", zf.name, escape(rel.Target, "()")))
} }
break break
} }
@ -534,6 +535,13 @@ func Docx2md(arg string, embed bool) (string, error) {
return "", err return "", err
} }
fileNames := strings.Split(arg, "/")
fileName := fileNames[len(fileNames)-1]
// make sure the file name
if !strings.HasSuffix(fileName, ".docx") {
log.Fatal("File name must end with .docx")
}
var buf bytes.Buffer var buf bytes.Buffer
zf := &file{ zf := &file{
r: r, r: r,
@ -541,6 +549,7 @@ func Docx2md(arg string, embed bool) (string, error) {
num: num, num: num,
embed: embed, embed: embed,
list: make(map[string]int), list: make(map[string]int),
name: fileName,
} }
err = zf.walk(node, &buf) err = zf.walk(node, &buf)
if err != nil { if err != nil {