From 5c7bb60dcb3148a6d8b94c170219928b4ff80bf0 Mon Sep 17 00:00:00 2001 From: Seednode Date: Fri, 28 Oct 2022 19:09:05 -0500 Subject: [PATCH] Entire 404 page is now clickable, and attempts to open a new random file --- cmd/files.go | 4 ---- cmd/version.go | 2 +- cmd/web.go | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index e3ec86e..0528cde 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -105,15 +105,11 @@ func humanReadableSize(bytes int) string { } func getImageDimensions(path string) (string, error) { - fmt.Println("Opening path " + path) file, err := os.Open(path) defer file.Close() - fmt.Println("Getting file dimensions") - myImage, _, err := image.DecodeConfig(file) if errors.Is(err, image.ErrFormat) { - fmt.Println("File not image") return "", nil } else if err != nil { return "", err diff --git a/cmd/version.go b/cmd/version.go index 06d3f7a..39aff7e 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.20.0" +var Version = "0.20.1" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index ed8edc0..27b1f07 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -69,6 +69,32 @@ func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +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 + +` + + _, err := io.WriteString(w, htmlBody) + if err != nil { + return err + } + + return nil +} + func splitQueryParams(query string) []string { if query == "" { return []string{} @@ -194,7 +220,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err } if !pathIsValid(filePath, paths) { - http.NotFound(w, r) + notFound(w, r) } exists, err := fileExists(filePath) @@ -203,7 +229,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err } if !exists { - http.NotFound(w, r) + notFound(w, r) return nil } @@ -307,7 +333,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app case r.URL.Path == "/" && sortOrder == "asc" && refererUri == "": filePath, err := pickFile(paths, &filters, sortOrder, fileCache) if err != nil && err == ErrNoImagesFound { - http.NotFound(w, r) + notFound(w, r) return nil } else if err != nil { @@ -350,7 +376,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app filePath, err = pickFile(paths, &filters, sortOrder, fileCache) switch { case err != nil && err == ErrNoImagesFound: - http.NotFound(w, r) + notFound(w, r) return nil case err != nil: @@ -377,7 +403,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app case r.URL.Path == "/" && sortOrder == "desc" && refererUri == "": filePath, err := pickFile(paths, &filters, sortOrder, fileCache) if err != nil && err == ErrNoImagesFound { - http.NotFound(w, r) + notFound(w, r) return nil } else if err != nil { @@ -403,7 +429,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app case r.URL.Path == "/": filePath, err := pickFile(paths, &filters, sortOrder, fileCache) if err != nil && err == ErrNoImagesFound { - http.NotFound(w, r) + notFound(w, r) return nil } else if err != nil { @@ -429,7 +455,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app } if !exists { - http.NotFound(w, r) + notFound(w, r) return nil } @@ -439,7 +465,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app return err } if !image { - http.NotFound(w, r) + notFound(w, r) return nil }