Minor cleanup and bugfi

This commit is contained in:
Dominic Ferrando
2025-04-22 17:28:02 -04:00
parent 0a2fd9951e
commit 024280e96d
4 changed files with 59 additions and 35 deletions
+19 -12
View File
@@ -62,14 +62,27 @@ func EnrichPost(body string) string {
return b.String()
}
func SavePostFile(file multipart.File, fileName string) (error, string, string) {
// read file type
func IsFileVideo(file multipart.File) (bool, error) {
buffer := make([]byte, 512)
file.Read(buffer)
file.Seek(0, 0)
_, err := file.Read(buffer)
if err != nil {
return false, err
}
_, err = file.Seek(0, 0)
if err != nil {
return false, err
}
fileType := http.DetectContentType(buffer)
isFileVideo := strings.HasPrefix(fileType, "video/")
return strings.HasPrefix(fileType, "video/"), nil
}
func SavePostFile(file multipart.File, fileName string) (error, string, string) {
isFileVideo, err := IsFileVideo(file)
if err != nil {
return err, "", ""
}
fileExt := strings.ToLower(filepath.Ext(fileName))
// FULL
@@ -175,13 +188,7 @@ func GetPostFileInfo(mediaPath string) PostFileInfo {
defer file.Close()
// read file type
buffer := make([]byte, 512)
file.Read(buffer)
file.Seek(0, 0)
fileType := http.DetectContentType(buffer)
isFileVideo := strings.HasPrefix(fileType, "video/")
isFileVideo, _ := IsFileVideo(file)
result.Size = fileInfo.Size()
if !isFileVideo {
+22 -9
View File
@@ -4,6 +4,7 @@ import (
"database/sql"
"errors"
"fmt"
"io"
"log"
"net/http"
"path/filepath"
@@ -133,7 +134,7 @@ func main() {
timeRemaining := util.IsOnCooldown(ip, util.ThreadCooldowns, util.THREAD_COOLDOWN)
if timeRemaining > 0 {
response := fmt.Sprintf("Please wait %.0f seconds.", timeRemaining.Seconds())
io.Copy(io.Discard, r.Body)
http.Error(w, response, http.StatusTooManyRequests)
return
}
@@ -155,6 +156,19 @@ func main() {
}
defer file.Close()
isFileVideo, err := util.IsFileVideo(file)
if err != nil {
http.Error(w, "Failed to detect if file is a video", http.StatusInternalServerError)
return
}
fileExt := strings.ToLower(filepath.Ext(header.Filename))
if isFileVideo && !slices.Contains(util.SUPPORTED_VID_FORMATS, fileExt) {
http.Error(w, "Unsupported file format", http.StatusBadRequest)
return
}
filename := strconv.FormatInt(time.Now().UnixNano(), 10) + filepath.Ext(header.Filename)
err, savedMediaPath, savedThumbPath := util.SavePostFile(file, filename)
if err != nil {
@@ -178,6 +192,7 @@ func main() {
timeRemaining := util.IsOnCooldown(ip, util.PostCooldowns, util.POST_COOLDOWN)
if timeRemaining > 0 {
response := fmt.Sprintf("Please wait %.0f seconds.", timeRemaining.Seconds())
io.Copy(io.Discard, r.Body)
http.Error(w, response, http.StatusTooManyRequests)
return
}
@@ -207,16 +222,14 @@ func main() {
} else {
defer file.Close()
isFileVideo := false
fileExt := strings.ToLower(filepath.Ext(header.Filename))
// file type
buffer := make([]byte, 512)
file.Read(buffer)
file.Seek(0, 0)
fileType := http.DetectContentType(buffer)
isFileVideo, err := util.IsFileVideo(file)
if err != nil {
http.Error(w, "Failed to detect if file is a video", http.StatusInternalServerError)
return
}
isFileVideo = strings.HasPrefix(fileType, "video/")
fileExt := strings.ToLower(filepath.Ext(header.Filename))
if isFileVideo && !slices.Contains(util.SUPPORTED_VID_FORMATS, fileExt) {
http.Error(w, "Unsupported file format", http.StatusBadRequest)
+15 -13
View File
@@ -16,19 +16,21 @@ templ NewThreadForm(board database.Board) {
id="newThreadForm"
hx-swap="none"
hx-post={ fmt.Sprintf("/%s/threads", board.Slug) }
_="on htmx:afterRequest
if event.detail.xhr.status is not 429
hide me
then show #newThreadBtn
then set #newPostBody.value to ''
then set #newPostFile.value to ''
then set #newPostSubject.value to ''
then hide #newPostWarning
then trigger refreshThreads on body
else
show #newPostWarning
then put event.detail.xhr.responseText into #newPostWarning
end"
_="
on htmx:beforeSend toggle @disabled on <button/> until htmx:afterRequest
on htmx:afterRequest
if event.detail.xhr.status is not 429
hide me
then show #newThreadBtn
then set #newPostBody.value to ''
then set #newPostFile.value to ''
then set #newPostSubject.value to ''
then hide #newPostWarning
then trigger refreshThreads on body
else
show #newPostWarning
then put event.detail.xhr.responseText into #newPostWarning
end"
>
<table>
<tbody>
+3 -1
View File
@@ -123,7 +123,9 @@ templ Thread(board database.Board, thread database.Thread, posts []database.Post
id="newPostForm"
hx-target="#newPostWarning"
hx-post={ fmt.Sprintf("/%s/threads/%d", board.Slug, thread.Id) }
_="on htmx:afterRequest
_="
on htmx:beforeSend toggle @disabled on <button/> until htmx:afterRequest
on htmx:afterRequest
if event.detail.xhr.status is not 429
set #newPostBody.value to ''
then set #newPostFile.value to ''