Base features
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using FileTime.Core.Command;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.StateManagement;
|
||||
|
||||
namespace FileTime.Providers.Local.CommandHandlers
|
||||
{
|
||||
public class CopyCommandHandler : ICommandHandler
|
||||
{
|
||||
private readonly List<Thread> _copyOperations = new();
|
||||
private readonly ElementCreationStates _elementCreationStates;
|
||||
|
||||
public CopyCommandHandler(ElementCreationStates elementCreationStates)
|
||||
{
|
||||
_elementCreationStates = elementCreationStates;
|
||||
}
|
||||
|
||||
public bool CanHandle(object command)
|
||||
{
|
||||
if (command is not CopyCommand copyCommand) return false;
|
||||
|
||||
if (copyCommand.Target != null && copyCommand.Target is not LocalFolder) return false;
|
||||
|
||||
if (copyCommand.Sources.Any(s => s.ContentProvider is not LocalContentProvider)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Execute(object command)
|
||||
{
|
||||
if (command is not CopyCommand copyCommand) throw new ArgumentException($"Can not execute command of type '{command.GetType()}'.");
|
||||
|
||||
var thread = new Thread(() => copyCommand.Execute(CopyElement));
|
||||
thread.Start();
|
||||
|
||||
_copyOperations.Add(thread);
|
||||
}
|
||||
|
||||
public void CopyElement(IAbsolutePath sourcePath, IAbsolutePath targetPath)
|
||||
{
|
||||
using var sourceStream = File.OpenRead(sourcePath.Path);
|
||||
using var sourceReader = new BinaryReader(sourceStream);
|
||||
|
||||
using var targetStream = File.OpenWrite(targetPath.Path);
|
||||
using var targetWriter = new BinaryWriter(targetStream);
|
||||
|
||||
var bufferSize = 1024 * 1024;
|
||||
byte[] dataRead;
|
||||
|
||||
do
|
||||
{
|
||||
dataRead = sourceReader.ReadBytes(bufferSize);
|
||||
targetWriter.Write(dataRead);
|
||||
targetWriter.Flush();
|
||||
}
|
||||
while (dataRead.Length > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace FileTime.Providers.Local.Extensions
|
||||
{
|
||||
public static class FormatExtensions
|
||||
{
|
||||
private const long OneKiloByte = 1024;
|
||||
private const long OneMegaByte = OneKiloByte * 1024;
|
||||
private const long OneGigaByte = OneMegaByte * 1024;
|
||||
private const long OneTerraByte = OneGigaByte * 1024;
|
||||
|
||||
public static string ToSizeString(this long fileSize, int precision = 1)
|
||||
{
|
||||
var fileSizeD = (decimal)fileSize;
|
||||
var (size, suffix) = fileSize switch
|
||||
{
|
||||
> OneTerraByte => (fileSizeD / OneTerraByte, "T"),
|
||||
> OneGigaByte => (fileSizeD / OneGigaByte, "G"),
|
||||
> OneMegaByte => (fileSizeD / OneMegaByte, "M"),
|
||||
> OneKiloByte => (fileSizeD / OneKiloByte, "K"),
|
||||
_ => (fileSizeD, "B")
|
||||
};
|
||||
|
||||
return string.Format("{0:N" + precision + "}", size).TrimEnd('0').Replace(',', '.').TrimEnd('.') + " " + suffix;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\FileTime.Core\FileTime.Core.csproj"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0"/>
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,61 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
|
||||
namespace FileTime.Providers.Local
|
||||
{
|
||||
public class LocalContentProvider : IContentProvider
|
||||
{
|
||||
public IReadOnlyList<IContainer> RootContainers { get; }
|
||||
|
||||
public IReadOnlyList<IItem> Items => RootContainers;
|
||||
|
||||
public IReadOnlyList<IContainer> Containers => RootContainers;
|
||||
|
||||
public IReadOnlyList<IElement> Elements { get; } = new List<IElement>();
|
||||
|
||||
public string Name { get; } = "local";
|
||||
|
||||
public string? FullName { get; }
|
||||
public bool IsHidden => false;
|
||||
|
||||
public IContentProvider Provider => this;
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
|
||||
public LocalContentProvider()
|
||||
{
|
||||
var rootDirectories = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
|
||||
? new DirectoryInfo("/").GetDirectories()
|
||||
: Environment.GetLogicalDrives().Select(d => new DirectoryInfo(d));
|
||||
|
||||
FullName = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "" : null;
|
||||
|
||||
RootContainers = rootDirectories.Select(d => new LocalFolder(d, this, this)).OrderBy(d => d.Name).ToList().AsReadOnly();
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
{
|
||||
var pathParts = path.TrimStart(Constants.SeparatorChar).Split(Constants.SeparatorChar);
|
||||
var rootContainer = RootContainers.FirstOrDefault(c => c.Name == pathParts[0]);
|
||||
|
||||
if (rootContainer == null) return null;
|
||||
|
||||
return rootContainer.GetByPath(string.Join(Constants.SeparatorChar, pathParts.Skip(1)));
|
||||
}
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
}
|
||||
|
||||
public IContainer? GetParent()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
public IContainer CreateContainer(string name) => throw new NotSupportedException();
|
||||
public IElement CreateElement(string name) => throw new NotSupportedException();
|
||||
public bool IsExists(string name) => Items.Any(i => i.Name == name);
|
||||
|
||||
public void Delete() => throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
40
src/Providers/FileTime.Providers.Local/LocalFile.cs
Normal file
40
src/Providers/FileTime.Providers.Local/LocalFile.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
using FileTime.Providers.Local.Extensions;
|
||||
using Mono.Unix;
|
||||
|
||||
namespace FileTime.Providers.Local
|
||||
{
|
||||
public class LocalFile : IElement
|
||||
{
|
||||
private readonly FileInfo _file;
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string FullName { get; }
|
||||
|
||||
public IContentProvider Provider { get; }
|
||||
|
||||
public bool IsHidden => (_file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
|
||||
public bool IsSpecial =>
|
||||
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
|
||||
&& (new UnixFileInfo(_file.FullName).FileAccessPermissions & FileAccessPermissions.UserExecute) == FileAccessPermissions.UserExecute;
|
||||
|
||||
public LocalFile(FileInfo file, IContentProvider contentProvider)
|
||||
{
|
||||
_file = file;
|
||||
|
||||
Name = file.Name;
|
||||
FullName = file.FullName;
|
||||
Provider = contentProvider;
|
||||
}
|
||||
|
||||
public string GetPrimaryAttributeText() => _file.Length.ToSizeString();
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
_file.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
125
src/Providers/FileTime.Providers.Local/LocalFolder.cs
Normal file
125
src/Providers/FileTime.Providers.Local/LocalFolder.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Providers;
|
||||
|
||||
namespace FileTime.Providers.Local
|
||||
{
|
||||
public class LocalFolder : IContainer
|
||||
{
|
||||
private IReadOnlyList<IItem>? _items;
|
||||
private IReadOnlyList<IContainer>? _containers;
|
||||
private IReadOnlyList<IElement>? _elements;
|
||||
private readonly IContainer? _parent;
|
||||
|
||||
public bool IsHidden => (Directory.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
|
||||
public DirectoryInfo Directory { get; }
|
||||
public IContentProvider Provider { get; }
|
||||
|
||||
public IReadOnlyList<IItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_items == null) Refresh();
|
||||
return _items!;
|
||||
}
|
||||
|
||||
private set => _items = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IContainer> Containers
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_containers == null) Refresh();
|
||||
return _containers!;
|
||||
}
|
||||
|
||||
private set => _containers = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<IElement> Elements
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_elements == null) Refresh();
|
||||
return _elements!;
|
||||
}
|
||||
|
||||
private set => _elements = value;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public string FullName { get; }
|
||||
|
||||
public event EventHandler? Refreshed;
|
||||
|
||||
public LocalFolder(DirectoryInfo directory, IContentProvider contentProvider, IContainer? parent)
|
||||
{
|
||||
Directory = directory;
|
||||
_parent = parent;
|
||||
|
||||
Name = directory.Name;
|
||||
FullName = parent?.FullName == null ? Name : parent.FullName + Constants.SeparatorChar + Name;
|
||||
Provider = contentProvider;
|
||||
}
|
||||
|
||||
public IContainer? GetParent() => _parent;
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
_containers = new List<IContainer>();
|
||||
_elements = new List<IElement>();
|
||||
|
||||
try
|
||||
{
|
||||
_containers = Directory.GetDirectories().Select(d => new LocalFolder(d, Provider, this)).OrderBy(d => d.Name).ToList().AsReadOnly();
|
||||
_elements = Directory.GetFiles().Select(f => new LocalFile(f, Provider)).OrderBy(f => f.Name).ToList().AsReadOnly();
|
||||
}
|
||||
catch { }
|
||||
|
||||
_items = _containers.Cast<IItem>().Concat(_elements).ToList().AsReadOnly();
|
||||
Refreshed?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public IItem? GetByPath(string path)
|
||||
{
|
||||
var paths = path.Split(Constants.SeparatorChar);
|
||||
|
||||
var item = Items.FirstOrDefault(i => i.Name == paths[0]);
|
||||
|
||||
if (paths.Length == 1)
|
||||
{
|
||||
return item;
|
||||
}
|
||||
|
||||
if (item is IContainer container)
|
||||
{
|
||||
return container.GetByPath(string.Join(Constants.SeparatorChar, paths.Skip(1)));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
public IContainer CreateContainer(string name)
|
||||
{
|
||||
Directory.CreateSubdirectory(name);
|
||||
Refresh();
|
||||
|
||||
return _containers!.FirstOrDefault(c => c.Name == name)!;
|
||||
}
|
||||
|
||||
public IElement CreateElement(string name)
|
||||
{
|
||||
using (File.Create(Path.Combine(Directory.FullName, name))) { }
|
||||
Refresh();
|
||||
|
||||
return _elements!.FirstOrDefault(e => e.Name == name)!;
|
||||
}
|
||||
|
||||
public bool IsExists(string name) => Items.Any(i => i.Name == name);
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
Directory.Delete(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/Providers/FileTime.Providers.Local/Startup.cs
Normal file
14
src/Providers/FileTime.Providers.Local/Startup.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using FileTime.Providers.Local.CommandHandlers;
|
||||
|
||||
namespace FileTime.Providers.Local
|
||||
{
|
||||
public static class Startup
|
||||
{
|
||||
public static Type[] GetCommandHandlers()
|
||||
{
|
||||
return new Type[]{
|
||||
typeof(CopyCommandHandler)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user