Unified error handling, fixed missing file checks, and added logging for requested (but not available) files

This commit is contained in:
Seednode 2022-11-10 00:26:21 -06:00
parent 0a251e8f5a
commit 283b4fccec
3 changed files with 47 additions and 44 deletions

View File

@ -112,7 +112,9 @@ func humanReadableSize(bytes int) string {
func getImageDimensions(path string) (*Dimensions, error) { func getImageDimensions(path string) (*Dimensions, error) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if errors.Is(err, os.ErrNotExist) {
return &Dimensions{}, nil
} else if err != nil {
return &Dimensions{}, err return &Dimensions{}, err
} }
defer file.Close() defer file.Close()
@ -355,7 +357,9 @@ func pathIsValid(filePath string, paths []string) bool {
func isImage(path string) (bool, error) { func isImage(path string) (bool, error) {
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if errors.Is(err, os.ErrNotExist) {
return false, nil
} else if err != nil {
return false, err return false, err
} }
defer file.Close() defer file.Close()

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.24.3" var Version = "0.25.0"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)

View File

@ -7,6 +7,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io" "io"
"log"
"math/rand" "math/rand"
"net/http" "net/http"
"net/url" "net/url"
@ -52,33 +53,27 @@ func (f *Filters) GetExcludes() string {
return strings.Join(f.Excludes, ",") return strings.Join(f.Excludes, ",")
} }
type appHandler func(http.ResponseWriter, *http.Request) error func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
startTime := time.Now()
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if Verbose {
if err := fn(w, r); err != nil { fmt.Printf("%v | Unavailable file %v requested by %v\n",
http.Error(w, err.Error(), 500) startTime.Format(LogDate),
} filePath,
r.RemoteAddr,
)
} }
func notFound(w http.ResponseWriter, r *http.Request) error {
w.WriteHeader(404) w.WriteHeader(404)
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
htmlBody := `<html lang="en"> var htmlBody strings.Builder
<head> htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
<style> htmlBody.WriteString(`<style>a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;}</style>`)
a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;} htmlBody.WriteString(`<title>Not Found</title></head>`)
</style> htmlBody.WriteString(`<body><a href="/">404 page not found</a></body></html>`)
<title>
Not Found
</title>
</head>
<body>
<a href="/">404 page not found</a>
</body>
</html>`
_, err := io.WriteString(w, htmlBody) _, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
if err != nil { if err != nil {
return err return err
} }
@ -238,7 +233,9 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
} }
if !pathIsValid(filePath, paths) { if !pathIsValid(filePath, paths) {
notFound(w, r) notFound(w, r, filePath)
return nil
} }
exists, err := fileExists(filePath) exists, err := fileExists(filePath)
@ -247,7 +244,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
} }
if !exists { if !exists {
notFound(w, r) notFound(w, r, filePath)
return nil return nil
} }
@ -274,22 +271,20 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
return nil return nil
} }
func serveStaticFileHandler(paths []string) appHandler { func serveStaticFileHandler(paths []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error { return func(w http.ResponseWriter, r *http.Request) {
err := serveStaticFile(w, r, paths) err := serveStaticFile(w, r, paths)
if err != nil { if err != nil {
return err log.Fatal(err)
} }
return nil
} }
} }
func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) appHandler { func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) error { return func(w http.ResponseWriter, r *http.Request) {
refererUri, err := stripQueryParams(refererToUri(r.Referer())) refererUri, err := stripQueryParams(refererToUri(r.Referer()))
if err != nil { if err != nil {
return err log.Fatal(err)
} }
filters := Filters{} filters := Filters{}
@ -310,7 +305,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
if refererUri != "" { if refererUri != "" {
filePath, err = getNextFile(refererUri, sortOrder, re) filePath, err = getNextFile(refererUri, sortOrder, re)
if err != nil { if err != nil {
return err log.Fatal(err)
} }
} }
@ -318,9 +313,11 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
filePath, err = getNewFile(paths, &filters, sortOrder, re, fileCache) filePath, err = getNewFile(paths, &filters, sortOrder, re, fileCache)
switch { switch {
case err != nil && err == ErrNoImagesFound: case err != nil && err == ErrNoImagesFound:
http.NotFound(w, r) notFound(w, r, filePath)
return
case err != nil: case err != nil:
return err log.Fatal(err)
} }
} }
@ -341,32 +338,34 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
exists, err := fileExists(filePath) exists, err := fileExists(filePath)
if err != nil { if err != nil {
return err log.Fatal(err)
} }
if !exists { if !exists {
notFound(w, r) notFound(w, r, filePath)
return
} }
image, err := isImage(filePath) image, err := isImage(filePath)
if err != nil { if err != nil {
return err log.Fatal(err)
} }
if !image { if !image {
notFound(w, r) notFound(w, r, filePath)
return
} }
dimensions, err := getImageDimensions(filePath) dimensions, err := getImageDimensions(filePath)
if err != nil { if err != nil {
return err log.Fatal(err)
} }
err = serveHtml(w, r, filePath, dimensions, &filters) err = serveHtml(w, r, filePath, dimensions, &filters)
if err != nil { if err != nil {
return err log.Fatal(err)
} }
} }
return nil
} }
} }