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