123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- using System;
- using System.IO;
- using System.Linq;
- using Luticate2.Utils.Dbo.Basic;
- using Luticate2.Utils.Dbo.Filter;
- using Luticate2.Utils.Dbo.FsFiles;
- using Luticate2.Utils.Dbo.PaginatedRequest;
- using Luticate2.Utils.Dbo.Result;
- using Luticate2.Utils.Interfaces;
- using Luticate2.Utils.Utils;
-
- namespace Luticate2.Utils.DataAccess
- {
- public abstract class LuFsFilesCrudDataAccess : ILuCrudInterface<LuFsFilesAddDbo, LuFsFilesDbo, LuFsFilesAddDbo, string>
- {
- public abstract string GetBasePath();
-
- public string GetFullPath(string path)
- {
- return Path.Combine(GetBasePath(), path);
- }
-
- public bool CanCreate(string fullpath)
- {
- try
- {
- using (File.Create(fullpath))
- {
- }
- File.Delete(fullpath);
- return true;
- }
- catch (Exception e)
- {
- return false;
- }
- }
-
- public static Func<FileInfo, object> GetOrderByFieldExpression(string fieldName)
- {
- if (fieldName == "path")
- {
- return x => x.Name.ToLower();
- }
- if (fieldName == "createdAt")
- {
- return x => x.LastWriteTime;
- }
- if (fieldName == "size")
- {
- return x => x.Length;
- }
- return null;
- }
-
- public static Func<FileInfo, bool> GetFilterExpression(LuFilterDbo filter)
- {
- if (filter == null)
- {
- return s => true;
- }
- return s => s.Name.ToLower().Contains(filter.Query.ToLower());
- }
-
- public virtual LuResult<T> HandleError<T>(Exception e)
- {
- return LuResult<T>.Error(LuStatus.FsError, e);
- }
-
- public string GetDboId(LuFsFilesDbo obj)
- {
- return obj.Path;
- }
-
- public LuResult<T> Add<T>(LuFsFilesAddDbo file, Func<LuFsFilesDbo, T> returnFunc)
- {
- var fullpath = GetFullPath(file.Path);
- if (File.Exists(fullpath))
- {
- return LuResult<T>.Error(LuStatus.InputError, $"File: {file.Path}", "File already exist");
- }
- if (!CanCreate(fullpath))
- {
- return LuResult<T>.Error(LuStatus.FsError, $"File: {file.Path}", "File can not be created");
- }
- try
- {
- using (var fileStream = new FileStream(fullpath, FileMode.CreateNew, FileAccess.Write))
- {
- file.File.CopyTo(fileStream);
- }
- return LuResult<T>.Ok(returnFunc(new FileInfo(fullpath).ToDbo()));
- }
- catch (Exception e)
- {
- return HandleError<T>(e);
- }
- }
-
- public LuResult<LuFsFilesDbo> GetSingleById(string id)
- {
- var fullpath = GetFullPath(id);
- if (!File.Exists(fullpath))
- {
- return LuResult<LuFsFilesDbo>.Error(LuStatus.NotFound, $"File {fullpath}", "File not found");
- }
- return LuResult<LuFsFilesDbo>.Ok(new FileInfo(fullpath).ToDbo());
- }
-
- public LuResult<LuPaginatedDbo<LuFsFilesDbo>> GetMultiple(LuPaginatedRequestDbo request)
- {
- try
- {
- var fullPath = GetBasePath();
- // var fullPath = GetFullPath(request.Filter.GetFilterString("folder", ""));
- // if (!Directory.Exists(fullPath))
- // {
- // return LuResult<LuPaginatedDbo<LuFsFilesDbo>>.Error(LuStatus.NotFound, $"Folder: {fullPath}", "Folder not found");
- // }
- return LuResult<LuPaginatedDbo<LuFsFilesDbo>>.Ok(Directory.GetFiles(fullPath)
- .Select(s => new FileInfo(s))
- .Where(GetFilterExpression(request.Filter))
- .Paginate(request, GetOrderByFieldExpression)
- .Select(info => info.ToDbo()));
- }
- catch (Exception e)
- {
- return HandleError<LuPaginatedDbo<LuFsFilesDbo>>(e);
- }
- }
-
- public LuResult<T> EditSingleById<T>(string id, LuFsFilesAddDbo update, Func<LuFsFilesDbo, T> returnFunc)
- {
- try
- {
- var get = GetSingleById(id);
- if (!get)
- {
- return get.To<T>();
- }
- var fullpath = GetFullPath(get.Data.Path);
- if (update.Path != null)
- {
- File.Move(fullpath, GetFullPath(update.Path));
- fullpath = GetFullPath(update.Path);
- }
- if (update.File != null)
- {
- using (var fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Write))
- {
- update.File.CopyTo(fileStream);
- }
- }
- return LuResult<T>.Ok(returnFunc(new FileInfo(fullpath).ToDbo()));
- }
- catch (Exception e)
- {
- return HandleError<T>(e);
- }
- }
-
- public LuResult<T> DeleteSingleById<T>(string id, Func<LuFsFilesDbo, T> returnFunc)
- {
- try
- {
- var get = GetSingleById(id);
- if (get)
- {
- var fullpath = GetFullPath(get.Data.Path);
- File.Delete(fullpath);
- }
- return get.To(returnFunc);
- }
- catch (Exception e)
- {
- return HandleError<T>(e);
- }
- }
- }
- }
|