This commit is contained in:
Egor Kovetskiy 2019-06-05 18:02:39 +03:00
parent eb30d1d1fc
commit 4ffbc12fa4
5 changed files with 83 additions and 20 deletions

View File

@ -8,10 +8,10 @@ import (
"io/ioutil" "io/ioutil"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"os"
"github.com/bndr/gopencils" "github.com/bndr/gopencils"
"github.com/kovetskiy/lorg" "github.com/kovetskiy/lorg"
"github.com/kovetskiy/mark/pkg/fs"
"github.com/reconquest/cog" "github.com/reconquest/cog"
"github.com/reconquest/karma-go" "github.com/reconquest/karma-go"
) )
@ -161,13 +161,14 @@ func (api *API) FindPage(space string, title string) (*PageInfo, error) {
func (api *API) CreateAttachment( func (api *API) CreateAttachment(
pageID string, pageID string,
name string, slug string,
comment string, comment string,
fs fs.FileSystem,
path string, path string,
) (AttachmentInfo, error) { ) (AttachmentInfo, error) {
var info AttachmentInfo var info AttachmentInfo
form, err := getAttachmentPayload(name, comment, path) form, err := getAttachmentPayload(slug, comment, fs, path)
if err != nil { if err != nil {
return AttachmentInfo{}, err return AttachmentInfo{}, err
} }
@ -223,11 +224,12 @@ func (api *API) UpdateAttachment(
attachID string, attachID string,
name string, name string,
comment string, comment string,
fs fs.FileSystem,
path string, path string,
) (AttachmentInfo, error) { ) (AttachmentInfo, error) {
var info AttachmentInfo var info AttachmentInfo
form, err := getAttachmentPayload(name, comment, path) form, err := getAttachmentPayload(name, comment, fs, path)
if err != nil { if err != nil {
return AttachmentInfo{}, err return AttachmentInfo{}, err
} }
@ -278,13 +280,18 @@ func (api *API) UpdateAttachment(
return info, nil 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 ( var (
payload = bytes.NewBuffer(nil) payload = bytes.NewBuffer(nil)
writer = multipart.NewWriter(payload) writer = multipart.NewWriter(payload)
) )
file, err := os.Open(path) file, err := fs.Open(path)
if err != nil { if err != nil {
return nil, karma.Format( return nil, karma.Format(
err, err,
@ -295,7 +302,7 @@ func getAttachmentPayload(name, comment, path string) (*form, error) {
defer file.Close() defer file.Close()
content, err := writer.CreateFormFile("file", name) content, err := writer.CreateFormFile("file", slug)
if err != nil { if err != nil {
return nil, karma.Format( return nil, karma.Format(
err, err,

21
pkg/fs/disk.go Normal file
View 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
View 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
View 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",
)
}
}
}

View File

@ -6,13 +6,12 @@ import (
"encoding/hex" "encoding/hex"
"io" "io"
"net/url" "net/url"
"os"
"path" "path"
"path/filepath"
"sort" "sort"
"strings" "strings"
"github.com/kovetskiy/mark/pkg/confluence" "github.com/kovetskiy/mark/pkg/confluence"
"github.com/kovetskiy/mark/pkg/fs"
"github.com/reconquest/karma-go" "github.com/reconquest/karma-go"
) )
@ -23,7 +22,6 @@ const (
type Attachment struct { type Attachment struct {
ID string ID string
Name string Name string
Filename string
Path string Path string
Checksum string Checksum string
Link string Link string
@ -32,18 +30,17 @@ type Attachment struct {
func ResolveAttachments( func ResolveAttachments(
api *confluence.API, api *confluence.API,
page *confluence.PageInfo, page *confluence.PageInfo,
base string, fs fs.FileSystem,
names []string, names []string,
) ([]Attachment, error) { ) ([]Attachment, error) {
attaches := []Attachment{} attaches := []Attachment{}
for _, name := range names { for _, name := range names {
attach := Attachment{ attach := Attachment{
Name: name, Path: name,
Filename: strings.ReplaceAll(name, "/", "_"), Name: strings.ReplaceAll(name, "/", "_"),
Path: filepath.Join(base, name),
} }
checksum, err := getChecksum(attach.Path) checksum, err := getChecksum(fs, attach.Name)
if err != nil { if err != nil {
return nil, karma.Format( return nil, karma.Format(
err, err,
@ -68,7 +65,7 @@ func ResolveAttachments(
var found bool var found bool
var same bool var same bool
for _, remote := range remotes { for _, remote := range remotes {
if remote.Filename == attach.Filename { if remote.Filename == attach.Name {
same = attach.Checksum == strings.TrimPrefix( same = attach.Checksum == strings.TrimPrefix(
remote.Metadata.Comment, remote.Metadata.Comment,
AttachmentChecksumPrefix, AttachmentChecksumPrefix,
@ -102,8 +99,9 @@ func ResolveAttachments(
info, err := api.CreateAttachment( info, err := api.CreateAttachment(
page.ID, page.ID,
attach.Filename, attach.Name,
AttachmentChecksumPrefix+attach.Checksum, AttachmentChecksumPrefix+attach.Checksum,
fs,
attach.Path, attach.Path,
) )
if err != nil { if err != nil {
@ -131,7 +129,8 @@ func ResolveAttachments(
attach.ID, attach.ID,
attach.Name, attach.Name,
AttachmentChecksumPrefix+attach.Checksum, AttachmentChecksumPrefix+attach.Checksum,
attach.Path, fs,
attach.Name,
) )
if err != nil { if err != nil {
return nil, karma.Format( return nil, karma.Format(
@ -198,8 +197,8 @@ func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte {
return markdown return markdown
} }
func getChecksum(filename string) (string, error) { func getChecksum(fs FileSystem, filename string) (string, error) {
file, err := os.Open(filename) file, err := fs.Open(filename)
if err != nil { if err != nil {
return "", karma.Format( return "", karma.Format(
err, err,