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 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 ... /// /// /// /// [HttpPost("{id}/Tags/Attach")] public virtual async Task AttachTag( [FromRoute] Guid id, [FromQuery] string tag ) { // // 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.AttachTag( id: id, tag: tag, userInfo: userInfo, connectionId: connectionId ); // return Ok(); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Detach Tag From Specified Model ... /// /// /// /// [HttpDelete("{id}/Tags/Detach")] public virtual async Task DetachTag( [FromRoute] Guid id, [FromQuery] string tag ) { // // 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.DetachTag( id: id, tag: tag, userInfo: userInfo, connectionId: connectionId ); // return Ok(); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Attach Tags to Specified Model ... /// /// /// /// [HttpPost("{id}/Tags/AttachMany")] public virtual async Task AttachTags( [FromRoute] Guid id, [FromQuery] string tags ) { // // 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.AttachTags( id: id, tags: tagsList, userInfo: userInfo, connectionId: connectionId ); // return Ok(); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Detach Tags From Specified Model ... /// /// /// /// [HttpDelete("{id}/Tags/DetachMany")] public virtual async Task DetachTags( [FromRoute] Guid id, [FromQuery] string tags ) { // // 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.DetachTags( id: id, tags: tagsList, userInfo: userInfo, connectionId: connectionId ); // return Ok(); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Retrieve Tags of Specified File ... /// /// /// [HttpGet("{id}/Tags")] public virtual async Task>> GetTags( [FromRoute] Guid id ) { // // Do ... try { // var result = await FileProvider.GetTags(id); // return Ok(result .ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } #endregion // #region Tools ... /// /// Stream Specified File ... /// /// /// [HttpGet("{id}/Stream/ById")] public virtual 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 virtual 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 virtual 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 virtual 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("")] [RequestSizeLimit(966_367_641)] public virtual 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("{id}")] public virtual async Task>> Remove( [FromRoute] 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 virtual 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 virtual 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 virtual 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("Query/Owned")] public virtual 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 virtual 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 virtual 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 } }