youp0m/api_images.go

79 lines
2.0 KiB
Go

package main
import (
"errors"
"io"
)
var ApiImagesRouting = map[string]struct{AuthFunction; DispatchFunction}{
"GET": {PublicPage, listImages},
"POST": {PublicPage, addImage},
"DELETE": {PrivatePage, hideImage},
}
var ApiNextImagesRouting = map[string]struct{AuthFunction; DispatchFunction}{
"GET": {PrivatePage, listNextImages},
"POST": {PublicPage, addImage},
"DELETE": {PrivatePage, deleteImage},
}
func listImages(u *User, args []string, body io.ReadCloser) (interface{}, error) {
if len(args) < 1 {
return GetPublishedImages()
} else if args[0] == "last" {
return GetLastImage()
} else {
return GetPublishedImage(args[0])
}
}
func listNextImages(u *User, args []string, body io.ReadCloser) (interface{}, error) {
if len(args) < 1 {
return GetNextImages()
} else if len(args) >= 2 && args[1] == "publish" {
if pict, err := GetNextImage(args[0]); err != nil {
return nil, err
} else if err := pict.Publish(); err != nil {
return nil, err
} else {
return true, nil
}
} else {
return GetNextImage(args[0])
}
}
func addImage(u *User, args []string, body io.ReadCloser) (interface{}, error) {
if len(args) < 1 {
return nil, errors.New("Need an image identifier to create")
} else if err := AddImage(args[0], body); err != nil {
return nil, err
} else {
return true, nil
}
}
func hideImage(u *User, args []string, body io.ReadCloser) (interface{}, error) {
if len(args) < 1 {
return nil, errors.New("Need an image identifier to delete")
} else if pict, err := GetPublishedImage(args[0]); err != nil {
return nil, errors.New("No matching image")
} else if err := pict.Unpublish(); err != nil {
return nil, err
} else {
return true, nil
}
}
func deleteImage(u *User, args []string, body io.ReadCloser) (interface{}, error) {
if len(args) < 1 {
return nil, errors.New("Need an image identifier to delete")
} else if pict, err := GetNextImage(args[0]); err != nil {
return nil, errors.New("No matching image")
} else if err := pict.Remove(); err != nil {
return nil, err
} else {
return true, nil
}
}