mirror of
https://github.com/kovetskiy/mark.git
synced 2025-05-25 14:32:40 +08:00
wip
This commit is contained in:
parent
eb30d1d1fc
commit
4ffbc12fa4
@ -8,10 +8,10 @@ import (
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/bndr/gopencils"
|
||||
"github.com/kovetskiy/lorg"
|
||||
"github.com/kovetskiy/mark/pkg/fs"
|
||||
"github.com/reconquest/cog"
|
||||
"github.com/reconquest/karma-go"
|
||||
)
|
||||
@ -161,13 +161,14 @@ func (api *API) FindPage(space string, title string) (*PageInfo, error) {
|
||||
|
||||
func (api *API) CreateAttachment(
|
||||
pageID string,
|
||||
name string,
|
||||
slug string,
|
||||
comment string,
|
||||
fs fs.FileSystem,
|
||||
path string,
|
||||
) (AttachmentInfo, error) {
|
||||
var info AttachmentInfo
|
||||
|
||||
form, err := getAttachmentPayload(name, comment, path)
|
||||
form, err := getAttachmentPayload(slug, comment, fs, path)
|
||||
if err != nil {
|
||||
return AttachmentInfo{}, err
|
||||
}
|
||||
@ -223,11 +224,12 @@ func (api *API) UpdateAttachment(
|
||||
attachID string,
|
||||
name string,
|
||||
comment string,
|
||||
fs fs.FileSystem,
|
||||
path string,
|
||||
) (AttachmentInfo, error) {
|
||||
var info AttachmentInfo
|
||||
|
||||
form, err := getAttachmentPayload(name, comment, path)
|
||||
form, err := getAttachmentPayload(name, comment, fs, path)
|
||||
if err != nil {
|
||||
return AttachmentInfo{}, err
|
||||
}
|
||||
@ -278,13 +280,18 @@ func (api *API) UpdateAttachment(
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func getAttachmentPayload(name, comment, path string) (*form, error) {
|
||||
func getAttachmentPayload(
|
||||
slug string,
|
||||
comment string,
|
||||
fs fs.FileSystem,
|
||||
path string,
|
||||
) (*form, error) {
|
||||
var (
|
||||
payload = bytes.NewBuffer(nil)
|
||||
writer = multipart.NewWriter(payload)
|
||||
)
|
||||
|
||||
file, err := os.Open(path)
|
||||
file, err := fs.Open(path)
|
||||
if err != nil {
|
||||
return nil, karma.Format(
|
||||
err,
|
||||
@ -295,7 +302,7 @@ func getAttachmentPayload(name, comment, path string) (*form, error) {
|
||||
|
||||
defer file.Close()
|
||||
|
||||
content, err := writer.CreateFormFile("file", name)
|
||||
content, err := writer.CreateFormFile("file", slug)
|
||||
if err != nil {
|
||||
return nil, karma.Format(
|
||||
err,
|
||||
|
21
pkg/fs/disk.go
Normal file
21
pkg/fs/disk.go
Normal file
@ -0,0 +1,21 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type DiskFileSystem struct {
|
||||
baseDir string
|
||||
}
|
||||
|
||||
func NewDiskFileSystem(baseDir string) *DiskFileSystem {
|
||||
return &DiskFileSystem{
|
||||
baseDir: baseDir,
|
||||
}
|
||||
}
|
||||
|
||||
func (system *DiskFileSystem) Open(path string) (io.ReadCloser, error) {
|
||||
return os.Open(filepath.Join(system.baseDir, path))
|
||||
}
|
9
pkg/fs/fs.go
Normal file
9
pkg/fs/fs.go
Normal file
@ -0,0 +1,9 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type FileSystem interface {
|
||||
Open(path string) (io.ReadCloser, error)
|
||||
}
|
27
pkg/fs/tar.go
Normal file
27
pkg/fs/tar.go
Normal file
@ -0,0 +1,27 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"io"
|
||||
|
||||
"github.com/reconquest/karma-go"
|
||||
)
|
||||
|
||||
type TarFileSystem struct {
|
||||
files map[string][]byte
|
||||
}
|
||||
|
||||
func NewTarFileSystem(input io.Reader) (*TarFileSystem, error) {
|
||||
files := map[string][]byte{}
|
||||
|
||||
archive := tar.NewReader(input)
|
||||
for {
|
||||
header, err := archive.Next()
|
||||
if err != nil {
|
||||
return nil, karma.Format(
|
||||
err,
|
||||
"asdasd",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
@ -6,13 +6,12 @@ import (
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/confluence"
|
||||
"github.com/kovetskiy/mark/pkg/fs"
|
||||
"github.com/reconquest/karma-go"
|
||||
)
|
||||
|
||||
@ -23,7 +22,6 @@ const (
|
||||
type Attachment struct {
|
||||
ID string
|
||||
Name string
|
||||
Filename string
|
||||
Path string
|
||||
Checksum string
|
||||
Link string
|
||||
@ -32,18 +30,17 @@ type Attachment struct {
|
||||
func ResolveAttachments(
|
||||
api *confluence.API,
|
||||
page *confluence.PageInfo,
|
||||
base string,
|
||||
fs fs.FileSystem,
|
||||
names []string,
|
||||
) ([]Attachment, error) {
|
||||
attaches := []Attachment{}
|
||||
for _, name := range names {
|
||||
attach := Attachment{
|
||||
Name: name,
|
||||
Filename: strings.ReplaceAll(name, "/", "_"),
|
||||
Path: filepath.Join(base, name),
|
||||
Path: name,
|
||||
Name: strings.ReplaceAll(name, "/", "_"),
|
||||
}
|
||||
|
||||
checksum, err := getChecksum(attach.Path)
|
||||
checksum, err := getChecksum(fs, attach.Name)
|
||||
if err != nil {
|
||||
return nil, karma.Format(
|
||||
err,
|
||||
@ -68,7 +65,7 @@ func ResolveAttachments(
|
||||
var found bool
|
||||
var same bool
|
||||
for _, remote := range remotes {
|
||||
if remote.Filename == attach.Filename {
|
||||
if remote.Filename == attach.Name {
|
||||
same = attach.Checksum == strings.TrimPrefix(
|
||||
remote.Metadata.Comment,
|
||||
AttachmentChecksumPrefix,
|
||||
@ -102,8 +99,9 @@ func ResolveAttachments(
|
||||
|
||||
info, err := api.CreateAttachment(
|
||||
page.ID,
|
||||
attach.Filename,
|
||||
attach.Name,
|
||||
AttachmentChecksumPrefix+attach.Checksum,
|
||||
fs,
|
||||
attach.Path,
|
||||
)
|
||||
if err != nil {
|
||||
@ -131,7 +129,8 @@ func ResolveAttachments(
|
||||
attach.ID,
|
||||
attach.Name,
|
||||
AttachmentChecksumPrefix+attach.Checksum,
|
||||
attach.Path,
|
||||
fs,
|
||||
attach.Name,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, karma.Format(
|
||||
@ -198,8 +197,8 @@ func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte {
|
||||
return markdown
|
||||
}
|
||||
|
||||
func getChecksum(filename string) (string, error) {
|
||||
file, err := os.Open(filename)
|
||||
func getChecksum(fs FileSystem, filename string) (string, error) {
|
||||
file, err := fs.Open(filename)
|
||||
if err != nil {
|
||||
return "", karma.Format(
|
||||
err,
|
||||
|
Loading…
x
Reference in New Issue
Block a user