Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LuFsFilesCrudDataAccess.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Luticate2.Utils.Dbo.Basic;
  5. using Luticate2.Utils.Dbo.Filter;
  6. using Luticate2.Utils.Dbo.FsFiles;
  7. using Luticate2.Utils.Dbo.PaginatedRequest;
  8. using Luticate2.Utils.Dbo.Result;
  9. using Luticate2.Utils.Interfaces;
  10. using Luticate2.Utils.Utils;
  11. namespace Luticate2.Utils.DataAccess
  12. {
  13. public abstract class LuFsFilesCrudDataAccess : ILuCrudInterface<LuFsFilesAddDbo, LuFsFilesDbo, LuFsFilesAddDbo, string>
  14. {
  15. public abstract string GetBasePath();
  16. public string GetFullPath(string path)
  17. {
  18. return Path.Combine(GetBasePath(), path);
  19. }
  20. public bool CanCreate(string fullpath)
  21. {
  22. try
  23. {
  24. using (File.Create(fullpath))
  25. {
  26. }
  27. File.Delete(fullpath);
  28. return true;
  29. }
  30. catch (Exception e)
  31. {
  32. return false;
  33. }
  34. }
  35. public static Func<FileInfo, object> GetOrderByFieldExpression(string fieldName)
  36. {
  37. if (fieldName == "path")
  38. {
  39. return x => x.Name.ToLower();
  40. }
  41. if (fieldName == "createdAt")
  42. {
  43. return x => x.CreationTime;
  44. }
  45. if (fieldName == "updatedAt")
  46. {
  47. return x => x.LastWriteTime;
  48. }
  49. if (fieldName == "size")
  50. {
  51. return x => x.Length;
  52. }
  53. return null;
  54. }
  55. public static Func<FileInfo, bool> GetFilterExpression(LuFilterDbo filter)
  56. {
  57. if (filter == null)
  58. {
  59. return s => true;
  60. }
  61. return s => s.Name.ToLower().Contains(filter.Query.ToLower());
  62. }
  63. public virtual LuResult<T> HandleError<T>(Exception e)
  64. {
  65. return LuResult<T>.Error(LuStatus.FsError, e);
  66. }
  67. public string GetDboId(LuFsFilesDbo obj)
  68. {
  69. return obj.Path;
  70. }
  71. public LuResult<T> Add<T>(LuFsFilesAddDbo file, Func<LuFsFilesDbo, T> returnFunc)
  72. {
  73. var fullpath = GetFullPath(file.Path);
  74. if (File.Exists(fullpath))
  75. {
  76. return LuResult<T>.Error(LuStatus.InputError, $"File: {file.Path}", "File already exist");
  77. }
  78. if (!CanCreate(fullpath))
  79. {
  80. return LuResult<T>.Error(LuStatus.FsError, $"File: {file.Path}", "File can not be created");
  81. }
  82. try
  83. {
  84. using (var fileStream = new FileStream(fullpath, FileMode.CreateNew, FileAccess.Write))
  85. {
  86. file.File.CopyTo(fileStream);
  87. }
  88. return LuResult<T>.Ok(returnFunc(new FileInfo(fullpath).ToDbo()));
  89. }
  90. catch (Exception e)
  91. {
  92. return HandleError<T>(e);
  93. }
  94. }
  95. public LuResult<LuFsFilesDbo> GetSingleById(string id)
  96. {
  97. var fullpath = GetFullPath(id);
  98. if (!File.Exists(fullpath))
  99. {
  100. return LuResult<LuFsFilesDbo>.Error(LuStatus.NotFound, $"File {fullpath}", "File not found");
  101. }
  102. return LuResult<LuFsFilesDbo>.Ok(new FileInfo(fullpath).ToDbo());
  103. }
  104. public LuResult<LuPaginatedDbo<LuFsFilesDbo>> GetMultiple(LuPaginatedRequestDbo request)
  105. {
  106. try
  107. {
  108. var fullPath = GetBasePath();
  109. // var fullPath = GetFullPath(request.Filter.GetFilterString("folder", ""));
  110. // if (!Directory.Exists(fullPath))
  111. // {
  112. // return LuResult<LuPaginatedDbo<LuFsFilesDbo>>.Error(LuStatus.NotFound, $"Folder: {fullPath}", "Folder not found");
  113. // }
  114. return LuResult<LuPaginatedDbo<LuFsFilesDbo>>.Ok(Directory.GetFiles(fullPath)
  115. .Select(s => new FileInfo(s))
  116. .Where(GetFilterExpression(request.Filter))
  117. .Paginate(request, GetOrderByFieldExpression)
  118. .Select(info => info.ToDbo()));
  119. }
  120. catch (Exception e)
  121. {
  122. return HandleError<LuPaginatedDbo<LuFsFilesDbo>>(e);
  123. }
  124. }
  125. public LuResult<T> EditSingleById<T>(string id, LuFsFilesAddDbo update, Func<LuFsFilesDbo, T> returnFunc)
  126. {
  127. try
  128. {
  129. var get = GetSingleById(id);
  130. if (!get)
  131. {
  132. return get.To<T>();
  133. }
  134. var fullpath = GetFullPath(get.Data.Path);
  135. if (update.Path != null && update.Path != get.Data.Path)
  136. {
  137. var existing = GetSingleById(update.Path);
  138. if (existing)
  139. {
  140. return LuResult<T>.Error(LuStatus.InputError, $"File {update.Path}", "File already exist");
  141. }
  142. if (existing.Status != LuStatus.NotFound)
  143. {
  144. return existing.To<T>();
  145. }
  146. File.Move(fullpath, GetFullPath(update.Path));
  147. fullpath = GetFullPath(update.Path);
  148. }
  149. if (update.File != null)
  150. {
  151. using (var fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Write))
  152. {
  153. update.File.CopyTo(fileStream);
  154. }
  155. }
  156. return LuResult<T>.Ok(returnFunc(new FileInfo(fullpath).ToDbo()));
  157. }
  158. catch (Exception e)
  159. {
  160. return HandleError<T>(e);
  161. }
  162. }
  163. public LuResult<T> DeleteSingleById<T>(string id, Func<LuFsFilesDbo, T> returnFunc)
  164. {
  165. try
  166. {
  167. var get = GetSingleById(id);
  168. if (get)
  169. {
  170. var fullpath = GetFullPath(get.Data.Path);
  171. File.Delete(fullpath);
  172. }
  173. return get.To(returnFunc);
  174. }
  175. catch (Exception e)
  176. {
  177. return HandleError<T>(e);
  178. }
  179. }
  180. }
  181. }