-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d033d1
commit fc64db1
Showing
46 changed files
with
2,641 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.