2023-09-22 19:22:36 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
model_classes "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/classes"
|
2023-09-26 20:15:28 +01:00
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
|
2023-10-06 12:13:19 +01:00
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
|
2023-09-22 19:22:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func InsertIfNotPresent(ss []string, s string) []string {
|
2023-10-06 12:13:19 +01:00
|
|
|
i := sort.SearchStrings(ss, s)
|
|
|
|
if len(ss) > i && ss[i] == s {
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
ss = append(ss, "")
|
|
|
|
copy(ss[i+1:], ss[i:])
|
|
|
|
ss[i] = s
|
|
|
|
return ss
|
2023-09-22 19:22:36 +01:00
|
|
|
}
|
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
func processZipFile(c *Context, model *BaseModel) {
|
|
|
|
reader, err := zip.OpenReader(path.Join("savedData", model.Id, "base_data.zip"))
|
|
|
|
if err != nil {
|
|
|
|
// TODO add msg to error
|
|
|
|
ModelUpdateStatus(c, model.Id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
fmt.Printf("Faield to proccess zip file failed to open reader\n")
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
training := []string{}
|
|
|
|
testing := []string{}
|
|
|
|
|
|
|
|
for _, file := range reader.Reader.File {
|
|
|
|
|
|
|
|
paths := strings.Split(file.Name, "/")
|
|
|
|
|
|
|
|
if paths[1] == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if paths[0] != "training" && paths[0] != "testing" {
|
|
|
|
fmt.Printf("Invalid file '%s' TODO add msg to response!!!\n", file.Name)
|
|
|
|
ModelUpdateStatus(c, model.Id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if paths[0] != "training" {
|
|
|
|
training = InsertIfNotPresent(training, paths[1])
|
|
|
|
} else if paths[0] != "testing" {
|
|
|
|
testing = InsertIfNotPresent(testing, paths[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(testing, training) {
|
|
|
|
fmt.Printf("testing and training are diferent\n")
|
|
|
|
fmt.Println(testing)
|
|
|
|
fmt.Println(training)
|
|
|
|
ModelUpdateStatus(c, model.Id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
base_path := path.Join("savedData", model.Id, "data")
|
|
|
|
if err = os.MkdirAll(base_path, os.ModePerm); err != nil {
|
|
|
|
fmt.Printf("Failed to create base_path dir\n")
|
|
|
|
ModelUpdateStatus(c, model.Id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := map[string]string{}
|
|
|
|
|
|
|
|
for i, name := range training {
|
|
|
|
id, err := model_classes.CreateClass(c.Db, model.Id, i, name)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to create class '%s' on db\n", name)
|
|
|
|
ModelUpdateStatus(c, id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ids[name] = id
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range reader.Reader.File {
|
|
|
|
if file.Name[len(file.Name)-1] == '/' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := reader.Open(file.Name)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Could not open file in zip %s\n", file.Name)
|
|
|
|
ModelUpdateStatus(c, model.Id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer data.Close()
|
|
|
|
file_data, err := io.ReadAll(data)
|
2023-09-22 19:22:36 +01:00
|
|
|
if err != nil {
|
2023-10-06 12:13:19 +01:00
|
|
|
fmt.Printf("Could not read file file in zip %s\n", file.Name)
|
|
|
|
ModelUpdateStatus(c, model.Id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check if the file is a valid photo that matched the defined photo on the database
|
|
|
|
|
|
|
|
parts := strings.Split(file.Name, "/")
|
|
|
|
|
|
|
|
mode := model_classes.DATA_POINT_MODE_TRAINING
|
|
|
|
if parts[0] == "testing" {
|
|
|
|
mode = model_classes.DATA_POINT_MODE_TESTING
|
2023-09-22 19:22:36 +01:00
|
|
|
}
|
2023-10-06 12:13:19 +01:00
|
|
|
|
|
|
|
data_point_id, err := model_classes.AddDataPoint(c.Db, ids[parts[1]], "id://", mode)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to add data point for %s\n", model.Id)
|
|
|
|
fmt.Println(err)
|
|
|
|
ModelUpdateStatus(c, model.Id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-01 23:03:25 +00:00
|
|
|
file_path := path.Join(base_path, data_point_id+"."+model.Format)
|
2023-10-02 21:15:31 +01:00
|
|
|
f, err := os.Create(file_path)
|
|
|
|
if err != nil {
|
2023-10-06 12:13:19 +01:00
|
|
|
fmt.Printf("Could not create file %s\n", file_path)
|
|
|
|
ModelUpdateStatus(c, model.Id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
return
|
2023-10-02 21:15:31 +01:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
f.Write(file_data)
|
2023-10-06 12:13:19 +01:00
|
|
|
|
|
|
|
if !testImgForModel(c, model, file_path) {
|
2024-03-01 23:03:25 +00:00
|
|
|
c.Logger.Errorf("Image did not have valid format for model %s (in zip: %s)!", file_path, file.Name)
|
|
|
|
c.Logger.Warn("Not failling updating data point to status -1")
|
|
|
|
message := "Image did not have valid format for the model"
|
|
|
|
if err = model_classes.UpdateDataPointStatus(c.Db, data_point_id, -1, &message); err != nil {
|
|
|
|
c.Logger.Error("Failed to update data point status")
|
|
|
|
ModelUpdateStatus(c, model.Id, FAILED_PREPARING_ZIP_FILE)
|
|
|
|
}
|
2023-10-06 12:13:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Added data to model '%s'!\n", model.Id)
|
|
|
|
ModelUpdateStatus(c, model.Id, CONFIRM_PRE_TRAINING)
|
2023-09-22 19:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleDataUpload(handle *Handle) {
|
|
|
|
handle.Post("/models/data/upload", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
2023-10-06 12:13:19 +01:00
|
|
|
if !CheckAuthLevel(1, w, r, c) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-01 23:03:25 +00:00
|
|
|
|
2023-09-22 19:22:36 +01:00
|
|
|
if c.Mode == JSON {
|
2024-03-01 23:03:25 +00:00
|
|
|
read_form, err := r.MultipartReader()
|
|
|
|
if err != nil {
|
|
|
|
return c.JsonBadRequest("Please provide a valid form data request!")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id string
|
|
|
|
var file []byte
|
|
|
|
|
|
|
|
for {
|
|
|
|
part, err_part := read_form.NextPart()
|
|
|
|
if err_part == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err_part != nil {
|
|
|
|
return c.JsonBadRequest("Please provide a valid form data request!")
|
|
|
|
}
|
|
|
|
if part.FormName() == "id" {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(part)
|
|
|
|
id = buf.String()
|
|
|
|
}
|
|
|
|
if part.FormName() == "file" {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(part)
|
|
|
|
file = buf.Bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
model, err := GetBaseModel(handle.Db, id)
|
|
|
|
if err == ModelNotFoundError {
|
|
|
|
return c.SendJSONStatus(http.StatusNotFound, "Model not found")
|
|
|
|
} else if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO mk this path configurable
|
|
|
|
dir_path := path.Join("savedData", id)
|
|
|
|
|
|
|
|
f, err := os.Create(path.Join(dir_path, "base_data.zip"))
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
f.Write(file)
|
|
|
|
|
|
|
|
ModelUpdateStatus(c, id, PREPARING_ZIP_FILE)
|
|
|
|
|
|
|
|
go processZipFile(c, model)
|
|
|
|
|
|
|
|
return c.SendJSON(model.Id)
|
2023-09-22 19:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
read_form, err := r.MultipartReader()
|
|
|
|
if err != nil {
|
|
|
|
LoadBasedOnAnswer(c.Mode, w, "models/add.html", c.AddMap(nil))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var id 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() == "id" {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(part)
|
|
|
|
id = buf.String()
|
|
|
|
}
|
|
|
|
if part.FormName() == "file" {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(part)
|
|
|
|
file = buf.Bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
model, err := GetBaseModel(handle.Db, id)
|
|
|
|
if err == ModelNotFoundError {
|
|
|
|
return c.ErrorCode(nil, http.StatusNotFound, AnyMap{
|
2023-09-22 19:22:36 +01:00
|
|
|
"NotFoundMessage": "Model not found",
|
|
|
|
"GoBackLink": "/models",
|
|
|
|
})
|
2023-10-06 12:13:19 +01:00
|
|
|
} else if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
2023-09-22 19:22:36 +01:00
|
|
|
|
|
|
|
// TODO mk this path configurable
|
|
|
|
dir_path := path.Join("savedData", id)
|
|
|
|
|
|
|
|
f, err := os.Create(path.Join(dir_path, "base_data.zip"))
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
f.Write(file)
|
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
ModelUpdateStatus(c, id, PREPARING_ZIP_FILE)
|
2023-09-22 19:22:36 +01:00
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
go processZipFile(c, model)
|
2023-09-22 19:22:36 +01:00
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
Redirect("/models/edit?id="+id, c.Mode, w, r)
|
2023-09-22 19:22:36 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
handle.Delete("/models/data/delete-zip-file", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
|
|
|
if !CheckAuthLevel(1, w, r, c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if c.Mode == JSON {
|
2024-03-01 23:03:25 +00:00
|
|
|
|
|
|
|
type ModelData struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var dat ModelData
|
|
|
|
|
|
|
|
if err := c.ToJSON(r, &dat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
model, err := GetBaseModel(handle.Db, dat.Id)
|
|
|
|
if err == ModelNotFoundError {
|
|
|
|
return c.SendJSONStatus(http.StatusNotFound, "Model not found");
|
|
|
|
} else if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if model.Status != FAILED_PREPARING_ZIP_FILE {
|
|
|
|
return c.SendJSONStatus(http.StatusNotFound, "Model not in the correct status")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(path.Join("savedData", model.Id, "base_data.zip"))
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(path.Join("savedData", model.Id, "data"))
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = handle.Db.Exec("delete from model_classes where model_id=$1;", model.Id)
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelUpdateStatus(c, model.Id, CONFIRM_PRE_TRAINING)
|
|
|
|
return c.SendJSON(model.Id)
|
2023-10-06 12:13:19 +01:00
|
|
|
}
|
2023-09-22 19:22:36 +01:00
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
f, err := MyParseForm(r)
|
|
|
|
if err != nil {
|
|
|
|
return ErrorCode(err, 400, c.AddMap(nil))
|
|
|
|
}
|
2023-09-22 19:22:36 +01:00
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
if !CheckId(f, "id") {
|
|
|
|
return ErrorCode(err, 400, c.AddMap(nil))
|
|
|
|
}
|
2023-09-22 19:22:36 +01:00
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
id := f.Get("id")
|
2023-09-22 19:22:36 +01:00
|
|
|
|
2023-10-06 12:13:19 +01:00
|
|
|
model, err := GetBaseModel(handle.Db, id)
|
|
|
|
if err == ModelNotFoundError {
|
2023-09-22 19:22:36 +01:00
|
|
|
return ErrorCode(nil, http.StatusNotFound, AnyMap{
|
|
|
|
"NotFoundMessage": "Model not found",
|
|
|
|
"GoBackLink": "/models",
|
|
|
|
})
|
2023-10-06 12:13:19 +01:00
|
|
|
} else if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if model.Status != FAILED_PREPARING_ZIP_FILE {
|
|
|
|
// TODO add message
|
|
|
|
return ErrorCode(nil, 400, c.AddMap(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(path.Join("savedData", id, "base_data.zip"))
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(path.Join("savedData", id, "data"))
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = handle.Db.Exec("delete from model_classes where model_id=$1;", id)
|
|
|
|
if err != nil {
|
|
|
|
return Error500(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelUpdateStatus(c, id, CONFIRM_PRE_TRAINING)
|
|
|
|
Redirect("/models/edit?id="+id, c.Mode, w, r)
|
|
|
|
return nil
|
|
|
|
})
|
2023-09-22 19:22:36 +01:00
|
|
|
}
|