794 lines
22 KiB
C#
794 lines
22 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xFileService.Interfaces;
|
|
using xFileService.Models.Dtos;
|
|
using xFileService.Models.Entities;
|
|
using xIdentityModels.Models;
|
|
using xModels.Dtos;
|
|
using XFileDto = xFileService.Models.Dtos.XFileDto;
|
|
|
|
namespace xFileService.Providers
|
|
{
|
|
public abstract class XBaseFileProvider<TKey> : IXBaseFileProvider<TKey>
|
|
{
|
|
//
|
|
#region Props ...
|
|
/// <summary>
|
|
/// Specified Provider ...
|
|
/// </summary>
|
|
public string ProvidedFor { get; }
|
|
|
|
/// <summary>
|
|
/// Specified Provider Repositroy ...
|
|
/// </summary>
|
|
public IXFileProvider Provider { get; }
|
|
#endregion
|
|
|
|
//
|
|
#region Constructor ...
|
|
public XBaseFileProvider(
|
|
string providedFor,
|
|
IXFileProvider provider
|
|
)
|
|
{
|
|
//
|
|
Provider = provider;
|
|
ProvidedFor = providedFor;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Helper ...
|
|
/// <summary>
|
|
/// Get Specified Identifier ...
|
|
/// </summary>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <returns></returns>
|
|
public string GetIdentifier(TKey forProvidedId)
|
|
{
|
|
//
|
|
return Provider.GetIdentifier(
|
|
providedFor: ProvidedFor,
|
|
forProvidedId: forProvidedId
|
|
);
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Tools ...
|
|
/// <summary>
|
|
/// Stream Specified File ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<FileStreamResult> Stream(Guid id)
|
|
{
|
|
//
|
|
// Check Validation and Owning ...
|
|
var isValid = await IsOwned(id);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Provider.Stream(id);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stream Specified File ...
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
public async Task<FileStreamResult> Stream(string fileName)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !fileName.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Dto and also Checking Owned ...
|
|
var dto = await GetDto(fileName);
|
|
isValid = !dto.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Provider.Stream(fileName);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Download Specified File ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<PhysicalFileResult> Download(Guid id)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = await IsOwned(id);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Provider.Download(id);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Download Specified File ...
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
public async Task<PhysicalFileResult> Download(string fileName)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !fileName.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Dto and also Checking Owned ...
|
|
var dto = await GetDto(fileName);
|
|
isValid = !dto.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Provider.Download(fileName);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Upload Files ...
|
|
/// </summary>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="files"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XFileDto>> Upload(
|
|
TKey forProvidedId,
|
|
IFormFileCollection files,
|
|
XUserClaimsInfoDto userInfo = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !forProvidedId.IsNull();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Here we are Following Custom Senario ...
|
|
var dtos = await Provider.Upload(
|
|
files: files,
|
|
userInfo: userInfo
|
|
);
|
|
|
|
//
|
|
// Validate Dtos ...
|
|
isValid = !dtos.IsNull() && dtos.HasChild();
|
|
if (isValid)
|
|
{
|
|
//
|
|
foreach (var dto in dtos)
|
|
{
|
|
//
|
|
await AddReference(
|
|
id: dto.Id,
|
|
userInfo: userInfo,
|
|
forProvidedId: forProvidedId
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
var result = new List<XFileDto>();
|
|
|
|
//
|
|
isValid = !dtos.IsNull() && dtos.HasChild();
|
|
if (isValid)
|
|
{
|
|
//
|
|
foreach (var dto in dtos)
|
|
{
|
|
//
|
|
var idto = await Provider.Get(dto.Id);
|
|
isValid = !idto.IsNullOrDefault() &&
|
|
await IsOwned(idto.Id);
|
|
if (isValid)
|
|
{
|
|
result.Add(idto);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Files ...
|
|
/// </summary>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="ids"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<Guid>> Remove(
|
|
TKey forProvidedId,
|
|
string ids = null,
|
|
XUserClaimsInfoDto userInfo = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = userInfo.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// if ids Provided, Find all id's Specified Dtos ...
|
|
// if not, gell all models which Owned and has Reference to forProvidedId ...
|
|
var dtos = await GetAll();
|
|
if (!forProvidedId.IsNull())
|
|
{
|
|
//
|
|
var identifier = GetIdentifier(forProvidedId);
|
|
dtos = dtos
|
|
.Where(e => e.References.Any(er => er.ToNormalString() == identifier.ToNormalString()));
|
|
}
|
|
|
|
//
|
|
// Filter Provided IDS ...
|
|
if (!ids.IsNullOrEmpty())
|
|
{
|
|
//
|
|
var idsList = ids.ParseListGuid();
|
|
dtos = dtos.Where(d => !idsList.Contains(d.Id));
|
|
}
|
|
|
|
//
|
|
var result = new List<Guid>();
|
|
isValid = !dtos.IsNullOrDefault() && dtos.HasChild();
|
|
if (isValid)
|
|
{
|
|
//
|
|
foreach (var dto in dtos)
|
|
{
|
|
//
|
|
var dtoIds = new List<string> { dto.Id.ToString() }.ToListString();
|
|
var removeIds = await Provider.Remove(dtoIds, userInfo);
|
|
isValid = !removeIds.IsNull() && removeIds.HasChild() && removeIds.Contains(dto.Id);
|
|
if (isValid)
|
|
{
|
|
result.Add(dto.Id);
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Specified File Streaming Info ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileStreamDescriptorDto> GetFileDescriptor(Guid id)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !id.IsNull() && !id.IsDefaultGuid();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Owned ...
|
|
isValid = await IsOwned(id);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Provider.GetFileDescriptor(id);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Specified File Streaming Info ...
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileStreamDescriptorDto> GetFileDescriptor(string fileName)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !fileName.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Owned ...
|
|
var dto = GetDto(fileName);
|
|
isValid = !dto.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Provider.GetFileDescriptor(fileName);
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Data Model ...
|
|
/// <summary>
|
|
/// Get Specified File Model ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileDto> Get(
|
|
Guid id
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !id.IsNull() && !id.IsDefaultGuid();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Owned ...
|
|
isValid = await IsOwned(id);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Provider.Get(id);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get All Exists File Models ...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XFileDto>> GetAll()
|
|
{
|
|
//
|
|
var result = await Provider
|
|
.FindMany(e => e.References.ToNormalString().Contains(ProvidedFor.ToNormalString()));
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get All Exists File Models ...
|
|
/// </summary>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XFileDto>> GetAllFor(TKey forProvidedId)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (forProvidedId.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var identifier = GetIdentifier(forProvidedId);
|
|
var result = await Provider
|
|
.FindMany(e => e.References.ToNormalString().Contains(identifier.ToNormalString()));
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// find an Entity by providing a Conditional Expression ...
|
|
/// </summary>
|
|
/// <param name="whereClause"></param>
|
|
public async Task<XFileDto> FindOne(Expression<Func<XFile, bool>> whereClause)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (whereClause.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var whereFunc = whereClause.Compile();
|
|
var result = await Provider
|
|
.FindOne(e => whereFunc(e) && e.References.ToNormalString().Contains(ProvidedFor.ToNormalString()));
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// find a collection of Entities by proving a Conditional Expression ...
|
|
/// </summary>
|
|
/// <param name="whereClause"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XFileDto>> FindMany(
|
|
Expression<Func<XFile, bool>> whereClause
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (whereClause.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var whereFunc = whereClause.Compile();
|
|
var result = await Provider
|
|
.FindMany(e => whereFunc(e) && e.References.ToNormalString().Contains(ProvidedFor.ToNormalString()));
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve Entities based on XQuery Pagination structure ...
|
|
/// </summary>
|
|
/// <param name="query"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XFileDto>> Query(
|
|
XQuery query
|
|
)
|
|
{
|
|
//
|
|
// Define Owning Conditions ...
|
|
Expression<Func<XFile, bool>> whereClause = e => e.References
|
|
.ToNormalString()
|
|
.Contains(ProvidedFor.ToNormalString());
|
|
|
|
//
|
|
var result = await Provider.ConditionalQuery(
|
|
query: query,
|
|
whereClause: whereClause
|
|
);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve Entities based on XQuery Pagination structure by providing a Conditional Expression ...
|
|
/// </summary>
|
|
/// <param name="whereClause"></param>
|
|
/// <param name="query"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XFileDto>> ConditionalQuery(
|
|
Expression<Func<XFile, bool>> whereClause,
|
|
XQuery query
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (whereClause.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Prepare a Combination Where Condition ...
|
|
var whereFunc = whereClause.Compile();
|
|
Expression<Func<XFile, bool>> condition = e => whereFunc(e) &&
|
|
e.References.ToNormalString().Contains(ProvidedFor.ToNormalString());
|
|
|
|
//
|
|
var result = await Provider.ConditionalQuery(
|
|
query: query,
|
|
whereClause: condition
|
|
);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update an Entity values ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="item"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileDto> Update(
|
|
Guid id,
|
|
XFileDto item,
|
|
XUserClaimsInfoDto userInfo = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!item.IsNullOrDefault() &&
|
|
!userInfo.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Validate Own ...
|
|
isValid = await IsOwned(item.Id);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Provider.Update(item.Id, item, userInfo);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// remove an Entity ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileDto> Remove(
|
|
Guid id,
|
|
XUserClaimsInfoDto userInfo = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!userInfo.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Validate Owned ...
|
|
isValid = await IsOwned(id);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Provider.Remove(id, userInfo);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// count all exists Entities ...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<int> Count()
|
|
{
|
|
//
|
|
var result = (await GetAll()).Count();
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check an Entity exists or not ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> IsExists(
|
|
Guid id
|
|
)
|
|
{
|
|
//
|
|
var result = await IsOwned(id);
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Private ...
|
|
/// <summary>
|
|
/// Check Specified Dto is Has Reference to Provider ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> IsOwned(Guid id)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var result = !id.IsNull() && !id.IsDefaultGuid();
|
|
if (!result)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Exists ...
|
|
result = await Provider.IsExists(id);
|
|
if (!result)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Dto ...
|
|
var dto = await Provider.Get(id);
|
|
result = !dto.IsNullOrDefault();
|
|
if (!result)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
result = !dto.References.IsNull() &&
|
|
dto.References.HasChild() &&
|
|
dto.References.Any(r => r.ToNormalString().Contains(ProvidedFor.ToNormalString()));
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Specified Owned Dto by File Name ...
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <returns></returns>
|
|
private async Task<XFileDto> GetDto(string fileName)
|
|
{
|
|
//
|
|
XFileDto result = null;
|
|
|
|
//
|
|
if (!fileName.IsNullOrEmpty())
|
|
{
|
|
//
|
|
try
|
|
{
|
|
//
|
|
result = await Provider.FindOne(d =>
|
|
d.Name == fileName ||
|
|
d.Thumb == fileName ||
|
|
d.FileName == fileName ||
|
|
d.Path.Contains(fileName) ||
|
|
d.ThumbPath.Contains(fileName)
|
|
);
|
|
if (!result.IsNullOrDefault())
|
|
{
|
|
//
|
|
// Check Owning ...
|
|
var isOwned = await IsOwned(result.Id);
|
|
if (!isOwned)
|
|
{
|
|
result = null;
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Specified Provided ID's Related Files ...
|
|
/// </summary>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <returns></returns>
|
|
private async Task<IEnumerable<XFileDto>> GetDtos(TKey forProvidedId)
|
|
{
|
|
//
|
|
var result = new List<XFileDto>();
|
|
|
|
//
|
|
var identifier = GetIdentifier(forProvidedId);
|
|
if (!forProvidedId.IsNull())
|
|
{
|
|
//
|
|
result = (await Provider
|
|
.FindMany(e => e.References.ToNormalString().Contains(identifier.ToNormalString())))
|
|
.ToList();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Specified File has Refernce to Provider ...
|
|
/// </summary>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> IsProvidedFor(
|
|
TKey forProvidedId,
|
|
Guid id
|
|
)
|
|
{
|
|
//
|
|
var result = await Provider.IsProvidedFor(
|
|
id: id,
|
|
providedFor: ProvidedFor,
|
|
forProvidedId: forProvidedId
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Specified Reference to Specified File ...
|
|
/// </summary>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
private async Task AddReference(
|
|
TKey forProvidedId,
|
|
Guid id,
|
|
XUserClaimsInfoDto userInfo = null
|
|
)
|
|
{
|
|
//
|
|
await Provider.AddReference(
|
|
id: id,
|
|
userInfo: userInfo,
|
|
providedFor: ProvidedFor,
|
|
forProvidedId: forProvidedId
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Reference from Specified File ...
|
|
/// </summary>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
private async Task RemoveReference(
|
|
TKey forProvidedId,
|
|
Guid id,
|
|
XUserClaimsInfoDto userInfo = null
|
|
)
|
|
{
|
|
//
|
|
await Provider.RemoveReference(
|
|
id: id,
|
|
userInfo: userInfo,
|
|
providedFor: ProvidedFor,
|
|
forProvidedId: forProvidedId
|
|
);
|
|
}
|
|
#endregion
|
|
}
|
|
} |