Skip to content

Instantly share code, notes, and snippets.

@fjl
Created January 20, 2014 21:48
Show Gist options
  • Save fjl/8529885 to your computer and use it in GitHub Desktop.
Save fjl/8529885 to your computer and use it in GitHub Desktop.
emit_attachment(SI = #sync_info{db = Db}, DocId, Filename, Type, Content) ->
?LOG_INFO("storing attachment ~s/~s/~s (~s, ~p bytes)", [
SI#sync_info.user,
DocId,
Filename,
Type,
byte_size(Content)
]),
case couch_db:open_doc(Db, DocId, [attachments]) of
{ok, Doc} ->
NewDoc = add_attachment(Doc, Filename, Type, Content),
couch_db:update_doc(Db, NewDoc, []);
Error ->
error({open_doc_error, Error})
end.
add_attachment(Doc = #doc{atts = Atts}, Filename, Type, Content) ->
NewAtt = #att{
name = Filename,
type = Type,
data = Content,
att_len = byte_size(Content)
},
NewAtts = [NewAtt | [A || A <- Atts, A#att.name /= Filename]],
Doc#doc{atts = NewAtts}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment