anchor.diff:              {ok, Req3, FieldList1, FileList};
             {file, FieldName, Filename, CType, CTransferEncoding} ->
   
  -            {ok, TMPFname, Req3} = stream_file(Req2),
  +            FileKey = lists:concat([
  +              binary_to_list(UKey),
  +              "_",
  +              hash_md5:build(binary_to_list(Filename))
  +            ]),
   
  -            FileList1 = lists:append([{Filename, {[
  -              {<<"fieldName">>, FieldName},
  -              {<<"filename">>, Filename},
  -              {<<"ctype">>, CType},
  -              {<<"ctransferEncoding">>, CTransferEncoding},
  -              {<<"tmpFile">>, TMPFname}
  -            ]}}], FileList),
  +            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(Req4, NewFieldList, NewFileList);
  +      multipart(UKey, Req4, NewFieldList, NewFileList);
       {done, Req2} ->
         {ok, Req2, FieldList, FileList}
     end.
   
  -stream_file(Req) ->
  -  {A, B, C} = now(),
  -  Rand = binary_to_list(base64:encode(binary_to_list(crypto:strong_rand_bytes(10)))),
  -  Fname = lists:flatten(io_lib:format("~p.~p.~p.~s.tmp", [A, B, C, Rand])),
  -  stream_file(Req, list_to_binary(Fname)).
  -
  -stream_file(Req, Fname) ->
  +stream_file(Req, UploadRef) ->
     case cowboy_req:part_body(Req) of
       {ok, Body, Req2} ->
  -      file:write_file(Fname, Body, [append, raw, binary]),
  -      {ok, Fname, Req2};
  +      ok = couch:stream_attach(UploadRef, Body),
  +      {ok, Result} = couch:stream_attach_done(UploadRef),
  +      {ok, Result, Req2};
       {more, Body, Req2} ->
  -      file:write_file(Fname, Body, [append, raw, binary]),
  -      stream_file(Req2, Fname)
  +      ok = couch:stream_attach(UploadRef, Body),
  +      stream_file(Req2, UploadRef)
     end.
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:     {error, Message}
    ]}), Req).
  
  terminate(_Reason, _Req, _State) -> ok.
  
  %%%===================================================================
  %%% Internal functions
  %%%===================================================================
  
  multipart(UKey, Req) -> multipart(UKey, Req, [], []).
  
  multipart(UKey, Req, FieldList, FileList) ->
    case cowboy_req:part(Req) of
      {ok, Headers, Req2} ->
        {ok, Req4, NewFieldList, NewFileList} =
  