diff --git a/go.mod b/go.mod
index 403bf49..58241cd 100644
--- a/go.mod
+++ b/go.mod
@@ -3,10 +3,21 @@ module git.andr3h3nriqu3s.com/andr3/fyp
go 1.20
require (
+ github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
+ github.com/charmbracelet/lipgloss v0.8.0 // indirect
+ github.com/charmbracelet/log v0.2.5 // indirect
github.com/galeone/tensorflow/tensorflow/go v0.0.0-20221023090153-6b7fa0680c3e // indirect
github.com/galeone/tfgo v0.0.0-20230715013254-16113111dc99 // indirect
+ github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/lib/pq v1.10.9 // indirect
+ github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
+ github.com/mattn/go-isatty v0.0.18 // indirect
+ github.com/mattn/go-runewidth v0.0.14 // indirect
+ github.com/muesli/reflow v0.3.0 // indirect
+ github.com/muesli/termenv v0.15.2 // indirect
+ github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
+ golang.org/x/sys v0.12.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
diff --git a/go.sum b/go.sum
index 37aced0..1202f51 100644
--- a/go.sum
+++ b/go.sum
@@ -1,15 +1,40 @@
+github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
+github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
+github.com/charmbracelet/lipgloss v0.8.0 h1:IS00fk4XAHcf8uZKc3eHeMUTCxUH6NkaTrdyCQk84RU=
+github.com/charmbracelet/lipgloss v0.8.0/go.mod h1:p4eYUZZJ/0oXTuCQKFF8mqyKCz0ja6y+7DniDDw5KKU=
+github.com/charmbracelet/log v0.2.5 h1:1yVvyKCKVV639RR4LIq1iy1Cs1AKxuNO+Hx2LJtk7Wc=
+github.com/charmbracelet/log v0.2.5/go.mod h1:nQGK8tvc4pS9cvVEH/pWJiZ50eUq1aoXUOjGpXvdD0k=
github.com/galeone/tensorflow/tensorflow/go v0.0.0-20221023090153-6b7fa0680c3e h1:9+2AEFZymTi25FIIcDwuzcOPH04z9+fV6XeLiGORPDI=
github.com/galeone/tensorflow/tensorflow/go v0.0.0-20221023090153-6b7fa0680c3e/go.mod h1:TelZuq26kz2jysARBwOrTv16629hyUsHmIoj54QqyFo=
github.com/galeone/tfgo v0.0.0-20230715013254-16113111dc99 h1:8Bt1P/zy1gb37L4n8CGgp1qmFwBV5729kxVfj0sqhJk=
github.com/galeone/tfgo v0.0.0-20230715013254-16113111dc99/go.mod h1:3YgYBeIX42t83uP27Bd4bSMxTnQhSbxl0pYSkCDB1tc=
+github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
+github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
+github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
+github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
+github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
+github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
+github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
+github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
+github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
+github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
+github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
+github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
+github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
+github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
+github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
+golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
+golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
diff --git a/logic/models/run.go b/logic/models/run.go
index 533df6d..3b51d15 100644
--- a/logic/models/run.go
+++ b/logic/models/run.go
@@ -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))
diff --git a/logic/models/test.go b/logic/models/test.go
new file mode 100644
index 0000000..f3da6b3
--- /dev/null
+++ b/logic/models/test.go
@@ -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
+}
diff --git a/logic/models/utils/types.go b/logic/models/utils/types.go
index 1d6cc32..5c3447c 100644
--- a/logic/models/utils/types.go
+++ b/logic/models/utils/types.go
@@ -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
}
diff --git a/logic/utils/handler.go b/logic/utils/handler.go
index 8020965..5e2e32a 100644
--- a/logic/utils/handler.go
+++ b/logic/utils/handler.go
@@ -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)
diff --git a/views/models/edit.html b/views/models/edit.html
index a1a1857..4b366fa 100644
--- a/views/models/edit.html
+++ b/views/models/edit.html
@@ -316,6 +316,11 @@
Image File
+ {{ if .ImageError }}
+
+ The provided image was not valid for this model
+
+ {{ end }}