diff --git a/cmd/files.go b/cmd/files.go
index 223e0c5..5244ad1 100644
--- a/cmd/files.go
+++ b/cmd/files.go
@@ -112,7 +112,9 @@ func humanReadableSize(bytes int) string {
func getImageDimensions(path string) (*Dimensions, error) {
file, err := os.Open(path)
- if err != nil {
+ if errors.Is(err, os.ErrNotExist) {
+ return &Dimensions{}, nil
+ } else if err != nil {
return &Dimensions{}, err
}
defer file.Close()
@@ -355,7 +357,9 @@ func pathIsValid(filePath string, paths []string) bool {
func isImage(path string) (bool, error) {
file, err := os.Open(path)
- if err != nil {
+ if errors.Is(err, os.ErrNotExist) {
+ return false, nil
+ } else if err != nil {
return false, err
}
defer file.Close()
diff --git a/cmd/version.go b/cmd/version.go
index ec145e5..88beaff 100644
--- a/cmd/version.go
+++ b/cmd/version.go
@@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"
)
-var Version = "0.24.3"
+var Version = "0.25.0"
func init() {
rootCmd.AddCommand(versionCmd)
diff --git a/cmd/web.go b/cmd/web.go
index 66eeb34..c0a948b 100644
--- a/cmd/web.go
+++ b/cmd/web.go
@@ -7,6 +7,7 @@ package cmd
import (
"fmt"
"io"
+ "log"
"math/rand"
"net/http"
"net/url"
@@ -52,33 +53,27 @@ func (f *Filters) GetExcludes() string {
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 err := fn(w, r); err != nil {
- http.Error(w, err.Error(), 500)
+ if Verbose {
+ fmt.Printf("%v | Unavailable file %v requested by %v\n",
+ startTime.Format(LogDate),
+ filePath,
+ r.RemoteAddr,
+ )
}
-}
-func notFound(w http.ResponseWriter, r *http.Request) error {
w.WriteHeader(404)
w.Header().Add("Content-Type", "text/html")
- htmlBody := `
-
-
-
- Not Found
-
-
-
- 404 page not found
-
-`
+ var htmlBody strings.Builder
+ htmlBody.WriteString(``)
+ htmlBody.WriteString(``)
+ htmlBody.WriteString(`Not Found`)
+ htmlBody.WriteString(`404 page not found`)
- _, err := io.WriteString(w, htmlBody)
+ _, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
if err != nil {
return err
}
@@ -238,7 +233,9 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
}
if !pathIsValid(filePath, paths) {
- notFound(w, r)
+ notFound(w, r, filePath)
+
+ return nil
}
exists, err := fileExists(filePath)
@@ -247,7 +244,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
}
if !exists {
- notFound(w, r)
+ notFound(w, r, filePath)
return nil
}
@@ -274,22 +271,20 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
return nil
}
-func serveStaticFileHandler(paths []string) appHandler {
- return func(w http.ResponseWriter, r *http.Request) error {
+func serveStaticFileHandler(paths []string) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
err := serveStaticFile(w, r, paths)
if err != nil {
- return err
+ log.Fatal(err)
}
-
- return nil
}
}
-func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) appHandler {
- return func(w http.ResponseWriter, r *http.Request) error {
+func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
if err != nil {
- return err
+ log.Fatal(err)
}
filters := Filters{}
@@ -310,7 +305,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
if refererUri != "" {
filePath, err = getNextFile(refererUri, sortOrder, re)
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)
switch {
case err != nil && err == ErrNoImagesFound:
- http.NotFound(w, r)
+ notFound(w, r, filePath)
+
+ return
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)
if err != nil {
- return err
+ log.Fatal(err)
}
if !exists {
- notFound(w, r)
+ notFound(w, r, filePath)
+
+ return
}
image, err := isImage(filePath)
if err != nil {
- return err
+ log.Fatal(err)
}
if !image {
- notFound(w, r)
+ notFound(w, r, filePath)
+
+ return
}
dimensions, err := getImageDimensions(filePath)
if err != nil {
- return err
+ log.Fatal(err)
}
err = serveHtml(w, r, filePath, dimensions, &filters)
if err != nil {
- return err
+ log.Fatal(err)
}
}
-
- return nil
}
}