Implement cooldowns
This commit is contained in:
+17
-61
@@ -4,78 +4,22 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/dominicf2001/comfychan/internal/database"
|
||||
"github.com/dominicf2001/comfychan/internal/util"
|
||||
"github.com/dominicf2001/comfychan/web/views"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// 10 MB memory limit
|
||||
const FILE_MEM_LIMIT int64 = 10 << 20
|
||||
const POST_IMG_FULL_PATH = "web/static/img/posts/full"
|
||||
const POST_IMG_THUMB_PATH = "web/static/img/posts/thumb"
|
||||
|
||||
var dev = true
|
||||
|
||||
func savePostFile(file *multipart.File, filename string) error {
|
||||
dstPathFull := filepath.Join(POST_IMG_FULL_PATH, filename)
|
||||
dstPathThumb := filepath.Join(POST_IMG_THUMB_PATH, filename)
|
||||
|
||||
// FULL
|
||||
if err := os.MkdirAll(POST_IMG_FULL_PATH, 0755); err != nil {
|
||||
log.Printf("MkdirAll (full): %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
dstFull, err := os.Create(dstPathFull)
|
||||
if err != nil {
|
||||
log.Printf("os.Create (full): %v", err)
|
||||
return err
|
||||
}
|
||||
defer dstFull.Close()
|
||||
if _, err := io.Copy(dstFull, *file); err != nil {
|
||||
log.Printf("io.Copy (full): %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := (*file).Seek(0, 0); err != nil { // rewind file
|
||||
log.Printf("seek file (thumb): %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// THUMBNAIL
|
||||
if err := os.MkdirAll(POST_IMG_THUMB_PATH, 0755); err != nil {
|
||||
log.Printf("MkdirAll (thumb): %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
img, _, err := image.Decode(*file)
|
||||
if err != nil {
|
||||
log.Printf("image.Decode: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
thumb := imaging.Resize(img, 300, 0, imaging.Lanczos)
|
||||
if err = imaging.Save(thumb, dstPathThumb); err != nil {
|
||||
log.Printf("imaging.Save: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func disableCacheInDevMode(next http.Handler) http.Handler {
|
||||
if !dev {
|
||||
return next
|
||||
@@ -174,12 +118,18 @@ func main() {
|
||||
|
||||
// CREATE THREAD
|
||||
r.Post("/{slug}/threads", func(w http.ResponseWriter, r *http.Request) {
|
||||
ip := util.GetIP(r)
|
||||
if util.IsOnCooldown(ip, util.ThreadCooldowns, util.THREAD_COOLDOWN) {
|
||||
http.Error(w, "Too many posts at once.", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
|
||||
slug := chi.URLParam(r, "slug")
|
||||
|
||||
subject := r.FormValue("subject")
|
||||
body := r.FormValue("body")
|
||||
|
||||
if err := r.ParseMultipartForm(FILE_MEM_LIMIT); err != nil {
|
||||
if err := r.ParseMultipartForm(util.FILE_MEM_LIMIT); err != nil {
|
||||
http.Error(w, "Failed to parse form", http.StatusBadRequest)
|
||||
log.Printf("ParseMultipartForm: %v", err)
|
||||
return
|
||||
@@ -194,7 +144,7 @@ func main() {
|
||||
defer file.Close()
|
||||
|
||||
filename := strconv.FormatInt(time.Now().UnixNano(), 10) + filepath.Ext(header.Filename)
|
||||
if err := savePostFile(&file, filename); err != nil {
|
||||
if err := util.SavePostFile(&file, filename); err != nil {
|
||||
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
||||
log.Printf("savePostFile: %v", err)
|
||||
return
|
||||
@@ -209,6 +159,12 @@ func main() {
|
||||
|
||||
// CREATE POST
|
||||
r.Post("/{slug}/threads/{threadId}", func(w http.ResponseWriter, r *http.Request) {
|
||||
ip := util.GetIP(r)
|
||||
if util.IsOnCooldown(ip, util.PostCooldowns, util.POST_COOLDOWN) {
|
||||
http.Error(w, "Too many posts at once.", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
|
||||
// slug := chi.URLParam(r, "slug")
|
||||
// TODO: use relative thread_nums (see issue #1)
|
||||
threadIdStr := chi.URLParam(r, "threadId")
|
||||
@@ -221,7 +177,7 @@ func main() {
|
||||
body := r.FormValue("body")
|
||||
mediaPath := ""
|
||||
|
||||
if err := r.ParseMultipartForm(FILE_MEM_LIMIT); err != nil {
|
||||
if err := r.ParseMultipartForm(util.FILE_MEM_LIMIT); err != nil {
|
||||
http.Error(w, "Failed to parse form", http.StatusBadRequest)
|
||||
log.Printf("ParseMultipartForm: %v", err)
|
||||
return
|
||||
@@ -237,7 +193,7 @@ func main() {
|
||||
} else {
|
||||
defer file.Close()
|
||||
filename := strconv.FormatInt(time.Now().UnixNano(), 10) + filepath.Ext(header.Filename)
|
||||
if err := savePostFile(&file, filename); err != nil {
|
||||
if err := util.SavePostFile(&file, filename); err != nil {
|
||||
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
||||
log.Printf("savePostFile: %v", err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user