WebDavRamfsStoreCollection.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using RamDavisk.Ramfs.Inters;
  4. using WebDAVSharp.Server.Exceptions;
  5. using WebDAVSharp.Server.Stores;
  6. namespace RamDavisk.Ramfs
  7. {
  8. internal class WebDavRamfsStoreCollection : WebDavRamfsStoreItem, IWebDavStoreCollection
  9. {
  10. private List<WebDavRamfsStoreItem> Items { get; }
  11. IEnumerable<IWebDavStoreItem> IWebDavStoreCollection.Items => Items;
  12. public WebDavRamfsStoreCollection()
  13. {
  14. Items = new List<WebDavRamfsStoreItem>();
  15. }
  16. public IWebDavStoreItem GetItemByName(string name) => Items.FirstOrDefault(p => p.Name == name);
  17. public IWebDavStoreCollection CreateCollection(string name)
  18. {
  19. if (Items.Any(p => p.Name == name)) throw new WebDavConflictException();
  20. var nc = new WebDavRamfsStoreCollection
  21. {
  22. Name = name,
  23. ParentCollection = this
  24. };
  25. Items.Add(nc);
  26. return nc;
  27. }
  28. public void Delete(IWebDavStoreItem item)
  29. {
  30. var webDavRamfsStoreItem = (WebDavRamfsStoreItem)item;
  31. Items.Remove(webDavRamfsStoreItem);
  32. webDavRamfsStoreItem.Dispose();
  33. }
  34. public IWebDavStoreDocument CreateDocument(string name)
  35. {
  36. if (Items.Any(p => p.Name == name)) throw new WebDavConflictException();
  37. var nd = new WebDavRamfsStoreDocument
  38. {
  39. Name = name,
  40. ParentCollection = this,
  41. };
  42. Items.Add(nd);
  43. return nd;
  44. }
  45. public IWebDavStoreItem CopyItemHere(IWebDavStoreItem source, string destinationName, bool includeContent)
  46. {
  47. throw new WebDavConflictException();
  48. }
  49. public IWebDavStoreItem MoveItemHere(IWebDavStoreItem source, string destinationName)
  50. {
  51. if (Items.Any(p => p.Name == destinationName)) throw new WebDavConflictException();
  52. var item = (WebDavRamfsStoreItem)source;
  53. //remove from orig collection, change name, and add to mine collection
  54. item.ParentCollection.Items.Remove(item);
  55. item.Name = destinationName;
  56. item.ParentCollection = this;
  57. Items.Add(item);
  58. return item;
  59. }
  60. public override long Size => 0;
  61. public override bool IsCollection => true;
  62. public override string Etag => Items
  63. .Select(p => $"{p.IsCollection},{p.Name},{p.ModificationDate}")
  64. .JoinString("|")
  65. .GetHashCode()
  66. .ToString();
  67. public override void Dispose()
  68. {
  69. base.Dispose();
  70. foreach (var document in Items.ToArray())
  71. {
  72. Items.Remove(document);
  73. document.Dispose();
  74. }
  75. }
  76. }
  77. }