From ea6d539c7b601e43b3d43deac457eb3c0f231320 Mon Sep 17 00:00:00 2001 From: akshayd2020 Date: Fri, 16 Feb 2024 16:48:11 -0500 Subject: [PATCH] Added get func --- backend/src/controllers/file.go | 27 ++++++++++++++-------- backend/src/errors/file.go | 4 ++++ backend/src/models/file.go | 1 + backend/src/server/routes/file.go | 10 +++++--- backend/src/server/server.go | 3 +++ backend/src/services/file.go | 38 +++++++++++++++++++++++-------- 6 files changed, 61 insertions(+), 22 deletions(-) diff --git a/backend/src/controllers/file.go b/backend/src/controllers/file.go index bb89dfd2e..fe50841a3 100644 --- a/backend/src/controllers/file.go +++ b/backend/src/controllers/file.go @@ -6,8 +6,8 @@ import ( "github.com/GenerateNU/sac/backend/src/errors" "github.com/GenerateNU/sac/backend/src/models" "github.com/GenerateNU/sac/backend/src/services" - "github.com/gofiber/fiber/v2" + "strings" ) type FileController struct { @@ -33,6 +33,9 @@ func (f *FileController) CreateFile(c *fiber.Ctx) error { if _, err = fileData.Read(buff); err != nil { return errors.InvalidImageFormat.FiberError(c) } + + + if !((http.DetectContentType(buff) == "image/png") || (http.DetectContentType(buff) == "image/jpeg")) { return errors.FailedToValidatedData.FiberError(c) } @@ -51,14 +54,18 @@ func (f *FileController) GetFile(c *fiber.Ctx) error { if err != nil { return err.FiberError(c) } - return c.Status(fiber.StatusOK).JSON(file) + arr := strings.SplitAfter(file.FileName, ".") + lenArr := len(arr) + print(arr[lenArr-1]) + c.Set("Content-Type", "image/jpeg") + return c.Send(file.FileData) } -// // Delete File -// func (f *FileController) DeleteFile(c *fiber.Ctx) error { -// fileID := c.Params("fid") -// if err := f.fileService.DeleteFile(fileID, false); err != nil { -// return err -// } -// return c.SendStatus(fiber.StatusNoContent) -// } +// Delete File +func (f *FileController) DeleteFile(c *fiber.Ctx) error { + fileID := c.Params("fileID") + if err := f.fileService.DeleteFile(fileID, false); err != nil { + return err + } + return c.SendStatus(fiber.StatusNoContent) +} diff --git a/backend/src/errors/file.go b/backend/src/errors/file.go index 573ec4342..c40c81236 100644 --- a/backend/src/errors/file.go +++ b/backend/src/errors/file.go @@ -43,4 +43,8 @@ var ( StatusCode: fiber.StatusBadRequest, Message: "invalid image format", } + FailedToDownloadFile = Error{ + StatusCode: fiber.StatusInternalServerError, + Message: "failed to download the file", + } ) diff --git a/backend/src/models/file.go b/backend/src/models/file.go index 82b052624..6d42fdf1d 100644 --- a/backend/src/models/file.go +++ b/backend/src/models/file.go @@ -5,6 +5,7 @@ type File struct { FileName string `gorm:"type:varchar(255)" json:"file_name"` FileSize int64 `gorm:"type:bigint;default:0" json:"file_size"` + FileData []byte ObjectKey string `gorm:"type:varchar(255);unique" json:"object_key"` Tags []*Tag `gorm:"many2many:file_tags;" json:"tags"` diff --git a/backend/src/server/routes/file.go b/backend/src/server/routes/file.go index 4be8ab686..6707aecb3 100644 --- a/backend/src/server/routes/file.go +++ b/backend/src/server/routes/file.go @@ -6,10 +6,14 @@ import ( "github.com/gofiber/fiber/v2" ) -func fileRoutes(router fiber.Router, fileService services.FileServiceInterface) { +func File(router fiber.Router, fileService services.FileServiceInterface) fiber.Router { fileController := controllers.NewFileController(fileService) - file := router.Group("/file") - file.Post("/", fileController.CreateFile) + files := router.Group("/files") + files.Post("/", fileController.CreateFile) + files.Get("/:fileID", fileController.GetFile) + files.Delete("/:fileID", fileController.DeleteFile) + + return files } diff --git a/backend/src/server/server.go b/backend/src/server/server.go index 77bf99d82..6dc4881a3 100644 --- a/backend/src/server/server.go +++ b/backend/src/server/server.go @@ -52,6 +52,9 @@ func Init(db *gorm.DB, settings config.Settings) *fiber.App { categoryRouter := routes.Category(apiv1, services.NewCategoryService(db, validate)) routes.CategoryTag(categoryRouter, services.NewCategoryTagService(db, validate)) + fileRouter := routes.File(apiv1, services.NewFileService(db, config.ConfigAWS(), validate)) + routes.File(fileRouter, services.NewFileService(db, config.ConfigAWS(), validate)) + return app } diff --git a/backend/src/services/file.go b/backend/src/services/file.go index 66ba71465..570401314 100644 --- a/backend/src/services/file.go +++ b/backend/src/services/file.go @@ -4,13 +4,13 @@ import ( "fmt" "io" "mime/multipart" - "strings" - "os" + "strings" - "github.com/GenerateNU/sac/backend/src/config" + "github.com/GenerateNU/sac/backend/src/config" "github.com/GenerateNU/sac/backend/src/errors" "github.com/GenerateNU/sac/backend/src/models" + "github.com/GenerateNU/sac/backend/src/utilities" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" @@ -21,8 +21,6 @@ import ( "gorm.io/gorm" ) - - type FileServiceInterface interface { CreateFile(file models.File, data *multipart.FileHeader, reader io.Reader) (*models.File, *errors.Error) DeleteFile(id string, s3Only bool) error @@ -32,6 +30,11 @@ type FileServiceInterface interface { type FileService struct { DB *gorm.DB Settings config.AWSSettings + Validate *validator.Validate +} + +func NewFileService(db *gorm.DB, settings config.AWSSettings, validate *validator.Validate) *FileService { + return &FileService{DB: db, Settings: settings, Validate: validate} } func createAWSSession(settings config.AWSSettings) (*session.Session, error) { @@ -47,11 +50,17 @@ func createAWSSession(settings config.AWSSettings) (*session.Session, error) { return sess, nil } -// // Get File +// Get File func (f *FileService) GetFile(id string) (*models.File, *errors.Error) { var file models.File - if err := f.DB.First(&file, id).Error; err != nil { + idAsUUID, errUUID := utilities.ValidateID(id) + + if errUUID != nil { + return nil, &errors.FailedToValidateID + } + + if err := f.DB.First(&file, idAsUUID).Error; err != nil { return &models.File{}, &errors.FailedToGetFile } @@ -68,7 +77,13 @@ func (f *FileService) GetFile(id string) (*models.File, *errors.Error) { &s3.GetObjectInput{ Bucket: aws.String("generate-sac-storage"), Key: aws.String(file.FileName), - }) + }) + + fileData, fileDataErr := io.ReadAll(downloadedFile) + if fileDataErr != nil { + return nil, &errors.FailedToDownloadFile + } + file.FileData = fileData return &file, nil } @@ -133,7 +148,12 @@ func (f *FileService) CreateFile(file models.File, data *multipart.FileHeader, r func (f *FileService) DeleteFile(id string, s3Only bool) error { var file models.File - if err := f.DB.First(&file, id).Error; err != nil { + idAsUUID, errUUID := utilities.ValidateID(id) + if errUUID != nil { + return &errors.FailedToValidateID + } + + if err := f.DB.First(&file, idAsUUID).Error; err != nil { return err }