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

[UPDATE] ingredient img path #61

Merged
merged 3 commits into from
Oct 18, 2024
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
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
Loading