Implement file upload for replies
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
internal/database/comfychan.db
|
internal/database/comfychan.db
|
||||||
**/*_templ.go
|
**/*_templ.go
|
||||||
tmp
|
tmp
|
||||||
|
web/static/img/posts
|
||||||
|
|||||||
@@ -118,8 +118,8 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaP
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PutPost(db *sql.DB, threadId int, body string) error {
|
func PutPost(db *sql.DB, threadId int, body string, mediaPath string) error {
|
||||||
_, err := db.Exec(`INSERT INTO posts (thread_id, body) VALUES (?, ?)`, threadId, body)
|
_, err := db.Exec(`INSERT INTO posts (thread_id, body, media_path) VALUES (?, ?, ?)`, threadId, body, mediaPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+57
-20
@@ -2,9 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -17,6 +19,8 @@ import (
|
|||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const FILE_MEM_LIMIT int64 = 10 << 20
|
||||||
|
|
||||||
var dev = true
|
var dev = true
|
||||||
|
|
||||||
func disableCacheInDevMode(next http.Handler) http.Handler {
|
func disableCacheInDevMode(next http.Handler) http.Handler {
|
||||||
@@ -29,6 +33,24 @@ func disableCacheInDevMode(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func savePostFile(file *multipart.File, header *multipart.FileHeader) error {
|
||||||
|
dstPath := filepath.Join("web/static/img/posts", header.Filename)
|
||||||
|
|
||||||
|
dst, err := os.Create(dstPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("os.Create: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer dst.Close()
|
||||||
|
|
||||||
|
if _, err := io.Copy(dst, *file); err != nil {
|
||||||
|
log.Printf("io.Copy: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// -----------------
|
// -----------------
|
||||||
// SETUP
|
// SETUP
|
||||||
@@ -119,37 +141,27 @@ func main() {
|
|||||||
r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) {
|
r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) {
|
||||||
slug := chi.URLParam(r, "slug")
|
slug := chi.URLParam(r, "slug")
|
||||||
|
|
||||||
|
subject := r.FormValue("subject")
|
||||||
|
body := r.FormValue("body")
|
||||||
|
|
||||||
// 10 MB memory limit
|
// 10 MB memory limit
|
||||||
if err := r.ParseMultipartForm(10 << 20); err != nil {
|
if err := r.ParseMultipartForm(FILE_MEM_LIMIT); err != nil {
|
||||||
http.Error(w, "Bad form data", http.StatusBadRequest)
|
http.Error(w, "Failed to parse form", http.StatusBadRequest)
|
||||||
log.Printf("ParseForm: %v", err)
|
log.Printf("ParseMultipartForm: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
file, header, err := r.FormFile("file")
|
file, header, err := r.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to retrieve file from form", http.StatusBadRequest)
|
http.Error(w, "Failed to retrive file from form", http.StatusBadRequest)
|
||||||
log.Printf("FormFile: %v", err)
|
log.Printf("FormFile: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
subject := r.FormValue("subject")
|
if err := savePostFile(&file, header); err != nil {
|
||||||
body := r.FormValue("body")
|
|
||||||
|
|
||||||
dstPath := filepath.Join("web/static/img/posts", header.Filename)
|
|
||||||
|
|
||||||
dst, err := os.Create(dstPath)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Failed to create file", http.StatusInternalServerError)
|
|
||||||
log.Printf("os.Create: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer dst.Close()
|
|
||||||
|
|
||||||
if _, err := io.Copy(dst, file); err != nil {
|
|
||||||
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
||||||
log.Printf("io.Copy: %v", err)
|
log.Printf("savePostFile: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,8 +184,33 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
body := r.FormValue("body")
|
body := r.FormValue("body")
|
||||||
|
mediaPath := ""
|
||||||
|
|
||||||
if err := database.PutPost(db, threadId, body); err != nil {
|
// 10 MB memory limit
|
||||||
|
if err := r.ParseMultipartForm(FILE_MEM_LIMIT); err != nil {
|
||||||
|
http.Error(w, "Failed to parse form", http.StatusBadRequest)
|
||||||
|
log.Printf("ParseMultipartForm: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
file, header, err := r.FormFile("file")
|
||||||
|
if err != nil {
|
||||||
|
if !errors.Is(err, http.ErrMissingFile) {
|
||||||
|
http.Error(w, "Failed to retrive file from form", http.StatusBadRequest)
|
||||||
|
log.Printf("FormFile: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
defer file.Close()
|
||||||
|
if err := savePostFile(&file, header); err != nil {
|
||||||
|
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
||||||
|
log.Printf("savePostFile: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mediaPath = header.Filename
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := database.PutPost(db, threadId, body, mediaPath); err != nil {
|
||||||
http.Error(w, "Failed to create post", http.StatusInternalServerError)
|
http.Error(w, "Failed to create post", http.StatusInternalServerError)
|
||||||
log.Printf("PutPost: %v", err)
|
log.Printf("PutPost: %v", err)
|
||||||
return
|
return
|
||||||
|
|||||||
+45
-41
@@ -1,56 +1,60 @@
|
|||||||
package views
|
package views
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
database "github.com/dominicf2001/comfychan/internal/database"
|
"github.com/dominicf2001/comfychan/internal/database"
|
||||||
"github.com/dominicf2001/comfychan/web/views/shared"
|
"github.com/dominicf2001/comfychan/web/views/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
templ NewThreadForm(board database.Board) {
|
templ NewThreadForm(board database.Board) {
|
||||||
<form hx-encoding="multipart/form-data" style="display: none;" id="newThreadForm" hx-swap="none" hx-post={
|
<form
|
||||||
fmt.Sprintf("/%s/threads", board.Slug) } _="on htmx:afterRequest
|
hx-encoding="multipart/form-data"
|
||||||
|
style="display: none;"
|
||||||
|
id="newThreadForm"
|
||||||
|
hx-swap="none"
|
||||||
|
hx-post={ fmt.Sprintf("/%s/threads", board.Slug) }
|
||||||
|
_="on htmx:afterRequest
|
||||||
hide me
|
hide me
|
||||||
then show #newThreadBtn
|
then show #newThreadBtn
|
||||||
then trigger refreshThreads on body">
|
then trigger refreshThreads on body"
|
||||||
<table>
|
>
|
||||||
<tbody>
|
<table>
|
||||||
<tr class="new-post-form-field">
|
<tbody>
|
||||||
<th>Subject</th>
|
<tr class="new-post-form-field">
|
||||||
<td><input name="subject" /></td>
|
<th>Subject</th>
|
||||||
</tr>
|
<td><input name="subject"/></td>
|
||||||
<tr class="new-post-form-field">
|
</tr>
|
||||||
<th>Comment</th>
|
<tr class="new-post-form-field">
|
||||||
<td><textarea name="body" required></textarea></td>
|
<th>Comment</th>
|
||||||
</tr>
|
<td><textarea name="body" required></textarea></td>
|
||||||
<tr class="new-post-form-field">
|
</tr>
|
||||||
<th>File</th>
|
<tr class="new-post-form-field">
|
||||||
<td><input name="file" type="file" required /></td>
|
<th>File</th>
|
||||||
</tr>
|
<td><input name="file" type="file" required/></td>
|
||||||
</tbody>
|
</tr>
|
||||||
</table>
|
</tbody>
|
||||||
<button type="submit">Submit</button>
|
</table>
|
||||||
</form>
|
<button type="submit">Submit</button>
|
||||||
|
</form>
|
||||||
}
|
}
|
||||||
|
|
||||||
templ BoardHeader(board database.Board) {
|
templ BoardHeader(board database.Board) {
|
||||||
<header class="board-header">
|
<header class="board-header">
|
||||||
<img src={ fmt.Sprintf("/static/img/banners/%s.png", board.Slug) } />
|
<img src={ fmt.Sprintf("/static/img/banners/%s.png", board.Slug) }/>
|
||||||
<h1>
|
<h1>
|
||||||
{ fmt.Sprintf("/%s/ - %s", board.Slug, board.Name) }
|
{ fmt.Sprintf("/%s/ - %s", board.Slug, board.Name) }
|
||||||
</h1>
|
</h1>
|
||||||
<p>{ board.Tag }</p>
|
<p>{ board.Tag }</p>
|
||||||
</header>
|
</header>
|
||||||
}
|
}
|
||||||
|
|
||||||
templ Board(board database.Board) {
|
templ Board(board database.Board) {
|
||||||
@shared.Layout() {
|
@shared.Layout() {
|
||||||
@BoardHeader(board)
|
@BoardHeader(board)
|
||||||
<div class="new-post-container">
|
<div class="new-post-container">
|
||||||
<button _="on click
|
<button _="on click hide me then show #newThreadForm" id="newThreadBtn">[New Thread]</button>
|
||||||
hide me
|
@NewThreadForm(board)
|
||||||
then show #newThreadForm" id="newThreadBtn">[New Thread]</button>
|
</div>
|
||||||
@NewThreadForm(board)
|
<div hx-get={ fmt.Sprintf("/hx/%s/catalog", board.Slug) } hx-trigger="load, refreshThreads from:body"></div>
|
||||||
</div>
|
}
|
||||||
<div hx-get={ fmt.Sprintf("/hx/%s/catalog", board.Slug) } hx-trigger="load, refreshThreads from:body"></div>
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+69
-53
@@ -1,65 +1,81 @@
|
|||||||
package views
|
package views
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dominicf2001/comfychan/internal/database"
|
"github.com/dominicf2001/comfychan/internal/database"
|
||||||
"github.com/dominicf2001/comfychan/web/views/shared"
|
"github.com/dominicf2001/comfychan/web/views/shared"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
templ ThreadPosts(posts []database.Post) {
|
templ ThreadPosts(posts []database.Post) {
|
||||||
for _, post := range (posts) {
|
for _, post := range (posts) {
|
||||||
<article class="post">
|
<article class="post">
|
||||||
<header class="post-header">
|
if post.MediaPath != "" {
|
||||||
<span class="post-author">{ post.Author }</span>
|
<div class="post-img-container">
|
||||||
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
<img src={ fmt.Sprintf("/static/img/posts/%s", post.MediaPath) } class="post-img"/>
|
||||||
<span class="post-time">{ post.CreatedAt.Format("15:04") }</span>
|
</div>
|
||||||
<span class="post-num">No. { strconv.Itoa(post.Id) }</span>
|
}
|
||||||
</header>
|
<header class="post-header">
|
||||||
<section>
|
<span class="post-author">{ post.Author }</span>
|
||||||
<p>{ post.Body }</p>
|
<span class="post-date">{ post.CreatedAt.Format("01/02/2006") }</span>
|
||||||
</section>
|
<span class="post-time">{ post.CreatedAt.Format("15:04") }</span>
|
||||||
</article>
|
<span class="post-num">No. { strconv.Itoa(post.Id) }</span>
|
||||||
<br />
|
</header>
|
||||||
}
|
<p>{ post.Body }</p>
|
||||||
|
</article>
|
||||||
|
<br/>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templ Thread(board database.Board, thread database.Thread, posts []database.Post) {
|
templ Thread(board database.Board, thread database.Thread, posts []database.Post) {
|
||||||
@shared.Layout() {
|
@shared.Layout() {
|
||||||
@BoardHeader(board)
|
@BoardHeader(board)
|
||||||
<div class="new-post-container">
|
<div class="new-post-container">
|
||||||
<form id="newPostForm" hx-swap="none" hx-post={ fmt.Sprintf("/%s/threads/%d", board.Slug, thread.Id) } _="on htmx:afterRequest
|
<form
|
||||||
|
hx-encoding="multipart/form-data"
|
||||||
|
id="newPostForm"
|
||||||
|
hx-swap="none"
|
||||||
|
hx-post={ fmt.Sprintf("/%s/threads/%d",
|
||||||
|
board.Slug, thread.Id) }
|
||||||
|
_="on htmx:afterRequest
|
||||||
set #newPostBody.value to ''
|
set #newPostBody.value to ''
|
||||||
then trigger refreshPosts on body">
|
then trigger refreshPosts on body"
|
||||||
<table>
|
>
|
||||||
<tbody>
|
<table>
|
||||||
<tr class="new-post-form-field">
|
<tbody>
|
||||||
<th>comment</th>
|
<tr class="new-post-form-field">
|
||||||
<td><textarea id="newPostBody" name="body" required></textarea></td>
|
<th>comment</th>
|
||||||
</tr>
|
<td><textarea id="newPostBody" name="body" required></textarea></td>
|
||||||
</tbody>
|
</tr>
|
||||||
</table>
|
<tr class="new-post-form-field">
|
||||||
<button type="submit">Submit</button>
|
<th>File</th>
|
||||||
</form>
|
<td><input name="file" type="file"/></td>
|
||||||
</div>
|
</tr>
|
||||||
<div class="thread">
|
</tbody>
|
||||||
<article class="op-post">
|
</table>
|
||||||
if posts[0].MediaPath != "" {
|
<button type="submit">Submit</button>
|
||||||
<div class="post-img-container">
|
</form>
|
||||||
<img src={ fmt.Sprintf("/static/img/posts/%s", posts[0].MediaPath) } class="post-img" />
|
|
||||||
</div>
|
</div>
|
||||||
}
|
<div class="thread">
|
||||||
<header class="post-header">
|
<article class="op-post">
|
||||||
<h1 class="thread-subject">{ thread.Subject } -</h1>
|
if posts[0].MediaPath != "" {
|
||||||
<span class="post-author">{ posts[0].Author }</span>
|
<div class="post-img-container">
|
||||||
<span class="post-date">{ posts[0].CreatedAt.Format("01/02/2006") }</span>
|
<img src={ fmt.Sprintf("/static/img/posts/%s", posts[0].MediaPath) } class="post-img"/>
|
||||||
<span class="post-time">{ posts[0].CreatedAt.Format("15:04") }</span>
|
</div>
|
||||||
<span class="post-num">No. { strconv.Itoa(posts[0].Id) }</span>
|
}
|
||||||
</header>
|
<header class="post-header">
|
||||||
<p class="post-body">{ posts[0].Body }</p>
|
<h1 class="thread-subject">{ thread.Subject } -</h1>
|
||||||
</article>
|
<span class="post-author">{ posts[0].Author }</span>
|
||||||
<div hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", board.Slug, thread.Id) }
|
<span class="post-date">{ posts[0].CreatedAt.Format("01/02/2006") }</span>
|
||||||
hx-trigger="load, refreshPosts from:body"></div>
|
<span class="post-time">{ posts[0].CreatedAt.Format("15:04") }</span>
|
||||||
</div>
|
<span class="post-num">No. { strconv.Itoa(posts[0].Id) }</span>
|
||||||
}
|
</header>
|
||||||
|
<p class="post-body">{ posts[0].Body }</p>
|
||||||
|
</article>
|
||||||
|
<div
|
||||||
|
hx-get={ fmt.Sprintf("/hx/%s/threads/%d/posts", board.Slug, thread.Id) }
|
||||||
|
hx-trigger="load, refreshPosts from:body"
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-14
@@ -3,22 +3,22 @@ package views
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type CatalogThreadPreview struct {
|
type CatalogThreadPreview struct {
|
||||||
Subject string
|
Subject string
|
||||||
Body string
|
Body string
|
||||||
ThreadURL string
|
ThreadURL string
|
||||||
MediaPath string
|
MediaPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
templ ThreadsCatalog(previews []CatalogThreadPreview) {
|
templ ThreadsCatalog(previews []CatalogThreadPreview) {
|
||||||
<div id="catalog">
|
<div id="catalog">
|
||||||
for _, preview := range previews {
|
for _, preview := range previews {
|
||||||
<div class="catalog-preview">
|
<div class="catalog-preview">
|
||||||
<a href={ templ.SafeURL(preview.ThreadURL) }>
|
<a href={ templ.SafeURL(preview.ThreadURL) }>
|
||||||
<img class="catalog-preview-img" src={ fmt.Sprintf("/static/img/posts/%s", preview.MediaPath) } />
|
<img class="catalog-preview-img" src={ fmt.Sprintf("/static/img/posts/%s", preview.MediaPath) }/>
|
||||||
</a>
|
</a>
|
||||||
<h1><a href={ templ.SafeURL(preview.ThreadURL) } class="catalog-preview-link">{ preview.Subject }</a></h1>
|
<h1><a href={ templ.SafeURL(preview.ThreadURL) } class="catalog-preview-link">{ preview.Subject }</a></h1>
|
||||||
<p>{ preview.Body }</p>
|
<p>{ preview.Body }</p>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
}
|
|
||||||
</div>
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user