sync: Markdown imports files using generic functions

This commit is contained in:
nemunaire 2025-04-07 10:16:30 +02:00
parent b55151623c
commit 8723f500cc

View file

@ -3,9 +3,7 @@ package sync
import (
"bytes"
"encoding/base32"
"io"
"net/url"
"os"
"path"
"strings"
@ -89,27 +87,10 @@ func (t *imageImporterTransformer) Transform(doc *ast.Document, reader text.Read
dPath := path.Join(fic.FilesDir, strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(t.hash[:])), iPath)
child.Destination = []byte(path.Join(t.absPath, string(child.Destination)))
if err := os.MkdirAll(path.Dir(dPath), 0755); err != nil {
err := importFile(t.importer, path.Join(t.rootDir, iPath), dPath)
if err != nil {
return ast.WalkStop, err
}
if fdto, err := os.Create(dPath); err != nil {
return ast.WalkStop, err
} else {
defer fdto.Close()
if fd, closer, err := GetFile(t.importer, path.Join(t.rootDir, iPath)); err != nil {
os.Remove(dPath)
return ast.WalkStop, err
} else {
defer closer()
_, err = io.Copy(fdto, fd)
if err != nil {
return ast.WalkStop, err
}
}
}
}
return ast.WalkContinue, nil