2021-08-26 20:23:55 +03:00
|
|
|
package controlers
|
|
|
|
|
|
|
|
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-08-26 20:23:55 +03:00
|
|
|
"net/http"
|
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
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-09-30 20:44:14 +03:00
|
|
|
csrf "github.com/utrack/gin-csrf"
|
2021-08-28 18:41:34 +03:00
|
|
|
|
2021-09-10 19:16:53 +03:00
|
|
|
Db "micrach/db"
|
2021-08-28 18:41:34 +03:00
|
|
|
Repositories "micrach/repositories"
|
2021-09-10 19:16:53 +03:00
|
|
|
Utils "micrach/utils"
|
2021-08-26 20:23:55 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetThreads(c *gin.Context) {
|
2021-09-10 15:06:37 +03:00
|
|
|
pageString := c.DefaultQuery("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 {
|
2021-09-10 17:09:15 +03:00
|
|
|
c.HTML(http.StatusNotFound, "404.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if page <= 0 {
|
|
|
|
c.HTML(http.StatusNotFound, "404.html", nil)
|
2021-08-30 12:09:27 +03:00
|
|
|
return
|
|
|
|
}
|
2021-09-10 17:09:15 +03:00
|
|
|
|
2021-09-10 02:17:45 +03:00
|
|
|
limit := 10
|
|
|
|
offset := limit * (page - 1)
|
|
|
|
threads, err := Repositories.Posts.Get(limit, offset)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2021-09-10 17:09:15 +03:00
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
2021-09-10 02:17:45 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
count, err := Repositories.Posts.GetCount()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2021-09-10 17:09:15 +03:00
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
2021-09-10 02:17:45 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-09-10 17:09:15 +03:00
|
|
|
c.HTML(http.StatusNotFound, "404.html", nil)
|
|
|
|
return
|
|
|
|
}
|
2021-09-10 02:17:45 +03:00
|
|
|
data := Repositories.IndexPageData{
|
|
|
|
Threads: threads,
|
2021-09-10 17:09:15 +03:00
|
|
|
PagesCount: pagesCount,
|
2021-09-10 02:17:45 +03:00
|
|
|
Page: page,
|
|
|
|
}
|
2021-09-30 20:44:14 +03:00
|
|
|
|
|
|
|
csrfToken := csrf.GetToken(c)
|
|
|
|
c.SetCookie("csrf", csrfToken, 60, "/", "", true, true)
|
2021-09-10 02:17:45 +03:00
|
|
|
c.HTML(http.StatusOK, "index.html", data)
|
2021-08-26 20:23:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetThread(c *gin.Context) {
|
2021-09-05 00:36:55 +03:00
|
|
|
threadIDString := c.Param("threadID")
|
|
|
|
threadID, err := strconv.Atoi(threadIDString)
|
|
|
|
if err != nil {
|
2021-09-10 17:09:15 +03:00
|
|
|
c.HTML(http.StatusNotFound, "404.html", nil)
|
2021-09-05 00:36:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
thread, err := Repositories.Posts.GetThreadByPostID(threadID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
2021-09-10 17:09:15 +03:00
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
2021-09-05 00:36:55 +03:00
|
|
|
return
|
|
|
|
}
|
2021-09-05 13:50:22 +03:00
|
|
|
if thread == nil {
|
2021-09-10 17:09:15 +03:00
|
|
|
c.HTML(http.StatusNotFound, "404.html", nil)
|
2021-09-05 13:50:22 +03:00
|
|
|
return
|
|
|
|
}
|
2021-09-30 20:44:14 +03:00
|
|
|
|
|
|
|
csrfToken := csrf.GetToken(c)
|
|
|
|
c.SetCookie("csrf", csrfToken, 60, "/", "", true, true)
|
2021-09-05 00:36:55 +03:00
|
|
|
c.HTML(http.StatusOK, "thread.html", thread)
|
2021-08-26 20:23:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateThread(c *gin.Context) {
|
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)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
2021-09-05 13:50:22 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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]
|
2021-09-11 16:56:22 +03:00
|
|
|
isPostValid := Utils.ValidatePost(title, text)
|
|
|
|
if !isPostValid {
|
|
|
|
c.HTML(http.StatusBadRequest, "400.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-10 19:16:53 +03:00
|
|
|
filesInRequest := form.File["files"]
|
|
|
|
|
|
|
|
conn, err := Db.Pool.Acquire(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
|
|
|
tx, err := conn.Begin(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer tx.Rollback(context.TODO())
|
|
|
|
|
|
|
|
post := Repositories.Post{
|
|
|
|
IsParent: true,
|
|
|
|
Title: title,
|
|
|
|
Text: text,
|
|
|
|
IsSage: false,
|
|
|
|
}
|
2021-09-12 14:06:52 +03:00
|
|
|
threadID, err := Repositories.Posts.CreateInTx(tx, post)
|
2021-09-10 19:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-12 14:06:52 +03:00
|
|
|
err = Utils.CreateThreadFolder(threadID)
|
2021-09-10 19:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileInRequest := range filesInRequest {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-11 16:05:31 +03:00
|
|
|
fileID, err := Repositories.Files.CreateInTx(tx, file)
|
2021-09-10 19:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-11 16:05:31 +03:00
|
|
|
path := filepath.Join(
|
|
|
|
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,
|
|
|
|
)
|
2021-09-10 19:16:53 +03:00
|
|
|
err = c.SaveUploadedFile(fileInRequest, path)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
2021-09-12 14:06:52 +03:00
|
|
|
// creating thumbnail
|
|
|
|
thumbImg, err := Utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// saving thumbnail
|
|
|
|
err = Utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
2021-09-10 19:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit(context.TODO())
|
|
|
|
|
2021-09-12 14:06:52 +03:00
|
|
|
c.Redirect(http.StatusFound, "/"+strconv.Itoa(threadID))
|
2021-08-26 20:23:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateThread(c *gin.Context) {
|
2021-09-11 14:59:16 +03:00
|
|
|
threadIDString := c.Param("threadID")
|
|
|
|
threadID, err := strconv.Atoi(threadIDString)
|
|
|
|
if err != nil {
|
|
|
|
c.HTML(http.StatusNotFound, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
form, err := c.MultipartForm()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: dat shit crashes if no fields in request
|
|
|
|
text := form.Value["text"][0]
|
2021-09-12 14:06:52 +03:00
|
|
|
// title := form.Value["title"][0]
|
|
|
|
isPostValid := Utils.ValidatePost("", text)
|
2021-09-11 16:56:22 +03:00
|
|
|
if !isPostValid {
|
|
|
|
c.HTML(http.StatusBadRequest, "400.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-11 14:59:16 +03:00
|
|
|
filesInRequest := form.File["files"]
|
|
|
|
isSageField := form.Value["sage"]
|
|
|
|
var isSageString string
|
|
|
|
if len(isSageField) != 0 {
|
|
|
|
isSageString = isSageField[0]
|
|
|
|
}
|
|
|
|
isSage := isSageString == "true"
|
|
|
|
|
|
|
|
conn, err := Db.Pool.Acquire(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
|
|
|
tx, err := conn.Begin(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer tx.Rollback(context.TODO())
|
|
|
|
|
|
|
|
post := Repositories.Post{
|
|
|
|
IsParent: false,
|
|
|
|
ParentID: threadID,
|
2021-09-12 14:06:52 +03:00
|
|
|
Title: "",
|
2021-09-11 14:59:16 +03:00
|
|
|
Text: text,
|
|
|
|
IsSage: isSage,
|
|
|
|
}
|
|
|
|
postID, err := Repositories.Posts.CreateInTx(tx, post)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fileInRequest := range filesInRequest {
|
|
|
|
file := Repositories.File{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-11 16:05:31 +03:00
|
|
|
fileID, err := Repositories.Files.CreateInTx(tx, file)
|
2021-09-11 14:59:16 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-11 16:05:31 +03:00
|
|
|
path := filepath.Join(
|
|
|
|
Utils.UPLOADS_DIR_PATH,
|
|
|
|
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,
|
|
|
|
)
|
2021-09-11 14:59:16 +03:00
|
|
|
err = c.SaveUploadedFile(fileInRequest, path)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
2021-09-12 14:06:52 +03:00
|
|
|
// creating thumbnail
|
|
|
|
thumbImg, err := Utils.MakeImageThumbnail(path, file.Ext, threadID, fileID)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// saving thumbnail
|
|
|
|
err = Utils.SaveImageThumbnail(thumbImg, threadID, fileID, file.Ext)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error:", err)
|
|
|
|
c.HTML(http.StatusInternalServerError, "500.html", nil)
|
|
|
|
return
|
|
|
|
}
|
2021-09-11 14:59:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit(context.TODO())
|
|
|
|
|
|
|
|
c.Redirect(http.StatusFound, "/"+strconv.Itoa(threadID))
|
2021-08-26 20:23:55 +03:00
|
|
|
}
|