chore: added logger and closes #33
This commit is contained in:
@@ -103,11 +103,21 @@ func handleRun(handle *Handle) {
|
||||
|
||||
img_file, err := os.Create(path.Join(run_path, "img.png"))
|
||||
if err != nil {
|
||||
return Error500(nil)
|
||||
return Error500(err)
|
||||
}
|
||||
defer img_file.Close()
|
||||
img_file.Write(file)
|
||||
|
||||
if !testImgForModel(c, model, path.Join(run_path, "img.png")) {
|
||||
LoadDefineTemplate(w, "/models/edit.html", "run-model-card", c.AddMap(AnyMap{
|
||||
"Model": model,
|
||||
"NotFound": false,
|
||||
"Result": nil,
|
||||
"ImageError": true,
|
||||
}))
|
||||
return nil
|
||||
}
|
||||
|
||||
root := tg.NewRoot()
|
||||
|
||||
tf_img := ReadPNG(root, path.Join(run_path, "img.png"), int64(model.ImageMode))
|
||||
|
||||
73
logic/models/test.go
Normal file
73
logic/models/test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
_ "image/png"
|
||||
"os"
|
||||
|
||||
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
|
||||
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
|
||||
)
|
||||
|
||||
func testImgForModel(c *Context, model *BaseModel, path string) (result bool) {
|
||||
result = false
|
||||
|
||||
infile, err := os.Open(path)
|
||||
if err != nil {
|
||||
c.Logger.Errorf("Failed to read image for model with id %s\nErr:%s", model.Id, err)
|
||||
return
|
||||
}
|
||||
defer infile.Close()
|
||||
|
||||
src, format, err := image.Decode(infile)
|
||||
if err != nil {
|
||||
c.Logger.Errorf("Failed to decode image for model with id %s\nErr:%s", model.Id, err)
|
||||
return
|
||||
}
|
||||
if format != "png" {
|
||||
c.Logger.Errorf("Found unkown format '%s' while testing an image\n", format)
|
||||
return
|
||||
}
|
||||
|
||||
var model_color string
|
||||
|
||||
bounds := src.Bounds()
|
||||
width, height := bounds.Max.X, bounds.Max.Y
|
||||
|
||||
switch src.ColorModel() {
|
||||
case color.Gray16Model:
|
||||
fallthrough
|
||||
case color.GrayModel:
|
||||
model_color = "greyscale"
|
||||
default:
|
||||
c.Logger.Error("Do not know how to handle this color model")
|
||||
|
||||
if src.ColorModel() == color.RGBA64Model {
|
||||
c.Logger.Info("Color is rgb")
|
||||
} else if src.ColorModel() == color.NRGBAModel {
|
||||
c.Logger.Info("Color is nrgb")
|
||||
} else if src.ColorModel() == color.YCbCrModel {
|
||||
c.Logger.Info("Color is ycbcr")
|
||||
} else if src.ColorModel() == color.AlphaModel {
|
||||
c.Logger.Info("Color is alpha")
|
||||
} else if src.ColorModel() == color.CMYKModel {
|
||||
c.Logger.Info("Color is cmyk")
|
||||
} else {
|
||||
c.Logger.Info("Other so assuming color")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (StringToImageMode(model_color) != model.ImageMode) {
|
||||
c.Logger.Warn("Color Mode does not match with model color mode", model_color, model.ImageMode)
|
||||
return
|
||||
}
|
||||
|
||||
if height != model.Height || width != model.Width {
|
||||
c.Logger.Warn("Image size does not match model size", width, height, model.Width, model.Height)
|
||||
return
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -48,11 +48,15 @@ func GetBaseModel(db *sql.DB, id string) (base *BaseModel, err error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
base.ImageMode = StringToImageMode(colorMode)
|
||||
return
|
||||
}
|
||||
|
||||
func StringToImageMode(colorMode string) (int){
|
||||
switch colorMode {
|
||||
case "greyscale":
|
||||
base.ImageMode = 1
|
||||
return 1
|
||||
default:
|
||||
panic("unkown color mode")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
@@ -14,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
dbtypes "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
func Mul (n1 int, n2 int) int {
|
||||
@@ -340,6 +340,16 @@ type Context struct {
|
||||
Token *string
|
||||
User *dbtypes.User
|
||||
Mode AnswerType
|
||||
Logger *log.Logger
|
||||
}
|
||||
|
||||
func (c Context) ErrorCode(err error, code int, data AnyMap) *Error {
|
||||
// TODO Improve Logging
|
||||
if err != nil {
|
||||
c.Logger.Errorf("Something went wrong returning with: %d\n.Err:\n", code)
|
||||
c.Logger.Error(err)
|
||||
}
|
||||
return &Error{code, nil, data}
|
||||
}
|
||||
|
||||
func (c Context) AddMap(m AnyMap) AnyMap {
|
||||
@@ -384,7 +394,13 @@ func (x Handle) createContext(mode AnswerType, r *http.Request) (*Context, error
|
||||
return nil, errors.Join(err, LogoffError)
|
||||
}
|
||||
|
||||
return &Context{token, user, mode}, nil
|
||||
logger := log.NewWithOptions(os.Stdout, log.Options{
|
||||
ReportTimestamp: true,
|
||||
TimeFormat: time.Kitchen,
|
||||
Prefix: r.URL.Path,
|
||||
})
|
||||
|
||||
return &Context{token, user, mode, logger}, nil
|
||||
}
|
||||
|
||||
// TODO check if I can use http.Redirect
|
||||
@@ -452,6 +468,7 @@ func (x Handle) StaticFiles(pathTest string, fileType string, contentType string
|
||||
}
|
||||
|
||||
func ErrorCode(err error, code int, data AnyMap) *Error {
|
||||
log.Warn("This function is deprecated please use the one provided by context")
|
||||
// TODO Improve Logging
|
||||
if err != nil {
|
||||
fmt.Printf("Something went wrong returning with: %d\n.Err:\n", code)
|
||||
|
||||
Reference in New Issue
Block a user