MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / handleDeleteAttachment

Method handleDeleteAttachment

rest/doc_api.go:484–534  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

482}
483
484func (h *handler) handleDeleteAttachment() error {
485 if h.isReadOnlyGuest() {
486 return base.HTTPErrorf(http.StatusForbidden, auth.GuestUserReadOnly)
487 }
488
489 docid := h.PathVar("docid")
490 attachmentName := h.PathVar("attach")
491
492 occValue, _, err := h.getOCCValue(nil)
493 if err != nil {
494 return err
495 }
496
497 body, err := h.collection.Get1xRevBody(h.ctx(), docid, occValue, false, nil)
498 if err != nil {
499 if base.IsDocNotFoundError(err) {
500 // Check here if error is relating to incorrect revid, if so return 409 code else return 404 code
501 if strings.Contains(err.Error(), "404 missing") {
502 return base.HTTPErrorf(http.StatusConflict, "Incorrect revision ID specified")
503 }
504 // Need to return an error if a document is not found
505 return base.HTTPErrorf(http.StatusNotFound, "Document specified is not found")
506 } else if err != nil {
507 return err
508 }
509 } else if body != nil {
510 if occValue == "" {
511 // If a revid is not specified and an active revision was found,
512 // return a conflict now, rather than letting db.Put do it further down...
513 return base.HTTPErrorf(http.StatusConflict, "Cannot modify attachments without a specific rev ID")
514 }
515 }
516
517 // get document attachments and check if attachment exists
518 attachments := db.GetBodyAttachments(body)
519 if _, ok := attachments[attachmentName]; !ok {
520 return base.HTTPErrorf(http.StatusNotFound, "Attachment %s is not found", attachmentName)
521 }
522 // delete specified attachment from the map
523 delete(attachments, attachmentName)
524 body[db.BodyAttachments] = attachments
525
526 newRev, doc, err := h.collection.Put(h.ctx(), docid, body)
527 if err != nil {
528 return err
529 }
530 h.setEtag(newRev)
531
532 h.writeRawJSONStatus(http.StatusOK, []byte(`{"id":`+base.ConvertToJSONString(docid)+`,"ok":true,"rev":"`+newRev+`","cv":"`+doc.HLV.GetCurrentVersionString()+`"}`))
533 return nil
534}
535
536// occVersionType is a type used to represent the type of document version in optimistic concurrency control (OCC). Can be Revision Tree ID or a CV.
537type occVersionType uint8

Callers

nothing calls this directly

Calls 15

isReadOnlyGuestMethod · 0.95
PathVarMethod · 0.95
getOCCValueMethod · 0.95
ctxMethod · 0.95
setEtagMethod · 0.95
writeRawJSONStatusMethod · 0.95
HTTPErrorfFunction · 0.92
IsDocNotFoundErrorFunction · 0.92
GetBodyAttachmentsFunction · 0.92
ConvertToJSONStringFunction · 0.92
Get1xRevBodyMethod · 0.80

Tested by

no test coverage detected