Get basic thumbnails working
This commit is contained in:
+3
-2
@@ -1,5 +1,6 @@
|
|||||||
internal/database/comfychan.db
|
internal/database/comfychan.db
|
||||||
**/*_templ.go
|
**/*_templ.go
|
||||||
tmp
|
tmp
|
||||||
web/static/img/posts/full/*
|
|
||||||
web/static/img/posts/thumb/*
|
web/static/img/posts/
|
||||||
|
web/static/img/posts/
|
||||||
|
|||||||
@@ -20,5 +20,4 @@ db/seed/f:
|
|||||||
rm ./internal/database/comfychan.db && sqlite3 ./internal/database/comfychan.db < ./internal/database/seed.sql
|
rm ./internal/database/comfychan.db && sqlite3 ./internal/database/comfychan.db < ./internal/database/seed.sql
|
||||||
|
|
||||||
img/clear:
|
img/clear:
|
||||||
rm -f ./web/static/img/posts/full/* || true
|
rm -rf ./web/static/img/posts || true
|
||||||
rm -f ./web/static/img/posts/thumb/* || true
|
|
||||||
|
|||||||
@@ -2,9 +2,13 @@ module github.com/dominicf2001/comfychan
|
|||||||
|
|
||||||
go 1.23.8
|
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 (
|
require (
|
||||||
github.com/go-chi/chi/v5 v5.2.1 // indirect
|
github.com/disintegration/imaging v1.6.2 // indirect
|
||||||
github.com/mattn/go-sqlite3 v1.14.28 // indirect
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
github.com/a-h/templ v0.3.857 h1:6EqcJuGZW4OL+2iZ3MD+NnIcG7nGkaQeF2Zq5kf9ZGg=
|
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/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 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
|
||||||
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
|
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 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
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 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
|
||||||
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
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
@@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
@@ -12,6 +13,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/disintegration/imaging"
|
||||||
"github.com/dominicf2001/comfychan/internal/database"
|
"github.com/dominicf2001/comfychan/internal/database"
|
||||||
"github.com/dominicf2001/comfychan/web/views"
|
"github.com/dominicf2001/comfychan/web/views"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
@@ -26,21 +28,16 @@ const POST_IMG_THUMB_PATH = "web/static/img/posts/thumb"
|
|||||||
|
|
||||||
var dev = true
|
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 {
|
func savePostFile(file *multipart.File, header *multipart.FileHeader) error {
|
||||||
dstPathFull := filepath.Join(POST_IMG_FULL_PATH, header.Filename)
|
dstPathFull := filepath.Join(POST_IMG_FULL_PATH, header.Filename)
|
||||||
dstPathThumb := filepath.Join(POST_IMG_THUMB_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)
|
dstFull, err := os.Create(dstPathFull)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("os.Create (full): %v", err)
|
log.Printf("os.Create (full): %v", err)
|
||||||
@@ -57,21 +54,37 @@ func savePostFile(file *multipart.File, header *multipart.FileHeader) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// save thumbnail
|
// THUMBNAIL
|
||||||
dstThumb, err := os.Create(dstPathThumb)
|
if err := os.MkdirAll(POST_IMG_THUMB_PATH, 0755); err != nil {
|
||||||
if err != nil {
|
log.Printf("MkdirAll (thumb): %v", err)
|
||||||
log.Printf("os.Create (thumb): %v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer dstThumb.Close()
|
|
||||||
if _, err := io.Copy(dstThumb, *file); err != nil {
|
img, _, err := image.Decode(*file)
|
||||||
log.Printf("io.Copy (thumb): %v", err)
|
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 err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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() {
|
func main() {
|
||||||
// -----------------
|
// -----------------
|
||||||
// SETUP
|
// SETUP
|
||||||
|
|||||||
@@ -74,7 +74,12 @@ templ ThreadsCatalog(previews []CatalogThreadPreview) {
|
|||||||
for _, preview := range previews {
|
for _, preview := range previews {
|
||||||
<div class="catalog-preview">
|
<div class="catalog-preview">
|
||||||
<a href={ templ.URL(preview.ThreadURL) }>
|
<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>
|
</a>
|
||||||
<h1>
|
<h1>
|
||||||
<a href={ templ.URL(preview.ThreadURL) } class="thread-subject catalog-preview-link">{ preview.Subject }</a>
|
<a href={ templ.URL(preview.ThreadURL) } class="thread-subject catalog-preview-link">{ preview.Subject }</a>
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ templ ThreadPost(post database.Post, thread *database.Thread) {
|
|||||||
<article id={ fmt.Sprintf("post-%d", post.Id) } class={ getPostClass(isOp) }>
|
<article id={ fmt.Sprintf("post-%d", post.Id) } class={ getPostClass(isOp) }>
|
||||||
if post.MediaPath != "" {
|
if post.MediaPath != "" {
|
||||||
<div class="post-img-container">
|
<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>
|
</div>
|
||||||
}
|
}
|
||||||
<header class="post-header">
|
<header class="post-header">
|
||||||
|
|||||||
Reference in New Issue
Block a user