mirror of
https://github.com/kovetskiy/mark.git
synced 2026-05-02 13:22:40 +00:00
create/update attachment
This commit is contained in:
@@ -1,9 +1,30 @@
|
||||
package mark
|
||||
|
||||
import "github.com/kovetskiy/mark/pkg/confluence"
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/confluence"
|
||||
"github.com/reconquest/karma-go"
|
||||
)
|
||||
|
||||
const (
|
||||
AttachmentChecksumPrefix = `mark:checksum: `
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
Name string
|
||||
ID string
|
||||
Name string
|
||||
Filename string
|
||||
Path string
|
||||
Checksum string
|
||||
Link string
|
||||
}
|
||||
|
||||
func ResolveAttachments(
|
||||
@@ -11,9 +32,144 @@ func ResolveAttachments(
|
||||
page *confluence.PageInfo,
|
||||
base string,
|
||||
names []string,
|
||||
) {
|
||||
err := api.GetAttachments(page.ID)
|
||||
) error {
|
||||
attachs := []Attachment{}
|
||||
for _, name := range names {
|
||||
attach := Attachment{
|
||||
Name: name,
|
||||
Filename: strings.ReplaceAll(name, "/", "_"),
|
||||
Path: filepath.Join(base, name),
|
||||
}
|
||||
|
||||
checksum, err := getChecksum(attach.Path)
|
||||
if err != nil {
|
||||
return karma.Format(
|
||||
err,
|
||||
"unable to get checksum for attachment: %q", attach.Name,
|
||||
)
|
||||
}
|
||||
|
||||
attach.Checksum = checksum
|
||||
|
||||
attachs = append(attachs, attach)
|
||||
}
|
||||
|
||||
remotes, err := api.GetAttachments(page.ID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
existing := []Attachment{}
|
||||
creating := []Attachment{}
|
||||
updating := []Attachment{}
|
||||
for _, attach := range attachs {
|
||||
var found bool
|
||||
var same bool
|
||||
for _, remote := range remotes {
|
||||
if remote.Filename == attach.Filename {
|
||||
same = attach.Checksum == strings.TrimPrefix(
|
||||
remote.Metadata.Comment,
|
||||
AttachmentChecksumPrefix,
|
||||
)
|
||||
|
||||
attach.ID = remote.ID
|
||||
attach.Link = remote.Links.Download
|
||||
|
||||
found = true
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
if same {
|
||||
existing = append(existing, attach)
|
||||
} else {
|
||||
updating = append(updating, attach)
|
||||
}
|
||||
} else {
|
||||
creating = append(creating, attach)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
marshaledXXX, _ := json.MarshalIndent(existing, "", " ")
|
||||
fmt.Printf("existing: %s\n", string(marshaledXXX))
|
||||
}
|
||||
|
||||
{
|
||||
marshaledXXX, _ := json.MarshalIndent(creating, "", " ")
|
||||
fmt.Printf("creating: %s\n", string(marshaledXXX))
|
||||
}
|
||||
|
||||
{
|
||||
marshaledXXX, _ := json.MarshalIndent(updating, "", " ")
|
||||
fmt.Printf("updating: %s\n", string(marshaledXXX))
|
||||
}
|
||||
|
||||
for i, attach := range creating {
|
||||
log.Infof(nil, "creating attachment: %q", attach.Name)
|
||||
|
||||
info, err := api.CreateAttachment(
|
||||
page.ID,
|
||||
attach.Filename,
|
||||
AttachmentChecksumPrefix+attach.Checksum,
|
||||
attach.Path,
|
||||
)
|
||||
if err != nil {
|
||||
return karma.Format(
|
||||
err,
|
||||
"unable to create attachment %q",
|
||||
attach.Name,
|
||||
)
|
||||
}
|
||||
|
||||
attach.ID = info.ID
|
||||
attach.Link = info.Links.Download
|
||||
|
||||
creating[i] = attach
|
||||
}
|
||||
|
||||
for i, attach := range updating {
|
||||
log.Infof(nil, "updating attachment: %q", attach.Name)
|
||||
|
||||
info, err := api.UpdateAttachment(
|
||||
page.ID,
|
||||
attach.ID,
|
||||
attach.Name,
|
||||
AttachmentChecksumPrefix+attach.Checksum,
|
||||
attach.Path,
|
||||
)
|
||||
if err != nil {
|
||||
return karma.Format(
|
||||
err,
|
||||
"unable to update attachment %q",
|
||||
attach.Name,
|
||||
)
|
||||
}
|
||||
|
||||
attach.Link = info.Links.Download
|
||||
|
||||
updating[i] = attach
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getChecksum(filename string) (string, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return "", karma.Format(
|
||||
err,
|
||||
"unable to open file",
|
||||
)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
hash := sha256.New()
|
||||
if _, err := io.Copy(hash, file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user