123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- 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<string, WebDavMtpStoreItem[]> _dirCache = new Dictionary<string, WebDavMtpStoreItem[]>();
- private readonly Dictionary<string, MediaFileInfo> _fileCache = new Dictionary<string, MediaFileInfo>();
- //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<WebDavMtpStoreItem>();
- 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;
- }
- }
- }
|