fix(image):

This commit is contained in:
pakintada@gmail.com 2024-06-03 10:12:16 +07:00
parent 2b8745679f
commit 040c3c7751
6 changed files with 459 additions and 437 deletions

2
.gitignore vendored
View file

@ -4,3 +4,5 @@ server/services/logger/serverlog.log
server/cofffeemachineConfig server/cofffeemachineConfig
node_modules node_modules
server/cofffeemachineConfig.diff server/cofffeemachineConfig.diff
**/.env
nginx_cfg/

3
client/.gitignore vendored
View file

@ -1,7 +1,8 @@
# See http://help.github.com/ignore-files/ for more about ignoring files. # See http://help.github.com/ignore-files/ for more about ignoring files.
# Compiled output # Compiled output
/dist /dist/client/
/dist/client.zip
/tmp /tmp
/out-tsc /out-tsc
/bazel-out /bazel-out

View file

@ -1,37 +1,37 @@
import { CommonModule, DatePipe } from '@angular/common'; import { CommonModule, DatePipe } from "@angular/common";
import { Component, EventEmitter, OnInit } from '@angular/core'; import { Component, EventEmitter, OnInit } from "@angular/core";
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms'; import { FormBuilder, FormsModule, ReactiveFormsModule } from "@angular/forms";
import { ActivatedRoute, Router, RouterLink } from '@angular/router'; import { ActivatedRoute, Router, RouterLink } from "@angular/router";
import { Observable, first, map } from 'rxjs'; import { Observable, first, map } from "rxjs";
import { RecipeService } from 'src/app/core/services/recipe.service'; import { RecipeService } from "src/app/core/services/recipe.service";
import { ConfirmModal } from 'src/app/shared/modal/confirm/confirm-modal.component'; import { ConfirmModal } from "src/app/shared/modal/confirm/confirm-modal.component";
import { animate, style, transition, trigger } from '@angular/animations'; import { animate, style, transition, trigger } from "@angular/animations";
import { RecipeListComponent } from './recipe-list/recipe-list.component'; import { RecipeListComponent } from "./recipe-list/recipe-list.component";
import { import {
Recipe01, Recipe01,
RecipeDetail, RecipeDetail,
RecipeDetailMat, RecipeDetailMat,
Topping, Topping,
ToppingSet, ToppingSet,
} from 'src/app/core/models/recipe.model'; } from "src/app/core/models/recipe.model";
import { ActionRecord } from 'src/app/shared/actionRecord/actionRecord'; import { ActionRecord } from "src/app/shared/actionRecord/actionRecord";
import { UserService } from 'src/app/core/services/user.service'; import { UserService } from "src/app/core/services/user.service";
import { UserPermissions } from 'src/app/core/auth/userPermissions'; import { UserPermissions } from "src/app/core/auth/userPermissions";
import { ToppingService } from 'src/app/core/services/topping.service'; import { ToppingService } from "src/app/core/services/topping.service";
import { copy, transformToTSV } from 'src/app/shared/helpers/copy'; import { copy, transformToTSV } from "src/app/shared/helpers/copy";
import { isEqual } from 'lodash'; import { isEqual } from "lodash";
import { environment } from 'src/environments/environment'; import { environment } from "src/environments/environment";
@Component({ @Component({
selector: 'app-recipe-details', selector: "app-recipe-details",
templateUrl: './recipe-details.component.html', templateUrl: "./recipe-details.component.html",
standalone: true, standalone: true,
animations: [ animations: [
trigger('inOutAnimation', [ trigger("inOutAnimation", [
transition(':enter', [ transition(":enter", [
style({ opacity: 0 }), style({ opacity: 0 }),
animate('1s ease-out', style({ opacity: 1 })), animate("1s ease-out", style({ opacity: 1 })),
]), ]),
]), ]),
], ],
@ -46,7 +46,7 @@ import { environment } from 'src/environments/environment';
], ],
}) })
export class RecipeDetailsComponent implements OnInit { export class RecipeDetailsComponent implements OnInit {
title: string = 'Recipe Detail'; title: string = "Recipe Detail";
recipeDetail$!: Observable<RecipeDetail>; recipeDetail$!: Observable<RecipeDetail>;
@ -58,10 +58,10 @@ export class RecipeDetailsComponent implements OnInit {
recipeOriginalDetail!: typeof this.recipeDetailForm.value; recipeOriginalDetail!: typeof this.recipeDetailForm.value;
commit_msg: string = ''; commit_msg: string = "";
// //
department: string = this._route.parent!.snapshot.params['department']; department: string = this._route.parent!.snapshot.params["department"];
constructor( constructor(
private _formBuilder: FormBuilder, private _formBuilder: FormBuilder,
@ -69,18 +69,18 @@ export class RecipeDetailsComponent implements OnInit {
private _router: Router, private _router: Router,
private _recipeService: RecipeService, private _recipeService: RecipeService,
private _toppingService: ToppingService, private _toppingService: ToppingService,
private _userService: UserService private _userService: UserService,
) {} ) {}
productCode!: any; productCode!: any;
changedProductCode: string | undefined = undefined; changedProductCode: string | undefined = undefined;
recipeDetailForm = this._formBuilder.group({ recipeDetailForm = this._formBuilder.group({
productCode: '', productCode: "",
name: '', name: "",
otherName: '', otherName: "",
description: '', description: "",
otherDescription: '', otherDescription: "",
lastModified: Date(), lastModified: Date(),
price: 0, price: 0,
isUse: false, isUse: false,
@ -97,23 +97,25 @@ export class RecipeDetailsComponent implements OnInit {
rawRecipe: {} | undefined | null = undefined; rawRecipe: {} | undefined | null = undefined;
async ngOnInit() { async ngOnInit() {
this.productCode = this._route.snapshot.params['productCode']; this.productCode = this._route.snapshot.params["productCode"];
this.recipeDetail$ = ( this.recipeDetail$ = (
await this._recipeService.getRecipeDetail(this.productCode) await this._recipeService.getRecipeDetail(this.productCode)
).pipe(first()); ).pipe(first());
this.recipeDetail$.subscribe((detail) => { this.recipeDetail$.subscribe((detail) => {
console.log('Recipe Detail', detail); console.log("Recipe Detail", detail);
// transform lastChange aka lastUpdated to type `Date` // transform lastChange aka lastUpdated to type `Date`
if(detail.lastUpdated){ if (detail.lastUpdated) {
console.log("Date ", detail.lastUpdated, typeof detail.lastUpdated) console.log("Date ", detail.lastUpdated, typeof detail.lastUpdated);
} }
this.recipeDetailForm.patchValue(detail); this.recipeDetailForm.patchValue(detail);
console.log("from recipeDetailForm", this.recipeDetailForm); console.log("from recipeDetailForm", this.recipeDetailForm);
this.recipeDetailForm.get('lastModified')?.setValue(detail.lastUpdated.toString()); this.recipeDetailForm
.get("lastModified")
?.setValue(detail.lastUpdated.toString());
this.isLoaded = true; this.isLoaded = true;
this.recipeOriginalDetail = { ...this.recipeDetailForm.getRawValue() }; this.recipeOriginalDetail = { ...this.recipeDetailForm.getRawValue() };
@ -123,10 +125,10 @@ export class RecipeDetailsComponent implements OnInit {
.getSubMenus( .getSubMenus(
await this._recipeService.getCurrentCountry(), await this._recipeService.getCurrentCountry(),
this._recipeService.getCurrentFile(), this._recipeService.getCurrentFile(),
this.productCode this.productCode,
) )
.subscribe((data) => { .subscribe((data) => {
console.log('Submenus', data); console.log("Submenus", data);
this.submenus = data; this.submenus = data;
}); });
@ -136,7 +138,7 @@ export class RecipeDetailsComponent implements OnInit {
await this._toppingService.getToppingsOfRecipe( await this._toppingService.getToppingsOfRecipe(
this.department, this.department,
this._recipeService.getCurrentFile(), this._recipeService.getCurrentFile(),
this.productCode this.productCode,
) )
).subscribe((data) => { ).subscribe((data) => {
this.toppingSet = data; this.toppingSet = data;
@ -148,25 +150,25 @@ export class RecipeDetailsComponent implements OnInit {
await this._recipeService.getRawRecipeOfProductCode( await this._recipeService.getRawRecipeOfProductCode(
this.department, this.department,
this._recipeService.getCurrentFile(), this._recipeService.getCurrentFile(),
this.productCode this.productCode,
) )
).subscribe((data) => { ).subscribe((data) => {
console.log('Raw Recipe', data); console.log("Raw Recipe", data);
this.rawRecipe = data; this.rawRecipe = data;
}); });
// snap recipe detail form value // snap recipe detail form value
this.actionRecord.registerOnAddAction((currAction, allAction) => { this.actionRecord.registerOnAddAction((currAction, allAction) => {
if (currAction.type === 'recipeListData') { if (currAction.type === "recipeListData") {
switch (currAction.action) { switch (currAction.action) {
case 'add': case "add":
break; break;
case 'delete': case "delete":
break; break;
} }
} }
console.log('Action Record', allAction); console.log("Action Record", allAction);
}); });
} }
@ -174,13 +176,13 @@ export class RecipeDetailsComponent implements OnInit {
showConfirmCloseModal: EventEmitter<boolean> = new EventEmitter<boolean>(); showConfirmCloseModal: EventEmitter<boolean> = new EventEmitter<boolean>();
confirmSave = { confirmSave = {
title: 'The changes detected!', title: "The changes detected!",
message: 'Do you want to save changes?', message: "Do you want to save changes?",
confirmCallBack: async () => { confirmCallBack: async () => {
console.log('confirm save'); console.log("confirm save");
// get username // get username
let username: string = ''; let username: string = "";
username = this._userService.getCurrentUser()!.name; username = this._userService.getCurrentUser()!.name;
@ -226,21 +228,21 @@ export class RecipeDetailsComponent implements OnInit {
SubMenu: [...(this.rawRecipe! as any).SubMenu!], SubMenu: [...(this.rawRecipe! as any).SubMenu!],
ToppingSet: [...(this.rawRecipe as any).ToppingSet], ToppingSet: [...(this.rawRecipe as any).ToppingSet],
}; };
console.log('pre to_send', to_send); console.log("pre to_send", to_send);
this.concatNoEditKeyToMap(this.rawRecipe, to_send); this.concatNoEditKeyToMap(this.rawRecipe, to_send);
console.log('to_send', to_send); console.log("to_send", to_send);
this._recipeService.editChanges( this._recipeService.editChanges(
await this._recipeService.getCurrentCountry(this.department), await this._recipeService.getCurrentCountry(this.department),
this._recipeService.getCurrentFile(), this._recipeService.getCurrentFile(),
{ {
...to_send, ...to_send,
} },
); );
console.log('Sending changes'); console.log("Sending changes");
void this._router void this._router
.navigate(['/' + this.department + '/recipes']) .navigate(["/" + this.department + "/recipes"])
.then(() => { .then(() => {
window.location.reload(); window.location.reload();
}); });
@ -252,11 +254,11 @@ export class RecipeDetailsComponent implements OnInit {
} }
confirmClose = { confirmClose = {
title: 'The changes will be lost!', title: "The changes will be lost!",
message: 'Do you want to close without saving?', message: "Do you want to close without saving?",
confirmCallBack: () => { confirmCallBack: () => {
console.log('confirm close'); console.log("confirm close");
this._router.navigate(['/' + this.department + '/recipes']); this._router.navigate(["/" + this.department + "/recipes"]);
}, },
}; };
@ -264,7 +266,7 @@ export class RecipeDetailsComponent implements OnInit {
if (this.isValueChanged) { if (this.isValueChanged) {
this.showConfirmSaveModal.emit(true); this.showConfirmSaveModal.emit(true);
} else { } else {
this._router.navigate(['/' + this.department + '/recipes']); this._router.navigate(["/" + this.department + "/recipes"]);
} }
} }
@ -272,7 +274,7 @@ export class RecipeDetailsComponent implements OnInit {
if (this.isValueChanged) { if (this.isValueChanged) {
this.showConfirmCloseModal.emit(true); this.showConfirmCloseModal.emit(true);
} else { } else {
this._router.navigate(['/' + this.department + '/recipes']); this._router.navigate(["/" + this.department + "/recipes"]);
} }
} }
@ -283,25 +285,25 @@ export class RecipeDetailsComponent implements OnInit {
} }
onRecipeListFormChange(repl: unknown[]) { onRecipeListFormChange(repl: unknown[]) {
console.log('Recipe List Form Changed', repl); console.log("Recipe List Form Changed", repl);
this.tpl = repl[0] as never[]; this.tpl = repl[0] as never[];
this.repl = repl[1] as never[]; this.repl = repl[1] as never[];
console.log('Recipe List Form Changed', this.repl, this.tpl); console.log("Recipe List Form Changed", this.repl, this.tpl);
for (let ti = 0; ti < this.tpl.length; ti++) { for (let ti = 0; ti < this.tpl.length; ti++) {
// check at the same index // check at the same index
if (!isEqual(this.tpl[ti][0], (this.rawRecipe as any).ToppingSet[ti])) { if (!isEqual(this.tpl[ti][0], (this.rawRecipe as any).ToppingSet[ti])) {
console.log( console.log(
'topping list changed', "topping list changed",
ti, ti,
this.tpl[ti][0], this.tpl[ti][0],
(this.rawRecipe as any).ToppingSet[ti] (this.rawRecipe as any).ToppingSet[ti],
); );
// update raw recipe // update raw recipe
(this.rawRecipe as any).ToppingSet[ti] = this.tpl[ti][0]; (this.rawRecipe as any).ToppingSet[ti] = this.tpl[ti][0];
console.log( console.log(
'after update topping', "after update topping",
(this.rawRecipe as any).ToppingSet[ti] (this.rawRecipe as any).ToppingSet[ti],
); );
} }
} }
@ -323,7 +325,7 @@ export class RecipeDetailsComponent implements OnInit {
} }
onProductCodeChange(event: any) { onProductCodeChange(event: any) {
if (event.target.value != '') { if (event.target.value != "") {
this.changedProductCode = event.target.value; this.changedProductCode = event.target.value;
} }
} }
@ -336,7 +338,7 @@ export class RecipeDetailsComponent implements OnInit {
selectSubmenu(productCode: string) { selectSubmenu(productCode: string) {
void this._router.navigate([ void this._router.navigate([
'/' + this.department + '/recipes/' + productCode, "/" + this.department + "/recipes/" + productCode,
]); ]);
} }
@ -352,7 +354,7 @@ export class RecipeDetailsComponent implements OnInit {
recipePrice = () => recipePrice = () =>
this.rawRecipe != undefined && this.rawRecipe != undefined &&
this.rawRecipe != null && this.rawRecipe != null &&
Object.keys(this.rawRecipe as any).includes('cashPrice') Object.keys(this.rawRecipe as any).includes("cashPrice")
? (this.rawRecipe as any).cashPrice ? (this.rawRecipe as any).cashPrice
: 0; : 0;
@ -360,13 +362,15 @@ export class RecipeDetailsComponent implements OnInit {
imagePath = (productCode: string) => { imagePath = (productCode: string) => {
const filename = this._recipeService.getCurrentFile(); const filename = this._recipeService.getCurrentFile();
return environment.api + return (
'/recipes/' + environment.api +
"/recipes/" +
this.department + this.department +
'/' + "/" +
filename + filename +
'/' + "/" +
productCode + productCode +
'/image'; "/image"
);
}; };
} }

View file

@ -189,7 +189,7 @@ var (
func NewData(taoLogger *logger.TaoLogger, redisClient *RedisCli) *Data { func NewData(taoLogger *logger.TaoLogger, redisClient *RedisCli) *Data {
allRecipeFiles := helpers.ScanRecipeFiles(helpers.LoadCountrySettings()) allRecipeFiles := helpers.ScanRecipeFiles(helpers.LoadCountrySettings())
fmt.Println(allRecipeFiles) // fmt.Println(allRecipeFiles)
defaultFile := "coffeethai02_600.json" defaultFile := "coffeethai02_600.json"

View file

@ -801,49 +801,70 @@ func (rr *RecipeRouter) getImageOfProductCode(w http.ResponseWriter, r *http.Req
// img_dir := "taobin_project/image/page_drink_picture2_n/" // img_dir := "taobin_project/image/page_drink_picture2_n/"
// new img dir // new img dir
img_dir := "cofffeemachineConfig/" + countryID + "/.img/" // img_dir := "cofffeemachineConfig/" + countryID + "/.img/"
fullPath := img_dir + uriName // fullPath := img_dir + uriName
rr.taoLogger.Log.Debug("RecipeRouter.getImageOfProductCode", zap.Any("fullPath", fullPath)) // rr.taoLogger.Log.Debug("RecipeRouter.getImageOfProductCode", zap.Any("fullPath", fullPath))
// check if image file exists // check if image file exists
if _, err := os.Stat(fullPath); os.IsNotExist(err) { // if _, err := os.Stat(fullPath); os.IsNotExist(err) {
http.Error(w, "Image not found", http.StatusNotFound) // http.Error(w, "Image not found", http.StatusNotFound)
return // return
} // }
// TODO: get from cache // TODO: get from cache
var imgResult image.Image var imgResult image.Image
// try cache // 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 { if err != nil {
// read image rr.taoLogger.Log.Debug("ImgGetFromFastErr", zap.Any("Error", err.Error()))
imgFile, err := os.Open(fullPath) http.Error(w, err.Error(), http.StatusNotFound)
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("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 // write image
png.Encode(w, imgResult) png.Encode(w, imgResult)
} }

View file

@ -3,9 +3,7 @@ package logger
import ( import (
"os" "os"
"recipe-manager/config" "recipe-manager/config"
"time"
"github.com/natefinch/lumberjack"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
@ -22,36 +20,32 @@ func (tl *TaoLogger) initConfig() *zap.Logger {
return lvl >= zap.InfoLevel || tl.enableDebug return lvl >= zap.InfoLevel || tl.enableDebug
}) })
logCore := zapcore.NewTee( productionConfig := zap.NewProductionEncoderConfig()
zapcore.NewCore(zapcore.NewJSONEncoder(zapcore.EncoderConfig{ productionConfig.EncodeTime = zapcore.ISO8601TimeEncoder
TimeKey: "timestamp",
LevelKey: "level", // fileEncoder := zapcore.NewJSONEncoder(productionConfig)
NameKey: "logger", consoleEncoder := zapcore.NewConsoleEncoder(productionConfig)
CallerKey: "caller",
MessageKey: "message", // logCore := zapcore.NewTee(
StacktraceKey: "error", // zapcore.NewCore(zapcore.NewJSONEncoder(productionConfig),
EncodeLevel: zapcore.LowercaseLevelEncoder, // zapcore.NewCore(zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
EncodeTime: zapcore.TimeEncoderOfLayout(time.DateTime), // TimeKey: "timestamp",
}), zapcore.AddSync(&lumberjack.Logger{ // LevelKey: "level",
Filename: "services/logger/serverlog.log", // NameKey: "logger",
MaxSize: 5, // megabytes // CallerKey: "caller",
MaxAge: 28, //days // MessageKey: "message",
LocalTime: true, // StacktraceKey: "error",
Compress: true, // EncodeLevel: zapcore.CapitalColorLevelEncoder,
}), enableDebugMode), // EncodeTime: zapcore.ISO8601TimeEncoder,
zapcore.NewCore(zapcore.NewConsoleEncoder(zapcore.EncoderConfig{ // }), zapcore.AddSync(os.Stdout), enableDebugMode),
TimeKey: "timestamp", // )
LevelKey: "level",
NameKey: "logger", core := zapcore.NewTee(
CallerKey: "caller", // zapcore.NewCore(fileEncoder, zapcore.AddSync(), enableDebugMode),
MessageKey: "message", zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), enableDebugMode),
StacktraceKey: "error",
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
}), zapcore.AddSync(os.Stdout), enableDebugMode),
) )
return zap.New(logCore) return zap.New(core)
} }
func NewTaoLogger(cfg *config.ServerConfig) *TaoLogger { func NewTaoLogger(cfg *config.ServerConfig) *TaoLogger {