Entire 404 page is now clickable, and attempts to open a new random file
This commit is contained in:
parent
0434353942
commit
5c7bb60dcb
|
@ -105,15 +105,11 @@ func humanReadableSize(bytes int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getImageDimensions(path string) (string, error) {
|
func getImageDimensions(path string) (string, error) {
|
||||||
fmt.Println("Opening path " + path)
|
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
fmt.Println("Getting file dimensions")
|
|
||||||
|
|
||||||
myImage, _, err := image.DecodeConfig(file)
|
myImage, _, err := image.DecodeConfig(file)
|
||||||
if errors.Is(err, image.ErrFormat) {
|
if errors.Is(err, image.ErrFormat) {
|
||||||
fmt.Println("File not image")
|
|
||||||
return "", nil
|
return "", nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version = "0.20.0"
|
var Version = "0.20.1"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(versionCmd)
|
rootCmd.AddCommand(versionCmd)
|
||||||
|
|
42
cmd/web.go
42
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 := `<html lang="en">
|
||||||
|
<head>
|
||||||
|
<style>
|
||||||
|
a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;}
|
||||||
|
</style>
|
||||||
|
<title>
|
||||||
|
Not Found
|
||||||
|
</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<a href="/">404 page not found</a>
|
||||||
|
</body>
|
||||||
|
</html>`
|
||||||
|
|
||||||
|
_, err := io.WriteString(w, htmlBody)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func splitQueryParams(query string) []string {
|
func splitQueryParams(query string) []string {
|
||||||
if query == "" {
|
if query == "" {
|
||||||
return []string{}
|
return []string{}
|
||||||
|
@ -194,7 +220,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !pathIsValid(filePath, paths) {
|
if !pathIsValid(filePath, paths) {
|
||||||
http.NotFound(w, r)
|
notFound(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
exists, err := fileExists(filePath)
|
exists, err := fileExists(filePath)
|
||||||
|
@ -203,7 +229,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
http.NotFound(w, r)
|
notFound(w, r)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -307,7 +333,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
|
||||||
case r.URL.Path == "/" && sortOrder == "asc" && refererUri == "":
|
case r.URL.Path == "/" && sortOrder == "asc" && refererUri == "":
|
||||||
filePath, err := pickFile(paths, &filters, sortOrder, fileCache)
|
filePath, err := pickFile(paths, &filters, sortOrder, fileCache)
|
||||||
if err != nil && err == ErrNoImagesFound {
|
if err != nil && err == ErrNoImagesFound {
|
||||||
http.NotFound(w, r)
|
notFound(w, r)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
} else if err != 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)
|
filePath, err = pickFile(paths, &filters, sortOrder, fileCache)
|
||||||
switch {
|
switch {
|
||||||
case err != nil && err == ErrNoImagesFound:
|
case err != nil && err == ErrNoImagesFound:
|
||||||
http.NotFound(w, r)
|
notFound(w, r)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
case err != 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 == "":
|
case r.URL.Path == "/" && sortOrder == "desc" && refererUri == "":
|
||||||
filePath, err := pickFile(paths, &filters, sortOrder, fileCache)
|
filePath, err := pickFile(paths, &filters, sortOrder, fileCache)
|
||||||
if err != nil && err == ErrNoImagesFound {
|
if err != nil && err == ErrNoImagesFound {
|
||||||
http.NotFound(w, r)
|
notFound(w, r)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
@ -403,7 +429,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
|
||||||
case r.URL.Path == "/":
|
case r.URL.Path == "/":
|
||||||
filePath, err := pickFile(paths, &filters, sortOrder, fileCache)
|
filePath, err := pickFile(paths, &filters, sortOrder, fileCache)
|
||||||
if err != nil && err == ErrNoImagesFound {
|
if err != nil && err == ErrNoImagesFound {
|
||||||
http.NotFound(w, r)
|
notFound(w, r)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
@ -429,7 +455,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
http.NotFound(w, r)
|
notFound(w, r)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -439,7 +465,7 @@ func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) app
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !image {
|
if !image {
|
||||||
http.NotFound(w, r)
|
notFound(w, r)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue