Basic file upload for thread creation
This commit is contained in:
@@ -89,7 +89,7 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func PutThread(db *sql.DB, boardSlug string, subject string, body string) error {
|
||||
func PutThread(db *sql.DB, boardSlug string, subject string, body string, mediaPath string) error {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -106,7 +106,7 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string) error
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.Exec(`INSERT INTO posts (thread_id, body, media_path) VALUES (?, ?, ?)`, threadId, body, "default.png")
|
||||
_, err = tx.Exec(`INSERT INTO posts (thread_id, body, media_path) VALUES (?, ?, ?)`, threadId, body, mediaPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+34
-11
@@ -3,14 +3,18 @@ package main
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/dominicf2001/comfychan/internal/database"
|
||||
"github.com/dominicf2001/comfychan/web/views"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var dev = true
|
||||
@@ -115,16 +119,41 @@ func main() {
|
||||
r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) {
|
||||
slug := chi.URLParam(r, "slug")
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
// 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)
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to retrieve file from form", http.StatusBadRequest)
|
||||
log.Printf("FormFile: %v", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
subject := r.FormValue("subject")
|
||||
body := r.FormValue("body")
|
||||
|
||||
if err := database.PutThread(db, slug, subject, body); err != nil {
|
||||
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)
|
||||
log.Printf("io.Copy: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := database.PutThread(db, slug, subject, body, header.Filename); err != nil {
|
||||
http.Error(w, "Failed to create thread", http.StatusInternalServerError)
|
||||
log.Printf("PutThread: %v", err)
|
||||
return
|
||||
@@ -142,12 +171,6 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, "Bad form data", http.StatusBadRequest)
|
||||
log.Printf("ParseForm: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
body := r.FormValue("body")
|
||||
|
||||
if err := database.PutPost(db, threadId, body); err != nil {
|
||||
|
||||
@@ -7,7 +7,8 @@ database "github.com/dominicf2001/comfychan/internal/database"
|
||||
)
|
||||
|
||||
templ NewThreadForm(board database.Board) {
|
||||
<form 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">
|
||||
@@ -21,6 +22,10 @@ templ NewThreadForm(board database.Board) {
|
||||
<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>
|
||||
|
||||
@@ -13,7 +13,9 @@ 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>
|
||||
|
||||
Reference in New Issue
Block a user