youp0m/api_images.go

95 lines
2.4 KiB
Go
Raw Permalink Normal View History

2016-06-26 09:23:29 +00:00
package main
import (
"errors"
"io"
)
2022-09-04 19:45:39 +00:00
func ApiImagesRouting(pe *PictureExplorer) map[string]struct {
2018-09-18 20:16:58 +00:00
AuthFunction
DispatchFunction
2022-09-04 19:45:39 +00:00
} {
return map[string]struct {
AuthFunction
DispatchFunction
}{
"GET": {PublicPage, pe.listImages},
"POST": {PublicPage, pe.addImage},
"DELETE": {PrivatePage, pe.hideImage},
}
2016-06-26 09:23:29 +00:00
}
2022-09-04 19:45:39 +00:00
func ApiNextImagesRouting(pe *PictureExplorer) map[string]struct {
2018-09-18 20:16:58 +00:00
AuthFunction
DispatchFunction
2022-09-04 19:45:39 +00:00
} {
return map[string]struct {
AuthFunction
DispatchFunction
}{
"GET": {PrivatePage, pe.listNextImages},
"POST": {PublicPage, pe.addImage},
"DELETE": {PrivatePage, pe.deleteImage},
}
2016-06-26 11:16:50 +00:00
}
2022-09-04 19:45:39 +00:00
func (pe *PictureExplorer) listImages(u *User, args []string, body io.ReadCloser) (interface{}, error) {
2016-06-26 09:23:29 +00:00
if len(args) < 1 {
2022-09-04 19:45:39 +00:00
return pe.GetPublishedImages()
2016-06-26 09:23:29 +00:00
} else if args[0] == "last" {
2022-09-04 19:45:39 +00:00
return pe.GetLastImage()
2016-06-26 09:23:29 +00:00
} else {
2022-09-04 19:45:39 +00:00
return pe.GetPublishedImage(args[0])
2016-06-26 09:23:29 +00:00
}
}
2022-09-04 19:45:39 +00:00
func (pe *PictureExplorer) listNextImages(u *User, args []string, body io.ReadCloser) (interface{}, error) {
2016-06-26 11:16:50 +00:00
if len(args) < 1 {
2022-09-04 19:45:39 +00:00
return pe.GetNextImages()
2016-06-26 11:16:50 +00:00
} else if len(args) >= 2 && args[1] == "publish" {
2022-09-04 19:45:39 +00:00
if pict, err := pe.GetNextImage(args[0]); err != nil {
2016-06-26 11:16:50 +00:00
return nil, err
2022-09-04 19:45:39 +00:00
} else if err := pe.Publish(pict); err != nil {
2016-06-26 11:16:50 +00:00
return nil, err
} else {
return true, nil
}
} else {
2022-09-04 19:45:39 +00:00
return pe.GetNextImage(args[0])
2016-06-26 11:16:50 +00:00
}
}
2022-09-04 19:45:39 +00:00
func (pe *PictureExplorer) addImage(u *User, args []string, body io.ReadCloser) (interface{}, error) {
2016-06-26 09:23:29 +00:00
if len(args) < 1 {
return nil, errors.New("Need an image identifier to create")
2022-09-04 19:45:39 +00:00
} else if err := pe.AddImage(args[0], body); err != nil {
2016-06-26 09:23:29 +00:00
return nil, err
} else {
return true, nil
}
}
2022-09-04 19:45:39 +00:00
func (pe *PictureExplorer) hideImage(u *User, args []string, body io.ReadCloser) (interface{}, error) {
2016-06-26 09:23:29 +00:00
if len(args) < 1 {
return nil, errors.New("Need an image identifier to delete")
2022-09-04 19:45:39 +00:00
} else if pict, err := pe.GetPublishedImage(args[0]); err != nil {
2016-06-26 09:23:29 +00:00
return nil, errors.New("No matching image")
2022-09-04 19:45:39 +00:00
} else if err := pe.Unpublish(pict); err != nil {
2016-06-26 09:23:29 +00:00
return nil, err
} else {
return true, nil
}
}
2016-06-26 11:16:50 +00:00
2022-09-04 19:45:39 +00:00
func (pe *PictureExplorer) deleteImage(u *User, args []string, body io.ReadCloser) (interface{}, error) {
2016-06-26 11:16:50 +00:00
if len(args) < 1 {
return nil, errors.New("Need an image identifier to delete")
2022-09-04 19:45:39 +00:00
} else if pict, err := pe.GetNextImage(args[0]); err != nil {
2016-06-26 11:16:50 +00:00
return nil, errors.New("No matching image")
2022-09-04 19:45:39 +00:00
} else if err := pe.Remove(pict); err != nil {
2016-06-26 11:16:50 +00:00
return nil, err
} else {
return true, nil
}
}