// PUT: /node/{id} -> multipart-form func (cr *NodeController) Update(id string, cx *goweb.Context) { // Log Request and check for Auth LogRequest(cx.Request) u, err := AuthenticateRequest(cx.Request) if err != nil { // No Auth is not damning. Other errors are probably a dead db connection if err.Error() != e.NoAuth { if err.Error() == e.MongoDocNotFound { cx.RespondWithErrorMessage("Invalid username or password", http.StatusBadRequest) return } else { fmt.Println("Error at Auth:", err.Error()) cx.RespondWithError(http.StatusInternalServerError) return } } } // Gather query params query := &Query{list: cx.Request.URL.Query()} // Fake public user if u == nil { u = &user.User{Uuid: ""} } node, err := store.LoadNode(id, u.Uuid) if err != nil { if err.Error() == e.UnAuth { fmt.Println("Unauthorized") cx.RespondWithError(http.StatusUnauthorized) return } else if err.Error() == e.MongoDocNotFound { cx.RespondWithNotFound() return } else { // In theory the db connection could be lost between // checking user and load but seems unlikely. fmt.Println("Err@node_Update:LoadNode:", err.Error()) cx.RespondWithError(http.StatusInternalServerError) return } } if query.Has("index") { if !node.HasFile() { cx.RespondWithErrorMessage("node file empty", http.StatusBadRequest) return } newIndexer := indexer.Indexer(query.Value("index")) f, _ := os.Open(node.DataPath()) defer f.Close() idxer := newIndexer(f) err := idxer.Create() if err != nil { fmt.Println(err.Error()) } err = idxer.Dump(node.IndexPath() + "/record") if err != nil { cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) return } else { cx.RespondWithOK() return } } else { params, files, err := ParseMultipartForm(cx.Request) if err != nil { fmt.Println("err", err.Error()) cx.RespondWithError(http.StatusBadRequest) return } err = node.Update(params, files) if err != nil { errors := []string{"node file already set and is immutable", "node file immutable", "node attributes immutable", "node part already exists and is immutable"} for e := range errors { if err.Error() == errors[e] { cx.RespondWithErrorMessage(err.Error(), http.StatusBadRequest) return } } fmt.Println("err", err.Error()) cx.RespondWithError(http.StatusBadRequest) return } cx.RespondWithData(node) } return }