fix(image):
This commit is contained in:
parent
2b8745679f
commit
040c3c7751
6 changed files with 459 additions and 437 deletions
|
|
@ -801,49 +801,70 @@ func (rr *RecipeRouter) getImageOfProductCode(w http.ResponseWriter, r *http.Req
|
|||
// img_dir := "taobin_project/image/page_drink_picture2_n/"
|
||||
|
||||
// new img dir
|
||||
img_dir := "cofffeemachineConfig/" + countryID + "/.img/"
|
||||
// img_dir := "cofffeemachineConfig/" + countryID + "/.img/"
|
||||
|
||||
fullPath := img_dir + uriName
|
||||
rr.taoLogger.Log.Debug("RecipeRouter.getImageOfProductCode", zap.Any("fullPath", fullPath))
|
||||
// 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
|
||||
}
|
||||
// if _, err := os.Stat(fullPath); os.IsNotExist(err) {
|
||||
// http.Error(w, "Image not found", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
|
||||
// TODO: get from cache
|
||||
var imgResult image.Image
|
||||
// try cache
|
||||
img, err := rr.cache_db.GetCacheImage(fullPath)
|
||||
// img, err := rr.cache_db.GetCacheImage(fullPath)
|
||||
// if err != nil {
|
||||
// // 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
|
||||
// }
|
||||
|
||||
// imgResult = thisImage
|
||||
|
||||
// // set cache
|
||||
// err = rr.cache_db.SetImageToCache(fullPath, thisImage)
|
||||
// if err != nil {
|
||||
// rr.taoLogger.Log.Debug("CacheImage.SetError", zap.Any("error", err))
|
||||
// }
|
||||
|
||||
// } else {
|
||||
// imgResult = img
|
||||
// rr.taoLogger.Log.Debug("CacheImage.OK", zap.Any("status", imgResult))
|
||||
// }
|
||||
|
||||
// New: try load from another server
|
||||
//
|
||||
|
||||
cli := http.Client{}
|
||||
resp, err := cli.Get("http://localhost:36527/image/" + uriName)
|
||||
if err != nil {
|
||||
// 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
|
||||
}
|
||||
|
||||
imgResult = thisImage
|
||||
|
||||
// set cache
|
||||
err = rr.cache_db.SetImageToCache(fullPath, thisImage)
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Debug("CacheImage.SetError", zap.Any("error", err))
|
||||
}
|
||||
|
||||
} else {
|
||||
imgResult = img
|
||||
rr.taoLogger.Log.Debug("CacheImage.OK", zap.Any("status", imgResult))
|
||||
rr.taoLogger.Log.Debug("ImgGetFromFastErr", zap.Any("Error", err.Error()))
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
}
|
||||
|
||||
rr.taoLogger.Log.Debug("GetImageFromFast", zap.Int("Code", resp.StatusCode));
|
||||
|
||||
img_data, err := png.Decode(resp.Body)
|
||||
if err != nil {
|
||||
rr.taoLogger.Log.Error("ReadFromFastRespErr", zap.Any("Error", err.Error()))
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
imgResult = img_data
|
||||
|
||||
|
||||
// write image
|
||||
png.Encode(w, imgResult)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue