You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

jizkadasha's avatar
jizkadasha
Explorer | Level 4
6 years ago

Dropbox api conflicted copy

Hello there,

I'm using Dropbox official Javascript SDK in my application. The thing is, I can have a case when two or more users try to edit the same file at the same time.

 

For editing I use filesUpload method with { mode: 'overwrite' }.

The first question is: will I receive the conflicted copy if the editing happens at the same time or one user will overwrite an another one's changes? If there won't be the conflicted copy, how can I implement file updating in the correct way?

 

The second question is: if I as a user try to update a file and the conflicted copy is created because of my update, will I have the name of conflicted copy in the Dropbox response for uploading?

 

Thanks a lot!

 

  • Greg-DB's avatar
    Greg-DB
    6 years ago

    Using the 'update' mode and 'autorename:true' should result in the behavior you're looking for, if I understand correctly.

    Here's a contrived example:

     

    var filePath = '/test_324605.txt';
    
    // gettting the current metadata for a file:
    dbx.filesGetMetadata({path: filePath})
      .then(function(response) {
        console.log("got filesGetMetadata response:");
        console.log(response);
    
        // saving the current rev for the file:
        var rev = response['rev'];
    
        // simulating the "first" persion to upload a new version
        dbx.filesUpload({path: filePath, mode: {'.tag': 'update', 'update': rev}, autorename: true, contents: "some new data"})
          .then(function(response) {
            console.log("got first filesUpload response:");
            // this should show the existing file updated in place:
            console.log(response);
    
            // simulating the "second" person to upload a new version, but using the original rev
            dbx.filesUpload({path: filePath, mode: {'.tag': 'update', 'update': rev}, autorename: true, contents: "some different data"})
              .then(function(response) {
                console.log("got second filesUpload response:");
                // this should show the new version as a conflicted copy:
                console.log(response);
              })
              .catch(function(error) {
                console.log("got second filesUpload error:");
                console.error(error);
              });
    
          })
          .catch(function(error) {
            console.log("got first filesUpload error:");
            console.error(error);
          });
    
    
      })
      .catch(function(error) {
        console.log("got filesGetMetadata error:");
        console.log(error);
      });

    Running that, I do get the following output:

     

    got filesGetMetadata response:
    {.tag: "file", name: "test_324605.txt", path_lower: "/test_324605.txt", path_display: "/test_324605.txt", id: "id:25N5ksooX-sAAAAAAAM5rw", …}
    
    got first filesUpload response:
    {name: "test_324605.txt", path_lower: "/test_324605.txt", path_display: "/test_324605.txt", id: "id:25N5ksooX-sAAAAAAAM5rw", client_modified: "2019-01-23T16:20:57Z", …}
    
    got second filesUpload response:
    {name: "test_324605 (API Test Account's conflicted copy).txt", path_lower: "/test_324605 (api test account's conflicted copy).txt", path_display: "/test_324605 (API Test Account's conflicted copy).txt", id: "id:25N5ksooX-sAAAAAAAM5sA", client_modified: "2019-01-23T16:20:59Z", …}
    

    The first upload updates the existing file, and the second one results in a conflicted copy.

    If something isn't working as expected for you, please share the relevant code and output.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    1. If you use the "overwrite" mode, your app can overwrite another user's changes, and will not result in a conflicted copy. For that reason, we generally recommend using the "update" mode instead (with "autorename:true" if you want conflicted copies). That allows your app to more safely upload new versions where there may be conflicts. Please refer to the documentation for more information.

    2. Yes, if a conflicted copy is produced (e.g., as described above), the returned metadata for the upload call will have the resulting metadata of the concflicted copy itself.

    • jizkadasha's avatar
      jizkadasha
      Explorer | Level 4

      I've tested this approach and it works not quite ok for me. The thing is, with this approach I receive a conflicted copy every time when I try to write to the existing file, and there is only me who is updating file actually.  I need the following:

      1. If there is only one user who tries to update the existing file at the moment, there shouldn't be a conflicted copy, the file should be just updated as is;

      2. If two or more users try to update the existing file at the moment, there should a conflicted copy appear, because this will allow me to check if the conflicted copy was created (it should be false for the first user and true for the second one) and resolve the conflict only in the second user's case.

       

      Is it possible?

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        Using the 'update' mode and 'autorename:true' should result in the behavior you're looking for, if I understand correctly.

        Here's a contrived example:

         

        var filePath = '/test_324605.txt';
        
        // gettting the current metadata for a file:
        dbx.filesGetMetadata({path: filePath})
          .then(function(response) {
            console.log("got filesGetMetadata response:");
            console.log(response);
        
            // saving the current rev for the file:
            var rev = response['rev'];
        
            // simulating the "first" persion to upload a new version
            dbx.filesUpload({path: filePath, mode: {'.tag': 'update', 'update': rev}, autorename: true, contents: "some new data"})
              .then(function(response) {
                console.log("got first filesUpload response:");
                // this should show the existing file updated in place:
                console.log(response);
        
                // simulating the "second" person to upload a new version, but using the original rev
                dbx.filesUpload({path: filePath, mode: {'.tag': 'update', 'update': rev}, autorename: true, contents: "some different data"})
                  .then(function(response) {
                    console.log("got second filesUpload response:");
                    // this should show the new version as a conflicted copy:
                    console.log(response);
                  })
                  .catch(function(error) {
                    console.log("got second filesUpload error:");
                    console.error(error);
                  });
        
              })
              .catch(function(error) {
                console.log("got first filesUpload error:");
                console.error(error);
              });
        
        
          })
          .catch(function(error) {
            console.log("got filesGetMetadata error:");
            console.log(error);
          });

        Running that, I do get the following output:

         

        got filesGetMetadata response:
        {.tag: "file", name: "test_324605.txt", path_lower: "/test_324605.txt", path_display: "/test_324605.txt", id: "id:25N5ksooX-sAAAAAAAM5rw", …}
        
        got first filesUpload response:
        {name: "test_324605.txt", path_lower: "/test_324605.txt", path_display: "/test_324605.txt", id: "id:25N5ksooX-sAAAAAAAM5rw", client_modified: "2019-01-23T16:20:57Z", …}
        
        got second filesUpload response:
        {name: "test_324605 (API Test Account's conflicted copy).txt", path_lower: "/test_324605 (api test account's conflicted copy).txt", path_display: "/test_324605 (API Test Account's conflicted copy).txt", id: "id:25N5ksooX-sAAAAAAAM5sA", client_modified: "2019-01-23T16:20:59Z", …}
        

        The first upload updates the existing file, and the second one results in a conflicted copy.

        If something isn't working as expected for you, please share the relevant code and output.