Basic file upload for thread creation

This commit is contained in:
Dominic Ferrando
2025-04-18 15:03:54 -04:00
parent 4265b8583b
commit 00c8057b5b
4 changed files with 45 additions and 15 deletions
+2 -2
View File
@@ -89,7 +89,7 @@ func GetPosts(db *sql.DB, threadId int) ([]Post, error) {
return result, rows.Err() 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() tx, err := db.Begin()
if err != nil { if err != nil {
return err return err
@@ -106,7 +106,7 @@ func PutThread(db *sql.DB, boardSlug string, subject string, body string) error
return err 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 { if err != nil {
return err return err
} }
+34 -11
View File
@@ -3,14 +3,18 @@ package main
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"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"
"github.com/go-chi/chi/v5/middleware" "github.com/go-chi/chi/v5/middleware"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"log"
"net/http"
"strconv"
) )
var dev = true var dev = true
@@ -115,16 +119,41 @@ 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")
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) http.Error(w, "Bad form data", http.StatusBadRequest)
log.Printf("ParseForm: %v", err) log.Printf("ParseForm: %v", err)
return 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") subject := r.FormValue("subject")
body := r.FormValue("body") 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) http.Error(w, "Failed to create thread", http.StatusInternalServerError)
log.Printf("PutThread: %v", err) log.Printf("PutThread: %v", err)
return return
@@ -142,12 +171,6 @@ func main() {
return 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") body := r.FormValue("body")
if err := database.PutPost(db, threadId, body); err != nil { if err := database.PutPost(db, threadId, body); err != nil {
+6 -1
View File
@@ -7,7 +7,8 @@ database "github.com/dominicf2001/comfychan/internal/database"
) )
templ NewThreadForm(board database.Board) { 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 hide me
then show #newThreadBtn then show #newThreadBtn
then trigger refreshThreads on body"> then trigger refreshThreads on body">
@@ -21,6 +22,10 @@ templ NewThreadForm(board database.Board) {
<th>Comment</th> <th>Comment</th>
<td><textarea name="body" required></textarea></td> <td><textarea name="body" required></textarea></td>
</tr> </tr>
<tr class="new-post-form-field">
<th>File</th>
<td><input name="file" type="file" required /></td>
</tr>
</tbody> </tbody>
</table> </table>
<button type="submit">Submit</button> <button type="submit">Submit</button>
+2
View File
@@ -13,7 +13,9 @@ 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) }>
<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>
<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>