Implement XFile Provider ...
This commit is contained in:
@@ -0,0 +1,103 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xFileService.Interfaces;
|
||||||
|
using xFileService.Providers;
|
||||||
|
|
||||||
|
namespace xFileService.DI
|
||||||
|
{
|
||||||
|
public static class XDIHelperExtension
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Register Module Provided Service on DI
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="config"></param>
|
||||||
|
public static void AddXFileService(
|
||||||
|
this IServiceCollection services,
|
||||||
|
ServiceLifetime lifeTime
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
AddFileService(
|
||||||
|
services,
|
||||||
|
lifeTime
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Private ...
|
||||||
|
/// <summary>
|
||||||
|
/// a LogTag for Service ...
|
||||||
|
/// </summary>
|
||||||
|
private static string XLogTag = "XFileService";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// print a log in Console ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
private static void Log(string message)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{XLogTag} => {message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Register Push Service ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
/// <param name="lifeTime"></param>
|
||||||
|
private static void AddFileService(
|
||||||
|
IServiceCollection services,
|
||||||
|
ServiceLifetime lifeTime
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var exception = XException.InvalidArgs.ToException(); ;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Services ...
|
||||||
|
var isServicesExists = !services.IsNull();
|
||||||
|
if (!isServicesExists)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
Log("AddFileService failed, services not provided ...");
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Lifetime ...
|
||||||
|
var isLifetimeExists = !lifeTime.IsNull();
|
||||||
|
if (!isLifetimeExists)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
Log("AddFileService failed, lifetime not provided ...");
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check String Repository Exists ...
|
||||||
|
var isRepositoryExists = !services
|
||||||
|
.GetRegisteredService<IXTagRepository>()
|
||||||
|
.IsNull();
|
||||||
|
if (!isRepositoryExists)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
Log("AddFileService failed, IXTagRepository not provided ...");
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Register Main Provider ...
|
||||||
|
services.Add(new ServiceDescriptor(typeof(IXFileProvider), typeof(XFileProvider), lifeTime));
|
||||||
|
|
||||||
|
//
|
||||||
|
Log("AddFileService Succeed ...");
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class IXTagRepository
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
using xCommons.Extensions;
|
||||||
|
using xFileService.Models.Dtos;
|
||||||
|
using xFileService.Models.Entities;
|
||||||
|
|
||||||
|
namespace xFileService.Extensions
|
||||||
|
{
|
||||||
|
public static class xFileServiceExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Map Global Properties From XFile to XFile Dto ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static XFileDto ToXFileDto(this XFile source)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
XFileDto result = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!source.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = new XFileDto();
|
||||||
|
result = result.UpdateData(source);
|
||||||
|
|
||||||
|
//
|
||||||
|
// result = new XFileDto
|
||||||
|
// {
|
||||||
|
// Id = source.Id,
|
||||||
|
// Type = source.Type,
|
||||||
|
// OwnerId = source.OwnerId,
|
||||||
|
// Name = source.Name,
|
||||||
|
// Path = source.Path,
|
||||||
|
// Thumb = source.Thumb,
|
||||||
|
// ThumbPath = source.ThumbPath,
|
||||||
|
// UploadedOn = source.UploadedOn,
|
||||||
|
// FileName = source.FileName
|
||||||
|
// };
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using xFileService.Models.Entities;
|
||||||
|
using xPushService.Base;
|
||||||
|
|
||||||
|
namespace xFileService.Hubs
|
||||||
|
{
|
||||||
|
public class XFileEntityHub : XBaseEntityHub<XFile, Guid>
|
||||||
|
{
|
||||||
|
//
|
||||||
|
#region Constructor ...
|
||||||
|
public XFileEntityHub(ILogger<XBaseHub> logger) : base(logger)
|
||||||
|
{ }
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,217 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using xDataService.Configuration;
|
||||||
|
using xFileService.Hubs;
|
||||||
|
using xFileService.Interfaces.Entities;
|
||||||
|
using xFileService.Models.Dtos;
|
||||||
|
using xFileService.Models.Entities;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using xModels.Dtos;
|
||||||
|
using xStorageService.Interfaces;
|
||||||
|
using XFileDto = xFileService.Models.Dtos.XFileDto;
|
||||||
|
|
||||||
|
namespace xFileService.Interfaces
|
||||||
|
{
|
||||||
|
public interface IXFileProvider
|
||||||
|
{
|
||||||
|
//
|
||||||
|
#region Props ...
|
||||||
|
IHubContext<XFileEntityHub> Hub { get; }
|
||||||
|
IXFileRepository FileRepository { get; }
|
||||||
|
IXStorageProvider StorageProvider { get; }
|
||||||
|
IXIdentityProvider IdentityProvider { get; }
|
||||||
|
XDataServiceConfiguration DataSercviceConfiguration { get; }
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Tools ...
|
||||||
|
/// <summary>
|
||||||
|
/// Stream Specified File ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<FileStreamResult> Stream(Guid id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Stream Specified File ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fileName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<FileStreamResult> Stream(string fileName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Download Specified File ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<PhysicalFileResult> Download(Guid id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Download Specified File ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fileName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<PhysicalFileResult> Download(string fileName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upload Files ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="files"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<XFileDto>> Upload(
|
||||||
|
IFormFileCollection files,
|
||||||
|
XUserClaimsInfoDto userInfo = null
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove Specified Files ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ids"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<Guid>> Remove(
|
||||||
|
string ids,
|
||||||
|
XUserClaimsInfoDto userInfo = null
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Specified File Streaming Info ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XFileStreamDescriptorDto> GetFileDescriptor(Guid id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Specified File Streaming Info ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fileName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XFileStreamDescriptorDto> GetFileDescriptor(string fileName);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Data Model ...
|
||||||
|
/// <summary>
|
||||||
|
/// Get Specified File Model ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XFileDto> Get(
|
||||||
|
Guid id
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get All Exists File Models ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<XFileDto>> GetAll();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// find an Entity by providing a Conditional Expression ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="whereClause"></param>
|
||||||
|
Task<XFileDto> FindOne(Expression<Func<XFile, bool>> whereClause);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// find a collection of Entities by proving a Conditional Expression ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="whereClause"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<XFileDto>> FindMany(
|
||||||
|
Expression<Func<XFile, bool>> whereClause
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// retrieve Entities based on XQuery Pagination structure ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="query"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XQueryResult<XFileDto>> Query(
|
||||||
|
XQuery query
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// retrieve Entities based on XQuery Pagination structure by providing a Conditional Expression ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="whereClause"></param>
|
||||||
|
/// <param name="query"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XQueryResult<XFileDto>> ConditionalQuery(
|
||||||
|
Expression<Func<XFile, bool>> whereClause,
|
||||||
|
XQuery query
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update an Entity values ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XFileDto> Update(
|
||||||
|
Guid id,
|
||||||
|
XFileDto item,
|
||||||
|
XUserClaimsInfoDto userInfo = null
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// remove an Entity ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XFileDto> Remove(
|
||||||
|
Guid id,
|
||||||
|
XUserClaimsInfoDto userInfo = null
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// count all exists Entities ...
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<int> Count();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check an Entity exists or not ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<bool> IsExists(
|
||||||
|
Guid id
|
||||||
|
);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Helpers ...
|
||||||
|
/// <summary>
|
||||||
|
/// Converts an Entity to Dto ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="model"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XFileDto> ToXFileDto(XFile model);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert a List of Entities to Dto ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="list"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IEnumerable<XFileDto>> ToXDtoList(IEnumerable<XFile> list);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts an Entity Query Result to Dto ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="queryResult"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<XQueryResult<XFileDto>> ToXDtoQueryResult(XQueryResult<XFile> queryResult);
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,967 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using xCommons.Extensions;
|
||||||
|
using xDataService.Configuration;
|
||||||
|
using xExceptions.Constants;
|
||||||
|
using xFileService.Extensions;
|
||||||
|
using xFileService.Hubs;
|
||||||
|
using xFileService.Interfaces;
|
||||||
|
using xFileService.Interfaces.Entities;
|
||||||
|
using xFileService.Models.Dtos;
|
||||||
|
using xFileService.Models.Entities;
|
||||||
|
using xIdentityModels.Models;
|
||||||
|
using xIdentityService.Interfaces;
|
||||||
|
using xModels.Dtos;
|
||||||
|
using xIdentityService.Extensions;
|
||||||
|
using XFileDto = xFileService.Models.Dtos.XFileDto;
|
||||||
|
using xStorageService.Interfaces;
|
||||||
|
using System.IO;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
using Microsoft.AspNetCore.StaticFiles;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace xFileService.Providers
|
||||||
|
{
|
||||||
|
public class XFileProvider : IXFileProvider
|
||||||
|
{
|
||||||
|
//
|
||||||
|
#region Props ...
|
||||||
|
public IHubContext<XFileEntityHub> Hub { get; }
|
||||||
|
public IXFileRepository FileRepository { get; }
|
||||||
|
public IXStorageProvider StorageProvider { get; }
|
||||||
|
public IXIdentityProvider IdentityProvider { get; }
|
||||||
|
public XDataServiceConfiguration DataSercviceConfiguration { get; }
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Constructor ...
|
||||||
|
public XFileProvider(
|
||||||
|
IXFileRepository fileRepository,
|
||||||
|
IXStorageProvider storageProvider,
|
||||||
|
IXIdentityProvider identityProvider,
|
||||||
|
XDataServiceConfiguration dataSercviceConfiguration,
|
||||||
|
IHubContext<XFileEntityHub> hub = null
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
Hub = hub;
|
||||||
|
FileRepository = fileRepository;
|
||||||
|
StorageProvider = storageProvider;
|
||||||
|
IdentityProvider = identityProvider;
|
||||||
|
DataSercviceConfiguration = dataSercviceConfiguration;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Tools ...
|
||||||
|
/// <summary>
|
||||||
|
/// Stream Specified File ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<FileStreamResult> Stream(Guid id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
var isValid = !id.IsNull() &&
|
||||||
|
!id.IsDefaultGuid();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Stream Info and Validate it ...
|
||||||
|
var stream = await GetFileDescriptor(id);
|
||||||
|
if (stream.IsNull())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Result Model ...
|
||||||
|
var result = new FileStreamResult(
|
||||||
|
stream.Stream,
|
||||||
|
stream.MIMEType
|
||||||
|
)
|
||||||
|
{
|
||||||
|
FileDownloadName = stream.Name
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Stream Specified File ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fileName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<FileStreamResult> Stream(string fileName)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
var isValid = !fileName.IsNullOrEmpty();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Stream Info and Validate it ...
|
||||||
|
var stream = await GetFileDescriptor(fileName);
|
||||||
|
if (stream.IsNull())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Result Model ...
|
||||||
|
var result = new FileStreamResult(
|
||||||
|
stream.Stream,
|
||||||
|
stream.MIMEType
|
||||||
|
)
|
||||||
|
{
|
||||||
|
FileDownloadName = stream.Name
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Download Specified File ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<PhysicalFileResult> Download(Guid id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
var isValid = !id.IsNull() &&
|
||||||
|
!id.IsDefaultGuid();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Stream Info and Validate it ...
|
||||||
|
var stream = await GetFileDescriptor(id);
|
||||||
|
if (stream.IsNull())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Result Model ...
|
||||||
|
var result = new PhysicalFileResult(
|
||||||
|
stream.Path,
|
||||||
|
stream.MIMEType
|
||||||
|
)
|
||||||
|
{
|
||||||
|
FileDownloadName = stream.Name
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Download Specified File ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fileName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<PhysicalFileResult> Download(string fileName)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
var isValid = !fileName.IsNullOrEmpty();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Stream Info and Validate it ...
|
||||||
|
var stream = await GetFileDescriptor(fileName);
|
||||||
|
if (stream.IsNull())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Result Model ...
|
||||||
|
var result = new PhysicalFileResult(
|
||||||
|
stream.Path,
|
||||||
|
stream.MIMEType
|
||||||
|
)
|
||||||
|
{
|
||||||
|
FileDownloadName = stream.Name
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Upload Files ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="files"></param>
|
||||||
|
/// <param name="userId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<IEnumerable<XFileDto>> Upload(
|
||||||
|
IFormFileCollection files,
|
||||||
|
XUserClaimsInfoDto userInfo = null
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
var isValid = !files.IsNull() &&
|
||||||
|
files.HasChild() &&
|
||||||
|
!userInfo.IsNullOrDefault();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Save Files On Server Side Physically ...
|
||||||
|
// and Validate it ...
|
||||||
|
var uploadResults = await StorageProvider.HandleFilesSave(files);
|
||||||
|
isValid = uploadResults.HasChild();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Result ...
|
||||||
|
var entities = new List<XFile>();
|
||||||
|
foreach (var ur in uploadResults)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var fileType = StorageProvider.GetFileType(ur.FileName);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Create Entity Model ...
|
||||||
|
var entity = new XFile
|
||||||
|
{
|
||||||
|
Type = fileType,
|
||||||
|
FileName = ur.Name,
|
||||||
|
Name = ur.FileName,
|
||||||
|
Path = ur.FilePath,
|
||||||
|
Thumb = ur.Thmbnail,
|
||||||
|
OwnerId = userInfo.UserId,
|
||||||
|
ThumbPath = ur.ThmbnailPath,
|
||||||
|
UploadedOn = DateTime.UtcNow,
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Add Entity Model to DB ...
|
||||||
|
entity = await FileRepository.AddAsync(entity);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!entity.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
entities.Add(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Preparing Result ...
|
||||||
|
var result = await ToXDtoList(entities);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove Specified Files ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ids"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<IEnumerable<Guid>> Remove(
|
||||||
|
string ids,
|
||||||
|
XUserClaimsInfoDto userInfo = null
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
var isValid = !ids.IsNullOrEmpty() &&
|
||||||
|
!userInfo.IsNullOrDefault();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Parse ID(s) List ...
|
||||||
|
var idsList = ids.ParseListGuid();
|
||||||
|
isValid = !idsList.IsNull() &&
|
||||||
|
idsList.HasChild();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Loop Through Parsed Ids and Try to Remove ...
|
||||||
|
var result = new List<Guid>();
|
||||||
|
foreach (var id in idsList)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Retrieve XFileDto and Validate it ...
|
||||||
|
var model = await Get(id);
|
||||||
|
isValid = !model.IsNull() && !model.Id.IsNull() && !model.Id.IsDefaultGuid();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Physical File Exists or not ...
|
||||||
|
var fileFullPath = StorageProvider.GetFileFullPath(model.Name);
|
||||||
|
var thumbFullPath = model.Thumb.IsNullOrEmpty()
|
||||||
|
? ""
|
||||||
|
: StorageProvider.GetThumbnailFileFullPath(model.Thumb);
|
||||||
|
var isFileExists = StorageProvider.IsFileExists(fileFullPath);
|
||||||
|
var isThumbExists = model.Thumb.IsNullOrEmpty()
|
||||||
|
? false
|
||||||
|
: StorageProvider.IsFileExists(thumbFullPath);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Remove Physical Thumbnail File ...
|
||||||
|
if (isThumbExists)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
StorageProvider.DeleteFile(
|
||||||
|
fileFullPath: thumbFullPath,
|
||||||
|
forceFileExists: false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Remove Pgysical File ...
|
||||||
|
if (isFileExists)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
StorageProvider.DeleteFile(
|
||||||
|
fileFullPath: fileFullPath,
|
||||||
|
forceFileExists: false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Remove File Entty ...
|
||||||
|
await FileRepository.RemoveAsync(model.Id);
|
||||||
|
result.Add(model.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Specified File Streaming Info ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XFileStreamDescriptorDto> GetFileDescriptor(Guid id)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Check File Model Exists or not ...
|
||||||
|
var isExists = await IsExists(
|
||||||
|
id: id
|
||||||
|
);
|
||||||
|
if (!isExists)
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Retrieve Entity and Validate it ...
|
||||||
|
var entity = await Get(
|
||||||
|
id: id
|
||||||
|
);
|
||||||
|
if (entity.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Physical File Path and Validate it ...
|
||||||
|
var fileFullPath = StorageProvider.GetFileFullPath(entity.Name);
|
||||||
|
isExists = StorageProvider.IsFileExists(fileFullPath);
|
||||||
|
var filePath = StorageProvider.FilePathResolver(fileFullPath);
|
||||||
|
filePath = Path.Combine("wwwroot", filePath);
|
||||||
|
if (!isExists)
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Physical File Info ...
|
||||||
|
IFileInfo fileInfo = StorageProvider.FileProvider.GetFileInfo(filePath);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Default Streaming Requirements ...
|
||||||
|
var mimeType = "application/octetstream";
|
||||||
|
var readStream = fileInfo.CreateReadStream();
|
||||||
|
var fileMimeProvider = new FileExtensionContentTypeProvider();
|
||||||
|
var fileName = entity.Name;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Try to Find Physical File's Content Type ...
|
||||||
|
fileMimeProvider.TryGetContentType(filePath, out mimeType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Result Model ...
|
||||||
|
var result = new XFileStreamDescriptorDto
|
||||||
|
{
|
||||||
|
Stream = readStream,
|
||||||
|
MIMEType = mimeType,
|
||||||
|
Name = fileName,
|
||||||
|
Path = fileFullPath
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve Specified File Streaming Info ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fileName"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XFileStreamDescriptorDto> GetFileDescriptor(string fileName)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (fileName.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
var entity = await FindOne(e =>
|
||||||
|
e.Name.Contains(fileName) ||
|
||||||
|
e.Path.Contains(fileName) ||
|
||||||
|
e.Thumb.Contains(fileName) ||
|
||||||
|
e.FileName.Contains(fileName) ||
|
||||||
|
e.ThumbPath.Contains(fileName)
|
||||||
|
);
|
||||||
|
var isExists = !entity.IsNullOrDefault();
|
||||||
|
if (!isExists)
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Extract File Name ...
|
||||||
|
fileName = Path.GetFileName(fileName);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check File is Thumbnail or File ...
|
||||||
|
var isThumb = fileName.StartsWith(StorageProvider.Configuration.ThumbPrefix);
|
||||||
|
var isFile = fileName.StartsWith(StorageProvider.Configuration.FilePrefix);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check File Type is Valid ...
|
||||||
|
isExists = isThumb || isFile;
|
||||||
|
if (!isExists)
|
||||||
|
{
|
||||||
|
XException.UnsupportedFileType.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Extract File Full Path ...
|
||||||
|
var fileFullPath = "";
|
||||||
|
if (isThumb)
|
||||||
|
{
|
||||||
|
fileFullPath = StorageProvider.GetThumbnailFileFullPath(fileName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fileFullPath = StorageProvider.GetFileFullPath(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Physical File Exists or Not ...
|
||||||
|
isExists = StorageProvider.IsFileExists(fileFullPath);
|
||||||
|
if (!isExists)
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Generate Physical File Info ...
|
||||||
|
var filePath = StorageProvider.FilePathResolver(fileFullPath);
|
||||||
|
filePath = Path.Combine("wwwroot", filePath);
|
||||||
|
IFileInfo fileInfo = StorageProvider.FileProvider.GetFileInfo(filePath);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Default Streaming Requirements ...
|
||||||
|
var mimeType = "application/octetstream";
|
||||||
|
var readStream = fileInfo.CreateReadStream();
|
||||||
|
var fileMimeProvider = new FileExtensionContentTypeProvider();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Try to Find Physical File's Content Type ...
|
||||||
|
fileMimeProvider.TryGetContentType(filePath, out mimeType);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Result Model ...
|
||||||
|
var result = new XFileStreamDescriptorDto
|
||||||
|
{
|
||||||
|
Stream = readStream,
|
||||||
|
MIMEType = mimeType,
|
||||||
|
Name = fileName,
|
||||||
|
Path = fileFullPath
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Data Model ...
|
||||||
|
/// <summary>
|
||||||
|
/// Get Specified File Model ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XFileDto> Get(
|
||||||
|
Guid id
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (id.IsNull() || id.IsDefaultGuid())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var entitiy = await FileRepository.GetAsync(id);
|
||||||
|
if (entitiy.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await ToXFileDto(entitiy);
|
||||||
|
if (result.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get All Exists File Models ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ignoreSoftDeleteds"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<IEnumerable<XFileDto>> GetAll()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new List<XFileDto>();
|
||||||
|
|
||||||
|
//
|
||||||
|
var entities = await FileRepository.GetAllAsync();
|
||||||
|
if (!entities.IsNull() && entities.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = (await ToXDtoList(entities))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new XFileDto();
|
||||||
|
|
||||||
|
//
|
||||||
|
var entity = await FileRepository.FindOneAsync(whereClause);
|
||||||
|
if (!entity.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
result = await ToXFileDto(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
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
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new List<XFileDto>();
|
||||||
|
|
||||||
|
//
|
||||||
|
var entities = await FileRepository.FindManyAsync(whereClause);
|
||||||
|
if (!entities.IsNull() && entities.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = (await ToXDtoList(entities))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
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
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new XQueryResult<XFileDto>();
|
||||||
|
|
||||||
|
//
|
||||||
|
var queryResult = await FileRepository.QueryAsync(query);
|
||||||
|
if (!queryResult.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
result = await ToXDtoQueryResult(queryResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
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
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new XQueryResult<XFileDto>();
|
||||||
|
|
||||||
|
//
|
||||||
|
var queryResult = await FileRepository.ConditionalQueryAsync(
|
||||||
|
query: query,
|
||||||
|
whereClause: whereClause
|
||||||
|
);
|
||||||
|
if (!queryResult.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
result = await ToXDtoQueryResult(queryResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
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 ...
|
||||||
|
bool isValid = !id.IsNull() &&
|
||||||
|
!id.IsDefaultGuid() &&
|
||||||
|
!item.IsNullOrDefault() &&
|
||||||
|
!userInfo.IsNullOrDefault();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Entitiy Exists and Retrieve it ...
|
||||||
|
isValid = await IsExists(id);
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var entity = await FileRepository.GetAsync(id);
|
||||||
|
if (entity.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Fill Data for Update ...
|
||||||
|
entity = entity.UpdateData(
|
||||||
|
updateWith: item,
|
||||||
|
propertyBlackList: new List<string>
|
||||||
|
{
|
||||||
|
nameof(XFile.Id),
|
||||||
|
nameof(XFile.Deleted),
|
||||||
|
nameof(XFileDto.Owner)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (entity.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.InvalidData.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
entity = await FileRepository.UpdateAsync(id, entity);
|
||||||
|
if (entity.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Action ...
|
||||||
|
isValid =
|
||||||
|
entity.OwnerId == userInfo.UserId ||
|
||||||
|
userInfo.Roles.Any(r => r.ToNormalString() == "admin".ToNormalString());
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await ToXFileDto(entity);
|
||||||
|
|
||||||
|
//
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check Entity Exists and Retrieve it ...
|
||||||
|
isValid = await IsExists(id);
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await Get(id);
|
||||||
|
isValid = !result.IsNullOrDefault();
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Action ...
|
||||||
|
isValid =
|
||||||
|
result.OwnerId == userInfo.UserId ||
|
||||||
|
userInfo.Roles.Any(r => r.ToNormalString() == "admin".ToNormalString());
|
||||||
|
if (!isValid)
|
||||||
|
{
|
||||||
|
XException.NotAllowed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var entitiy = await FileRepository.RemoveAsync(id);
|
||||||
|
if (entitiy.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
result = await ToXFileDto(entitiy);
|
||||||
|
if (result.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// count all exists Entities ...
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<int> Count()
|
||||||
|
{
|
||||||
|
return await FileRepository.CountAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check an Entity exists or not ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<bool> IsExists(
|
||||||
|
Guid id
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return await FileRepository.IsExistsAsync(id);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Helpers ...
|
||||||
|
/// <summary>
|
||||||
|
/// Converts an Entity to Dto ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="model"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XFileDto> ToXFileDto(XFile model)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
XFileDto result = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!model.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result = model.ToXFileDto();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Inner API Providers Device Dto ...
|
||||||
|
var device = IdentityProvider.GetDevice();
|
||||||
|
var userInfo = await IdentityProvider.GetUserInfo(
|
||||||
|
device: device,
|
||||||
|
userSelectByParam: model.OwnerId
|
||||||
|
);
|
||||||
|
if (userInfo.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
XException.NotFound.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
result.Owner = userInfo.ToXPersonDto();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert a List of Entities to Dto ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="list"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<IEnumerable<XFileDto>> ToXDtoList(IEnumerable<XFile> list)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new List<XFileDto>();
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!list.IsNull() && list.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
foreach (var item in list)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var dto = await ToXFileDto(item);
|
||||||
|
if (!dto.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
result.Add(dto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Converts an Entity Query Result to Dto ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="queryResult"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<XQueryResult<XFileDto>> ToXDtoQueryResult(XQueryResult<XFile> queryResult)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = new XQueryResult<XFileDto>();
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!queryResult.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
result.Page = queryResult.Page;
|
||||||
|
result.PageSize = queryResult.PageSize;
|
||||||
|
result.TotalItems = queryResult.TotalItems;
|
||||||
|
result.TotalPages = queryResult.TotalPages;
|
||||||
|
result.TotalFilteredItems = queryResult.TotalFilteredItems;
|
||||||
|
result.TotalFilteredPages = queryResult.TotalFilteredPages;
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!queryResult.Items.IsNull() && queryResult.Items.HasChild())
|
||||||
|
{
|
||||||
|
result.Items = await ToXDtoList(queryResult.Items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user