MegaApiClient icon indicating copy to clipboard operation
MegaApiClient copied to clipboard

Add API to update existing file (example attached)

Open Sergio0694 opened this issue 6 years ago • 6 comments

Description: Since MEGA supports duplicate files with the same name, it'd be useful to have an API that would upload/update a file, making sure that a duplicate is not created. The behavior should be:

  • If a file with the same name doesn't exist, just upload it
  • Otherwise, replace the existing file

Example: I'm not sure whether or not the MEGA APIs explicitly allow this, but a possible workaround (the one I'm using now) could be something like this:

public async Task UpdateAsync(Stream stream, String filename, INode parent, IProgress<double> progress, CancellationToken token)
{
    var uploaded = await Client.UploadAsync(stream, filename, parent, progress, null, token);
    var nodes = await Client.GetNodesAsync(parent);
    foreach (var child in nodes)
        if (child.Name.Equals(filename) && !child.Id.Equals(uploaded.Id))
            await Client.DeleteAsync(child, false);            
}

Even though an obvious issue with this method is that if the connection is lost right after uploading the file, the duplicate will still be left there. Same if the duplicate is deleted first: if the upload fails the user will not find any file in the target directory. A better solution is probably available, but I'm not sure on how to proceed as I'm not so familiar with the actual MEGA APIs.

MegaApiClient Version: Latest 1.6.2 version

Thank you for your hard work, keep it up! 😄 Sergio

Sergio0694 avatar Feb 10 '18 11:02 Sergio0694

Thanks for the report. Let's plan this feature for the next release

Thanks

gpailler avatar Feb 13 '18 04:02 gpailler

@gpailler Do you have ideas on how to actually implement this, other than that (not perfect) solution I posted? Maybe some API that allows this, that I'm not aware of?

Sergio0694 avatar Feb 15 '18 18:02 Sergio0694

I plan to investigate the actual behavior in the web client to 'maybe' discover a new API (or at least use the same way)

gpailler avatar Feb 19 '18 02:02 gpailler

@Sergio0694 What do you think about this new API? https://github.com/gpailler/MegaApiClient/blob/66331a95df277df2f27e96e58acead9588378620/MegaApiClient/Interface/IMegaApiClient.cs#L56-L77

gpailler avatar Mar 23 '18 08:03 gpailler

@gpailler That looks great, nice job! 😄 I just have a few questions (better safe than sorry ahahah):

  1. What's the behavior of the Update API if the node to replace doesn't exist? Does it fail/throw or does it upload the file anyways like an "UploadOrUpdate" method?
  2. Why does the first UpdateFile API have the filename parameter, if it already takes the target node to replace?
  3. In case the answer to the first question is "it throws", what about adding an overload to the UploadFile/Upload APIs that takes a "CollisionMode" parameter, with options such as "ReplaceExisting", "Fail" and "GenerateUniqueName" options? That would make the API very similar to the CreateFile API in UWP, which has the CreationCollisionOption enum that does exactly this. I mean, either something like this, or the Update API automatically creating the remote file if it doesn't exist, as long as on the user-side you can use a single API call to upload/update a remote file.

Again, great work! 👍

Sergio0694 avatar Mar 23 '18 11:03 Sergio0694

any update on when this can be expected?

PhilippC avatar Jan 31 '22 11:01 PhilippC