From 4ffbc12fa41c38afe1c10569c57f5c631a333c2d Mon Sep 17 00:00:00 2001 From: Egor Kovetskiy Date: Wed, 5 Jun 2019 18:02:39 +0300 Subject: [PATCH] wip --- pkg/confluence/api.go | 21 ++++++++++++++------- pkg/fs/disk.go | 21 +++++++++++++++++++++ pkg/fs/fs.go | 9 +++++++++ pkg/fs/tar.go | 27 +++++++++++++++++++++++++++ pkg/mark/attachment.go | 25 ++++++++++++------------- 5 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 pkg/fs/disk.go create mode 100644 pkg/fs/fs.go create mode 100644 pkg/fs/tar.go diff --git a/pkg/confluence/api.go b/pkg/confluence/api.go index 1cec8ee..1fe77d7 100644 --- a/pkg/confluence/api.go +++ b/pkg/confluence/api.go @@ -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, diff --git a/pkg/fs/disk.go b/pkg/fs/disk.go new file mode 100644 index 0000000..e5dddc2 --- /dev/null +++ b/pkg/fs/disk.go @@ -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)) +} diff --git a/pkg/fs/fs.go b/pkg/fs/fs.go new file mode 100644 index 0000000..bbf98b3 --- /dev/null +++ b/pkg/fs/fs.go @@ -0,0 +1,9 @@ +package fs + +import ( + "io" +) + +type FileSystem interface { + Open(path string) (io.ReadCloser, error) +} diff --git a/pkg/fs/tar.go b/pkg/fs/tar.go new file mode 100644 index 0000000..05caea2 --- /dev/null +++ b/pkg/fs/tar.go @@ -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", + ) + } + } +} diff --git a/pkg/mark/attachment.go b/pkg/mark/attachment.go index 79d8b7d..9367dfb 100644 --- a/pkg/mark/attachment.go +++ b/pkg/mark/attachment.go @@ -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,