From 9190f231881bf5cad5dc5c695b220fe1c0f74a92 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Fri, 31 Dec 2021 17:28:37 +0100 Subject: [PATCH] preview img and some details --- cmd/piwigo-cli/images.go | 1 + cmd/piwigo-cli/images_tag.go | 57 ++++++++++++++++++++ internal/piwigo/piwigotools/image_details.go | 44 +++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 cmd/piwigo-cli/images_tag.go diff --git a/cmd/piwigo-cli/images.go b/cmd/piwigo-cli/images.go index 520e1f4..30fead8 100644 --- a/cmd/piwigo-cli/images.go +++ b/cmd/piwigo-cli/images.go @@ -5,6 +5,7 @@ type ImagesGroup struct { Details ImageDetailsCommand `command:"details" description:"Details of the images"` Upload ImagesUploadCommand `command:"upload" description:"Upload of an images"` UploadTree ImagesUploadTreeCommand `command:"upload-tree" description:"Upload of a directory of images"` + Tag ImagesTagCommand `command:"tag" description:"Tag an image"` } var imagesGroup ImagesGroup diff --git a/cmd/piwigo-cli/images_tag.go b/cmd/piwigo-cli/images_tag.go new file mode 100644 index 0000000..df9f37a --- /dev/null +++ b/cmd/piwigo-cli/images_tag.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "net/url" + "os" + "strings" + + "github.com/celogeek/piwigo-cli/internal/piwigo" + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" + "github.com/jedib0t/go-pretty/v6/table" +) + +type ImagesTagCommand struct { + Id int `short:"i" long:"id" description:"image id to tag" required:"true"` +} + +func (c *ImagesTagCommand) Execute(args []string) error { + p := piwigo.Piwigo{} + if err := p.LoadConfig(); err != nil { + return err + } + + _, err := p.Login() + if err != nil { + return err + } + + var resp piwigotools.ImageDetails + if err := p.Post("pwg.images.getInfo", &url.Values{ + "image_id": []string{fmt.Sprint(c.Id)}, + }, &resp); err != nil { + return err + } + + img, err := resp.Preview(25) + if err != nil { + return err + } + + fmt.Println(img) + t := table.NewWriter() + t.AppendRows([]table.Row{ + {"Name", resp.Name}, + {"Url", resp.Url}, + {"CreatedAt", resp.DateCreation}, + {"Size", fmt.Sprintf("%d x %d", resp.Width, resp.Height)}, + {"Categories", strings.Join(resp.Categories.Names(), "\n")}, + {"Tags", strings.Join(resp.Tags.NamesWithAgeAt(resp.DateCreation), "\n")}, + }) + + t.SetOutputMirror(os.Stdout) + t.SetStyle(table.StyleLight) + t.Render() + + return nil +} diff --git a/internal/piwigo/piwigotools/image_details.go b/internal/piwigo/piwigotools/image_details.go index 42391c4..c802a99 100644 --- a/internal/piwigo/piwigotools/image_details.go +++ b/internal/piwigo/piwigotools/image_details.go @@ -1,5 +1,13 @@ package piwigotools +import ( + "bytes" + "encoding/base64" + "fmt" + "io" + "net/http" +) + type ImageDetails struct { Id int `json:"id"` Md5 string `json:"md5sum"` @@ -21,3 +29,39 @@ type ImageDetails struct { Url string `json:"url"` } `json:"derivatives"` } + +func (img *ImageDetails) Preview(height int) (string, error) { + url := img.ImageUrl + if der, ok := img.Derivatives["medium"]; ok { + url = der.Url + } + + resp, err := http.Get(url) + if err != nil { + return "", err + } + if resp.StatusCode != 200 { + return "", fmt.Errorf("[error %d] failed to get image", resp.StatusCode) + } + defer resp.Body.Close() + + buf := bytes.NewBuffer([]byte{}) + + buf.WriteString("\033]1337") + buf.WriteString(";File=") + buf.WriteString(";inline=1") + buf.WriteString(fmt.Sprintf(";size=%d;", resp.ContentLength)) + if height > 0 { + buf.WriteString(fmt.Sprintf(";height=%d", height)) + } + buf.WriteString(":") + + encoder := base64.NewEncoder(base64.StdEncoding, buf) + defer encoder.Close() + if _, err := io.Copy(encoder, resp.Body); err != nil { + return "", err + } + buf.WriteString("\a") + + return buf.String(), nil +}