format + bump

This commit is contained in:
nemunaire 2018-09-18 22:16:58 +02:00
parent 4febf49b3e
commit bd76956b46
7 changed files with 50 additions and 36 deletions

9
api.go
View File

@ -12,17 +12,20 @@ import (
type DispatchFunction func(*User, []string, io.ReadCloser) (interface{}, error)
var apiRoutes = map[string]*(map[string]struct{AuthFunction;DispatchFunction}){
var apiRoutes = map[string]*(map[string]struct {
AuthFunction
DispatchFunction
}){
"images": &ApiImagesRouting,
"next": &ApiNextImagesRouting,
"version": &ApiVersionRouting,
}
type apiHandler struct {
Authenticate func(*http.Request) (*User)
Authenticate func(*http.Request) *User
}
func ApiHandler(Authenticate func(*http.Request) (*User)) (apiHandler) {
func ApiHandler(Authenticate func(*http.Request) *User) apiHandler {
return apiHandler{Authenticate}
}

View File

@ -5,13 +5,19 @@ import (
"io"
)
var ApiImagesRouting = map[string]struct{AuthFunction; DispatchFunction}{
var ApiImagesRouting = map[string]struct {
AuthFunction
DispatchFunction
}{
"GET": {PublicPage, listImages},
"POST": {PublicPage, addImage},
"DELETE": {PrivatePage, hideImage},
}
var ApiNextImagesRouting = map[string]struct{AuthFunction; DispatchFunction}{
var ApiNextImagesRouting = map[string]struct {
AuthFunction
DispatchFunction
}{
"GET": {PrivatePage, listNextImages},
"POST": {PublicPage, addImage},
"DELETE": {PrivatePage, deleteImage},

View File

@ -52,10 +52,9 @@ func (h Htpasswd) Authenticate(username, password string) *User {
}
}
/// Request authentication
func Authenticate(htpasswd Htpasswd, r *http.Request) (*User) {
func Authenticate(htpasswd Htpasswd, r *http.Request) *User {
// Authenticate the user if any
if username, password, ok := r.BasicAuth(); ok {
return htpasswd.Authenticate(username, password)
@ -64,7 +63,6 @@ func Authenticate(htpasswd Htpasswd, r *http.Request) (*User) {
}
}
/// Page rules
type AuthFunction func(*User, []string) bool

View File

@ -6,14 +6,14 @@ import (
"strings"
)
type imagesHandler struct{
type imagesHandler struct {
name string
auth func(*http.Request) (bool)
auth func(*http.Request) bool
hndlr http.Handler
getter func(fname string) (Picture, error)
}
func ImagesHandler(name string, auth func(*http.Request) (bool), hndlr http.Handler, getter func(fname string) (Picture, error)) (imagesHandler) {
func ImagesHandler(name string, auth func(*http.Request) bool, hndlr http.Handler, getter func(fname string) (Picture, error)) imagesHandler {
return imagesHandler{name, auth, hndlr, getter}
}
@ -39,7 +39,7 @@ func (i imagesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// Check rights
if ! i.auth(r) {
if !i.auth(r) {
w.Header().Set("WWW-Authenticate", "Basic realm=\"YouP0m\"")
http.Error(w, "You are not allowed to perform this request.", http.StatusUnauthorized)
return

14
main.go
View File

@ -48,7 +48,7 @@ func main() {
}
log.Println("Registering handlers...")
authFunc := func (r *http.Request) (*User){ return Authenticate(*htpasswd, r) }
authFunc := func(r *http.Request) *User { return Authenticate(*htpasswd, r) }
staticDir, _ := filepath.Abs("static")
if _, err := os.Stat(staticDir); os.IsNotExist(err) {
@ -59,7 +59,9 @@ func main() {
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, filepath.Join(staticDir, "index.html")) })
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(staticDir, "index.html"))
})
mux.Handle("/css/", http.FileServer(http.Dir(staticDir)))
mux.Handle("/js/", http.FileServer(http.Dir(staticDir)))
@ -67,26 +69,26 @@ func main() {
mux.Handle("/images/", http.StripPrefix("/images", ImagesHandler(
"images",
func (*http.Request) (bool){ return true; },
func(*http.Request) bool { return true },
http.FileServer(http.Dir(PublishedImgDir)),
GetPublishedImage,
)))
mux.Handle("/images/next/", http.StripPrefix("/images/next", ImagesHandler(
"next",
func (r *http.Request) (bool){ return authFunc(r) != nil; },
func(r *http.Request) bool { return authFunc(r) != nil },
http.FileServer(http.Dir(NextImgDir)),
GetNextImage,
)))
mux.Handle("/images/thumbs/", http.StripPrefix("/images/thumbs", ImagesHandler(
"thumbs",
func (*http.Request) (bool){ return true; },
func(*http.Request) bool { return true },
http.FileServer(http.Dir(ThumbsDir)),
GetPublishedImage,
)))
mux.Handle("/images/next/thumbs/", http.StripPrefix("/images/next/thumbs", ImagesHandler(
"nexthumbs",
func (r *http.Request) (bool){ return authFunc(r) != nil; },
func(r *http.Request) bool { return authFunc(r) != nil },
http.FileServer(http.Dir(ThumbsDir)),
GetNextImage,
)))

View File

@ -2,12 +2,12 @@ package main
import (
"errors"
"io"
"io/ioutil"
"image"
_ "image/gif"
"image/jpeg"
_ "image/png"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
@ -28,7 +28,9 @@ type ByUploadTime []Picture
func (a ByUploadTime) Len() int { return len(a) }
func (a ByUploadTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByUploadTime) Less(i, j int) bool { return a[i].UploadTime.Sub(a[j].UploadTime).Nanoseconds() < 0 }
func (a ByUploadTime) Less(i, j int) bool {
return a[i].UploadTime.Sub(a[j].UploadTime).Nanoseconds() < 0
}
func getImages(dir string) ([]Picture, error) {
if files, err := ioutil.ReadDir(dir); err != nil {
@ -41,7 +43,7 @@ func getImages(dir string) ([]Picture, error) {
pictures = append(pictures, Picture{
filepath.Join(dir, filename), // Path
filename, // Basename
filename[:len(filename) - len(filepath.Ext(filename))], // Sanitized filename
filename[:len(filename)-len(filepath.Ext(filename))], // Sanitized filename
file.ModTime(), // UploadTime
})
}
@ -94,7 +96,7 @@ func GetNextImage(fname string) (Picture, error) {
return getImage(GetNextImages, fname)
}
func UniqueImage(filename string) (bool) {
func UniqueImage(filename string) bool {
if pict, _ := GetPublishedImage(filename); pict.path != "" {
return false
} else {
@ -106,7 +108,7 @@ func UniqueImage(filename string) (bool) {
}
}
func AddImage(filename string, blob io.ReadCloser) (error) {
func AddImage(filename string, blob io.ReadCloser) error {
// Check the name is not already used
if ok := UniqueImage(filename); !ok {
return errors.New("This filename is already used, please choose another one.")
@ -116,7 +118,7 @@ func AddImage(filename string, blob io.ReadCloser) (error) {
return err
// Save file
} else if fw, err := os.Create(filepath.Join(NextImgDir, filename + ".jpg")); err != nil {
} else if fw, err := os.Create(filepath.Join(NextImgDir, filename+".jpg")); err != nil {
return err
} else if err := jpeg.Encode(fw, img, nil); err != nil {
return err
@ -126,7 +128,7 @@ func AddImage(filename string, blob io.ReadCloser) (error) {
thumb := resize.Thumbnail(300, 185, img, resize.Lanczos3)
// Save thumbnail
if fw, err := os.Create(filepath.Join(ThumbsDir, filename + ".jpg")); err != nil {
if fw, err := os.Create(filepath.Join(ThumbsDir, filename+".jpg")); err != nil {
return err
} else if err := jpeg.Encode(fw, thumb, nil); err != nil {
return err
@ -138,7 +140,7 @@ func AddImage(filename string, blob io.ReadCloser) (error) {
return nil
}
func (p Picture) Publish() (error) {
func (p Picture) Publish() error {
npath := filepath.Join(PublishedImgDir, p.basename)
if err := os.Rename(p.path, npath); err != nil {
return err
@ -149,7 +151,7 @@ func (p Picture) Publish() (error) {
}
}
func (p Picture) Unpublish() (error) {
func (p Picture) Unpublish() error {
if err := os.Rename(p.path, filepath.Join(NextImgDir, p.basename)); err != nil {
return err
} else {
@ -157,7 +159,7 @@ func (p Picture) Unpublish() (error) {
}
}
func (p Picture) Remove() (error) {
func (p Picture) Remove() error {
if err := os.Remove(p.path); err != nil {
return err
} else {

View File

@ -4,12 +4,15 @@ import (
"io"
)
var ApiVersionRouting = map[string]struct{AuthFunction; DispatchFunction}{
var ApiVersionRouting = map[string]struct {
AuthFunction
DispatchFunction
}{
"GET": {PublicPage, showVersion},
}
func showVersion(u *User, args []string, body io.ReadCloser) (interface{}, error) {
m := map[string]interface{}{"version": 0.1}
m := map[string]interface{}{"version": 0.2}
if u != nil {
m["youare"] = *u