swirl/api/image.go

119 lines
2.6 KiB
Go
Raw Permalink Normal View History

2021-12-06 12:24:22 +00:00
package api
import (
"github.com/cuigh/auxo/data"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
2022-01-06 08:54:14 +00:00
"github.com/cuigh/swirl/misc"
2021-12-06 12:24:22 +00:00
)
// ImageHandler encapsulates image related handlers.
type ImageHandler struct {
2021-12-20 06:28:43 +00:00
Search web.HandlerFunc `path:"/search" auth:"image.view" desc:"search images"`
Find web.HandlerFunc `path:"/find" auth:"image.view" desc:"find image by id"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"image.delete" desc:"delete image"`
Prune web.HandlerFunc `path:"/prune" method:"post" auth:"image.delete" desc:"delete unused images"`
2021-12-06 12:24:22 +00:00
}
// NewImage creates an instance of ImageHandler
func NewImage(b biz.ImageBiz) *ImageHandler {
return &ImageHandler{
2021-12-20 06:28:43 +00:00
Search: imageSearch(b),
Find: imageFind(b),
Delete: imageDelete(b),
Prune: imagePrune(b),
2021-12-06 12:24:22 +00:00
}
}
func imageSearch(b biz.ImageBiz) web.HandlerFunc {
type Args struct {
2021-12-20 06:28:43 +00:00
Node string `json:"node" bind:"node"`
2021-12-06 12:24:22 +00:00
Name string `json:"name" bind:"name"`
PageIndex int `json:"pageIndex" bind:"pageIndex"`
PageSize int `json:"pageSize" bind:"pageSize"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) (err error) {
2021-12-06 12:24:22 +00:00
var (
args = &Args{}
images []*biz.Image
total int
)
2022-01-06 08:54:14 +00:00
if err = c.Bind(args); err == nil {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
images, total, err = b.Search(ctx, args.Node, args.Name, args.PageIndex, args.PageSize)
2021-12-06 12:24:22 +00:00
}
if err != nil {
return
}
2022-01-06 08:54:14 +00:00
return success(c, data.Map{
2021-12-06 12:24:22 +00:00
"items": images,
"total": total,
})
}
}
func imageFind(b biz.ImageBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
node := c.Query("node")
id := c.Query("id")
image, raw, err := b.Find(ctx, node, id)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
return success(c, data.Map{"image": image, "raw": raw})
2021-12-06 12:24:22 +00:00
}
}
func imageDelete(b biz.ImageBiz) web.HandlerFunc {
type Args struct {
2021-12-20 06:28:43 +00:00
Node string `json:"node"`
2021-12-06 12:24:22 +00:00
ID string `json:"id"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) (err error) {
2021-12-06 12:24:22 +00:00
args := &Args{}
2022-01-06 08:54:14 +00:00
if err = c.Bind(args); err == nil {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
err = b.Delete(ctx, args.Node, args.ID, c.User())
2021-12-06 12:24:22 +00:00
}
2022-01-06 08:54:14 +00:00
return ajax(c, err)
2021-12-06 12:24:22 +00:00
}
}
func imagePrune(b biz.ImageBiz) web.HandlerFunc {
type Args struct {
Node string `json:"node"`
}
2022-01-06 08:54:14 +00:00
return func(c web.Context) (err error) {
args := &Args{}
2022-01-06 08:54:14 +00:00
if err = c.Bind(args); err != nil {
return err
}
2022-01-06 08:54:14 +00:00
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
count, size, err := b.Prune(ctx, args.Node, c.User())
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
return success(c, data.Map{
"count": count,
"size": size,
})
}
}