184 lines
4.2 KiB
Go
184 lines
4.2 KiB
Go
package models
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"image"
|
|
_ "image/png"
|
|
"image/color"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
|
|
)
|
|
|
|
func loadBaseImage(handle *Handle, id string) {
|
|
// TODO handle more types than png
|
|
infile, err := os.Open(path.Join("savedData", id, "baseimage.png"))
|
|
if err != nil {
|
|
// TODO better logging
|
|
fmt.Println(err)
|
|
fmt.Printf("Failed to read image for model with id %s\n", id)
|
|
modelUpdateStatus(handle, id, -1)
|
|
return
|
|
}
|
|
defer infile.Close()
|
|
|
|
src, format, err := image.Decode(infile)
|
|
if err != nil {
|
|
// TODO better logging
|
|
fmt.Println(err)
|
|
fmt.Printf("Failed to load image for model with id %s\n", id)
|
|
modelUpdateStatus(handle, id, -1)
|
|
return
|
|
}
|
|
if format != "png" {
|
|
// TODO better logging
|
|
fmt.Printf("Found unkown format '%s'\n", format)
|
|
panic("Handle diferent files than .png")
|
|
}
|
|
|
|
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:
|
|
fmt.Println("Do not know how to handle this color model")
|
|
|
|
if src.ColorModel() == color.RGBA64Model {
|
|
fmt.Println("Color is rgb")
|
|
} else if src.ColorModel() == color.NRGBAModel {
|
|
fmt.Println("Color is nrgb")
|
|
} else if src.ColorModel() == color.YCbCrModel {
|
|
fmt.Println("Color is ycbcr")
|
|
} else if src.ColorModel() == color.AlphaModel {
|
|
fmt.Println("Color is alpha")
|
|
} else if src.ColorModel() == color.CMYKModel {
|
|
fmt.Println("Color is cmyk")
|
|
} else {
|
|
fmt.Println("Other so assuming color")
|
|
}
|
|
|
|
modelUpdateStatus(handle, id, -1)
|
|
return
|
|
}
|
|
|
|
// Note: this also updates the status to 2
|
|
_, err = handle.Db.Exec("update models set width=$1, height=$2, color_mode=$3, status=$4 where id=$5", width, height, model_color, CONFIRM_PRE_TRAINING, id)
|
|
if err != nil {
|
|
// TODO better logging
|
|
fmt.Println(err)
|
|
fmt.Printf("Could not update model\n")
|
|
modelUpdateStatus(handle, id, -1)
|
|
return
|
|
}
|
|
}
|
|
|
|
func handleAdd(handle *Handle) {
|
|
handle.GetHTML("/models/add", AnswerTemplate("models/add.html", nil, 1))
|
|
// TODO json
|
|
handle.Post("/models/add", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
|
if c.Mode == JSON {
|
|
panic("TODO JSON")
|
|
}
|
|
if !CheckAuthLevel(1, w, r, c) {
|
|
return nil
|
|
}
|
|
|
|
read_form, err := r.MultipartReader()
|
|
if err != nil {
|
|
LoadBasedOnAnswer(c.Mode, w, "models/add.html", c.AddMap(nil))
|
|
return nil
|
|
}
|
|
|
|
var name string
|
|
var file []byte
|
|
|
|
for {
|
|
part, err_part := read_form.NextPart()
|
|
if err_part == io.EOF {
|
|
break
|
|
} else if err_part != nil {
|
|
return &Error{Code: http.StatusBadRequest}
|
|
}
|
|
if part.FormName() == "name" {
|
|
buf := new(bytes.Buffer)
|
|
buf.ReadFrom(part)
|
|
name = buf.String()
|
|
}
|
|
if part.FormName() == "file" {
|
|
buf := new(bytes.Buffer)
|
|
buf.ReadFrom(part)
|
|
file = buf.Bytes()
|
|
}
|
|
}
|
|
|
|
if name == "" || len(file) == 0 {
|
|
LoadBasedOnAnswer(c.Mode, w, "models/add.html", c.AddMap(nil))
|
|
return nil
|
|
}
|
|
|
|
row, err := handle.Db.Query("select id from models where name=$1 and user_id=$2;", name, c.User.Id)
|
|
if err != nil {
|
|
return Error500(err)
|
|
}
|
|
|
|
if row.Next() {
|
|
LoadBasedOnAnswer(c.Mode, w, "models/add.html", c.AddMap(AnyMap{
|
|
"NameFoundError": true,
|
|
"Name": name,
|
|
}))
|
|
return nil
|
|
}
|
|
|
|
_, err = handle.Db.Exec("insert into models (user_id, name) values ($1, $2)", c.User.Id, name)
|
|
if err != nil {
|
|
return Error500(err)
|
|
}
|
|
|
|
row, err = handle.Db.Query("select id from models where name=$1 and user_id=$2;", name, c.User.Id)
|
|
if err != nil {
|
|
return Error500(err)
|
|
}
|
|
|
|
if !row.Next() {
|
|
return &Error{Code: http.StatusInternalServerError}
|
|
}
|
|
|
|
var id string
|
|
err = row.Scan(&id)
|
|
if err != nil {
|
|
return Error500(err)
|
|
}
|
|
|
|
// TODO mk this path configurable
|
|
dir_path := path.Join("savedData", id)
|
|
|
|
err = os.Mkdir(dir_path, os.ModePerm)
|
|
if err != nil {
|
|
return Error500(err)
|
|
}
|
|
f, err := os.Create(path.Join(dir_path, "baseimage.png"))
|
|
if err != nil {
|
|
return Error500(err)
|
|
}
|
|
defer f.Close()
|
|
|
|
f.Write(file)
|
|
|
|
fmt.Printf("Created model with id %s! Started to proccess image!\n", id)
|
|
go loadBaseImage(handle, id)
|
|
|
|
Redirect("/models/edit?id="+id, c.Mode, w, r)
|
|
return nil
|
|
})
|
|
}
|