From 1c153a8e8ea709cfc89eda810c41b5d2c9fa8472 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sat, 9 Dec 2023 05:37:40 +0100 Subject: Make some HTTP handlers work --- main.go | 51 ++++++++++++++++++++++++++++++++++++++++++++------- test.sh | 2 +- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 2a46b21..7d6d311 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "time" _ "github.com/mattn/go-sqlite3" @@ -87,6 +88,35 @@ func cmdInit(args []string) error { return nil } +var hashRE = regexp.MustCompile(`^/.*?/([0-9a-f]{40})$`) +var staticHandler http.Handler + +func handleRequest(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + staticHandler.ServeHTTP(w, r) + return + } + + // TODO: Return something. + http.NotFound(w, r) +} + +func handleImages(w http.ResponseWriter, r *http.Request) { + if m := hashRE.FindStringSubmatch(r.URL.Path); m == nil { + http.NotFound(w, r) + } else { + http.ServeFile(w, r, imagePath(m[1])) + } +} + +func handleThumbs(w http.ResponseWriter, r *http.Request) { + if m := hashRE.FindStringSubmatch(r.URL.Path); m == nil { + http.NotFound(w, r) + } else { + http.ServeFile(w, r, thumbPath(m[1])) + } +} + // cmdRun runs a web UI against GD on ADDRESS. func cmdRun(args []string) error { if len(args) != 2 { @@ -98,13 +128,10 @@ func cmdRun(args []string) error { address := args[1] - http.Handle("/", http.FileServer(http.Dir("public"))) - // TODO: These subdirectories should be indirect - // (skip the hash subpath, don't require the .webp suffix). - http.Handle("/images", - http.FileServer(http.Dir(filepath.Join(gd, "images")))) - http.Handle("/thumbs", - http.FileServer(http.Dir(filepath.Join(gd, "thumbs")))) + staticHandler = http.FileServer(http.Dir("public")) + http.HandleFunc("/", handleRequest) + http.HandleFunc("/images/", handleImages) + http.HandleFunc("/thumbs/", handleThumbs) host, port, err := net.SplitHostPort(address) if err != nil { @@ -258,6 +285,16 @@ func makeThumbnail(pathImage, pathThumb string) (int, int, error) { return 0, 0, err } + // Create a normalized thumbnail. Since we don't particularly need + // any complex processing, such as surrounding of metadata, + // simply push it through ImageMagick. + // + // - http://www.ericbrasseur.org/gamma.html + // - https://www.imagemagick.org/Usage/thumbnails/ + // - https://imagemagick.org/script/command-line-options.php#layers + // + // TODO: See if we can optimize resulting WebP animations. + // (Do -layers optimize* apply to this format at all?) cmd := exec.Command("convert", pathImage, "-coalesce", "-colorspace", "RGB", "-auto-orient", "-strip", "-resize", "256x128>", "-colorspace", "sRGB", "-format", "%w %h", "+write", "info:", pathThumb) diff --git a/test.sh b/test.sh index adacca5..13cb028 100755 --- a/test.sh +++ b/test.sh @@ -4,7 +4,7 @@ target=/tmp/G input=/tmp/Gi rm -rf $target $input mkdir -p $target -cp -r $HOME/Pictures/Anime $input +cp -ra $HOME/Pictures/Anime $input ./gallery init $target ./gallery import $target $input ./gallery thumbnail $target -- cgit v1.2.3-70-g09d2