Skip to content

Commit

Permalink
implemented session management
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Jan 6, 2024
1 parent 8039d57 commit 8e2b39d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
60 changes: 60 additions & 0 deletions controllers/DeleteUploadSessionController.go
Original file line number Diff line number Diff line change
@@ -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")
}
39 changes: 39 additions & 0 deletions controllers/GetUploadSessionsController.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions models/UploadSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
2 changes: 2 additions & 0 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 8e2b39d

Please sign in to comment.