From bdd3762cd195fd3fbc989e7f1490bc2bf170c7d2 Mon Sep 17 00:00:00 2001 From: "pakintada@gmail.com" Date: Wed, 24 Jan 2024 11:58:39 +0700 Subject: [PATCH] update image path & add user --- .../src/app/core/services/material.service.ts | 2 + .../recipe-details.component.html | 10 +++- .../recipe-details.component.ts | 15 +++++ server/.gitignore | 1 + server/routers/recipe.go | 58 +++++++++++++++++++ server/server.go | 1 + 6 files changed, 84 insertions(+), 3 deletions(-) diff --git a/client/src/app/core/services/material.service.ts b/client/src/app/core/services/material.service.ts index 4f3019f..4bb51ce 100644 --- a/client/src/app/core/services/material.service.ts +++ b/client/src/app/core/services/material.service.ts @@ -42,6 +42,7 @@ export class MaterialService { ): Promise>{ console.log("getFullMaterialDetail", country, "where filename = ",filename, "department.short = ", this.department!); @@ -59,6 +60,7 @@ export class MaterialService { return this._httpClient.get<{ "materialId": number, "name": string, + "nameEN": string, "type": string }[]>(`${environment.api}/materials/full/${country}/${filename}`, { withCredentials: true, diff --git a/client/src/app/features/recipes/recipe-details/recipe-details.component.html b/client/src/app/features/recipes/recipe-details/recipe-details.component.html index 53398a7..9d36615 100644 --- a/client/src/app/features/recipes/recipe-details/recipe-details.component.html +++ b/client/src/app/features/recipes/recipe-details/recipe-details.component.html @@ -42,7 +42,9 @@
/assets/{{ 'bn_hot_america_no' }}.png
@@ -110,10 +112,12 @@ -
+
+ +

Related Menu

{{ sub }} diff --git a/client/src/app/features/recipes/recipe-details/recipe-details.component.ts b/client/src/app/features/recipes/recipe-details/recipe-details.component.ts index 91f953e..06faa62 100644 --- a/client/src/app/features/recipes/recipe-details/recipe-details.component.ts +++ b/client/src/app/features/recipes/recipe-details/recipe-details.component.ts @@ -21,6 +21,7 @@ import { ToppingService } from 'src/app/core/services/topping.service'; import { copy, transformToTSV } from 'src/app/shared/helpers/copy'; import { isEqual } from 'lodash'; +import { environment } from 'src/environments/environment'; @Component({ selector: 'app-recipe-details', @@ -346,4 +347,18 @@ export class RecipeDetailsComponent implements OnInit { Object.keys(this.rawRecipe as any).includes('cashPrice') ? (this.rawRecipe as any).cashPrice : 0; + + // image path + imagePath = (productCode: string) => { + const filename = this._recipeService.getCurrentFile(); + + return environment.api + + '/recipes/' + + this.department + + '/' + + filename + + '/' + + productCode + + '/image'; + }; } diff --git a/server/.gitignore b/server/.gitignore index 9373ac5..a3300dd 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -1,4 +1,5 @@ **/cofffeemachineConfig/* +**/taobin_project/* **/*.log token.json client_secret.json diff --git a/server/routers/recipe.go b/server/routers/recipe.go index 385ef7b..3f5bee8 100644 --- a/server/routers/recipe.go +++ b/server/routers/recipe.go @@ -3,6 +3,7 @@ package routers import ( "encoding/json" "fmt" + "image/png" "net/http" "os" "path" @@ -64,6 +65,9 @@ func (rr *RecipeRouter) Route(r chi.Router) { // fetch raw recipe json r.Get("/{country}/{filename}/{product_code}/raw_full", rr.getRawRecipeOfProductCode) + // fetch image + r.Get("/{country}/{filename}/{product_code}/image", rr.getImageOfProductCode) + r.Get("/{country}/{filename}/json", rr.getRecipeJson) r.Post("/edit/{country}/{filename}", rr.updateRecipe) @@ -574,6 +578,60 @@ func (rr *RecipeRouter) getRawRecipeOfProductCode(w http.ResponseWriter, r *http json.NewEncoder(w).Encode(recipe) } +func (rr *RecipeRouter) getImageOfProductCode(w http.ResponseWriter, r *http.Request) { + + countryID := chi.URLParam(r, "country") + filename := chi.URLParam(r, "filename") + productCode := chi.URLParam(r, "product_code") + + w.Header().Add("Content-Type", "image/png") + + // get image + recipe, err := rr.data.GetRecipe01ByProductCode(filename, countryID, productCode) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + + // check if image string is not empty + if recipe.UriData == "" { + http.Error(w, "Image not found", http.StatusNotFound) + return + } + + // clean image uri name + clean1 := strings.Replace(recipe.UriData, "\u003d", "=", -1) + uriName := strings.Split(clean1, "=")[1] + + img_dir := "taobin_project/image/page_drink_picture2_n/" + + fullPath := img_dir + uriName + rr.taoLogger.Log.Debug("RecipeRouter.getImageOfProductCode", zap.Any("fullPath", fullPath)) + + // check if image file exists + if _, err := os.Stat(fullPath); os.IsNotExist(err) { + http.Error(w, "Image not found", http.StatusNotFound) + return + } + + // read image + imgFile, err := os.Open(fullPath) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + defer imgFile.Close() + + thisImage, err := png.Decode(imgFile) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + + // write image + png.Encode(w, thisImage) +} + func (rr *RecipeRouter) doMergeJson(w http.ResponseWriter, r *http.Request) { // TODO: v2, change to binary instead if !APIhandler(w, r) { diff --git a/server/server.go b/server/server.go index efcfc84..51fa37f 100644 --- a/server/server.go +++ b/server/server.go @@ -116,6 +116,7 @@ func (s *Server) createHandler() { _ = userService.CreateNewUser(context.WithValue(context.Background(), "user", &models.User{Email: "system"}), "kenta420", "poomipat.c@forth.co.th", "", permissions.SuperAdmin) _ = userService.CreateNewUser(context.WithValue(context.Background(), "user", &models.User{Email: "system"}), "phu", "pakin.t@forth.co.th", "", permissions.SuperAdmin) _ = userService.CreateNewUser(context.WithValue(context.Background(), "user", &models.User{Email: "system"}), "wanlop", "wanlop.r@forth.co.th", "", permissions.SuperAdmin) + _ = userService.CreateNewUser(context.WithValue(context.Background(), "user", &models.User{Email: "system"}), "dawit", "dawit.o@forth.co.th", "", permissions.SuperAdmin) // Auth Router r.Group(func(r chi.Router) {