using System.Collections.Generic; using System.Linq; using RamDavisk.Ramfs.Inters; using WebDAVSharp.Server.Exceptions; using WebDAVSharp.Server.Stores; namespace RamDavisk.Ramfs { internal class WebDavRamfsStoreCollection : WebDavRamfsStoreItem, IWebDavStoreCollection { private List Items { get; } IEnumerable IWebDavStoreCollection.Items => Items; public WebDavRamfsStoreCollection() { Items = new List(); } public IWebDavStoreItem GetItemByName(string name) => Items.FirstOrDefault(p => p.Name == name); public IWebDavStoreCollection CreateCollection(string name) { if (Items.Any(p => p.Name == name)) throw new WebDavConflictException(); var nc = new WebDavRamfsStoreCollection { Name = name, ParentCollection = this }; Items.Add(nc); return nc; } public void Delete(IWebDavStoreItem item) { var webDavRamfsStoreItem = (WebDavRamfsStoreItem)item; Items.Remove(webDavRamfsStoreItem); webDavRamfsStoreItem.Dispose(); } public IWebDavStoreDocument CreateDocument(string name) { if (Items.Any(p => p.Name == name)) throw new WebDavConflictException(); var nd = new WebDavRamfsStoreDocument { Name = name, ParentCollection = this, }; Items.Add(nd); return nd; } public IWebDavStoreItem CopyItemHere(IWebDavStoreItem source, string destinationName, bool includeContent) { throw new WebDavConflictException(); } public IWebDavStoreItem MoveItemHere(IWebDavStoreItem source, string destinationName) { if (Items.Any(p => p.Name == destinationName)) throw new WebDavConflictException(); var item = (WebDavRamfsStoreItem)source; //remove from orig collection, change name, and add to mine collection item.ParentCollection.Items.Remove(item); item.Name = destinationName; item.ParentCollection = this; Items.Add(item); return item; } public override long Size => 0; public override bool IsCollection => true; public override string Etag => Items .Select(p => $"{p.IsCollection},{p.Name},{p.ModificationDate}") .JoinString("|") .GetHashCode() .ToString(); public override void Dispose() { base.Dispose(); foreach (var document in Items.ToArray()) { Items.Remove(document); document.Dispose(); } } } }