/* * GNU AFFERO GENERAL PUBLIC LICENSE * Version 3, 19 November 2007 * Copyright (C) 2007 Free Software Foundation, Inc. * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. */ using System; using System.IO; namespace UVtools.Core.Objects; public class GenericFileRepresentation : BindableBase, ICloneable, IComparable, IEquatable, IComparable, IEquatable { #region Members private string _filePath = null!; #endregion #region Properties /// /// Gets or sets the file path /// public string FilePath { get => _filePath; set { if (RaiseAndSetIfChanged(ref _filePath, value)) return; RaisePropertyChanged(nameof(FileName)); RaisePropertyChanged(nameof(Exists)); RaisePropertyChanged(nameof(FileInfo)); } } /// /// Gets the file name with extension /// public string FileName => Path.GetFileName(_filePath); /// /// Gets the file name without extension /// public string FileNameWithoutExtension => Path.GetFileNameWithoutExtension(_filePath); /// /// Gets the file extension. The returned value includes the period (".") /// public string FileExtension => Path.GetExtension(_filePath); /// /// Gets if the file exists /// public bool Exists => File.Exists(FilePath); /// /// Gets an instance on the /// public FileInfo FileInfo => new(_filePath); #endregion #region Constructor public GenericFileRepresentation() { } public GenericFileRepresentation(string filePath) { _filePath = filePath; } #endregion #region Methods /// /// Checks if the ends with /// /// Extension name /// True if found, otherwise false public bool IsExtension(string extension) { if (string.IsNullOrWhiteSpace(extension)) return false; if (extension[0] != '.') extension = $".{extension}"; return _filePath.EndsWith(extension, StringComparison.OrdinalIgnoreCase); } #endregion #region Overrides public override string ToString() { return FileName; } public object Clone() { return MemberwiseClone(); } public int CompareTo(GenericFileRepresentation? other) { if (ReferenceEquals(this, other)) return 0; if (ReferenceEquals(null, other)) return 1; return string.Compare(FileName, other.FileName, StringComparison.Ordinal); } public int CompareTo(string? other) { return string.Compare(FileName, other, StringComparison.Ordinal); } public bool Equals(GenericFileRepresentation? other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return _filePath == other._filePath; } public bool Equals(string? other) { return _filePath.Equals(other); } public override bool Equals(object? obj) { return ReferenceEquals(this, obj) || obj is GenericFileRepresentation other && Equals(other); } public override int GetHashCode() { return _filePath.GetHashCode(); } #endregion }