preview img and some details

This commit is contained in:
Celogeek 2021-12-31 17:28:37 +01:00
parent 10c210a383
commit 9190f23188
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
3 changed files with 102 additions and 0 deletions

View File

@ -5,6 +5,7 @@ type ImagesGroup struct {
Details ImageDetailsCommand `command:"details" description:"Details of the images"` Details ImageDetailsCommand `command:"details" description:"Details of the images"`
Upload ImagesUploadCommand `command:"upload" description:"Upload of an images"` Upload ImagesUploadCommand `command:"upload" description:"Upload of an images"`
UploadTree ImagesUploadTreeCommand `command:"upload-tree" description:"Upload of a directory of images"` UploadTree ImagesUploadTreeCommand `command:"upload-tree" description:"Upload of a directory of images"`
Tag ImagesTagCommand `command:"tag" description:"Tag an image"`
} }
var imagesGroup ImagesGroup var imagesGroup ImagesGroup

View File

@ -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
}

View File

@ -1,5 +1,13 @@
package piwigotools package piwigotools
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/http"
)
type ImageDetails struct { type ImageDetails struct {
Id int `json:"id"` Id int `json:"id"`
Md5 string `json:"md5sum"` Md5 string `json:"md5sum"`
@ -21,3 +29,39 @@ type ImageDetails struct {
Url string `json:"url"` Url string `json:"url"`
} `json:"derivatives"` } `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
}