Skip to content

Commit

Permalink
Merge pull request #61 from BakingUp/update_ingredient_img_path
Browse files Browse the repository at this point in the history
[UPDATE] ingredient img path
  • Loading branch information
bbbung authored Oct 18, 2024
2 parents 694e89e + 551681a commit a4d659f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/core/service/ingredient.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (s *IngredientService) AddIngredient(c *fiber.Ctx, ingredients *domain.AddI
err := s.ingredientRepo.AddIngredient(c, addIngredientPayload)
imgIndex := 1
for _, img := range ingredients.Img {
imgUrl, err := util.UploadIngredientImage(userID, ingredientID, img)
imgUrl, err := util.UploadIngredientImage(userID, ingredientID, img, strconv.Itoa(imgIndex))
if err != nil {
return err
}
Expand Down
11 changes: 3 additions & 8 deletions internal/core/util/ingredient.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,14 @@ func ExpirationDate(daysSince2000 string) time.Time {
return expirationDate
}

func UploadIngredientImage(userId string, ingredientId string, imgBase64 string) (string, error) {
func UploadIngredientImage(userId string, ingredientId string, imgBase64 string, index string) (string, error) {
// Decode the base64 string to a byte slice
imgBytes, err := base64.StdEncoding.DecodeString(imgBase64)
if err != nil {
return "", err
}

// Generate a unique filename based on the current timestamp
filename := fmt.Sprintf("%d.jpg", time.Now().UnixNano())
// Create the path based on userId, and ingredientId
filePath := filepath.Join(fmt.Sprintf("images/%s/ingredients/%s", userId, ingredientId), filename)
filePath := filepath.Join(fmt.Sprintf("images/%s/ingredients/%s/%s.jpg", userId, ingredientId, index))

// Create the directory if it doesn't exist
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
Expand Down Expand Up @@ -128,10 +125,8 @@ func UploadIngredientStockImage(userId string, ingredientId string, ingredientSt
return "", err
}

// Generate a unique filename based on the current timestamp
filename := fmt.Sprintf("%d.jpg", time.Now().UnixNano())
// Create the path based on userId, and ingredientId
filePath := filepath.Join(fmt.Sprintf("images/%s/ingredients/%s/%s", userId, ingredientId, ingredientStockId), filename)
filePath := filepath.Join(fmt.Sprintf("images/%s/ingredients/%s/%s.jpg", userId, ingredientId, ingredientStockId))

// Create the directory if it doesn't exist
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
Expand Down

0 comments on commit a4d659f

Please sign in to comment.