MegaApiClient
MegaApiClient copied to clipboard
Add API to update existing file (example attached)
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
Thanks for the report. Let's plan this feature for the next release
Thanks
@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?
I plan to investigate the actual behavior in the web client to 'maybe' discover a new API (or at least use the same way)
@Sergio0694 What do you think about this new API? https://github.com/gpailler/MegaApiClient/blob/66331a95df277df2f27e96e58acead9588378620/MegaApiClient/Interface/IMegaApiClient.cs#L56-L77
@gpailler That looks great, nice job! 😄 I just have a few questions (better safe than sorry ahahah):
- 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? - Why does the first
UpdateFile
API have the filename parameter, if it already takes the target node to replace? - 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 theCreateFile
API in UWP, which has theCreationCollisionOption
enum that does exactly this. I mean, either something like this, or theUpdate
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! 👍
any update on when this can be expected?