Skip to content

Commit

Permalink
Added get func
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayd2020 committed Feb 16, 2024
1 parent 324e110 commit ea6d539
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 22 deletions.
27 changes: 17 additions & 10 deletions backend/src/controllers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
}
4 changes: 4 additions & 0 deletions backend/src/errors/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ var (
StatusCode: fiber.StatusBadRequest,
Message: "invalid image format",
}
FailedToDownloadFile = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to download the file",
}
)
1 change: 1 addition & 0 deletions backend/src/models/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
10 changes: 7 additions & 3 deletions backend/src/server/routes/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

3 changes: 3 additions & 0 deletions backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
38 changes: 29 additions & 9 deletions backend/src/services/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit ea6d539

Please sign in to comment.