2022-10-20 16:59:33 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"GoWeb/app"
|
2023-02-09 01:39:53 +00:00
|
|
|
"GoWeb/models"
|
2022-11-14 18:29:55 +00:00
|
|
|
"GoWeb/security"
|
2023-05-04 13:26:44 +00:00
|
|
|
"io/ioutil"
|
2022-10-20 16:59:33 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-05-04 13:26:44 +00:00
|
|
|
"os"
|
2022-10-20 16:59:33 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PostController is a wrapper struct for the App struct
|
|
|
|
type PostController struct {
|
|
|
|
App *app.App
|
|
|
|
}
|
|
|
|
|
2022-11-01 16:23:08 +00:00
|
|
|
func (postController *PostController) Login(w http.ResponseWriter, r *http.Request) {
|
2022-11-14 18:29:55 +00:00
|
|
|
// Validate csrf token
|
|
|
|
_, err := security.VerifyCsrfToken(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error verifying csrf token")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-20 16:59:33 +00:00
|
|
|
username := r.FormValue("username")
|
|
|
|
password := r.FormValue("password")
|
2023-04-06 13:56:48 +00:00
|
|
|
remember := r.FormValue("remember") == "on"
|
2022-10-20 16:59:33 +00:00
|
|
|
|
|
|
|
if username == "" || password == "" {
|
2022-11-01 21:14:49 +00:00
|
|
|
log.Println("Tried to login user with empty username or password")
|
|
|
|
http.Redirect(w, r, "/login", http.StatusFound)
|
2022-10-20 16:59:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 13:56:48 +00:00
|
|
|
_, err = models.AuthenticateUser(postController.App, w, username, password, remember)
|
2022-11-01 21:14:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error authenticating user")
|
|
|
|
log.Println(err)
|
|
|
|
http.Redirect(w, r, "/login", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2022-10-20 16:59:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 16:23:08 +00:00
|
|
|
func (postController *PostController) Register(w http.ResponseWriter, r *http.Request) {
|
2022-11-14 18:29:55 +00:00
|
|
|
// Validate csrf token
|
|
|
|
_, err := security.VerifyCsrfToken(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error verifying csrf token")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-20 16:59:33 +00:00
|
|
|
username := r.FormValue("username")
|
|
|
|
password := r.FormValue("password")
|
2022-11-01 16:23:08 +00:00
|
|
|
createdAt := time.Now()
|
|
|
|
updatedAt := time.Now()
|
2022-10-20 16:59:33 +00:00
|
|
|
|
|
|
|
if username == "" || password == "" {
|
|
|
|
log.Println("Tried to create user with empty username or password")
|
|
|
|
http.Redirect(w, r, "/register", http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2022-11-14 18:29:55 +00:00
|
|
|
_, err = models.CreateUser(postController.App, username, password, createdAt, updatedAt)
|
2022-11-01 16:23:08 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Error creating user")
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-20 16:59:33 +00:00
|
|
|
http.Redirect(w, r, "/login", http.StatusFound)
|
|
|
|
}
|
2023-05-04 13:26:44 +00:00
|
|
|
|
|
|
|
func (postController *PostController) FileUpload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
max := postController.App.Config.Upload.MaxSize
|
|
|
|
r.ParseMultipartForm(max)
|
|
|
|
|
|
|
|
// FormFile returns the first file for the given key `file`
|
|
|
|
// it also returns the FileHeader so we can get the Filename,
|
|
|
|
// the Header and the size of the file
|
|
|
|
file, handler, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error Retrieving the File")
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if handler.Size > max {
|
|
|
|
log.Println("User tried uploading a file which is too large.")
|
|
|
|
http.Redirect(w, r, "/", http.StatusRequestHeaderFieldsTooLarge)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a temporary file within upload directory
|
|
|
|
tempFile, err := os.Create(postController.App.Config.Upload.BaseName + handler.Filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Redirect(w, r, "/", http.StatusNotAcceptable)
|
|
|
|
}
|
|
|
|
defer tempFile.Close()
|
|
|
|
|
|
|
|
// read all of the contents of our uploaded file into a
|
|
|
|
// byte array
|
|
|
|
fileBytes, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
// write this byte array to our temporary file
|
|
|
|
tempFile.Write(fileBytes)
|
|
|
|
// return that we have successfully uploaded our file!
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
}
|