Implement file upload for replies

This commit is contained in:
Dominic Ferrando
2025-04-18 15:41:29 -04:00
parent 00c8057b5b
commit a80a7a4dba
6 changed files with 188 additions and 130 deletions
+57 -20
View File
@@ -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