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