1751 lines
50 KiB
C#
1751 lines
50 KiB
C#
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;
|
|
using xPushService.Constants;
|
|
using xDataService.Models;
|
|
|
|
namespace xFileService.Providers
|
|
{
|
|
public class XFileProvider : IXFileProvider
|
|
{
|
|
//
|
|
#region Props ...
|
|
public IXFileTagProvider TagProvider { get; }
|
|
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(
|
|
IXFileTagProvider tagProvider,
|
|
IXFileRepository fileRepository,
|
|
IXStorageProvider storageProvider,
|
|
IXIdentityProvider identityProvider,
|
|
XDataServiceConfiguration dataSercviceConfiguration,
|
|
IHubContext<XFileEntityHub> hub = null
|
|
)
|
|
{
|
|
//
|
|
Hub = hub;
|
|
TagProvider = tagProvider;
|
|
FileRepository = fileRepository;
|
|
StorageProvider = storageProvider;
|
|
IdentityProvider = identityProvider;
|
|
DataSercviceConfiguration = dataSercviceConfiguration;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Helpers ...
|
|
/// <summary>
|
|
/// Converts an Entity to Dto ...
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileDto> ToDto(XFile model)
|
|
{
|
|
//
|
|
XFileDto result = null;
|
|
|
|
//
|
|
if (!model.IsNullOrDefault())
|
|
{
|
|
//
|
|
result = model.ToDto();
|
|
|
|
//
|
|
// 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>> ToDtoList(IEnumerable<XFile> list)
|
|
{
|
|
//
|
|
var result = new List<XFileDto>();
|
|
|
|
//
|
|
if (!list.IsNull() && list.HasChild())
|
|
{
|
|
//
|
|
foreach (var item in list)
|
|
{
|
|
//
|
|
var dto = await ToDto(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>> ToDtoQueryResult(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 ToDtoList(queryResult.Items);
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Specified User Info Has Permissions for Specified File ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> HasPermission(
|
|
Guid id,
|
|
XUserClaimsInfoDto userInfo = null
|
|
)
|
|
{
|
|
//
|
|
var result = false;
|
|
|
|
//
|
|
// Validate ...
|
|
result = !id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!userInfo.IsNullOrDefault();
|
|
if (!result)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Item Exists ...
|
|
result = await IsExists(id);
|
|
if (!result)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Dto ...
|
|
var dto = await Get(id);
|
|
result = !dto.IsNullOrDefault();
|
|
if (!result)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
result =
|
|
dto.OwnerId == userInfo.UserId ||
|
|
userInfo.Roles.Any(r => r.ToNormalString() == "admin");
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Tags ...
|
|
/// <summary>
|
|
/// Attach Tag to Specified File ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="tag"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task AttachTag(
|
|
Guid id,
|
|
string tag,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !tag.IsNullOrEmpty() &&
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!userInfo.IsNullOrDefault();
|
|
if (isValid)
|
|
{
|
|
//
|
|
// Check Permissions ...
|
|
isValid = await HasPermission(
|
|
id: id,
|
|
userInfo: userInfo
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
await TagProvider.AddReference(
|
|
tag: tag,
|
|
providedForId: id,
|
|
connectionId: connectionId
|
|
);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Detach Tag fro Specified File ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="tag"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task DetachTag(
|
|
Guid id,
|
|
string tag,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !tag.IsNullOrEmpty() &&
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid();
|
|
if (isValid)
|
|
{
|
|
//
|
|
// Check Permissions ...
|
|
isValid = await HasPermission(
|
|
id: id,
|
|
userInfo: userInfo
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
await TagProvider.RemoveReference(
|
|
tag: tag,
|
|
providedForId: id,
|
|
connectionId: connectionId
|
|
);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attach Tags for Specified File ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="tags"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task AttachTags(
|
|
Guid id,
|
|
IEnumerable<string> tags,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !tags.IsNull() &&
|
|
tags.HasChild() &&
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid();
|
|
if (isValid)
|
|
{
|
|
//
|
|
// Check Permissions ...
|
|
isValid = await HasPermission(
|
|
id: id,
|
|
userInfo: userInfo
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
foreach (var tag in tags)
|
|
{
|
|
//
|
|
await AttachTag(
|
|
id: id,
|
|
tag: tag,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId
|
|
);
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Detach Tags for Specified File ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="tags"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task DetachTags(
|
|
Guid id,
|
|
IEnumerable<string> tags,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !tags.IsNull() &&
|
|
tags.HasChild() &&
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid();
|
|
if (isValid)
|
|
{
|
|
//
|
|
// Check Permissions ...
|
|
isValid = await HasPermission(
|
|
id: id,
|
|
userInfo: userInfo
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
foreach (var tag in tags)
|
|
{
|
|
//
|
|
await DetachTag(
|
|
id: id,
|
|
tag: tag,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId
|
|
);
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Detach all Attached Tags for Specified File ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task DetachTags(
|
|
Guid id,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !id.IsNull() && !id.IsDefaultGuid();
|
|
if (isValid)
|
|
{
|
|
//
|
|
// Check Permissions ...
|
|
isValid = await HasPermission(
|
|
id: id,
|
|
userInfo: userInfo
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
await TagProvider.RemoveReferences(
|
|
providedForId: id,
|
|
connectionId: connectionId
|
|
);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Specified Model's Tag ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<string>> GetTags(Guid id)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !id.IsNull() &&
|
|
!id.IsDefaultGuid();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Model Exists ...
|
|
isValid = await IsExists(id);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var tags = await TagProvider.GetAllReferences(id);
|
|
|
|
//
|
|
var result = new List<string>();
|
|
isValid = !tags.IsNull() && tags.HasChild();
|
|
if (isValid)
|
|
{
|
|
//
|
|
result = tags.Select(t => t.Tag)
|
|
.ToList();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#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>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XFileDto>> Upload(
|
|
IFormFileCollection files,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = 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())
|
|
{
|
|
//
|
|
await SendPush(
|
|
action: XBaseEntityHubAction.Add.GetStringValue(),
|
|
payLoad: entity.ToJSON(camelCase: true),
|
|
connectionId: connectionId
|
|
);
|
|
|
|
//
|
|
entities.Add(entity);
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
//
|
|
// Preparing Result ...
|
|
var result = await ToDtoList(entities);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Files ...
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<Guid>> Remove(
|
|
string ids,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = 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 ...
|
|
var entity = await FileRepository.RemoveAsync(model.Id);
|
|
result.Add(model.Id);
|
|
|
|
//
|
|
await SendPush(
|
|
action: XBaseEntityHubAction.Delete.GetStringValue(),
|
|
payLoad: entity.ToJSON(camelCase: true),
|
|
connectionId: connectionId
|
|
);
|
|
}
|
|
|
|
//
|
|
// Remove Tags ...
|
|
if (!result.IsNull() && result.HasChild())
|
|
{
|
|
//
|
|
foreach (var id in result)
|
|
{
|
|
//
|
|
// Here we send Null Connection ID for Preventing Update Push ...
|
|
// since we Remove Entity ...
|
|
await DetachTags(
|
|
id: id,
|
|
userInfo: userInfo,
|
|
connectionId: null
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
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 Reference ...
|
|
/// <summary>
|
|
/// Get Reference Identifier for Specified Provider and Specified File ...
|
|
/// </summary>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <returns></returns>
|
|
public string GetIdentifier<TKey>(
|
|
string providedFor,
|
|
TKey forProvidedId
|
|
)
|
|
{
|
|
return $"{providedFor}_{forProvidedId}";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Specified File has Refernce to Provider ...
|
|
/// </summary>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> IsProvidedFor<TKey>(
|
|
string providedFor,
|
|
TKey forProvidedId,
|
|
Guid id
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!providedFor.IsNullOrEmpty() &&
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!forProvidedId.IsNull();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Exists ...
|
|
isValid = await IsExists(id);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var dto = await Get(id);
|
|
isValid = !dto.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
var result =
|
|
dto.References.IsNull() &&
|
|
dto.References.HasChild() &&
|
|
dto.References.Any(r => r.ProvidedFor == providedFor &&
|
|
r.ReferencedTo == $"{forProvidedId}");
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Specified Reference to Specified File ...
|
|
/// </summary>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="forIndex"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task AddReference<TKey>(
|
|
string providedFor,
|
|
TKey forProvidedId,
|
|
Guid id,
|
|
int forIndex = 0,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!providedFor.IsNullOrEmpty() &&
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!providedFor.IsNull() &&
|
|
!userInfo.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Has Reference or not ...
|
|
isValid = await IsProvidedFor(
|
|
providedFor: providedFor,
|
|
forProvidedId: forProvidedId,
|
|
id: id
|
|
);
|
|
if (!isValid)
|
|
{
|
|
//
|
|
// Check Permissions ...
|
|
isValid = await HasPermission(
|
|
id: id,
|
|
userInfo: userInfo
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var dto = await Get(id);
|
|
isValid = !dto.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
var reference = new XReference<string>
|
|
{
|
|
Index = forIndex,
|
|
ProvidedFor = providedFor,
|
|
ReferencedTo = $"{forProvidedId}"
|
|
};
|
|
dto.References.Add(reference);
|
|
|
|
//
|
|
dto = await Update(
|
|
item: dto,
|
|
id: dto.Id,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId
|
|
);
|
|
isValid = !dto.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Reference from Specified File ...
|
|
/// </summary>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="forIndex"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task RemoveReference<TKey>(
|
|
string providedFor,
|
|
TKey forProvidedId,
|
|
Guid id,
|
|
int forIndex = 0,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!providedFor.IsNullOrEmpty() &&
|
|
!id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!forProvidedId.IsNull() &&
|
|
!userInfo.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Has Reference or not ...
|
|
isValid = await IsProvidedFor(
|
|
providedFor: providedFor,
|
|
forProvidedId: forProvidedId,
|
|
id: id
|
|
);
|
|
if (isValid)
|
|
{
|
|
//
|
|
// Check Permissions ...
|
|
isValid = await HasPermission(
|
|
id: id,
|
|
userInfo: userInfo
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Dto ...
|
|
var dto = await Get(id);
|
|
isValid = !dto.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
var identifier = GetIdentifier(
|
|
providedFor: providedFor,
|
|
forProvidedId: forProvidedId
|
|
);
|
|
dto.References = dto.References
|
|
.Where(r =>
|
|
r.Index != forIndex &&
|
|
r.ProvidedFor != providedFor &&
|
|
r.ReferencedTo != $"{forProvidedId}"
|
|
)
|
|
.ToList();
|
|
dto = await Update(
|
|
item: dto,
|
|
id: dto.Id,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId
|
|
);
|
|
isValid = !dto.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
}
|
|
}
|
|
#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 entity = await FileRepository.GetAsync(id);
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await ToDto(entity);
|
|
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 ToDtoList(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 ToDto(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 ToDtoList(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 ToDtoQueryResult(queryResult);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve Owned Entities based on XQuery Pagination structure ...
|
|
/// </summary>
|
|
/// <param name="query"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XFileDto>> QueryOwned(
|
|
XQuery query,
|
|
XUserClaimsInfoDto userInfo = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !userInfo.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Create Where Clause ...
|
|
Expression<Func<XFile, bool>> whereClause = e => e.OwnerId == userInfo.UserId;
|
|
var result = await 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
|
|
)
|
|
{
|
|
//
|
|
var result = new XQueryResult<XFileDto>();
|
|
|
|
//
|
|
var queryResult = await FileRepository.ConditionalQueryAsync(
|
|
query: query,
|
|
whereClause: whereClause
|
|
);
|
|
if (!queryResult.IsNullOrDefault())
|
|
{
|
|
result = await ToDtoQueryResult(queryResult);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// retrieve Owned Entities based on XQuery Pagination structure by providing a Conditional Expression ...
|
|
/// </summary>
|
|
/// <param name="whereClause"></param>
|
|
/// <param name="query"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XFileDto>> ConditionalQueryOwned(
|
|
Expression<Func<XFile, bool>> whereClause,
|
|
XQuery query,
|
|
XUserClaimsInfoDto userInfo = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !whereClause.IsNull() &&
|
|
!userInfo.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Compile Where Clause ...
|
|
var whereFunc = whereClause.Compile();
|
|
|
|
//
|
|
// Create new Where Clause ...
|
|
Expression<Func<XFile, bool>> condition =
|
|
e => whereFunc(e) && e.OwnerId == userInfo.UserId;
|
|
|
|
//
|
|
var result = await 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>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileDto> Update(
|
|
Guid id,
|
|
XFileDto item,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid = !id.IsNull() &&
|
|
!id.IsDefaultGuid() &&
|
|
!item.IsNullOrDefault() &&
|
|
!userInfo.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check entity Exists and Retrieve it ...
|
|
isValid = await IsExists(id);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Permissions ...
|
|
isValid = await HasPermission(
|
|
id: id,
|
|
userInfo: userInfo
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Entity ...
|
|
var entity = await FileRepository.GetAsync(id);
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Fill Data for Update ...
|
|
var itemEntity = item.FromDto();
|
|
entity = entity.UpdateData(
|
|
updateWith: item,
|
|
propertyBlackList: new List<string>
|
|
{
|
|
nameof(XFile.Id),
|
|
nameof(XFile.Deleted),
|
|
nameof(XFileDto.Owner),
|
|
nameof(XFile.References),
|
|
}
|
|
);
|
|
|
|
//
|
|
if (!itemEntity.IsNullOrDefault())
|
|
{
|
|
entity.References = itemEntity.References;
|
|
}
|
|
|
|
//
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.InvalidData.Throw();
|
|
}
|
|
|
|
//
|
|
entity = await FileRepository.UpdateAsync(id, entity);
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
// Converts to Dto ...
|
|
var result = await ToDto(entity);
|
|
|
|
//
|
|
if (!result.IsNullOrDefault())
|
|
{
|
|
//
|
|
await SendPush(
|
|
action: XBaseEntityHubAction.Update.GetStringValue(),
|
|
payLoad: entity.ToJSON(camelCase: true),
|
|
connectionId: connectionId
|
|
);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// remove an Entity ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileDto> Remove(
|
|
Guid id,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = 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();
|
|
}
|
|
|
|
//
|
|
// Check Permissions ...
|
|
isValid = await HasPermission(
|
|
id: id,
|
|
userInfo: userInfo
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var entity = await FileRepository.RemoveAsync(id);
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
result = await ToDto(entity);
|
|
if (result.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
// Removing Tags ...
|
|
await TagProvider.RemoveReferences(result.Id);
|
|
|
|
//
|
|
await SendPush(
|
|
action: XBaseEntityHubAction.Delete.GetStringValue(),
|
|
payLoad: entity.ToJSON(camelCase: true),
|
|
connectionId: connectionId
|
|
);
|
|
|
|
//
|
|
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 Hub Actions ...
|
|
/// <summary>
|
|
/// Send Custom Push Message ...
|
|
/// </summary>
|
|
/// <param name="action"></param>
|
|
/// <param name="payLoad"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <returns></returns>
|
|
public async Task SendPush(
|
|
string action,
|
|
string payLoad,
|
|
string connectionId = null
|
|
)
|
|
{
|
|
//
|
|
var actions = new List<string>
|
|
{
|
|
XBaseEntityHubAction.Add.GetStringValue(),
|
|
XBaseEntityHubAction.Update.GetStringValue(),
|
|
XBaseEntityHubAction.Delete.GetStringValue(),
|
|
XBaseEntityHubAction.AddMany.GetStringValue(),
|
|
XBaseEntityHubAction.DeleteMany.GetStringValue(),
|
|
XBaseEntityHubAction.UpdateMany.GetStringValue(),
|
|
XBaseEntityHubAction.AddOrUpdate.GetStringValue(),
|
|
};
|
|
|
|
//
|
|
// Validate ...
|
|
var isValid =
|
|
!Hub.IsNull() &&
|
|
!action.IsNullOrEmpty() &&
|
|
actions.Contains(action);
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
var clients = Hub.Clients.All;
|
|
if (!connectionId.IsNullOrEmpty())
|
|
{
|
|
clients = Hub.Clients.AllExcept(connectionId);
|
|
}
|
|
try
|
|
{
|
|
await clients.SendAsync(action, payLoad, connectionId);
|
|
}
|
|
catch { }
|
|
}
|
|
#endregion
|
|
}
|
|
} |