Get basic thumbnails working

This commit is contained in:
Dominic Ferrando
2025-04-19 18:46:56 -04:00
parent b785ba0448
commit 9096c00db5
7 changed files with 54 additions and 27 deletions
+3 -2
View File
@@ -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/
+1 -2
View File
@@ -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
+7 -3
View File
@@ -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
)
+5
View File
@@ -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=
+31 -18
View File
@@ -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
+6 -1
View File
@@ -74,7 +74,12 @@ templ ThreadsCatalog(previews []CatalogThreadPreview) {
for _, preview := range previews {
<div class="catalog-preview">
<a href={ templ.URL(preview.ThreadURL) }>
<img class="catalog-preview-img" src={ fmt.Sprintf("/static/img/posts/full/%s", preview.MediaPath) }/>
<img
loading="lazy"
class="catalog-preview-img"
src={ fmt.Sprintf("/static/img/posts/thumb/%s",
preview.MediaPath) }
/>
</a>
<h1>
<a href={ templ.URL(preview.ThreadURL) } class="thread-subject catalog-preview-link">{ preview.Subject }</a>
+1 -1
View File
@@ -20,7 +20,7 @@ templ ThreadPost(post database.Post, thread *database.Thread) {
<article id={ fmt.Sprintf("post-%d", post.Id) } class={ getPostClass(isOp) }>
if post.MediaPath != "" {
<div class="post-img-container">
<img src={ fmt.Sprintf("/static/img/posts/full/%s", post.MediaPath) } class="post-img"/>
<img loading="lazy" src={ fmt.Sprintf("/static/img/posts/thumb/%s", post.MediaPath) } class="post-img"/>
</div>
}
<header class="post-header">