anchor.diff:      {error, Message}
     ]}), Req).
   
  -terminate(_Reason, _Req, _State) ->
  -  ok.
  +terminate(_Reason, _Req, _State) -> ok.
   
   %%%===================================================================
   %%% Internal functions
   %%%===================================================================
   
  -multipart(Req) -> multipart(Req, [], []).
  +multipart(UKey, Req) -> multipart(UKey, Req, [], []).
   
  -multipart(Req, FieldList, FileList) ->
  +multipart(UKey, Req, FieldList, FileList) ->
     case cowboy_req:part(Req) of
       {ok, Headers, Req2} ->
         {ok, Req4, NewFieldList, NewFileList} =
  
positive.code:   end;
  
  process(<<"POST">>, Req) ->
  
    case cowboy_req:parse_header(<<"content-type">>, Req) of
      {ok, {<<"multipart">>, <<"form-data">>, _}, _Req} ->
        {ok, Req1, FieldList, FileList} = multipart(Req),
  
        echo(200, jiffy:encode({[
          {fieldList, {FieldList}},
          {fileList, {FileList}}
        ]}), Req1);
  
      _ -> end_with_failure(400, <<"Bad content-type; Accepts only multipart">>, Req)
    end;
  
  process(_, Req) -> end_with_failure(405, "Method not allowed.", Req).
  
  handle_query({file_info, OUkey, FileKey}, Req) -> {info, [], Req};
  
  handle_query({file_upload, OUkey}, Req) ->
    {info, [], Req}.
  
  echo(Status, Echo, Req) ->
    cowboy_req:reply(Status, [
  
negative.code:             {ok, Req3, FieldList1, FileList};
            {file, FieldName, Filename, CType, CTransferEncoding} ->
  
              FileKey = lists:concat([
                binary_to_list(UKey),
                "_",
                hash_md5:build(binary_to_list(Filename))
              ]),
  
              Opts =
                case couch:get("user_uploads", FileKey) of
                  {ok, {FileKVList}} ->
                    case proplists:get_value(<<"_rev">>, FileKVList) of
                      undefined -> [];
                      Rev -> [{rev, Rev}]
                    end;
                  {error, {error, not_found}} -> []
                end,
  
              %% initiate attachmanet upload
              {ok, UploadRef} = couch:attach("user_uploads", FileKey, {Filename, stream}, Opts),
  
              %% commit upload
              {ok, {ResultList}, Req3} = stream_file(Req2, UploadRef),
  
              Rev1 = proplists:get_value(<<"rev">>, ResultList),
              Id = proplists:get_value(<<"id">>, ResultList),
  
              %% get current view
              {ok, {DocKVList}} = couch:get("user_uploads", Id, Rev1),
  
              %% append meta info
              Doc = {lists:concat([
                DocKVList,
                [
                  %% {<<"_id">>, Id},
                  %% {<<"_rev">>, Rev1},
                  {<<"fileKey">>, list_to_binary(FileKey)},
                  {<<"fieldName">>, FieldName},
                  {<<"filename">>, Filename},
                  {<<"ctype">>, CType},
                  {<<"ctransferEncoding">>, CTransferEncoding}
                ]
              ])},
  
              {ok, ResultDoc} = couch:save("user_uploads", Doc),
  
              FileList1 = lists:append([{Filename, ResultDoc}], FileList),
  
              {ok, Req3, FieldList, FileList1}
          end,
        multipart(UKey, Req4, NewFieldList, NewFileList);
      {done, Req2} ->
        {ok, Req2, FieldList, FileList}
    end.
  
  stream_file(Req, UploadRef) ->
    case cowboy_req:part_body(Req) of
      {ok, Body, Req2} ->
        ok = couch:stream_attach(UploadRef, Body),
        {ok, Result} = couch:stream_attach_done(UploadRef),
        {ok, Result, Req2};
      {more, Body, Req2} ->
        ok = couch:stream_attach(UploadRef, Body),
        stream_file(Req2, UploadRef)
    end.