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: Clean up App.Filesystem phase 1 #12408

Merged
merged 2 commits into from
May 21, 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
15 changes: 0 additions & 15 deletions src/Files.App/Filesystem/Archive/ArchiveCompressionLevels.cs

This file was deleted.

143 changes: 83 additions & 60 deletions src/Files.App/Filesystem/Archive/ArchiveCreator.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,29 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.DependencyInjection;
using Files.Shared;
using Microsoft.Extensions.Logging;
using SevenZip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Files.App.Filesystem.Archive
{
/// <summary>
/// Provides an archive creation support.
/// </summary>
public class ArchiveCreator : IArchiveCreator
{
// Represents the total number of items to be processed.
// It is used to calculate a weighted progress with this formula:
// Progress = [OldProgress + (ProgressDelta / ItemsAmount)]
private int itemsAmount = 1;
private int processedItems = 0;

private string archivePath = string.Empty;
public string ArchivePath
{
get => archivePath;
set => archivePath = value;
}

public string Directory { get; init; } = string.Empty;
public string FileName { get; init; } = string.Empty;
public string Password { get; init; } = string.Empty;
/// <summary>
/// Represents the total number of items to be processed.
/// </summary>
/// <remarks>
/// It is used to calculate a weighted progress with this formula:
/// <code>Progress = [OldProgress + (ProgressDelta / ItemsAmount)]</code>
/// </remarks>
private int _itemsAmount = 1;

public IEnumerable<string> Sources { get; init; } = Enumerable.Empty<string>();
private int _processedItems = 0;

public ArchiveFormats FileFormat { get; init; } = ArchiveFormats.Zip;
public ArchiveCompressionLevels CompressionLevel { get; init; } = ArchiveCompressionLevels.Normal;
public ArchiveSplittingSizes SplittingSize { get; init; } = ArchiveSplittingSizes.None;

private IProgress<FileSystemProgress> progress = new Progress<FileSystemProgress>();
public IProgress<FileSystemProgress> Progress
{
get => progress;
set
{
progress = value;
fsProgress = new(Progress, true, Shared.Enums.FileSystemStatusCode.InProgress);
fsProgress.Report(0);
}
}

private FileSystemProgress fsProgress;

public ArchiveCreator()
{
fsProgress = new(Progress, true, Shared.Enums.FileSystemStatusCode.InProgress);
fsProgress.Report(0);
}
private FileSystemProgress _fileSystemProgress;

private string ArchiveExtension => FileFormat switch
{
Expand All @@ -66,6 +33,7 @@ public ArchiveCreator()
ArchiveFormats.GZip => ".gz",
_ => throw new ArgumentOutOfRangeException(nameof(FileFormat)),
};

private OutArchiveFormat SevenZipArchiveFormat => FileFormat switch
{
ArchiveFormats.Zip => OutArchiveFormat.Zip,
Expand All @@ -74,6 +42,7 @@ public ArchiveCreator()
ArchiveFormats.GZip => OutArchiveFormat.GZip,
_ => throw new ArgumentOutOfRangeException(nameof(FileFormat)),
};

private CompressionLevel SevenZipCompressionLevel => CompressionLevel switch
{
ArchiveCompressionLevels.Ultra => SevenZip.CompressionLevel.Ultra,
Expand All @@ -84,6 +53,7 @@ public ArchiveCreator()
ArchiveCompressionLevels.None => SevenZip.CompressionLevel.None,
_ => throw new ArgumentOutOfRangeException(nameof(CompressionLevel)),
};

private long SevenZipVolumeSize => SplittingSize switch
{
ArchiveSplittingSizes.None => 0L,
Expand All @@ -100,11 +70,63 @@ public ArchiveCreator()
_ => throw new ArgumentOutOfRangeException(nameof(SplittingSize)),
};

private IProgress<FileSystemProgress> _Progress;
public IProgress<FileSystemProgress> Progress
{
get => _Progress;
set
{
_Progress = value;
_fileSystemProgress = new(Progress, true, FileSystemStatusCode.InProgress);
_fileSystemProgress.Report(0);
}
}

/// <inheritdoc/>
public string ArchivePath { get; set; }

/// <inheritdoc/>
public string Directory { get; init; }

/// <inheritdoc/>
public string FileName { get; init; }

/// <inheritdoc/>
public string Password { get; init; }

/// <inheritdoc/>
public IEnumerable<string> Sources { get; init; }

/// <inheritdoc/>
public ArchiveFormats FileFormat { get; init; }

/// <inheritdoc/>
public ArchiveCompressionLevels CompressionLevel { get; init; }

/// <inheritdoc/>
public ArchiveSplittingSizes SplittingSize { get; init; }

public ArchiveCreator()
{
// Initialize
_fileSystemProgress = new(Progress, true, FileSystemStatusCode.InProgress);
_Progress = new Progress<FileSystemProgress>();
ArchivePath = string.Empty;
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
Sources = Enumerable.Empty<string>();
FileFormat = ArchiveFormats.Zip;
CompressionLevel = ArchiveCompressionLevels.Normal;
SplittingSize = ArchiveSplittingSizes.None;

_fileSystemProgress.Report(0);
}

/// <inheritdoc/>
public string GetArchivePath(string suffix = "")
{
return Path.Combine(Directory, $"{FileName}{suffix}{ArchiveExtension}");
}

/// <inheritdoc/>
public async Task<bool> RunCreationAsync()
{
string[] sources = Sources.ToArray();
Expand All @@ -125,54 +147,55 @@ public async Task<bool> RunCreationAsync()

try
{
var files = sources.Where(source => File.Exists(source)).ToArray();
var directories = sources.Where(source => System.IO.Directory.Exists(source));
var files = sources.Where(File.Exists).ToArray();
var directories = sources.Where(SystemIO.Directory.Exists);

itemsAmount = files.Length + directories.Count();
_itemsAmount = files.Length + directories.Count();

foreach (string directory in directories)
{
await compressor.CompressDirectoryAsync(directory, archivePath, Password);
await compressor.CompressDirectoryAsync(directory, ArchivePath, Password);

compressor.CompressionMode = CompressionMode.Append;
}

if (files.Any())
{
if (string.IsNullOrEmpty(Password))
await compressor.CompressFilesAsync(archivePath, files);
await compressor.CompressFilesAsync(ArchivePath, files);
else
await compressor.CompressFilesEncryptedAsync(archivePath, Password, files);
await compressor.CompressFilesEncryptedAsync(ArchivePath, Password, files);
}

return true;
}
catch (Exception ex)
{
var logger = Ioc.Default.GetRequiredService<ILogger<App>>();
logger?.LogWarning(ex, $"Error compressing folder: {archivePath}");
logger?.LogWarning(ex, $"Error compressing folder: {ArchivePath}");

return false;
}
}

private void Compressor_CompressionFinished(object? sender, EventArgs e)
{
if (++processedItems == itemsAmount)
if (++_processedItems == _itemsAmount)
{
fsProgress.Percentage = null;
fsProgress.ReportStatus(Shared.Enums.FileSystemStatusCode.Success);
_fileSystemProgress.Percentage = null;
_fileSystemProgress.ReportStatus(FileSystemStatusCode.Success);
}
else
{
fsProgress.Percentage = processedItems * 100 / itemsAmount;
fsProgress.Report(fsProgress.Percentage);
_fileSystemProgress.Percentage = _processedItems * 100 / _itemsAmount;
_fileSystemProgress.Report(_fileSystemProgress.Percentage);
}
}

private void Compressor_Compressing(object? _, ProgressEventArgs e)
{
fsProgress.Percentage += e.PercentDelta / itemsAmount;
fsProgress.Report(fsProgress.Percentage);
_fileSystemProgress.Percentage += e.PercentDelta / _itemsAmount;
_fileSystemProgress.Report(_fileSystemProgress.Percentage);
}
}
}
13 changes: 0 additions & 13 deletions src/Files.App/Filesystem/Archive/ArchiveFormats.cs

