Files.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace OpenVIII
  7. {
  8. namespace Movie
  9. {
  10. public class Files : IReadOnlyList<string>
  11. {
  12. #region Fields
  13. private static readonly IReadOnlyList<string> Extensions = new[] { ".avi", ".mkv", ".mp4", ".bik" }.ToList().AsReadOnly();
  14. private readonly IReadOnlyList<string> _files;
  15. #endregion Fields
  16. #region Constructors
  17. private Files()
  18. {
  19. var a = (ArchiveZzz)ArchiveZzz.Load(Memory.Archives.ZZZ_OTHER);
  20. List<string> files;
  21. if (a != null)
  22. {
  23. var listOfFiles = a.GetListOfFiles();
  24. files = (from file in listOfFiles
  25. from extension in Extensions
  26. where file.EndsWith(extension, StringComparison.OrdinalIgnoreCase)
  27. orderby Path.GetFileNameWithoutExtension(file)
  28. select file).ToList();
  29. ZZZ = true;
  30. }
  31. else
  32. {
  33. //Gather all movie files.
  34. var d = Directories.Instance;
  35. files = (from directory in d
  36. where Directory.Exists(directory)
  37. from file in Directory.GetFiles(directory, "*", SearchOption.AllDirectories)
  38. from extension in Extensions
  39. where file.EndsWith(extension, StringComparison.OrdinalIgnoreCase)
  40. orderby Path.GetFileNameWithoutExtension(file)
  41. select file).ToList();
  42. }
  43. //Remove duplicate movies ignoring extension that have same name.
  44. (from s1 in files.Select((value, key) => new { Key = key, Value = value })
  45. from s2 in files.Select((value, key) => new { Key = key, Value = value })
  46. where s1?.Value != null
  47. where s2?.Value != null
  48. where s1.Key < s2.Key
  49. where Path.GetFileNameWithoutExtension(s1.Value ?? throw new NullReferenceException($"{nameof(Files)}::{s1} value cannot be null")).Equals(Path.GetFileNameWithoutExtension(s2.Value), StringComparison.OrdinalIgnoreCase)
  50. orderby s2.Key descending
  51. select s2.Key).ForEach(key => files.RemoveAt(key));
  52. foreach (var s in files)
  53. Memory.Log.WriteLine($"{nameof(Movie)} :: {nameof(Files)} :: {s} ");
  54. _files = files.AsReadOnly();
  55. }
  56. #endregion Constructors
  57. #region Properties
  58. public static Files Instance { get; } = new Files();
  59. public int Count =>
  60. _files.Count;
  61. public bool ZZZ { get; }
  62. #endregion Properties
  63. #region Indexers
  64. public string this[int index] => _files[index];
  65. #endregion Indexers
  66. #region Methods
  67. public bool Exists(int i) => Count > i && i >= 0 && File.Exists(_files[i]);
  68. public IEnumerator<string> GetEnumerator() => _files.GetEnumerator();
  69. IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_files).GetEnumerator();
  70. #endregion Methods
  71. }
  72. }
  73. }