123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- using MediaBrowser.Controller.Entities;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using MediaBrowser.Controller.IO;
- namespace MediaBrowser.Controller.Library
- {
- /// <summary>
- /// These are arguments relating to the file system that are collected once and then referred to
- /// whenever needed. Primarily for entity resolution.
- /// </summary>
- public class ItemResolveArgs : EventArgs
- {
- /// <summary>
- /// Gets the file system children.
- /// </summary>
- /// <value>The file system children.</value>
- public IEnumerable<WIN32_FIND_DATA> FileSystemChildren
- {
- get { return FileSystemDictionary.Values; }
- }
- /// <summary>
- /// Gets or sets the file system dictionary.
- /// </summary>
- /// <value>The file system dictionary.</value>
- public Dictionary<string, WIN32_FIND_DATA> FileSystemDictionary { get; set; }
- /// <summary>
- /// Gets or sets the parent.
- /// </summary>
- /// <value>The parent.</value>
- public Folder Parent { get; set; }
- /// <summary>
- /// Gets or sets the file info.
- /// </summary>
- /// <value>The file info.</value>
- public WIN32_FIND_DATA FileInfo { get; set; }
- /// <summary>
- /// Gets or sets the path.
- /// </summary>
- /// <value>The path.</value>
- public string Path { get; set; }
- /// <summary>
- /// Gets a value indicating whether this instance is directory.
- /// </summary>
- /// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
- public bool IsDirectory
- {
- get
- {
- return FileInfo.dwFileAttributes.HasFlag(FileAttributes.Directory);
- }
- }
- /// <summary>
- /// Gets a value indicating whether this instance is hidden.
- /// </summary>
- /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
- public bool IsHidden
- {
- get
- {
- return FileInfo.IsHidden;
- }
- }
- /// <summary>
- /// Gets a value indicating whether this instance is system file.
- /// </summary>
- /// <value><c>true</c> if this instance is system file; otherwise, <c>false</c>.</value>
- public bool IsSystemFile
- {
- get
- {
- return FileInfo.IsSystemFile;
- }
- }
- /// <summary>
- /// Gets a value indicating whether this instance is vf.
- /// </summary>
- /// <value><c>true</c> if this instance is vf; otherwise, <c>false</c>.</value>
- public bool IsVf
- {
- // we should be considered a virtual folder if we are a child of one of the children of the system root folder.
- // this is a bit of a trick to determine that... the directory name of a sub-child of the root will start with
- // the root but not be equal to it
- get
- {
- if (!IsDirectory)
- {
- return false;
- }
- var parentDir = FileInfo.Path != null ? System.IO.Path.GetDirectoryName(FileInfo.Path) ?? string.Empty : string.Empty;
-
- return (parentDir.Length > Kernel.Instance.ApplicationPaths.RootFolderPath.Length
- && parentDir.StartsWith(Kernel.Instance.ApplicationPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase));
- }
- }
- /// <summary>
- /// Gets a value indicating whether this instance is physical root.
- /// </summary>
- /// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value>
- public bool IsPhysicalRoot
- {
- get
- {
- return IsDirectory && Path.Equals(Kernel.Instance.ApplicationPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase);
- }
- }
- /// <summary>
- /// Gets a value indicating whether this instance is root.
- /// </summary>
- /// <value><c>true</c> if this instance is root; otherwise, <c>false</c>.</value>
- public bool IsRoot
- {
- get
- {
- return Parent == null;
- }
- }
- /// <summary>
- /// Gets or sets the additional locations.
- /// </summary>
- /// <value>The additional locations.</value>
- private List<string> AdditionalLocations { get; set; }
- /// <summary>
- /// Adds the additional location.
- /// </summary>
- /// <param name="path">The path.</param>
- /// <exception cref="System.ArgumentNullException"></exception>
- public void AddAdditionalLocation(string path)
- {
- if (string.IsNullOrEmpty(path))
- {
- throw new ArgumentNullException();
- }
-
- if (AdditionalLocations == null)
- {
- AdditionalLocations = new List<string>();
- }
- AdditionalLocations.Add(path);
- }
- /// <summary>
- /// Gets the physical locations.
- /// </summary>
- /// <value>The physical locations.</value>
- public IEnumerable<string> PhysicalLocations
- {
- get
- {
- var paths = string.IsNullOrWhiteSpace(Path) ? new string[] {} : new[] {Path};
- return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations);
- }
- }
- /// <summary>
- /// Store these to reduce disk access in Resolvers
- /// </summary>
- /// <value>The metadata file dictionary.</value>
- private Dictionary<string, WIN32_FIND_DATA> MetadataFileDictionary { get; set; }
- /// <summary>
- /// Gets the metadata files.
- /// </summary>
- /// <value>The metadata files.</value>
- public IEnumerable<WIN32_FIND_DATA> MetadataFiles
- {
- get
- {
- if (MetadataFileDictionary != null)
- {
- return MetadataFileDictionary.Values;
- }
- return new WIN32_FIND_DATA[] {};
- }
- }
- /// <summary>
- /// Adds the metadata file.
- /// </summary>
- /// <param name="path">The path.</param>
- /// <exception cref="System.IO.FileNotFoundException"></exception>
- public void AddMetadataFile(string path)
- {
- var file = FileSystem.GetFileData(path);
- if (!file.HasValue)
- {
- throw new FileNotFoundException(path);
- }
- AddMetadataFile(file.Value);
- }
- /// <summary>
- /// Adds the metadata file.
- /// </summary>
- /// <param name="fileInfo">The file info.</param>
- public void AddMetadataFile(WIN32_FIND_DATA fileInfo)
- {
- AddMetadataFiles(new[] { fileInfo });
- }
- /// <summary>
- /// Adds the metadata files.
- /// </summary>
- /// <param name="files">The files.</param>
- /// <exception cref="System.ArgumentNullException"></exception>
- public void AddMetadataFiles(IEnumerable<WIN32_FIND_DATA> files)
- {
- if (files == null)
- {
- throw new ArgumentNullException();
- }
-
- if (MetadataFileDictionary == null)
- {
- MetadataFileDictionary = new Dictionary<string, WIN32_FIND_DATA>(StringComparer.OrdinalIgnoreCase);
- }
- foreach (var file in files)
- {
- MetadataFileDictionary[file.cFileName] = file;
- }
- }
- /// <summary>
- /// Gets the name of the file system entry by.
- /// </summary>
- /// <param name="name">The name.</param>
- /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
- /// <exception cref="System.ArgumentNullException"></exception>
- public WIN32_FIND_DATA? GetFileSystemEntryByName(string name)
- {
- if (string.IsNullOrEmpty(name))
- {
- throw new ArgumentNullException();
- }
-
- return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
- }
- /// <summary>
- /// Gets the file system entry by path.
- /// </summary>
- /// <param name="path">The path.</param>
- /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
- /// <exception cref="System.ArgumentNullException"></exception>
- public WIN32_FIND_DATA? GetFileSystemEntryByPath(string path)
- {
- if (string.IsNullOrEmpty(path))
- {
- throw new ArgumentNullException();
- }
-
- if (FileSystemDictionary != null)
- {
- WIN32_FIND_DATA entry;
- if (FileSystemDictionary.TryGetValue(path, out entry))
- {
- return entry;
- }
- }
- return null;
- }
- /// <summary>
- /// Gets the meta file by path.
- /// </summary>
- /// <param name="path">The path.</param>
- /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
- /// <exception cref="System.ArgumentNullException"></exception>
- public WIN32_FIND_DATA? GetMetaFileByPath(string path)
- {
- if (string.IsNullOrEmpty(path))
- {
- throw new ArgumentNullException();
- }
-
- if (MetadataFileDictionary != null)
- {
- WIN32_FIND_DATA entry;
- if (MetadataFileDictionary.TryGetValue(System.IO.Path.GetFileName(path), out entry))
- {
- return entry;
- }
- }
- return GetFileSystemEntryByPath(path);
- }
- /// <summary>
- /// Gets the name of the meta file by.
- /// </summary>
- /// <param name="name">The name.</param>
- /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
- /// <exception cref="System.ArgumentNullException"></exception>
- public WIN32_FIND_DATA? GetMetaFileByName(string name)
- {
- if (string.IsNullOrEmpty(name))
- {
- throw new ArgumentNullException();
- }
-
- if (MetadataFileDictionary != null)
- {
- WIN32_FIND_DATA entry;
- if (MetadataFileDictionary.TryGetValue(name, out entry))
- {
- return entry;
- }
- }
- return GetFileSystemEntryByName(name);
- }
- /// <summary>
- /// Determines whether [contains meta file by name] [the specified name].
- /// </summary>
- /// <param name="name">The name.</param>
- /// <returns><c>true</c> if [contains meta file by name] [the specified name]; otherwise, <c>false</c>.</returns>
- public bool ContainsMetaFileByName(string name)
- {
- return GetMetaFileByName(name).HasValue;
- }
- /// <summary>
- /// Determines whether [contains file system entry by name] [the specified name].
- /// </summary>
- /// <param name="name">The name.</param>
- /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
- public bool ContainsFileSystemEntryByName(string name)
- {
- return GetFileSystemEntryByName(name).HasValue;
- }
- #region Equality Overrides
- /// <summary>
- /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
- /// </summary>
- /// <param name="obj">The object to compare with the current object.</param>
- /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
- public override bool Equals(object obj)
- {
- return (Equals(obj as ItemResolveArgs));
- }
- /// <summary>
- /// Returns a hash code for this instance.
- /// </summary>
- /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
- public override int GetHashCode()
- {
- return Path.GetHashCode();
- }
- /// <summary>
- /// Equalses the specified args.
- /// </summary>
- /// <param name="args">The args.</param>
- /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
- protected bool Equals(ItemResolveArgs args)
- {
- if (args != null)
- {
- if (args.Path == null && Path == null) return true;
- return args.Path != null && args.Path.Equals(Path, StringComparison.OrdinalIgnoreCase);
- }
- return false;
- }
- #endregion
- }
- }
|