youp0m/api_images.go

45 lines
1.1 KiB
Go
Raw Normal View History

2016-06-26 09:23:29 +00:00
package main
import (
"errors"
"io"
)
var ApiImagesRouting = map[string]struct{AuthFunction; DispatchFunction}{
"GET": {PublicPage, listImages},
"POST": {PublicPage, addImage},
"DELETE": {PrivatePage, hideImage},
}
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 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
}
}