You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuFsFilesCrudDataAccess.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.LastWriteTime;
  44. }
  45. if (fieldName == "size")
  46. {
  47. return x => x.Length;
  48. }
  49. return null;
  50. }
  51. public static Func<FileInfo, bool> GetFilterExpression(LuFilterDbo filter)
  52. {
  53. if (filter == null)
  54. {
  55. return s => true;
  56. }
  57. return s => s.Name.ToLower().Contains(filter.Query.ToLower());
  58. }
  59. public virtual LuResult<T> HandleError<T>(Exception e)
  60. {
  61. return LuResult<T>.Error(LuStatus.FsError, e);
  62. }
  63. public string GetDboId(LuFsFilesDbo obj)
  64. {
  65. return obj.Path;
  66. }
  67. public LuResult<T> Add<T>(LuFsFilesAddDbo file, Func<LuFsFilesDbo, T> returnFunc)
  68. {
  69. var fullpath = GetFullPath(file.Path);
  70. if (File.Exists(fullpath))
  71. {
  72. return LuResult<T>.Error(LuStatus.InputError, $"File: {file.Path}", "File already exist");
  73. }
  74. if (!CanCreate(fullpath))
  75. {
  76. return LuResult<T>.Error(LuStatus.FsError, $"File: {file.Path}", "File can not be created");
  77. }
  78. try
  79. {
  80. using (var fileStream = new FileStream(fullpath, FileMode.CreateNew, FileAccess.Write))
  81. {
  82. file.File.CopyTo(fileStream);
  83. }
  84. return LuResult<T>.Ok(returnFunc(new FileInfo(fullpath).ToDbo()));
  85. }
  86. catch (Exception e)
  87. {
  88. return HandleError<T>(e);
  89. }
  90. }
  91. public LuResult<LuFsFilesDbo> GetSingleById(string id)
  92. {
  93. var fullpath = GetFullPath(id);
  94. if (!File.Exists(fullpath))
  95. {
  96. return LuResult<LuFsFilesDbo>.Error(LuStatus.NotFound, $"File {fullpath}", "File not found");
  97. }
  98. return LuResult<LuFsFilesDbo>.Ok(new FileInfo(fullpath).ToDbo());
  99. }
  100. public LuResult<LuPaginatedDbo<LuFsFilesDbo>> GetMultiple(LuPaginatedRequestDbo request)
  101. {
  102. try
  103. {
  104. var fullPath = GetBasePath();
  105. // var fullPath = GetFullPath(request.Filter.GetFilterString("folder", ""));
  106. // if (!Directory.Exists(fullPath))
  107. // {
  108. // return LuResult<LuPaginatedDbo<LuFsFilesDbo>>.Error(LuStatus.NotFound, $"Folder: {fullPath}", "Folder not found");
  109. // }
  110. return LuResult<LuPaginatedDbo<LuFsFilesDbo>>.Ok(Directory.GetFiles(fullPath)
  111. .Select(s => new FileInfo(s))
  112. .Where(GetFilterExpression(request.Filter))
  113. .Paginate(request, GetOrderByFieldExpression)
  114. .Select(info => info.ToDbo()));
  115. }
  116. catch (Exception e)
  117. {
  118. return HandleError<LuPaginatedDbo<LuFsFilesDbo>>(e);
  119. }
  120. }
  121. public LuResult<T> EditSingleById<T>(string id, LuFsFilesAddDbo update, Func<LuFsFilesDbo, T> returnFunc)
  122. {
  123. try
  124. {
  125. var get = GetSingleById(id);
  126. if (!get)
  127. {
  128. return get.To<T>();
  129. }
  130. var fullpath = GetFullPath(get.Data.Path);
  131. if (update.Path != null)
  132. {
  133. File.Move(fullpath, GetFullPath(update.Path));
  134. fullpath = GetFullPath(update.Path);
  135. }
  136. if (update.File != null)
  137. {
  138. using (var fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Write))
  139. {
  140. update.File.CopyTo(fileStream);
  141. }
  142. }
  143. return LuResult<T>.Ok(returnFunc(new FileInfo(fullpath).ToDbo()));
  144. }
  145. catch (Exception e)
  146. {
  147. return HandleError<T>(e);
  148. }
  149. }
  150. public LuResult<T> DeleteSingleById<T>(string id, Func<LuFsFilesDbo, T> returnFunc)
  151. {
  152. try
  153. {
  154. var get = GetSingleById(id);
  155. if (get)
  156. {
  157. var fullpath = GetFullPath(get.Data.Path);
  158. File.Delete(fullpath);
  159. }
  160. return get.To(returnFunc);
  161. }
  162. catch (Exception e)
  163. {
  164. return HandleError<T>(e);
  165. }
  166. }
  167. }
  168. }