PUT: /node/{id} -> multipart-form
(id string, ctx context.Context)
| 18 | |
| 19 | // PUT: /node/{id} -> multipart-form |
| 20 | func (cr *NodeController) Replace(id string, ctx context.Context) (err error) { |
| 21 | u, err := request.Authenticate(ctx.HttpRequest()) |
| 22 | if err != nil && err.Error() != e.NoAuth { |
| 23 | return request.AuthError(err, ctx) |
| 24 | } |
| 25 | |
| 26 | // public user (no auth) can be used in some cases |
| 27 | if u == nil { |
| 28 | if conf.ANON_WRITE { |
| 29 | u = &user.User{Uuid: "public"} |
| 30 | } else { |
| 31 | return responder.RespondWithError(ctx, http.StatusUnauthorized, e.NoAuth) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // lock before loading |
| 36 | err = locker.NodeLockMgr.LockNode(id) |
| 37 | if err != nil { |
| 38 | err_msg := "err@node_Update: (LockMgr.LockNode) id=" + id + ": " + err.Error() |
| 39 | logger.Error(err_msg) |
| 40 | return responder.RespondWithError(ctx, http.StatusBadRequest, err_msg) |
| 41 | } |
| 42 | defer locker.NodeLockMgr.UnlockNode(id) |
| 43 | |
| 44 | // Load node by id |
| 45 | n, err := node.Load(id) |
| 46 | if err != nil { |
| 47 | if err == mgo.ErrNotFound { |
| 48 | logger.Error("err@node_Update: (node.Load) id=" + id + ": " + e.NodeNotFound) |
| 49 | return responder.RespondWithError(ctx, http.StatusNotFound, e.NodeNotFound) |
| 50 | } else { |
| 51 | // In theory the db connection could be lost between |
| 52 | // checking user and load but seems unlikely. |
| 53 | err_msg := "err@node_Update: (node.Load) " + id + ": " + err.Error() |
| 54 | logger.Error(err_msg) |
| 55 | return responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | rights := n.Acl.Check(u.Uuid) |
| 60 | prights := n.Acl.Check("public") |
| 61 | if rights["write"] == false && u.Admin == false && n.Acl.Owner != u.Uuid && prights["write"] == false { |
| 62 | logger.Error("err@node_Update: (Authenticate) id=" + id + ": " + e.UnAuth) |
| 63 | return responder.RespondWithError(ctx, http.StatusUnauthorized, e.UnAuth) |
| 64 | } |
| 65 | |
| 66 | if conf.LOG_PERF { |
| 67 | logger.Perf("START PUT data: " + id) |
| 68 | } |
| 69 | params, files, err := request.ParseMultipartForm(ctx.HttpRequest()) |
| 70 | // clean up temp dir !! |
| 71 | defer file.RemoveAllFormFiles(files) |
| 72 | if err != nil { |
| 73 | err_msg := "err@node_Update: (ParseMultipartForm) id=" + id + ": " + err.Error() |
| 74 | logger.Error(err_msg) |
| 75 | return responder.RespondWithError(ctx, http.StatusBadRequest, err_msg) |
| 76 | } |
| 77 |