POST: /node
(ctx context.Context)
| 21 | |
| 22 | // POST: /node |
| 23 | func (cr *NodeController) Create(ctx context.Context) error { |
| 24 | u, err := request.Authenticate(ctx.HttpRequest()) |
| 25 | if err != nil && err.Error() != e.NoAuth { |
| 26 | return request.AuthError(err, ctx) |
| 27 | } |
| 28 | |
| 29 | // public user |
| 30 | if u == nil { |
| 31 | if conf.ANON_WRITE { |
| 32 | u = &user.User{Uuid: "public"} |
| 33 | } else { |
| 34 | return responder.RespondWithError(ctx, http.StatusUnauthorized, e.NoAuth) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Parse uploaded form |
| 39 | // all POSTed files writen to temp dir |
| 40 | params, files, err := request.ParseMultipartForm(ctx.HttpRequest()) |
| 41 | // clean up temp dir !! |
| 42 | defer file.RemoveAllFormFiles(files) |
| 43 | if err != nil { |
| 44 | if err.Error() == "request Content-Type isn't multipart/form-data" { |
| 45 | // If not multipart/form-data it will try to read the Body of the |
| 46 | // request. If the Body is not empty it will create a file from |
| 47 | // the Body contents. If the Body is empty it will create an empty |
| 48 | // node. |
| 49 | if ctx.HttpRequest().ContentLength != 0 { |
| 50 | params, files, err = request.DataUpload(ctx.HttpRequest()) |
| 51 | if err != nil { |
| 52 | err_msg := "err@node_Create: (request.DataUpload) " + err.Error() |
| 53 | logger.Error(err_msg) |
| 54 | return responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | n, cn_err := node.CreateNodeUpload(u, params, files) |
| 59 | |
| 60 | if cn_err != nil { |
| 61 | err_msg := "err@node_Create: (node.CreateNodeUpload) " + cn_err.Error() |
| 62 | logger.Error(err_msg) |
| 63 | return responder.RespondWithError(ctx, http.StatusInternalServerError, err_msg) |
| 64 | } |
| 65 | if n == nil { |
| 66 | // Not sure how you could get an empty node with no error |
| 67 | // Assume it's the user's fault |
| 68 | err_msg := "err@node_Create: could not create node" |
| 69 | logger.Error(err_msg) |
| 70 | return responder.RespondWithError(ctx, http.StatusBadRequest, err_msg) |
| 71 | } else { |
| 72 | return responder.RespondWithData(ctx, n) |
| 73 | } |
| 74 | } else { |
| 75 | // Some error other than request encoding. Theoretically |
| 76 | // could be a lost db connection between user lookup and parsing. |
| 77 | // Blame the user, Its probaby their fault anyway. |
| 78 | err_msg := "err@node_Create: unable to parse form: " + err.Error() |
| 79 | logger.Error(err_msg) |
| 80 | return responder.RespondWithError(ctx, http.StatusBadRequest, err_msg) |