diff --git a/internal/util/posts.go b/internal/util/posts.go index 9a05c66..74a2c4d 100644 --- a/internal/util/posts.go +++ b/internal/util/posts.go @@ -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 { diff --git a/web/main.go b/web/main.go index 6382bb2..69805fb 100644 --- a/web/main.go +++ b/web/main.go @@ -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) diff --git a/web/views/board.templ b/web/views/board.templ index bb1d9ad..dae5aa0 100644 --- a/web/views/board.templ +++ b/web/views/board.templ @@ -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 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" >