diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 51 |
1 files changed, 44 insertions, 7 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) |