From 9096c00db5407f05cec95a5d6583e4f0f5e382bd Mon Sep 17 00:00:00 2001 From: Dominic Ferrando Date: Sat, 19 Apr 2025 18:46:56 -0400 Subject: [PATCH] Get basic thumbnails working --- .gitignore | 5 +++-- Makefile | 3 +-- go.mod | 10 ++++++--- go.sum | 5 +++++ web/main.go | 49 ++++++++++++++++++++++++++---------------- web/views/board.templ | 7 +++++- web/views/thread.templ | 2 +- 7 files changed, 54 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 103efe3..22f3670 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ internal/database/comfychan.db **/*_templ.go tmp -web/static/img/posts/full/* -web/static/img/posts/thumb/* + +web/static/img/posts/ +web/static/img/posts/ diff --git a/Makefile b/Makefile index 0962b2c..a7b85b4 100644 --- a/Makefile +++ b/Makefile @@ -20,5 +20,4 @@ db/seed/f: rm ./internal/database/comfychan.db && sqlite3 ./internal/database/comfychan.db < ./internal/database/seed.sql img/clear: - rm -f ./web/static/img/posts/full/* || true - rm -f ./web/static/img/posts/thumb/* || true + rm -rf ./web/static/img/posts || true diff --git a/go.mod b/go.mod index 1743f54..4e9ec63 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,13 @@ module github.com/dominicf2001/comfychan go 1.23.8 -require github.com/a-h/templ v0.3.857 +require ( + github.com/a-h/templ v0.3.857 + github.com/go-chi/chi/v5 v5.2.1 + github.com/mattn/go-sqlite3 v1.14.28 +) require ( - github.com/go-chi/chi/v5 v5.2.1 // indirect - github.com/mattn/go-sqlite3 v1.14.28 // indirect + github.com/disintegration/imaging v1.6.2 // indirect + golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect ) diff --git a/go.sum b/go.sum index 7289ee0..51281a6 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,13 @@ github.com/a-h/templ v0.3.857 h1:6EqcJuGZW4OL+2iZ3MD+NnIcG7nGkaQeF2Zq5kf9ZGg= github.com/a-h/templ v0.3.857/go.mod h1:qhrhAkRFubE7khxLZHsBFHfX+gWwVNKbzKeF9GlPV4M= +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8= github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/web/main.go b/web/main.go index fcbd4a1..afb5435 100644 --- a/web/main.go +++ b/web/main.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "fmt" + "image" "io" "log" "mime/multipart" @@ -12,6 +13,7 @@ import ( "path/filepath" "strconv" + "github.com/disintegration/imaging" "github.com/dominicf2001/comfychan/internal/database" "github.com/dominicf2001/comfychan/web/views" "github.com/go-chi/chi/v5" @@ -26,21 +28,16 @@ const POST_IMG_THUMB_PATH = "web/static/img/posts/thumb" var dev = true -func disableCacheInDevMode(next http.Handler) http.Handler { - if !dev { - return next - } - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Cache-Control", "no-store") - next.ServeHTTP(w, r) - }) -} - func savePostFile(file *multipart.File, header *multipart.FileHeader) error { dstPathFull := filepath.Join(POST_IMG_FULL_PATH, header.Filename) dstPathThumb := filepath.Join(POST_IMG_THUMB_PATH, header.Filename) - // save full + // FULL + if err := os.MkdirAll(POST_IMG_FULL_PATH, 0755); err != nil { + log.Printf("MkdirAll (full): %v", err) + return err + } + dstFull, err := os.Create(dstPathFull) if err != nil { log.Printf("os.Create (full): %v", err) @@ -57,21 +54,37 @@ func savePostFile(file *multipart.File, header *multipart.FileHeader) error { return err } - // save thumbnail - dstThumb, err := os.Create(dstPathThumb) - if err != nil { - log.Printf("os.Create (thumb): %v", err) + // THUMBNAIL + if err := os.MkdirAll(POST_IMG_THUMB_PATH, 0755); err != nil { + log.Printf("MkdirAll (thumb): %v", err) return err } - defer dstThumb.Close() - if _, err := io.Copy(dstThumb, *file); err != nil { - log.Printf("io.Copy (thumb): %v", err) + + img, _, err := image.Decode(*file) + if err != nil { + log.Printf("image.Decode: %v", err) + return err + } + + thumb := imaging.Resize(img, 200, 0, imaging.Lanczos) + if err = imaging.Save(thumb, dstPathThumb); err != nil { + log.Printf("imaging.Save: %v", err) return err } return nil } +func disableCacheInDevMode(next http.Handler) http.Handler { + if !dev { + return next + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Cache-Control", "no-store") + next.ServeHTTP(w, r) + }) +} + func main() { // ----------------- // SETUP diff --git a/web/views/board.templ b/web/views/board.templ index 087eda9..1038723 100644 --- a/web/views/board.templ +++ b/web/views/board.templ @@ -74,7 +74,12 @@ templ ThreadsCatalog(previews []CatalogThreadPreview) { for _, preview := range previews {
- +

{ preview.Subject } diff --git a/web/views/thread.templ b/web/views/thread.templ index 4289d3f..8d56fa2 100644 --- a/web/views/thread.templ +++ b/web/views/thread.templ @@ -20,7 +20,7 @@ templ ThreadPost(post database.Post, thread *database.Thread) {
if post.MediaPath != "" {
- +
}