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 { 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 GetOrderByFieldExpression(string fieldName) { if (fieldName == "path") { return x => x.Name.ToLower(); } if (fieldName == "createdAt") { return x => x.CreationTime; } if (fieldName == "updatedAt") { return x => x.LastWriteTime; } if (fieldName == "size") { return x => x.Length; } return null; } public static Func GetFilterExpression(LuFilterDbo filter) { if (filter == null) { return s => true; } return s => s.Name.ToLower().Contains(filter.Query.ToLower()); } public virtual LuResult HandleError(Exception e) { return LuResult.Error(LuStatus.FsError, e); } public string GetDboId(LuFsFilesDbo obj) { return obj.Path; } public LuResult Add(LuFsFilesAddDbo file, Func returnFunc) { var fullpath = GetFullPath(file.Path); if (File.Exists(fullpath)) { return LuResult.Error(LuStatus.InputError, $"File: {file.Path}", "File already exist"); } if (!CanCreate(fullpath)) { return LuResult.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.Ok(returnFunc(new FileInfo(fullpath).ToDbo())); } catch (Exception e) { return HandleError(e); } } public LuResult GetSingleById(string id) { var fullpath = GetFullPath(id); if (!File.Exists(fullpath)) { return LuResult.Error(LuStatus.NotFound, $"File {fullpath}", "File not found"); } return LuResult.Ok(new FileInfo(fullpath).ToDbo()); } public LuResult> GetMultiple(LuPaginatedRequestDbo request) { try { var fullPath = GetBasePath(); // var fullPath = GetFullPath(request.Filter.GetFilterString("folder", "")); // if (!Directory.Exists(fullPath)) // { // return LuResult>.Error(LuStatus.NotFound, $"Folder: {fullPath}", "Folder not found"); // } return LuResult>.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>(e); } } public LuResult EditSingleById(string id, LuFsFilesAddDbo update, Func returnFunc) { try { var get = GetSingleById(id); if (!get) { return get.To(); } var fullpath = GetFullPath(get.Data.Path); if (update.Path != null && update.Path != get.Data.Path) { var existing = GetSingleById(update.Path); if (existing) { return LuResult.Error(LuStatus.InputError, $"File {update.Path}", "File already exist"); } if (existing.Status != LuStatus.NotFound) { return existing.To(); } 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.Ok(returnFunc(new FileInfo(fullpath).ToDbo())); } catch (Exception e) { return HandleError(e); } } public LuResult DeleteSingleById(string id, Func 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(e); } } } }