diff --git a/api.go b/api.go index 66cecd0..d955498 100644 --- a/api.go +++ b/api.go @@ -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} } diff --git a/api_images.go b/api_images.go index 422b767..a54d8f2 100644 --- a/api_images.go +++ b/api_images.go @@ -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}, diff --git a/auth.go b/auth.go index 96fe98d..4112116 100644 --- a/auth.go +++ b/auth.go @@ -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 diff --git a/images.go b/images.go index a18baf6..56b38eb 100644 --- a/images.go +++ b/images.go @@ -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 diff --git a/main.go b/main.go index 1d32fce..9affd0f 100644 --- a/main.go +++ b/main.go @@ -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, ))) diff --git a/picture.go b/picture.go index ac05793..b9e272e 100644 --- a/picture.go +++ b/picture.go @@ -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" @@ -26,9 +26,11 @@ type Picture struct { type ByUploadTime []Picture -func (a ByUploadTime) Len() int { return len(a) } +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 { @@ -40,9 +42,9 @@ func getImages(dir string) ([]Picture, error) { filename := file.Name() pictures = append(pictures, Picture{ filepath.Join(dir, filename), // Path - filename, // Basename - filename[:len(filename) - len(filepath.Ext(filename))], // Sanitized filename - file.ModTime(), // UploadTime + filename, // Basename + 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,17 +108,17 @@ 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.") - // Convert to JPEG + // Convert to JPEG } else if img, _, err := image.Decode(blob); err != nil { return err - // Save file - } else if fw, err := os.Create(filepath.Join(NextImgDir, filename + ".jpg")); err != nil { + // Save file + } 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 { diff --git a/version.go b/version.go index 85f9f6c..82911a9 100644 --- a/version.go +++ b/version.go @@ -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