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

Method handlePutAttachment

rest/doc_api.go:416–482  ·  view source on GitHub ↗

HTTP handler for a PUT of an attachment

()

Source from the content-addressed store, hash-verified

414
415// HTTP handler for a PUT of an attachment
416func (h *handler) handlePutAttachment() error {
417
418 if h.isReadOnlyGuest() {
419 return base.HTTPErrorf(http.StatusForbidden, auth.GuestUserReadOnly)
420 }
421
422 docid := h.PathVar("docid")
423 attachmentName := h.PathVar("attach")
424 attachmentContentType := h.rq.Header.Get("Content-Type")
425 if attachmentContentType == "" {
426 attachmentContentType = "application/octet-stream"
427 }
428
429 occValue, occValueType, err := h.getOCCValue(nil)
430 if err != nil {
431 return err
432 }
433
434 attachmentData, err := h.readBody()
435 if err != nil {
436 return err
437 }
438
439 body, err := h.collection.Get1xRevBody(h.ctx(), docid, occValue, false, nil)
440 if err != nil {
441 if base.IsDocNotFoundError(err) {
442 bodyKey, err := bodyKeyForOCCVersionType(occValueType)
443 if err != nil {
444 return base.HTTPErrorf(http.StatusBadRequest, "Invalid OCC version type: %v", err)
445 }
446 // couchdb creates empty body on attachment PUT for non-existent doc id
447 body = db.Body{bodyKey: occValue}
448 } else if err != nil {
449 return err
450 }
451 } else if body != nil {
452 if occValue == "" {
453 // If a revid is not specified and an active revision was found,
454 // return a conflict now, rather than letting db.Put do it further down...
455 return base.HTTPErrorf(http.StatusConflict, "Cannot modify attachments without a specific rev ID")
456 }
457 }
458
459 // find attachment (if it existed)
460 attachments := db.GetBodyAttachments(body)
461 if attachments == nil {
462 attachments = make(map[string]interface{})
463 }
464
465 // create new attachment
466 attachment := make(map[string]interface{})
467 attachment["data"] = attachmentData
468 attachment["content_type"] = attachmentContentType
469
470 // attach it
471 attachments[attachmentName] = attachment
472 body[db.BodyAttachments] = attachments
473

Callers

nothing calls this directly

Calls 15

isReadOnlyGuestMethod · 0.95
PathVarMethod · 0.95
getOCCValueMethod · 0.95
readBodyMethod · 0.95
ctxMethod · 0.95
setEtagMethod · 0.95
writeRawJSONStatusMethod · 0.95
HTTPErrorfFunction · 0.92
IsDocNotFoundErrorFunction · 0.92
GetBodyAttachmentsFunction · 0.92
ConvertToJSONStringFunction · 0.92
bodyKeyForOCCVersionTypeFunction · 0.85

Tested by

no test coverage detected