Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor asset image upload in AssetUseCase #83

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion backend/internal/controller/http/v1/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,15 @@ func (r *assetsRoutes) uploadAssetImage(c *gin.Context) {
}

assetId := c.Param("id")
if err := r.as.UploadImage(assetId, decodedBytes); err != nil {

asset, err := r.as.GetById(assetId)
if err != nil {
r.logger.Error(err, "http - v1 - get asset by id - get by id")
errorResponse(c, http.StatusInternalServerError, "error getting asset", err)
return
}

if err := r.as.UploadImage(asset, decodedBytes); err != nil {
r.logger.Error(err, "http - v1 - upload asset image - upload image")
errorResponse(c, http.StatusInternalServerError, "Failed to store the image", err)
return
Expand Down
14 changes: 10 additions & 4 deletions backend/internal/usecase/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package usecase

import (
"fmt"
"strconv"
"time"

"github.com/CheesecakeLabs/token-factory-v2/backend/config"
"github.com/CheesecakeLabs/token-factory-v2/backend/internal/entity"
Expand Down Expand Up @@ -42,7 +44,8 @@ func (uc *AssetUseCase) Create(data entity.Asset, imageBytes []byte) (entity.Ass

// Upload image to S3 if exists
if len(imageBytes) > 0 {
assetImage, err := uc.storageService.UploadFile(fmt.Sprint(data.Id), imageBytes)
s3Name := fmt.Sprint(data.Code, "_", data.Issuer.Key.PublicKey)
assetImage, err := uc.storageService.UploadFile(s3Name, imageBytes)
if err != nil {
return entity.Asset{}, fmt.Errorf("AssetUseCase - Create - uc.awsService.UploadAssetImage: %w", err)
}
Expand Down Expand Up @@ -84,13 +87,16 @@ func (uc *AssetUseCase) GetAll() ([]entity.Asset, error) {
return assets, nil
}

func (uc *AssetUseCase) UploadImage(assetId string, imageBytes []byte) error {
assetImage, err := uc.storageService.UploadFile(assetId, imageBytes)
func (uc *AssetUseCase) UploadImage(data entity.Asset, imageBytes []byte) error {
timestamp := time.Now().Format("20060102_150405")
s3Name := fmt.Sprintf("%s_%s_%s", data.Code, data.Issuer.Key.PublicKey, timestamp)

assetImage, err := uc.storageService.UploadFile(s3Name, imageBytes)
if err != nil {
return fmt.Errorf("AssetUseCase - Create - uc.awsService.UploadAssetImage: %w", err)
}

err = uc.aRepo.StoreAssetImage(assetId, assetImage)
err = uc.aRepo.StoreAssetImage(strconv.Itoa(data.Id), assetImage)
if err != nil {
return fmt.Errorf("ImageUseCase - UploadImage - uc.aRepo.StoreAssetImage: %w", err)
}
Expand Down
Loading