using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using MediaDevices; using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Stores; namespace Mtp2Dav.MtpStore._1d2086a502937936ebc6bfe19cfa15d855be1c31 { [DebuggerDisplay("Mtp Store ({_device})")] internal class WebDavMtpStore : IWebDavStore { public WebDavMtpStore(MediaDevice device, bool enableRo) { if (device == null) throw new ArgumentNullException(nameof(device)); _device = device; _enableRo = enableRo; } private readonly MediaDevice _device; private readonly bool _enableRo; private readonly Dictionary _dirCache = new Dictionary(); private readonly Dictionary _fileCache = new Dictionary(); //For WebDAVSharp.Server public WebDavMtpStoreCollection Root { get { //HACK: var rootEntries = GetEntries("\\"); if (rootEntries.Length == 1 && rootEntries[0].IsCollection) return new WebDavMtpStoreCollection(this, rootEntries[0].ItemPath); return new WebDavMtpStoreCollection(this, "\\"); } } IWebDavStoreCollection IWebDavStore.Root => Root; //My opti public WebDavMtpStoreItem[] GetEntries(string path, bool useCache = true) { #if !DEBUG if (_dirCache.ContainsKey(path) && useCache) return _dirCache[path]; #endif var entries = new List(); entries.AddRange(_device.EnumerateDirectories(path).Select(p => new WebDavMtpStoreCollection(this, path + p + "\\"))); entries.AddRange(_device.EnumerateFiles(path).Select(p => new WebDavMtpStoreDocument(this, path + p))); return _dirCache[path] = entries.ToArray(); } public MediaFileInfo GetInfo(string path, bool useCache = true) { #if !DEBUG if (_fileCache.ContainsKey(path) && useCache) return _fileCache[path]; #endif try { return _fileCache[path] = _device.GetFileInfo(path); } catch (Exception ex) { Debug.Print(ex.ToString()); return _fileCache[path] = null; } } public void FullBuf() { BufDir("\\"); } private void BufDir(string path) { Console.WriteLine($"bufing - dir - {path}"); foreach (var item in GetEntries(path)) { if (item.IsCollection) BufDir(item.ItemPath); else { Console.WriteLine($"bufing - file - {item.ItemPath}"); GetInfo(path); } } } public Stream OpenReadStream(string path) { if (!_enableRo) throw new NotSupportedException(); var ms = new MemoryStream(); _device.DownloadFile(path, ms); ms.Position = 0; return ms; } } }