Allow posting image without body

This commit is contained in:
Dominic Ferrando
2025-04-25 22:38:20 -04:00
parent ccee57caff
commit e3e8f9075f
2 changed files with 17 additions and 6 deletions
+8 -5
View File
@@ -369,22 +369,20 @@ func main() {
mediaPath := "" mediaPath := ""
thumbPath := "" thumbPath := ""
if body == "" {
http.Error(w, "Body is empty", http.StatusBadRequest)
return
}
if len(body) > util.MAX_BODY_LEN { if len(body) > util.MAX_BODY_LEN {
http.Error(w, fmt.Sprintf("Body exceeds %d characters", util.MAX_BODY_LEN), http.StatusBadRequest) http.Error(w, fmt.Sprintf("Body exceeds %d characters", util.MAX_BODY_LEN), http.StatusBadRequest)
return return
} }
fileIsEmpty := false
file, header, err := r.FormFile("file") file, header, err := r.FormFile("file")
if err != nil { if err != nil {
if !errors.Is(err, http.ErrMissingFile) { if !errors.Is(err, http.ErrMissingFile) {
http.Error(w, "Failed to retrieve file from form", http.StatusBadRequest) http.Error(w, "Failed to retrieve file from form", http.StatusBadRequest)
log.Printf("FormFile: %v", err) log.Printf("FormFile: %v", err)
return return
} else {
fileIsEmpty = true
} }
} else { } else {
defer file.Close() defer file.Close()
@@ -417,6 +415,11 @@ func main() {
thumbPath = savedThumbPath thumbPath = savedThumbPath
} }
if body == "" && fileIsEmpty {
http.Error(w, "No body or file provided", http.StatusBadRequest)
return
}
if err := database.PutPost(db, slug, threadId, body, mediaPath, thumbPath, ipHash); err != nil { if err := database.PutPost(db, slug, threadId, body, mediaPath, thumbPath, ipHash); 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)
+9 -1
View File
@@ -40,7 +40,15 @@ templ NewPostForm(board database.Board, endpoint string, isForThread bool) {
</tr> </tr>
<tr class="new-post-form-field"> <tr class="new-post-form-field">
<th>Comment</th> <th>Comment</th>
<td><textarea id="newPostBody" name="body" required></textarea></td> <td>
<textarea
id="newPostBody"
name="body"
if isForThread {
required
}
></textarea>
</td>
</tr> </tr>
<tr class="new-post-form-field"> <tr class="new-post-form-field">
<th>File</th> <th>File</th>