This file was deleted.

20 changes: 0 additions & 20 deletions src/Files.App/Filesystem/Archive/ArchiveSplittingSizes.cs

This file was deleted.

43 changes: 43 additions & 0 deletions src/Files.App/Filesystem/Archive/IArchiveCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,67 @@

namespace Files.App.Filesystem.Archive
{
/// <summary>
/// Represents an interface for archive creation support.
/// </summary>
public interface IArchiveCreator
{
/// <summary>
/// File path to archive.
/// </summary>
string ArchivePath { get; set; }

/// <summary>
/// Directory name.
/// </summary>
string Directory { get; }

/// <summary>
/// File name.
/// </summary>
string FileName { get; }

/// <summary>
/// Password.
/// </summary>
string Password { get; }

/// <summary>
/// Source.
/// </summary>
IEnumerable<string> Sources { get; }

/// <summary>
/// Archive file format.
/// </summary>
ArchiveFormats FileFormat { get; }

/// <summary>
/// Archive compression level.
/// </summary>
ArchiveCompressionLevels CompressionLevel { get; }

/// <summary>
/// 7zip archive splitting size.
/// </summary>
ArchiveSplittingSizes SplittingSize { get; }

/// <summary>
/// Archiving progress.
/// </summary>
IProgress<FileSystemProgress> Progress { get; set; }

/// <summary>
/// Get path which target will be archived to.
/// </summary>
/// <param name="suffix"></param>
/// <returns></returns>
string GetArchivePath(string suffix = "");

/// <summary>
/// Run archive creation command.
/// </summary>
/// <returns></returns>
Task<bool> RunCreationAsync();
}
}
12 changes: 7 additions & 5 deletions src/Files.App/Filesystem/BaseStorage/BaseBasicProperties.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using System;

namespace Files.App.Filesystem.StorageItems
{
public class BaseBasicProperties : BaseStorageItemExtraProperties
{
public virtual ulong Size => 0;
public virtual ulong Size
=> 0;

public virtual DateTimeOffset ItemDate
=> DateTimeOffset.Now;

public virtual DateTimeOffset ItemDate => DateTimeOffset.Now;
public virtual DateTimeOffset DateModified => DateTimeOffset.Now;
public virtual DateTimeOffset DateModified
=> DateTimeOffset.Now;
}
}
Loading