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

Method handlePutDocReplicator2

rest/doc_api.go:667–742  ·  view source on GitHub ↗
(docid string, roundTrip bool)

Source from the content-addressed store, hash-verified

665}
666
667func (h *handler) handlePutDocReplicator2(docid string, roundTrip bool) (err error) {
668 if !base.IsEnterpriseEdition() {
669 return base.HTTPErrorf(http.StatusNotImplemented, "replicator2 endpoints are only supported in EE")
670 }
671 if h.isReadOnlyGuest() {
672 return base.HTTPErrorf(http.StatusForbidden, auth.GuestUserReadOnly)
673 }
674
675 bodyBytes, err := h.readBody()
676 if err != nil {
677 return err
678 }
679 if bodyBytes == nil || len(bodyBytes) == 0 {
680 return base.ErrEmptyDocument
681 }
682
683 newDoc := &db.Document{
684 ID: docid,
685 }
686 newDoc.UpdateBodyBytes(bodyBytes)
687
688 var parentRev string
689 if oldRev := h.getQuery("rev"); oldRev != "" {
690 parentRev = oldRev
691 } else if ifMatch, _ := h.getEtag("If-Match"); ifMatch != "" {
692 parentRev = ifMatch
693 }
694
695 generation, _ := db.ParseRevID(h.ctx(), parentRev)
696 generation++
697
698 deleted, _ := h.getOptBoolQuery("deleted", false)
699 newDoc.Deleted = deleted
700
701 newDoc.RevID = db.CreateRevIDWithBytes(generation, parentRev, bodyBytes)
702 history := []string{newDoc.RevID}
703
704 if parentRev != "" {
705 history = append(history, parentRev)
706 }
707
708 // Handle and pull out expiry
709 if bytes.Contains(bodyBytes, []byte(db.BodyExpiry)) {
710 body := newDoc.Body(h.ctx())
711 expiry, err := body.ExtractExpiry()
712 if err != nil {
713 return base.HTTPErrorf(http.StatusBadRequest, "Invalid expiry: %v", err)
714 }
715 newDoc.DocExpiry = expiry
716 newDoc.UpdateBody(body)
717 }
718
719 // Pull out attachments
720 if bytes.Contains(bodyBytes, []byte(db.BodyAttachments)) {
721 body := newDoc.Body(h.ctx())
722
723 newDoc.SetAttachments(db.GetBodyAttachments(body))
724 delete(body, db.BodyAttachments)

Callers 1

handlePutDocMethod · 0.95

Calls 15

isReadOnlyGuestMethod · 0.95
readBodyMethod · 0.95
UpdateBodyBytesMethod · 0.95
getQueryMethod · 0.95
getEtagMethod · 0.95
ctxMethod · 0.95
getOptBoolQueryMethod · 0.95
BodyMethod · 0.95
UpdateBodyMethod · 0.95
SetAttachmentsMethod · 0.95
writeRawJSONStatusMethod · 0.95
IsEnterpriseEditionFunction · 0.92

Tested by

no test coverage detected