FileSystemRepository.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using MediaBrowser.Common.Extensions;
  2. using System;
  3. using System.IO;
  4. namespace MediaBrowser.Common.IO
  5. {
  6. /// <summary>
  7. /// This is a wrapper for storing large numbers of files within a directory on a file system.
  8. /// Simply pass a filename into GetResourcePath and it will return a full path location of where the file should be stored.
  9. /// </summary>
  10. public class FileSystemRepository
  11. {
  12. /// <summary>
  13. /// Gets or sets the path.
  14. /// </summary>
  15. /// <value>The path.</value>
  16. protected string Path { get; set; }
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="FileSystemRepository" /> class.
  19. /// </summary>
  20. /// <param name="path">The path.</param>
  21. /// <exception cref="System.ArgumentNullException"></exception>
  22. public FileSystemRepository(string path)
  23. {
  24. if (string.IsNullOrEmpty(path))
  25. {
  26. throw new ArgumentNullException();
  27. }
  28. Path = path;
  29. }
  30. /// <summary>
  31. /// Gets the full path of where a resource should be stored within the repository
  32. /// </summary>
  33. /// <param name="uniqueName">Name of the unique.</param>
  34. /// <param name="fileExtension">The file extension.</param>
  35. /// <returns>System.String.</returns>
  36. /// <exception cref="System.ArgumentNullException">
  37. /// </exception>
  38. public string GetResourcePath(string uniqueName, string fileExtension)
  39. {
  40. if (string.IsNullOrEmpty(uniqueName))
  41. {
  42. throw new ArgumentNullException("uniqueName");
  43. }
  44. if (string.IsNullOrEmpty(fileExtension))
  45. {
  46. throw new ArgumentNullException("fileExtension");
  47. }
  48. var filename = uniqueName.GetMD5() + fileExtension;
  49. return GetResourcePath(filename);
  50. }
  51. /// <summary>
  52. /// Gets the resource path.
  53. /// </summary>
  54. /// <param name="filename">The filename.</param>
  55. /// <returns>System.String.</returns>
  56. /// <exception cref="System.ArgumentNullException"></exception>
  57. public string GetResourcePath(string filename)
  58. {
  59. if (string.IsNullOrEmpty(filename))
  60. {
  61. throw new ArgumentNullException("filename");
  62. }
  63. var prefix = filename.Substring(0, 1);
  64. var path = System.IO.Path.Combine(Path, prefix);
  65. return System.IO.Path.Combine(path, filename);
  66. }
  67. /// <summary>
  68. /// Determines if a resource is present in the repository
  69. /// </summary>
  70. /// <param name="uniqueName">Name of the unique.</param>
  71. /// <param name="fileExtension">The file extension.</param>
  72. /// <returns><c>true</c> if the specified unique name contains resource; otherwise, <c>false</c>.</returns>
  73. public bool ContainsResource(string uniqueName, string fileExtension)
  74. {
  75. return ContainsFilePath(GetResourcePath(uniqueName, fileExtension));
  76. }
  77. /// <summary>
  78. /// Determines if a file with a given name is present in the repository
  79. /// </summary>
  80. /// <param name="filename">The filename.</param>
  81. /// <returns><c>true</c> if the specified filename contains filename; otherwise, <c>false</c>.</returns>
  82. /// <exception cref="System.ArgumentNullException"></exception>
  83. public bool ContainsFilename(string filename)
  84. {
  85. if (string.IsNullOrEmpty(filename))
  86. {
  87. throw new ArgumentNullException();
  88. }
  89. return ContainsFilePath(GetResourcePath(filename));
  90. }
  91. /// <summary>
  92. /// Determines if a file is present in the repository
  93. /// </summary>
  94. /// <param name="path">The path.</param>
  95. /// <returns><c>true</c> if [contains file path] [the specified path]; otherwise, <c>false</c>.</returns>
  96. /// <exception cref="System.ArgumentNullException"></exception>
  97. public bool ContainsFilePath(string path)
  98. {
  99. if (string.IsNullOrEmpty(path))
  100. {
  101. throw new ArgumentNullException();
  102. }
  103. return File.Exists(path);
  104. }
  105. }
  106. }