Skip to content

Commit

Permalink
YandexDisk.Client fork
Browse files Browse the repository at this point in the history
  • Loading branch information
mikasoukhov committed Nov 19, 2024
1 parent 1d033d1 commit fc64db1
Show file tree
Hide file tree
Showing 46 changed files with 2,641 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Backup.Yandex/Backup.Yandex.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\common.props" />
<ItemGroup>
<PackageReference Include="YandexDisk.Client" Version="$(YDCVer)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Backup\Backup.csproj" />
<ProjectReference Include="..\Reflection\Reflection.csproj" />
<ProjectReference Include="..\YandexDisk.Client\YandexDisk.Client.csproj" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions Ecng.sln
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MathLight", "MathLight\Math
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Backup.Mega", "Backup.Mega\Backup.Mega.csproj", "{6202135C-F945-4047-828B-284E1E00BBEA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YandexDisk.Client", "YandexDisk.Client\YandexDisk.Client.csproj", "{5BA2E8AB-3569-4780-BDC6-7920B93A770D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -201,6 +203,10 @@ Global
{6202135C-F945-4047-828B-284E1E00BBEA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6202135C-F945-4047-828B-284E1E00BBEA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6202135C-F945-4047-828B-284E1E00BBEA}.Release|Any CPU.Build.0 = Release|Any CPU
{5BA2E8AB-3569-4780-BDC6-7920B93A770D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5BA2E8AB-3569-4780-BDC6-7920B93A770D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5BA2E8AB-3569-4780-BDC6-7920B93A770D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5BA2E8AB-3569-4780-BDC6-7920B93A770D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
46 changes: 46 additions & 0 deletions YandexDisk.Client/AboutInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;

namespace YandexDisk.Client
{
/// <summary>
/// Class provided assymbly and file description
/// </summary>
[PublicAPI]
public class AboutInfo
{
private readonly Assembly _assembly;
private string _productTitle;
private static string _version;

/// <param name="assembly">Assembly for information providing</param>
public AboutInfo(Assembly assembly)
{
_assembly = assembly;
}

/// <summary>
/// Return product title from AssemblyTitleAttribute
/// </summary>
[PublicAPI, NotNull]
public string ProductTitle => _productTitle ?? (_productTitle = GetAttribute<AssemblyTitleAttribute>().Title);

/// <summary>
/// Return version of assembly
/// </summary>
[PublicAPI, NotNull]
public string Version => _version ?? (_version = _assembly.GetName().Version.ToString());

private TAttr GetAttribute<TAttr>()
{
return (TAttr)_assembly.GetCustomAttributes(typeof(TAttr), true).First();
}

/// <summary>
/// Default Info for IDiskApi
/// </summary>
[PublicAPI, NotNull]
public static readonly AboutInfo Client = new AboutInfo(typeof(IDiskApi).Assembly);
}
}
152 changes: 152 additions & 0 deletions YandexDisk.Client/Clients/CommandsClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using YandexDisk.Client.Protocol;

namespace YandexDisk.Client.Clients
{
/// <summary>
/// Extended file commands
/// </summary>
[PublicAPI]
public static class CommandsClientExtensions
{
/// <summary>
/// Default pull period for waiting operation.
/// </summary>
public static TimeSpan DefaultPullPeriod = TimeSpan.FromSeconds(3);

private static async Task WaitOperationAsync([NotNull] this ICommandsClient client, [NotNull] Link operationLink, CancellationToken cancellationToken, TimeSpan? pullPeriod)
{
if (pullPeriod == null)
{
pullPeriod = DefaultPullPeriod;
}

Operation operation;
do
{
Thread.Sleep(pullPeriod.Value);
operation = await client.GetOperationStatus(operationLink, cancellationToken).ConfigureAwait(false);
} while (operation.Status == OperationStatus.InProgress &&
!cancellationToken.IsCancellationRequested);
}

/// <summary>
/// Copy file or folder on Disk from one path to another and wait until operation is done
/// </summary>
public static async Task CopyAndWaitAsync([NotNull] this ICommandsClient client, [NotNull] CopyFileRequest request, CancellationToken cancellationToken = default(CancellationToken), TimeSpan? pullPeriod = null)
{
var link = await client.CopyAsync(request, cancellationToken).ConfigureAwait(false);

if (link.HttpStatusCode == HttpStatusCode.Accepted)
{
await client.WaitOperationAsync(link, cancellationToken, pullPeriod).ConfigureAwait(false);
}
}

/// <summary>
/// Move file or folder on Disk from one path to another and wait until operation is done
/// </summary>
public static async Task MoveAndWaitAsync([NotNull] this ICommandsClient client, [NotNull] MoveFileRequest request, CancellationToken cancellationToken = default(CancellationToken), TimeSpan? pullPeriod = null)
{
var link = await client.MoveAsync(request, cancellationToken).ConfigureAwait(false);

if (link.HttpStatusCode == HttpStatusCode.Accepted)
{
await client.WaitOperationAsync(link, cancellationToken, pullPeriod).ConfigureAwait(false);
}
}

/// <summary>
/// Delete file or folder on Disk and wait until operation is done
/// </summary>
public static async Task DeleteAndWaitAsync([NotNull] this ICommandsClient client, [NotNull] DeleteFileRequest request, CancellationToken cancellationToken = default(CancellationToken), TimeSpan? pullPeriod = null)
{
var link = await client.DeleteAsync(request, cancellationToken).ConfigureAwait(false);

if (link.HttpStatusCode == HttpStatusCode.Accepted)
{
await client.WaitOperationAsync(link, cancellationToken, pullPeriod).ConfigureAwait(false);
}
}

/// <summary>
/// Empty trash
/// </summary>
public static async Task EmptyTrashAndWaitAsyncAsync([NotNull] this ICommandsClient client, [NotNull] string path, CancellationToken cancellationToken = default(CancellationToken), TimeSpan? pullPeriod = null)
{
var link = await client.EmptyTrashAsync(path, cancellationToken).ConfigureAwait(false);

if (link.HttpStatusCode == HttpStatusCode.Accepted)
{
await client.WaitOperationAsync(link, cancellationToken, pullPeriod).ConfigureAwait(false);
}
}

/// <summary>
/// Restore files from trash
/// </summary>
public static async Task RestoreFromTrashAndWaitAsyncAsync([NotNull] this ICommandsClient client, [NotNull] RestoreFromTrashRequest request, CancellationToken cancellationToken = default(CancellationToken), TimeSpan? pullPeriod = null)
{
var link = await client.RestoreFromTrashAsync(request, cancellationToken).ConfigureAwait(false);

if (link.HttpStatusCode == HttpStatusCode.Accepted)
{
await client.WaitOperationAsync(link, cancellationToken, pullPeriod).ConfigureAwait(false);
}
}


#region Obsoleted
/// <summary>
/// Copy file or folder on Disk from one path to another and wait until operation is done
/// </summary>
[Obsolete("Method is obsolete. Please use CopyAndWaitAsync(ICommandsClient, CopyFileRequest, CancellationToken, TimeSpan) instead.")]
public static Task CopyAndWaitAsync([NotNull] this ICommandsClient client, [NotNull] CopyFileRequest request, CancellationToken cancellationToken, int pullPeriod)
{
return CopyAndWaitAsync(client, request, cancellationToken, TimeSpan.FromSeconds(pullPeriod));
}


/// <summary>
/// Move file or folder on Disk from one path to another and wait until operation is done
/// </summary>
[Obsolete("Method is obsolete. Please use MoveAndWaitAsync(ICommandsClient, MoveFileRequest, CancellationToken, TimeSpan) instead.")]
public static Task MoveAndWaitAsync([NotNull] this ICommandsClient client, [NotNull] MoveFileRequest request, CancellationToken cancellationToken, int pullPeriod)
{
return MoveAndWaitAsync(client, request, cancellationToken, TimeSpan.FromSeconds(pullPeriod));
}

/// <summary>
/// Delete file or folder on Disk and wait until operation is done
/// </summary>
[Obsolete("Method is obsolete. Please use DeleteAndWaitAsync(ICommandsClient, DeleteFileRequest, CancellationToken, TimeSpan) instead.")]
public static Task DeleteAndWaitAsync([NotNull] this ICommandsClient client, [NotNull] DeleteFileRequest request, CancellationToken cancellationToken, int pullPeriod)
{
return DeleteAndWaitAsync(client, request, cancellationToken, TimeSpan.FromSeconds(pullPeriod));
}

/// <summary>
/// Empty trash
/// </summary>
[Obsolete("Method is obsolete. Please use EmptyTrashAndWaitAsyncAsync(ICommandsClient, string, CancellationToken, TimeSpan) instead.")]
public static Task EmptyTrashAndWaitAsyncAsync([NotNull] this ICommandsClient client, [NotNull] string path, CancellationToken cancellationToken, int pullPeriod)
{
return EmptyTrashAndWaitAsyncAsync(client, path, cancellationToken, TimeSpan.FromSeconds(pullPeriod));
}

/// <summary>
/// Restore files from trash
/// </summary>
[Obsolete("Method is obsolete. Please use RestoreFromTrashAndWaitAsyncAsync(ICommandsClient, RestoreFromTrashRequest, CancellationToken, TimeSpan) instead.")]
public static Task RestoreFromTrashAndWaitAsyncAsync([NotNull] this ICommandsClient client, [NotNull] RestoreFromTrashRequest request, CancellationToken cancellationToken, int pullPeriod)
{
return RestoreFromTrashAndWaitAsyncAsync(client, request, cancellationToken, TimeSpan.FromSeconds(pullPeriod));
}

#endregion
}
}
113 changes: 113 additions & 0 deletions YandexDisk.Client/Clients/FilesClientExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using YandexDisk.Client.Protocol;

namespace YandexDisk.Client.Clients
{
/// <summary>
/// Extended helpers from uploading and downloading files
/// </summary>
public static class FilesClientExtension
{
/// <summary>
/// Just upload stream data to Yandex Disk
/// </summary>
[PublicAPI]
public static async Task UploadFileAsync([NotNull] this IFilesClient client, [NotNull] string path, bool overwrite, [NotNull] Stream file, CancellationToken cancellationToken = default(CancellationToken))
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
if (String.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException(nameof(path));
}
if (file == null)
{
throw new ArgumentNullException(nameof(file));
}

Link link = await client.GetUploadLinkAsync(path, overwrite, cancellationToken).ConfigureAwait(false);

await client.UploadAsync(link, file, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Just upload file from local disk to Yandex Disk
/// </summary>
[PublicAPI]
public static async Task UploadFileAsync([NotNull] this IFilesClient client, [NotNull] string path, bool overwrite, [NotNull] string localFile, CancellationToken cancellationToken)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
if (String.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException(nameof(path));
}
if (String.IsNullOrWhiteSpace(localFile))
{
throw new ArgumentNullException(nameof(localFile));
}

Link link = await client.GetUploadLinkAsync(path, overwrite, cancellationToken).ConfigureAwait(false);

using (var file = new FileStream(localFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
await client.UploadAsync(link, file, cancellationToken).ConfigureAwait(false);
}
}

/// <summary>
/// Get downloaded file from Yandex Disk as stream
/// </summary>
[PublicAPI]
public static async Task<Stream> DownloadFileAsync([NotNull] this IFilesClient client, [NotNull] string path, CancellationToken cancellationToken = default(CancellationToken))
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
if (String.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException(nameof(path));
}

Link link = await client.GetDownloadLinkAsync(path, cancellationToken).ConfigureAwait(false);

return await client.DownloadAsync(link, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Downloaded data from Yandex Disk to local file
/// </summary>
[PublicAPI]
public static async Task DownloadFileAsync([NotNull] this IFilesClient client, [NotNull] string path, [NotNull] string localFile, CancellationToken cancellationToken = default(CancellationToken))
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}
if (String.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException(nameof(path));
}
if (String.IsNullOrWhiteSpace(localFile))
{
throw new ArgumentNullException(nameof(localFile));
}

Stream data = await DownloadFileAsync(client, path, cancellationToken).ConfigureAwait(false);

using (var file = new FileStream(localFile, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite))
{
await data.CopyToAsync(file, bufferSize: 81920/*keep default*/, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
}
}
Loading

0 comments on commit fc64db1

Please sign in to comment.