2021-10-03 11:48:59 +03:00
|
|
|
package controllers
|
2021-08-26 20:23:55 +03:00
|
|
|
|
|
|
|
import (
|
2021-09-10 19:16:53 +03:00
|
|
|
"context"
|
2021-09-05 00:36:55 +03:00
|
|
|
"log"
|
2021-09-10 02:17:45 +03:00
|
|
|
"math"
|
2021-09-10 19:16:53 +03:00
|
|
|
"path/filepath"
|
2021-09-05 00:36:55 +03:00
|
|
|
"strconv"
|
2021-09-11 16:05:31 +03:00
|
|
|
"strings"
|
2021-08-26 20:23:55 +03:00
|
|
|
|
2021-10-03 11:48:59 +03:00
|
|
|
"github.com/dchest/captcha"
|
2022-04-09 15:16:09 +03:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-08-28 18:41:34 +03:00
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
"micrach/config"
|
|
|
|
"micrach/db"
|
|
|
|
"micrach/repositories"
|
|
|
|
"micrach/utils"
|
2021-08-26 20:23:55 +03:00
|
|
|
)
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
func GetThreads(c *fiber.Ctx) error {
|
|
|
|
pageString := c.Query("page", "1")
|
2021-09-10 02:17:45 +03:00
|
|
|
page, err := strconv.Atoi(pageString)
|
2021-08-30 12:09:27 +03:00
|
|
|
if err != nil {
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
2021-09-10 17:09:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if page <= 0 {
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
2021-08-30 12:09:27 +03:00
|
|
|
}
|
2021-09-10 17:09:15 +03:00
|
|
|
|
2021-09-10 02:17:45 +03:00
|
|
|
limit := 10
|
|
|
|
offset := limit * (page - 1)
|
2022-04-09 15:16:09 +03:00
|
|
|
threads, err := repositories.Posts.Get(limit, offset)
|
2021-09-10 02:17:45 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 02:17:45 +03:00
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
count, err := repositories.Posts.GetThreadsCount()
|
2021-09-10 02:17:45 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 02:17:45 +03:00
|
|
|
}
|
|
|
|
|
2021-09-10 17:09:15 +03:00
|
|
|
pagesCount := int(math.Ceil(float64(count) / 10))
|
2021-09-11 01:20:56 +03:00
|
|
|
if page > pagesCount && count != 0 {
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
2021-09-10 17:09:15 +03:00
|
|
|
}
|
2021-09-30 20:44:14 +03:00
|
|
|
|
2021-10-03 11:48:59 +03:00
|
|
|
captchaID := captcha.New()
|
2022-04-09 15:16:09 +03:00
|
|
|
htmlData := repositories.GetThreadsHtmlData{
|
2022-01-31 19:56:21 +03:00
|
|
|
Threads: threads,
|
2022-04-09 15:16:09 +03:00
|
|
|
Pagination: repositories.HtmlPaginationData{
|
2022-01-31 19:56:21 +03:00
|
|
|
PagesCount: pagesCount,
|
|
|
|
Page: page,
|
|
|
|
},
|
2022-04-09 15:16:09 +03:00
|
|
|
FormData: repositories.HtmlFormData{
|
2022-01-27 20:23:51 +03:00
|
|
|
CaptchaID: captchaID,
|
2022-04-09 15:16:09 +03:00
|
|
|
IsCaptchaActive: config.App.IsCaptchaActive,
|
2021-10-04 11:40:53 +03:00
|
|
|
},
|
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Render("pages/index", htmlData)
|
2021-08-26 20:23:55 +03:00
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
func GetThread(c *fiber.Ctx) error {
|
|
|
|
threadID, err := c.ParamsInt("threadID")
|
2021-09-05 00:36:55 +03:00
|
|
|
if err != nil {
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
2021-09-05 00:36:55 +03:00
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
thread, err := repositories.Posts.GetThreadByPostID(threadID)
|
2021-09-05 00:36:55 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-05 00:36:55 +03:00
|
|
|
}
|
2021-09-05 13:50:22 +03:00
|
|
|
if thread == nil {
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
2021-09-05 13:50:22 +03:00
|
|
|
}
|
2021-09-30 20:44:14 +03:00
|
|
|
|
2021-10-04 11:40:53 +03:00
|
|
|
firstPost := thread[0]
|
|
|
|
captchaID := captcha.New()
|
2022-04-09 15:16:09 +03:00
|
|
|
htmlData := repositories.GetThreadHtmlData{
|
2021-10-04 11:40:53 +03:00
|
|
|
Thread: thread,
|
2022-04-09 15:16:09 +03:00
|
|
|
FormData: repositories.HtmlFormData{
|
2022-01-27 20:23:51 +03:00
|
|
|
FirstPostID: firstPost.ID,
|
|
|
|
CaptchaID: captchaID,
|
2022-04-09 15:16:09 +03:00
|
|
|
IsCaptchaActive: config.App.IsCaptchaActive,
|
2021-10-04 11:40:53 +03:00
|
|
|
},
|
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Render("pages/thread", htmlData)
|
2021-08-26 20:23:55 +03:00
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
func CreateThread(c *fiber.Ctx) error {
|
2021-09-05 13:50:22 +03:00
|
|
|
form, err := c.MultipartForm()
|
|
|
|
if err != nil {
|
2021-09-10 19:16:53 +03:00
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-05 13:50:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: dat shit crashes if no fields in request
|
2021-09-10 19:16:53 +03:00
|
|
|
text := form.Value["text"][0]
|
|
|
|
title := form.Value["title"][0]
|
|
|
|
filesInRequest := form.File["files"]
|
2022-04-09 15:16:09 +03:00
|
|
|
validationErrorMessage := utils.ValidatePost(title, text, filesInRequest)
|
2021-10-11 11:08:55 +03:00
|
|
|
if validationErrorMessage != "" {
|
2022-04-09 15:16:09 +03:00
|
|
|
errorHtmlData := repositories.BadRequestHtmlData{
|
2021-10-11 11:08:55 +03:00
|
|
|
Message: validationErrorMessage,
|
2021-10-10 10:41:36 +03:00
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
2021-10-10 10:41:36 +03:00
|
|
|
}
|
2021-09-10 19:16:53 +03:00
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
if config.App.IsCaptchaActive {
|
2022-02-09 10:18:51 +03:00
|
|
|
captchaID := form.Value["captchaId"][0]
|
|
|
|
captchaString := form.Value["captcha"][0]
|
|
|
|
isCaptchaValid := captcha.VerifyString(captchaID, captchaString)
|
|
|
|
if !isCaptchaValid {
|
2022-04-09 15:16:09 +03:00
|
|
|
errorHtmlData := repositories.BadRequestHtmlData{
|
|
|
|
Message: repositories.InvalidCaptchaErrorMessage,
|
2022-02-09 10:18:51 +03:00
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
2021-11-17 11:44:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
conn, err := db.Pool.Acquire(context.TODO())
|
2021-09-10 19:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 19:16:53 +03:00
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
threadsCount, err := repositories.Posts.GetThreadsCount()
|
2021-11-20 20:13:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-11-20 20:13:05 +03:00
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
if threadsCount >= config.App.ThreadsMaxCount {
|
|
|
|
oldestThreadUpdatedAt, err := repositories.Posts.GetOldestThreadUpdatedAt()
|
2021-11-20 20:13:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-11-20 20:13:05 +03:00
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
err = repositories.Posts.ArchiveThreadsFrom(oldestThreadUpdatedAt)
|
2021-11-20 20:13:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-11-20 20:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 19:16:53 +03:00
|
|
|
tx, err := conn.Begin(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 19:16:53 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback(context.TODO())
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
post := repositories.Post{
|
2021-09-10 19:16:53 +03:00
|
|
|
IsParent: true,
|
|
|
|
Title: title,
|
|
|
|
Text: text,
|
|
|
|
IsSage: false,
|
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
threadID, err := repositories.Posts.CreateInTx(tx, post)
|
2021-09-10 19:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 19:16:53 +03:00
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
err = utils.CreateThreadFolder(threadID)
|
2021-09-10 19:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 19:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileInRequest := range filesInRequest {
|
2022-04-09 15:16:09 +03:00
|
|
|
file := repositories.File{
|
2021-09-12 14:06:52 +03:00
|
|
|
PostID: threadID,
|
2021-09-10 19:16:53 +03:00
|
|
|
Name: fileInRequest.Filename,
|
2021-09-11 16:05:31 +03:00
|
|
|
// image/jpeg -> jpeg
|
|
|
|
Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1],
|
|
|
|
Size: int(fileInRequest.Size),
|
2021-09-10 19:16:53 +03:00
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
fileID, err := repositories.Files.CreateInTx(tx, file)
|
2021-09-10 19:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 19:16:53 +03:00
|
|
|
}
|
|
|
|
|
2021-09-11 16:05:31 +03:00
|
|
|
path := filepath.Join(
|
2022-04-09 15:16:09 +03:00
|
|
|
utils.UPLOADS_DIR_PATH,
|
2021-09-12 14:06:52 +03:00
|
|
|
strconv.Itoa(threadID),
|
2021-09-12 11:31:43 +03:00
|
|
|
"o",
|
2021-09-11 16:05:31 +03:00
|
|
|
strconv.Itoa(fileID)+"."+file.Ext,
|
|
|
|
)
|
2022-04-09 15:16:09 +03:00
|
|
|
err = c.SaveFile(fileInRequest, path)
|
2021-09-10 19:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-10 19:16:53 +03:00
|
|
|
}
|
2021-09-12 14:06:52 +03:00
|
|
|
// creating thumbnail
|
2022-04-09 15:16:09 +03:00
|
|
|
thumbImg, err := utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
2021-09-12 14:06:52 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-12 14:06:52 +03:00
|
|
|
}
|
|
|
|
// saving thumbnail
|
2022-04-09 15:16:09 +03:00
|
|
|
err = utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
2021-09-12 14:06:52 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-12 14:06:52 +03:00
|
|
|
}
|
2021-09-10 19:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit(context.TODO())
|
|
|
|
|
2022-04-11 11:19:19 +03:00
|
|
|
path := "/" + strconv.Itoa(threadID)
|
|
|
|
return c.Redirect(path, fiber.StatusFound)
|
2021-08-26 20:23:55 +03:00
|
|
|
}
|
|
|
|
|
2022-01-23 20:32:02 +03:00
|
|
|
// Add new post in thread
|
2022-04-09 15:16:09 +03:00
|
|
|
func UpdateThread(c *fiber.Ctx) error {
|
|
|
|
threadID, err := c.ParamsInt("threadID")
|
2021-09-11 14:59:16 +03:00
|
|
|
if err != nil {
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusNotFound).Render("pages/404", nil)
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
isArchived, err := repositories.Posts.GetIfThreadIsArchived(threadID)
|
2022-01-23 20:32:02 +03:00
|
|
|
if isArchived {
|
2022-04-09 15:16:09 +03:00
|
|
|
errorHtmlData := repositories.BadRequestHtmlData{
|
|
|
|
Message: repositories.ThreadIsArchivedErrorMessage,
|
2022-01-23 20:32:02 +03:00
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
2022-01-23 20:32:02 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2022-01-23 20:32:02 +03:00
|
|
|
}
|
|
|
|
|
2021-09-11 14:59:16 +03:00
|
|
|
form, err := c.MultipartForm()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: dat shit crashes if no fields in request
|
|
|
|
text := form.Value["text"][0]
|
|
|
|
filesInRequest := form.File["files"]
|
2022-04-12 11:39:02 +03:00
|
|
|
|
|
|
|
validationErrors := utils.ValidatePost2("", text, filesInRequest)
|
|
|
|
if validationErrors != nil {
|
|
|
|
thread, err := repositories.Posts.GetThreadByPostID(threadID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
2021-10-10 10:41:36 +03:00
|
|
|
}
|
2022-04-12 11:39:02 +03:00
|
|
|
|
|
|
|
firstPost := thread[0]
|
|
|
|
captchaID := form.Value["captchaId"][0]
|
|
|
|
htmlData := repositories.GetThreadHtmlData{
|
|
|
|
Thread: thread,
|
|
|
|
FormData: repositories.HtmlFormData{
|
|
|
|
FirstPostID: firstPost.ID,
|
|
|
|
CaptchaID: captchaID,
|
|
|
|
IsCaptchaActive: config.App.IsCaptchaActive,
|
|
|
|
Errors: *validationErrors,
|
2022-04-13 09:38:15 +03:00
|
|
|
Inputs: repositories.Inputs{
|
|
|
|
Text: text,
|
|
|
|
Files: filesInRequest,
|
|
|
|
},
|
2022-04-12 11:39:02 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Render("pages/thread", htmlData)
|
2021-10-10 10:41:36 +03:00
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
if config.App.IsCaptchaActive {
|
2022-02-09 10:18:51 +03:00
|
|
|
captchaID := form.Value["captchaId"][0]
|
|
|
|
captchaString := form.Value["captcha"][0]
|
|
|
|
isCaptchaValid := captcha.VerifyString(captchaID, captchaString)
|
|
|
|
if !isCaptchaValid {
|
2022-04-09 15:16:09 +03:00
|
|
|
errorHtmlData := repositories.BadRequestHtmlData{
|
|
|
|
Message: repositories.InvalidCaptchaErrorMessage,
|
2022-02-09 10:18:51 +03:00
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData)
|
2021-11-17 11:44:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 14:59:16 +03:00
|
|
|
isSageField := form.Value["sage"]
|
|
|
|
var isSageString string
|
|
|
|
if len(isSageField) != 0 {
|
|
|
|
isSageString = isSageField[0]
|
|
|
|
}
|
2021-10-07 10:51:18 +03:00
|
|
|
isSage := isSageString == "on"
|
2021-09-11 14:59:16 +03:00
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
conn, err := db.Pool.Acquire(context.TODO())
|
2021-09-11 14:59:16 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
|
|
|
tx, err := conn.Begin(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback(context.TODO())
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
post := repositories.Post{
|
2021-09-11 14:59:16 +03:00
|
|
|
IsParent: false,
|
2022-02-03 18:22:46 +03:00
|
|
|
ParentID: &threadID,
|
2021-09-12 14:06:52 +03:00
|
|
|
Title: "",
|
2021-09-11 14:59:16 +03:00
|
|
|
Text: text,
|
|
|
|
IsSage: isSage,
|
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
postID, err := repositories.Posts.CreateInTx(tx, post)
|
2021-09-11 14:59:16 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
postsCountInThread, err := repositories.Posts.GetThreadPostsCount(threadID)
|
2022-01-27 20:23:51 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
|
2022-01-27 20:23:51 +03:00
|
|
|
}
|
2022-04-09 15:16:09 +03:00
|
|
|
isBumpLimit := postsCountInThread >= config.App.ThreadBumpLimit
|
2022-01-27 20:23:51 +03:00
|
|
|
isThreadBumped := !isBumpLimit && !isSage && !post.IsParent
|
|
|
|
if isThreadBumped {
|
2022-04-09 15:16:09 +03:00
|
|
|
err = repositories.Posts.BumpThreadInTx(tx, threadID)
|
2022-01-27 20:23:51 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
|
2022-01-27 20:23:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 14:59:16 +03:00
|
|
|
for _, fileInRequest := range filesInRequest {
|
2022-04-09 15:16:09 +03:00
|
|
|
file := repositories.File{
|
2021-09-11 14:59:16 +03:00
|
|
|
PostID: postID,
|
|
|
|
Name: fileInRequest.Filename,
|
2021-09-11 16:05:31 +03:00
|
|
|
// image/jpeg -> jpeg
|
|
|
|
Ext: strings.Split(fileInRequest.Header["Content-Type"][0], "/")[1],
|
|
|
|
Size: int(fileInRequest.Size),
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
2022-04-09 15:16:09 +03:00
|
|
|
fileID, err := repositories.Files.CreateInTx(tx, file)
|
2021-09-11 14:59:16 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
2021-09-11 16:05:31 +03:00
|
|
|
path := filepath.Join(
|
2022-04-09 15:16:09 +03:00
|
|
|
utils.UPLOADS_DIR_PATH,
|
2021-09-11 16:05:31 +03:00
|
|
|
strconv.Itoa(threadID),
|
2021-09-12 11:31:43 +03:00
|
|
|
"o",
|
2021-09-11 16:05:31 +03:00
|
|
|
strconv.Itoa(fileID)+"."+file.Ext,
|
|
|
|
)
|
2022-04-09 15:16:09 +03:00
|
|
|
err = c.SaveFile(fileInRequest, path)
|
2021-09-11 14:59:16 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
2021-09-12 14:06:52 +03:00
|
|
|
// creating thumbnail
|
2022-04-09 15:16:09 +03:00
|
|
|
thumbImg, err := utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
2021-09-12 14:06:52 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
|
2021-09-12 14:06:52 +03:00
|
|
|
}
|
|
|
|
// saving thumbnail
|
2022-04-09 15:16:09 +03:00
|
|
|
err = utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
2021-09-12 14:06:52 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2022-04-09 15:16:09 +03:00
|
|
|
return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil)
|
|
|
|
|
2021-09-12 14:06:52 +03:00
|
|
|
}
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit(context.TODO())
|
|
|
|
|
2022-04-11 11:19:19 +03:00
|
|
|
path := "/" + strconv.Itoa(threadID) + "#" + strconv.Itoa(postID)
|
|
|
|
return c.Redirect(path)
|
2021-08-26 20:23:55 +03:00
|
|
|
}
|