diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2023-12-09 05:37:40 +0100 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2023-12-09 05:37:40 +0100 |
commit | 1c153a8e8ea709cfc89eda810c41b5d2c9fa8472 (patch) | |
tree | c6814a26b51dee34430f6242a1ee2cfe029d22f9 | |
parent | cd62d6a86e29be1d0643ba0efd70f23ba693344b (diff) | |
download | gallery-1c153a8e8ea709cfc89eda810c41b5d2c9fa8472.tar.gz gallery-1c153a8e8ea709cfc89eda810c41b5d2c9fa8472.tar.xz gallery-1c153a8e8ea709cfc89eda810c41b5d2c9fa8472.zip |
Make some HTTP handlers work
-rw-r--r-- | main.go | 51 | ||||
-rwxr-xr-x | test.sh | 2 |
2 files changed, 45 insertions, 8 deletions
@@ -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) @@ -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 |