From 8e2b39df9efa1e40c2b235e2823bc71c80675a70 Mon Sep 17 00:00:00 2001 From: kirari04 Date: Sat, 6 Jan 2024 18:10:33 +0100 Subject: [PATCH] implemented session management --- controllers/DeleteUploadSessionController.go | 60 ++++++++++++++++++++ controllers/GetUploadSessionsController.go | 39 +++++++++++++ models/UploadSession.go | 4 ++ routes/api.go | 2 + 4 files changed, 105 insertions(+) create mode 100755 controllers/DeleteUploadSessionController.go create mode 100755 controllers/GetUploadSessionsController.go diff --git a/controllers/DeleteUploadSessionController.go b/controllers/DeleteUploadSessionController.go new file mode 100755 index 0000000..cf0740b --- /dev/null +++ b/controllers/DeleteUploadSessionController.go @@ -0,0 +1,60 @@ +package controllers + +import ( + "ch/kirari04/videocms/helpers" + "ch/kirari04/videocms/inits" + "ch/kirari04/videocms/models" + "fmt" + "log" + "os" + + "github.com/gofiber/fiber/v2" +) + +func DeleteUploadSession(c *fiber.Ctx) error { + // parse & validate request + var validation models.DeleteUploadSessionValidation + if err := c.BodyParser(&validation); err != nil { + return c.Status(fiber.StatusBadRequest).SendString("Invalid body request format") + } + + if errors := helpers.ValidateStruct(validation); len(errors) > 0 { + return c.Status(fiber.StatusBadRequest).SendString(fmt.Sprintf("%s [%s] : %s", errors[0].FailedField, errors[0].Tag, errors[0].Value)) + } + + userId, ok := c.Locals("UserID").(uint) + if !ok { + log.Println("GetUploadSessions: Failed to catch userId") + return c.SendStatus(fiber.StatusInternalServerError) + } + + var uploadSession models.UploadSession + if res := inits.DB.Where(&models.UploadSession{ + UUID: validation.UploadSessionUUID, + }, "UUID").First(&uploadSession); res.Error != nil { + return c.Status(fiber.StatusBadRequest).SendString("Upload Session not found") + } + + if uploadSession.UserID != userId { + return c.Status(fiber.StatusBadRequest).SendString("Upload Session not found") + } + + if res := inits.DB. + Model(&models.UploadChunck{}). + Where(&models.UploadChunck{ + UploadSessionID: uploadSession.ID, + }). + Delete(&models.UploadChunck{}); res.Error != nil { + log.Printf("[WARNING] createUploadFileCleanup -> remove upload chuncks from database (%d): %v\n", uploadSession.ID, res.Error) + } + if res := inits.DB. + Delete(&models.UploadSession{}, uploadSession.ID); res.Error != nil { + log.Printf("[WARNING] createUploadFileCleanup -> remove upload session from database (%d): %v\n", uploadSession.ID, res.Error) + } + + if err := os.RemoveAll(uploadSession.SessionFolder); err != nil { + log.Printf("[WARNING] createUploadFileCleanup -> remove session folder: %v\n", err) + } + + return c.Status(fiber.StatusOK).SendString("ok") +} diff --git a/controllers/GetUploadSessionsController.go b/controllers/GetUploadSessionsController.go new file mode 100755 index 0000000..88fdefb --- /dev/null +++ b/controllers/GetUploadSessionsController.go @@ -0,0 +1,39 @@ +package controllers + +import ( + "ch/kirari04/videocms/inits" + "ch/kirari04/videocms/models" + "log" + "time" + + "github.com/gofiber/fiber/v2" +) + +type GetUploadSessionsRes struct { + CreatedAt *time.Time + Name string + UUID string + Size int64 + ChunckCount int +} + +func GetUploadSessions(c *fiber.Ctx) error { + userId, ok := c.Locals("UserID").(uint) + if !ok { + log.Println("GetUploadSessions: Failed to catch userId") + return c.SendStatus(fiber.StatusInternalServerError) + } + + var sessions []GetUploadSessionsRes + if res := inits.DB. + Model(&models.UploadSession{}). + Where(&models.UploadSession{ + UserID: userId, + }, "UserID"). + Find(&sessions); res.Error != nil { + log.Println("Failed to list upload sessions", res.Error) + return c.SendStatus(fiber.StatusInternalServerError) + } + + return c.Status(fiber.StatusOK).JSON(&sessions) +} diff --git a/models/UploadSession.go b/models/UploadSession.go index b30dc2d..c20400c 100755 --- a/models/UploadSession.go +++ b/models/UploadSession.go @@ -28,3 +28,7 @@ type UploadSessionValidation struct { Size int64 `validate:"required,number,min=1"` ParentFolderID uint `validate:"number"` } + +type DeleteUploadSessionValidation struct { + UploadSessionUUID string `validate:"required,uuid_rfc4122"` +} diff --git a/routes/api.go b/routes/api.go index 1e3709b..ae51510 100755 --- a/routes/api.go +++ b/routes/api.go @@ -58,7 +58,9 @@ func Api() { protectedApi.Delete("/webhook", controllers.DeleteWebhook) protectedApi.Get("/webhooks", controllers.ListWebhooks) + protectedApi.Get("/pcu/sessions", controllers.GetUploadSessions) protectedApi.Post("/pcu/session", controllers.CreateUploadSession) + protectedApi.Delete("/pcu/session", controllers.DeleteUploadSession) protectedApi.Post("/pcu/chunck", controllers.CreateUploadChunck) protectedApi.Post("/pcu/file", controllers.CreateUploadFile) }