Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality: Improved app startup routine #12861

Merged
merged 8 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/Files.App.Storage/FtpStorage/FtpStorable.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.Core.Storage;
using Files.Core.Storage.LocatableStorage;
using Files.Shared.Helpers;
using Files.Core.Storage.NestedStorage;
using FluentFTP;
using System.Threading;
using System.Threading.Tasks;

namespace Files.App.Storage.FtpStorage
{
public abstract class FtpStorable : ILocatableStorable
public abstract class FtpStorable : ILocatableStorable, INestedStorable
{
private string? _computedId;

/// <inheritdoc/>
public string Path { get; protected set; }
public virtual string Path { get; protected set; }

/// <inheritdoc/>
public string Name { get; protected set; }
public virtual string Name { get; protected set; }

/// <inheritdoc/>
public virtual string Id => _computedId ??= ChecksumHelpers.CalculateChecksumForPath(Path);
public virtual string Id { get; }

/// <summary>
/// Gets the parent folder of the storable, if any.
/// </summary>
protected virtual IFolder? Parent { get; }

protected internal FtpStorable(string path, string name)
protected internal FtpStorable(string path, string name, IFolder? parent)
{
Path = FtpHelpers.GetFtpPath(path);
Name = name;
Id = Path;
Parent = parent;
}

/// <inheritdoc/>
public virtual Task<ILocatableFolder?> GetParentAsync(CancellationToken cancellationToken = default)
public Task<IFolder?> GetParentAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult<ILocatableFolder?>(null);
return Task.FromResult<IFolder?>(Parent);
}

protected AsyncFtpClient GetFtpClient()
Expand Down
14 changes: 5 additions & 9 deletions src/Files.App.Storage/FtpStorage/FtpStorageFile.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.Core.Storage;
using Files.Core.Storage.LocatableStorage;
using Files.Core.Storage.ModifiableStorage;
using Files.Core.Storage.NestedStorage;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Files.App.Storage.FtpStorage
{
public sealed class FtpStorageFile : FtpStorable, IModifiableFile, ILocatableFile
public sealed class FtpStorageFile : FtpStorable, IModifiableFile, ILocatableFile, INestedFile
{
public FtpStorageFile(string path, string name)
: base(path, name)
public FtpStorageFile(string path, string name, IFolder? parent)
: base(path, name, parent)
{
}

Expand All @@ -24,17 +26,11 @@ public async Task<Stream> OpenStreamAsync(FileAccess access, CancellationToken c
await ftpClient.EnsureConnectedAsync(cancellationToken);

if (access.HasFlag(FileAccess.Write))
{
return await ftpClient.OpenWrite(Path, token: cancellationToken);
}
else if (access.HasFlag(FileAccess.Read))
{
return await ftpClient.OpenRead(Path, token: cancellationToken);
}
else
{
throw new ArgumentException($"Invalid {nameof(access)} flag.");
}
}
}
}
71 changes: 30 additions & 41 deletions src/Files.App.Storage/FtpStorage/FtpStorageFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// Licensed under the MIT License. See the LICENSE.

using Files.Core.Storage;
using Files.Core.Storage.DirectStorage;
using Files.Core.Storage.Enums;
using Files.Core.Storage.ExtendableStorage;
using Files.Core.Storage.Extensions;
using Files.Core.Storage.LocatableStorage;
using Files.Core.Storage.ModifiableStorage;
using Files.Core.Storage.NestedStorage;
using Files.Shared.Helpers;
using FluentFTP;
using System;
Expand All @@ -17,15 +20,15 @@

namespace Files.App.Storage.FtpStorage
{
public sealed class FtpStorageFolder : FtpStorable, ILocatableFolder, IModifiableFolder
public sealed class FtpStorageFolder : FtpStorable, ILocatableFolder, IModifiableFolder, IFolderExtended, INestedFolder, IDirectCopy, IDirectMove
{
public FtpStorageFolder(string path, string name)
: base(path, name)
public FtpStorageFolder(string path, string name, IFolder? parent)
: base(path, name, parent)
{
}

/// <inheritdoc/>
public async Task<IFile> GetFileAsync(string fileName, CancellationToken cancellationToken = default)
public async Task<INestedFile> GetFileAsync(string fileName, CancellationToken cancellationToken = default)
{
using var ftpClient = GetFtpClient();
await ftpClient.EnsureConnectedAsync(cancellationToken);
Expand All @@ -36,11 +39,11 @@ public async Task<IFile> GetFileAsync(string fileName, CancellationToken cancell
if (item is null || item.Type != FtpObjectType.File)
throw new FileNotFoundException();

return new FtpStorageFile(path, item.Name);
return new FtpStorageFile(path, item.Name, this);
}

/// <inheritdoc/>
public async Task<IFolder> GetFolderAsync(string folderName, CancellationToken cancellationToken = default)
public async Task<INestedFolder> GetFolderAsync(string folderName, CancellationToken cancellationToken = default)
{
using var ftpClient = GetFtpClient();
await ftpClient.EnsureConnectedAsync(cancellationToken);
Expand All @@ -51,11 +54,11 @@ public async Task<IFolder> GetFolderAsync(string folderName, CancellationToken c
if (item is null || item.Type != FtpObjectType.Directory)
throw new DirectoryNotFoundException();

return new FtpStorageFolder(path, item.Name);
return new FtpStorageFolder(path, item.Name, this);
}

/// <inheritdoc/>
public async IAsyncEnumerable<IStorable> GetItemsAsync(StorableKind kind = StorableKind.All, [EnumeratorCancellation] CancellationToken cancellationToken = default)
public async IAsyncEnumerable<INestedStorable> GetItemsAsync(StorableKind kind = StorableKind.All, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
using var ftpClient = GetFtpClient();
await ftpClient.EnsureConnectedAsync(cancellationToken);
Expand All @@ -65,32 +68,32 @@ public async IAsyncEnumerable<IStorable> GetItemsAsync(StorableKind kind = Stora
foreach (var item in await ftpClient.GetListing(Path, cancellationToken))
{
if (item.Type == FtpObjectType.File)
yield return new FtpStorageFile(item.FullName, item.Name);
yield return new FtpStorageFile(item.FullName, item.Name, this);
}
}
else if (kind == StorableKind.Folders)
{
foreach (var item in await ftpClient.GetListing(Path, cancellationToken))
{
if (item.Type == FtpObjectType.Directory)
yield return new FtpStorageFolder(item.FullName, item.Name);
yield return new FtpStorageFolder(item.FullName, item.Name, this);
}
}
else
{
foreach (var item in await ftpClient.GetListing(Path, cancellationToken))
{
if (item.Type == FtpObjectType.File)
yield return new FtpStorageFile(item.FullName, item.Name);
yield return new FtpStorageFile(item.FullName, item.Name, this);

if (item.Type == FtpObjectType.Directory)
yield return new FtpStorageFolder(item.FullName, item.Name);
yield return new FtpStorageFolder(item.FullName, item.Name, this);
}
}
}

/// <inheritdoc/>
public async Task DeleteAsync(IStorable item, bool permanently = false, CancellationToken cancellationToken = default)
public async Task DeleteAsync(INestedStorable item, bool permanently = false, CancellationToken cancellationToken = default)
{
using var ftpClient = GetFtpClient();
await ftpClient.EnsureConnectedAsync(cancellationToken);
Expand All @@ -110,11 +113,11 @@ public async Task DeleteAsync(IStorable item, bool permanently = false, Cancella
}

/// <inheritdoc/>
public async Task<IStorable> CreateCopyOfAsync(IStorable itemToCopy, CreationCollisionOption collisionOption = default, CancellationToken cancellationToken = default)
public async Task<INestedStorable> CreateCopyOfAsync(INestedStorable itemToCopy, bool overwrite = default, CancellationToken cancellationToken = default)
{
if (itemToCopy is IFile sourceFile)
{
var copiedFile = await CreateFileAsync(itemToCopy.Name, collisionOption, cancellationToken);
var copiedFile = await CreateFileAsync(itemToCopy.Name, overwrite, cancellationToken);
await sourceFile.CopyContentsToAsync(copiedFile, cancellationToken);

return copiedFile;
Expand All @@ -126,41 +129,34 @@ public async Task<IStorable> CreateCopyOfAsync(IStorable itemToCopy, CreationCol
}

/// <inheritdoc/>
public async Task<IStorable> MoveFromAsync(IStorable itemToMove, IModifiableFolder source, CreationCollisionOption collisionOption = default, CancellationToken cancellationToken = default)
public async Task<INestedStorable> MoveFromAsync(INestedStorable itemToMove, IModifiableFolder source, bool overwrite = default, CancellationToken cancellationToken = default)
{
using var ftpClient = GetFtpClient();
await ftpClient.EnsureConnectedAsync(cancellationToken);

var newItem = await CreateCopyOfAsync(itemToMove, collisionOption, cancellationToken);
var newItem = await CreateCopyOfAsync(itemToMove, overwrite, cancellationToken);
await source.DeleteAsync(itemToMove, true, cancellationToken);

return newItem;
}

/// <inheritdoc/>
public async Task<IFile> CreateFileAsync(string desiredName, CreationCollisionOption collisionOption = default, CancellationToken cancellationToken = default)
public async Task<INestedFile> CreateFileAsync(string desiredName, bool overwrite = default, CancellationToken cancellationToken = default)
{
using var ftpClient = GetFtpClient();
await ftpClient.EnsureConnectedAsync(cancellationToken);

var newPath = $"{Path}/{desiredName}";
if (await ftpClient.FileExists(newPath, cancellationToken))
{
if (collisionOption == CreationCollisionOption.FailIfExists)
throw new IOException("File already exists.");

if (collisionOption == CreationCollisionOption.OpenIfExists)
return new FtpStorageFile(newPath, desiredName);
}
if (overwrite && await ftpClient.FileExists(newPath, cancellationToken))
throw new IOException("File already exists.");

using var stream = new MemoryStream();
var replaceExisting = collisionOption == CreationCollisionOption.ReplaceExisting;
var result = await ftpClient.UploadStream(stream, newPath, replaceExisting ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip, token: cancellationToken);
var result = await ftpClient.UploadStream(stream, newPath, overwrite ? FtpRemoteExists.Overwrite : FtpRemoteExists.Skip, token: cancellationToken);

if (result == FtpStatus.Success)
{
// Success
return new FtpStorageFile(newPath, desiredName);
return new FtpStorageFile(newPath, desiredName, this);
}
else if (result == FtpStatus.Skipped)
{
Expand All @@ -175,27 +171,20 @@ public async Task<IFile> CreateFileAsync(string desiredName, CreationCollisionOp
}

/// <inheritdoc/>
public async Task<IFolder> CreateFolderAsync(string desiredName, CreationCollisionOption collisionOption = default, CancellationToken cancellationToken = default)
public async Task<INestedFolder> CreateFolderAsync(string desiredName, bool overwrite = default, CancellationToken cancellationToken = default)
{
using var ftpClient = GetFtpClient();
await ftpClient.EnsureConnectedAsync(cancellationToken);

var newPath = $"{Path}/{desiredName}";
if (await ftpClient.DirectoryExists(newPath, cancellationToken))
{
if (collisionOption == CreationCollisionOption.FailIfExists)
throw new IOException("Directory already exists.");

if (collisionOption == CreationCollisionOption.OpenIfExists)
return new FtpStorageFolder(newPath, desiredName);
}
if (overwrite && await ftpClient.DirectoryExists(newPath, cancellationToken))
throw new IOException("Directory already exists.");

var replaceExisting = collisionOption == CreationCollisionOption.ReplaceExisting;
var isSuccessful = await ftpClient.CreateDirectory(newPath, replaceExisting, cancellationToken);
var isSuccessful = await ftpClient.CreateDirectory(newPath, overwrite, cancellationToken);
if (!isSuccessful)
throw new IOException("Directory was not successfully created.");

return new FtpStorageFolder(newPath, desiredName);
return new FtpStorageFolder(newPath, desiredName, this);
}
}
}
51 changes: 11 additions & 40 deletions src/Files.App.Storage/FtpStorage/FtpStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,41 @@
using Files.Core.Storage;
using Files.Core.Storage.LocatableStorage;
using FluentFTP;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Files.App.Storage.FtpStorage
{
/// <inheritdoc cref="IFtpStorageService"/>
public sealed class FtpStorageService : IFtpStorageService
{
public Task<bool> IsAccessibleAsync(CancellationToken cancellationToken = default)
/// <inheritdoc/>
public async Task<IFolder> GetFolderAsync(string id, CancellationToken cancellationToken = default)
{
return Task.FromResult(true); // TODO: Check if FTP is available
}

public async Task<bool> FileExistsAsync(string path, CancellationToken cancellationToken = default)
{
try
{
_ = await GetFileFromPathAsync(path, cancellationToken);
return true;
}
catch (Exception)
{
return false;
}
}

public async Task<bool> DirectoryExistsAsync(string path, CancellationToken cancellationToken = default)
{
try
{
_ = await GetFolderFromPathAsync(path, cancellationToken);
return true;
}
catch (Exception)
{
return false;
}
}

public async Task<ILocatableFolder> GetFolderFromPathAsync(string path, CancellationToken cancellationToken = default)
{
using var ftpClient = FtpHelpers.GetFtpClient(path);
using var ftpClient = FtpHelpers.GetFtpClient(id);
await ftpClient.EnsureConnectedAsync(cancellationToken);

var ftpPath = FtpHelpers.GetFtpPath(path);
var ftpPath = FtpHelpers.GetFtpPath(id);
var item = await ftpClient.GetObjectInfo(ftpPath, token: cancellationToken);
if (item is null || item.Type != FtpObjectType.Directory)
throw new DirectoryNotFoundException("Directory was not found from path.");

return new FtpStorageFolder(ftpPath, item.Name);
return new FtpStorageFolder(ftpPath, item.Name, null);
}

public async Task<ILocatableFile> GetFileFromPathAsync(string path, CancellationToken cancellationToken = default)
/// <inheritdoc/>
public async Task<IFile> GetFileAsync(string id, CancellationToken cancellationToken = default)
{
using var ftpClient = FtpHelpers.GetFtpClient(path);
using var ftpClient = FtpHelpers.GetFtpClient(id);
await ftpClient.EnsureConnectedAsync(cancellationToken);

var ftpPath = FtpHelpers.GetFtpPath(path);
var ftpPath = FtpHelpers.GetFtpPath(id);
var item = await ftpClient.GetObjectInfo(ftpPath, token: cancellationToken);
if (item is null || item.Type != FtpObjectType.File)
throw new FileNotFoundException("File was not found from path.");

return new FtpStorageFile(ftpPath, item.Name);
return new FtpStorageFile(ftpPath, item.Name, null);
}
}
}
Loading