Multi-Platform Package Manager for Stable Diffusion
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.8 KiB

using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text.Json.Serialization;
using StabilityMatrix.Core.Converters.Json;
namespace StabilityMatrix.Core.Models.FileInterfaces;
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[JsonConverter(typeof(StringJsonConverter<FilePath>))]
public class FilePath : FileSystemPath, IPathObject
{
private FileInfo? _info;
1 year ago
// ReSharper disable once MemberCanBePrivate.Global
[JsonIgnore]
public FileInfo Info => _info ??= new FileInfo(FullPath);
[JsonIgnore]
public bool IsSymbolicLink
{
get
{
Info.Refresh();
return Info.Attributes.HasFlag(FileAttributes.ReparsePoint);
}
}
1 year ago
[JsonIgnore]
public bool Exists => Info.Exists;
1 year ago
[JsonIgnore]
public string Name => Info.Name;
1 year ago
[JsonIgnore]
public string NameWithoutExtension => Path.GetFileNameWithoutExtension(Info.Name);
/// <summary>
/// Get the directory of the file.
/// </summary>
[JsonIgnore]
public DirectoryPath? Directory
{
get
{
try
{
1 year ago
return Info.Directory == null ? null : new DirectoryPath(Info.Directory);
}
catch (DirectoryNotFoundException)
{
return null;
}
}
}
1 year ago
public FilePath(string path)
: base(path) { }
public FilePath(FileInfo fileInfo)
: base(fileInfo.FullName)
{
_info = fileInfo;
}
1 year ago
public FilePath(FileSystemPath path)
: base(path) { }
public FilePath(params string[] paths)
: base(paths) { }
public long GetSize()
{
Info.Refresh();
return Info.Length;
}
1 year ago
public long GetSize(bool includeSymbolicLinks)
{
1 year ago
if (!includeSymbolicLinks && IsSymbolicLink)
return 0;
return GetSize();
}
public Task<long> GetSizeAsync(bool includeSymbolicLinks)
{
return Task.Run(() => GetSize(includeSymbolicLinks));
}
1 year ago
/// <summary> Creates an empty file. </summary>
public void Create() => File.Create(FullPath).Close();
1 year ago
/// <summary> Deletes the file </summary>
public void Delete() => File.Delete(FullPath);
1 year ago
// Methods specific to files
1 year ago
/// <summary> Read text </summary>
public string ReadAllText() => File.ReadAllText(FullPath);
1 year ago
/// <summary> Read text asynchronously </summary>
public Task<string> ReadAllTextAsync(CancellationToken ct = default)
{
return File.ReadAllTextAsync(FullPath, ct);
}
1 year ago
/// <summary> Write text </summary>
public void WriteAllText(string text) => File.WriteAllText(FullPath, text, Encoding.UTF8);
1 year ago
/// <summary> Write text asynchronously </summary>
public Task WriteAllTextAsync(string text, CancellationToken ct = default)
{
return File.WriteAllTextAsync(FullPath, text, Encoding.UTF8, ct);
}
1 year ago
/// <summary> Read bytes </summary>
public byte[] ReadAllBytes() => File.ReadAllBytes(FullPath);
1 year ago
/// <summary> Read bytes asynchronously </summary>
public Task<byte[]> ReadAllBytesAsync(CancellationToken ct = default)
{
return File.ReadAllBytesAsync(FullPath, ct);
}
1 year ago
/// <summary> Write bytes </summary>
public void WriteAllBytes(byte[] bytes) => File.WriteAllBytes(FullPath, bytes);
1 year ago
/// <summary> Write bytes asynchronously </summary>
public Task WriteAllBytesAsync(byte[] bytes, CancellationToken ct = default)
{
return File.WriteAllBytesAsync(FullPath, bytes, ct);
}
1 year ago
/// <summary>
/// Move the file to a directory.
/// </summary>
public FilePath MoveTo(FilePath destinationFile)
{
Info.MoveTo(destinationFile.FullPath, true);
// Return the new path
return destinationFile;
}
1 year ago
/// <summary>
/// Move the file to a directory.
/// </summary>
public async Task<FilePath> MoveToAsync(DirectoryPath directory)
{
await Task.Run(() => Info.MoveTo(directory.FullPath)).ConfigureAwait(false);
// Return the new path
return directory.JoinFile(this);
}
1 year ago
/// <summary>
/// Move the file to a target path.
/// </summary>
public async Task<FilePath> MoveToAsync(FilePath destinationFile)
{
await Task.Run(() => Info.MoveTo(destinationFile.FullPath)).ConfigureAwait(false);
// Return the new path
return destinationFile;
}
1 year ago
/// <summary>
/// Copy the file to a target path.
/// </summary>
public FilePath CopyTo(FilePath destinationFile, bool overwrite = false)
{
Info.CopyTo(destinationFile.FullPath, overwrite);
// Return the new path
return destinationFile;
}
// Implicit conversions to and from string
public static implicit operator string(FilePath path) => path.FullPath;
1 year ago
public static implicit operator FilePath(string path) => new(path);
}