WebDavMtpStore.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using MediaDevices;
  7. using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Stores;
  8. namespace Mtp2Dav.MtpStore._1d2086a502937936ebc6bfe19cfa15d855be1c31
  9. {
  10. [DebuggerDisplay("Mtp Store ({_device})")]
  11. internal class WebDavMtpStore : IWebDavStore
  12. {
  13. public WebDavMtpStore(MediaDevice device, bool enableRo)
  14. {
  15. if (device == null)
  16. throw new ArgumentNullException(nameof(device));
  17. _device = device;
  18. _enableRo = enableRo;
  19. }
  20. private readonly MediaDevice _device;
  21. private readonly bool _enableRo;
  22. private readonly Dictionary<string, WebDavMtpStoreItem[]> _dirCache = new Dictionary<string, WebDavMtpStoreItem[]>();
  23. private readonly Dictionary<string, MediaFileInfo> _fileCache = new Dictionary<string, MediaFileInfo>();
  24. //For WebDAVSharp.Server
  25. public WebDavMtpStoreCollection Root
  26. {
  27. get
  28. {
  29. //HACK:
  30. var rootEntries = GetEntries("\\");
  31. if (rootEntries.Length == 1 && rootEntries[0].IsCollection)
  32. return new WebDavMtpStoreCollection(this, rootEntries[0].ItemPath);
  33. return new WebDavMtpStoreCollection(this, "\\");
  34. }
  35. }
  36. IWebDavStoreCollection IWebDavStore.Root => Root;
  37. //My opti
  38. public WebDavMtpStoreItem[] GetEntries(string path, bool useCache = true)
  39. {
  40. #if !DEBUG
  41. if (_dirCache.ContainsKey(path) && useCache) return _dirCache[path];
  42. #endif
  43. var entries = new List<WebDavMtpStoreItem>();
  44. entries.AddRange(_device.EnumerateDirectories(path).Select(p => new WebDavMtpStoreCollection(this, path + p + "\\")));
  45. entries.AddRange(_device.EnumerateFiles(path).Select(p => new WebDavMtpStoreDocument(this, path + p)));
  46. return _dirCache[path] = entries.ToArray();
  47. }
  48. public MediaFileInfo GetInfo(string path, bool useCache = true)
  49. {
  50. #if !DEBUG
  51. if (_fileCache.ContainsKey(path) && useCache) return _fileCache[path];
  52. #endif
  53. try
  54. {
  55. return _fileCache[path] = _device.GetFileInfo(path);
  56. }
  57. catch (Exception ex)
  58. {
  59. Debug.Print(ex.ToString());
  60. return _fileCache[path] = null;
  61. }
  62. }
  63. public void FullBuf()
  64. {
  65. BufDir("\\");
  66. }
  67. private void BufDir(string path)
  68. {
  69. Console.WriteLine($"bufing - dir - {path}");
  70. foreach (var item in GetEntries(path))
  71. {
  72. if (item.IsCollection) BufDir(item.ItemPath);
  73. else
  74. {
  75. Console.WriteLine($"bufing - file - {item.ItemPath}");
  76. GetInfo(path);
  77. }
  78. }
  79. }
  80. public Stream OpenReadStream(string path)
  81. {
  82. if (!_enableRo)
  83. throw new NotSupportedException();
  84. var ms = new MemoryStream();
  85. _device.DownloadFile(path, ms);
  86. ms.Position = 0;
  87. return ms;
  88. }
  89. }
  90. }