update image path & add user

This commit is contained in:
pakintada@gmail.com 2024-01-24 11:58:39 +07:00
parent dd532e7e7d
commit bdd3762cd1
6 changed files with 84 additions and 3 deletions

1
server/.gitignore vendored
View file

@ -1,4 +1,5 @@
**/cofffeemachineConfig/*
**/taobin_project/*
**/*.log
token.json
client_secret.json

View file

@ -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) {

View file

@ -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) {