using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using xCommons.Configurations;
using xCommons.Extensions;
using xCommons.Providers;
using xFileService.Interfaces;
using xFileService.Models.Dtos;
using xIdentityService.Controllers;
using xIdentityService.Interfaces;
using xModels.Dtos;
using XFileDto = xFileService.Models.Dtos.XFileDto;
namespace xFileService.Controllers
{
///
/// a Controller base Class which implement based Actions to Provides File Provider Access ...
///
public abstract class XFileServiceControllerBase : XIBaseProviderController, IXFileServiceControllerActions
{
//
#region Props ...
public IXFileProvider FileProvider { get; }
#endregion
//
#region Constructor ...
protected XFileServiceControllerBase(
ILogger logger,
IXFileProvider fileProvider,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
XValidationProvider validationProvider
) : base(
logger,
appConfiguration,
identityProvider,
validationProvider
)
{
//
FileProvider = fileProvider;
}
#endregion
//
#region Tags ...
///
/// Attach Tag to Specified Model ...
///
///
///
///
[HttpGet("Tags/Attach")]
public async Task TagAttach(
[FromQuery] string tag,
[FromQuery] Guid id
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
await FileProvider.TagAttach(
id: id,
tag: tag,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok();
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Detach Tag From Specified Model ...
///
///
///
///
[HttpGet("Tags/Detach")]
public async Task TagDetach(
[FromQuery] string tag,
[FromQuery] Guid id
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
await FileProvider.TagDetach(
id: id,
tag: tag,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok();
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Attach Tags to Specified Model ...
///
///
///
///
[HttpGet("Tags/AttachMany")]
public async Task TagsAttach(
[FromQuery] string tags,
[FromQuery] Guid id
)
{
//
// Do ...
try
{
//
var tagsList = tags.ParseListString();
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
await FileProvider.TagsAttach(
id: id,
tags: tagsList,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok();
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Detach Tags From Specified Model ...
///
///
///
///
[HttpGet("Tags/DetachMany")]
public async Task TagsDetach(
[FromQuery] string tags,
[FromQuery] Guid id
)
{
//
// Do ...
try
{
//
var tagsList = tags.ParseListString();
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
await FileProvider.TagsDetach(
id: id,
tags: tagsList,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok();
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
//
#region Tools ...
///
/// Stream Specified File ...
///
///
///
[HttpGet("{id}/Stream/ById")]
public async Task Stream(
[FromRoute] Guid id
)
{
//
// Do ...
try
{
//
var result = await FileProvider.Stream(id);
//
return result;
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Stream Specified File ...
///
///
///
[HttpGet("{name}/Stream/ByName")]
public async Task> Stream(
[FromRoute] string name
)
{
//
// Do ...
try
{
//
var result = await FileProvider.Stream(name);
//
return result;
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Download Specified File ...
///
///
///
[HttpGet("{id}/Download/ById")]
public async Task Download(
[FromRoute] Guid id
)
{
//
// Do ...
try
{
//
var result = await FileProvider.Download(id);
//
return result;
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Download Specified File ...
///
///
///
[HttpGet("{name}/Download/ByName")]
public async Task Download(
[FromRoute] string name
)
{
//
// Do ...
try
{
//
var result = await FileProvider.Download(name);
//
return result;
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Upload Files ...
///
///
///
[HttpPost("Upload")]
[RequestSizeLimit(966_367_641)]
public async Task>> Upload(
[FromForm] IFormFileCollection files
)
{
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
var result = await FileProvider.Upload(
files: files,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Remove Specified Files ...
///
///
///
/// [HttpDelete("Remove")]
[HttpDelete("Remove")]
public async Task>> Remove(
[FromQuery] string ids
) {
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
bool isAdmin = userInfo.Roles.Any(r => r.ToNormalString() == "admin");
var userId = userInfo.UserId;
//
var result = await FileProvider.Remove(
ids: ids,
userInfo: userInfo,
connectionId: connectionId
);
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
//
#region Model ...
///
/// Get Specified File Model ...
///
///
///
///
[HttpGet("{id}")]
public async Task> Get(
[FromRoute] Guid id
) {
//
// Do ...
try
{
//
var result = await FileProvider.Get(id);
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Get All Exists File Models ...
///
///
///
[HttpGet("All")]
public async Task>> GetAll() {
//
// Do ...
try
{
//
var result = await FileProvider.GetAll();
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// retrieve Entities based on XQuery Pagination structure ...
///
///
///
[HttpGet("Query")]
public async Task>> Query(
[FromQuery] XQuery query
) {
//
// Do ...
try
{
//
var result = await FileProvider.Query(query);
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// retrieve Owned Entities based on XQuery Pagination structure ...
///
///
///
[HttpGet("Owned/Query")]
public async Task>> QueryOwned(
[FromQuery] XQuery query
) {
//
// Do ...
try
{
//
// Retrieve User Info ...
var userInfo = await GetUserInfo();
//
var result = await FileProvider.QueryOwned(
query: query,
userInfo: userInfo
);
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// count all exists Entities ...
///
///
[HttpGet("Count")]
public async Task> Count() {
//
// Do ...
try
{
//
var result = await FileProvider.Count();
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
///
/// Check an Entity exists or not ...
///
///
///
[HttpGet("{id}/IsExists")]
public async Task> IsExists(
[FromRoute] Guid id
) {
//
// Do ...
try
{
//
var result = await FileProvider.IsExists(id);
//
return Ok(result
.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
}
}