diff --git a/src/Blob/Blob.csproj b/src/Blob/Blob.csproj index ff3b577d2e2ac2c09c9123e406c949bc7773eed5..b2618dbb0872566f4d6374a5a3536739c1c18bf9 100644 --- a/src/Blob/Blob.csproj +++ b/src/Blob/Blob.csproj @@ -22,6 +22,7 @@ <PackageReference Include="Coscine.Database" Version="2.*-*" /> <PackageReference Include="Coscine.Logging" Version="2.*-*" /> <PackageReference Include="Coscine.ResourceLoader" Version="2.*-*" /> + <PackageReference Include="Coscine.ResourceTypeBase" Version="2.*-*" /> <PackageReference Include="Coscine.WaterbutlerHelper" Version="2.*-*" /> </ItemGroup> -</Project> \ No newline at end of file +</Project> diff --git a/src/Blob/Controllers/BlobController.cs b/src/Blob/Controllers/BlobController.cs index 4b44b21a084ee9a926406657abb7afbd7f02c359..5040f681c336430868100d043a5ff00151e465fe 100644 --- a/src/Blob/Controllers/BlobController.cs +++ b/src/Blob/Controllers/BlobController.cs @@ -27,7 +27,6 @@ namespace Coscine.Api.Blob.Controllers /// This controller represents the actions which can be taken with a Blob object. /// </summary> [Authorize] - public class BlobController : Controller { private readonly IConfiguration _configuration; @@ -74,13 +73,13 @@ namespace Coscine.Api.Blob.Controllers /// This method returns the amount of allocated space for the given resource /// </summary> /// <param name="resourceId">Id of a resource</param> - /// <returns>Data, file count and bytesize used or Status Code 400, 404, 401 or 500 </returns> + /// <returns>Data, file count and byte size used or Status Code 400, 404, 401 or 500 </returns> [HttpGet("[controller]/{resourceId}/quota")] public IActionResult GetQuota(string resourceId) { if (!Guid.TryParse(resourceId, out Guid resourceGuid)) { - return BadRequest($"{resourceId} is not a guid."); + return BadRequest($"{resourceId} is not a GUID."); } var user = _authenticator.GetUser(); @@ -109,28 +108,31 @@ namespace Coscine.Api.Blob.Controllers return BadRequest("User does not have permission to the resource."); } - if ((resource.Type.DisplayName.ToLower() == "rds" || resource.Type.DisplayName.ToLower() == "rdss3") && resource.ResourceTypeOptionId.HasValue) + var resourceTypeOptions = _resourceModel.GetResourceTypeOptions(resource.Id); + var resourceTypeDefinition = ResourceTypeFactory.CreateResourceTypeObject(resource.Type.DisplayName, _configuration); + if (resourceTypeDefinition == null) + { + return BadRequest($"No provider for: \"{resource.Type.DisplayName}\"."); + } + + var resourceTypeInformation = resourceTypeDefinition.GetResourceTypeInformation().Result; + + if (resource.ResourceTypeOptionId.HasValue && resourceTypeInformation.IsQuotaAvailable) { try { - var resourceTypeOptions = _resourceModel.GetResourceTypeOptions(resource.Id); - var resourceTypeDefinition = ResourceTypeFactory.CreateResourceTypeObject(resource.Type.DisplayName, _configuration); - if (resourceTypeDefinition == null) - { - return BadRequest($"No provider for: \"{resource.Type.DisplayName}\"."); - } var totalFileSize = resourceTypeDefinition.GetResourceQuotaUsed(resourceId, resourceTypeOptions).Result; return Ok($"{{ \"data\": {{ \"usedSizeByte\": {totalFileSize} }}}}"); } catch (Exception e) { _coscineLogger.Log("Get Quota failed", e); - return BadRequest($"Error in communication with the resource"); + return BadRequest("Error in communication with the resource"); } } else { - return BadRequest("The resource type must be rds."); + return BadRequest("The resource quota must be adjustable."); } } @@ -139,7 +141,7 @@ namespace Coscine.Api.Blob.Controllers /// </summary> /// <param name="resourceId">Id of the resource</param> /// <param name="path"> Path to the file </param> - /// <returns>File if file exists otherwise Statuscode 204, 400, 401 or 404 </returns> + /// <returns>File if file exists otherwise status code 204, 400, 401 or 404 </returns> [HttpGet("[controller]/{resourceId}/{*path}")] [DisableRequestSizeLimit] public async Task<IActionResult> GetFile(string resourceId, string path) @@ -171,14 +173,14 @@ namespace Coscine.Api.Blob.Controllers } var infos = await resourceTypeDefinition.GetEntry(resource.Id.ToString(), path, null, resourceTypeOptions); var response = await resourceTypeDefinition.LoadEntry(resource.Id.ToString(), path, null, resourceTypeOptions); - new FileExtensionContentTypeProvider().TryGetContentType(path.Substring(path.LastIndexOf("/")), out string contentType); - LogAnalytics("Download File", resourceId, path.Substring(1), user); + new FileExtensionContentTypeProvider().TryGetContentType(path[path.LastIndexOf("/")..], out string contentType); + LogAnalytics("Download File", resourceId, path[1..], user); return File(response, contentType ?? "application/octet-stream"); } catch (Exception e) { _coscineLogger.Log("Get File failed", e); - return BadRequest($"Error in communication with the resource"); + return BadRequest("Error in communication with the resource"); } } @@ -188,7 +190,7 @@ namespace Coscine.Api.Blob.Controllers /// <param name="resourceId">Id of the resource </param> /// <param name="path">Path to the file</param> /// <param name="files">List of files.</param> - /// <returns>Statuscode 204 if file is uploaded otherwise Statuscode 400 or 403</returns> + /// <returns>status code 204 if file is uploaded otherwise status code 400 or 403</returns> [DisableRequestSizeLimit] [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)] [HttpPut("[controller]/{resourceId}/{*path}")] @@ -214,19 +216,19 @@ namespace Coscine.Api.Blob.Controllers if (resource.Archived == "1") { - return BadRequest("The resource is readonly!"); + return BadRequest("The resource is read only!"); } if (files.Count != 1) { - return BadRequest($"Only one file can be uploaded per request."); + return BadRequest("Only one file can be uploaded per request."); } var id = GenerateId(resourceId, path); if (!_rdfStoreConnector.HasGraph(id.AbsoluteUri)) { return StatusCode((int)HttpStatusCode.Forbidden, - "No metadataset has been added for this file."); + "No metadata set has been added for this file."); } try @@ -255,7 +257,7 @@ namespace Coscine.Api.Blob.Controllers catch (Exception e) { _coscineLogger.Log("Upload File failed", e); - return BadRequest($"Error in communication with the resource"); + return BadRequest("Error in communication with the resource"); } } @@ -264,7 +266,7 @@ namespace Coscine.Api.Blob.Controllers /// </summary> /// <param name="resourceId">Id of the resource </param> /// <param name="path">Path to the file</param> - /// <returns>Statuscode 204 if deletion successful otherwise Statuscode 400, 401 or 404 </returns> + /// <returns>status code 204 if deletion successful otherwise status code 400, 401 or 404 </returns> [HttpDelete("[controller]/{resourceId}/{*path}")] public async Task<IActionResult> DeleteFile(string resourceId, string path) { @@ -288,7 +290,7 @@ namespace Coscine.Api.Blob.Controllers if (resource.Archived == "1") { - return BadRequest("The resource is readonly!"); + return BadRequest("The resource is read only!"); } try @@ -306,14 +308,14 @@ namespace Coscine.Api.Blob.Controllers catch (Exception e) { _coscineLogger.Log("Delete failed", e); - return BadRequest($"Error in communication with the resource"); + return BadRequest("Error in communication with the resource"); } } /// <summary> /// This method checks if the resource is valid /// </summary> - /// <returns>Statuscode 204 if resource is valid otherwise Statuscode 400 or 404</returns> + /// <returns>status code 204 if resource is valid otherwise status code 400 or 404</returns> [HttpPost("[controller]/validate")] public async Task<IActionResult> IsResourceValid([FromBody] JToken resource) { @@ -346,20 +348,20 @@ namespace Coscine.Api.Blob.Controllers catch (Exception e) { _coscineLogger.Log("Resource validation failed", e); - return BadRequest($"Error in communication with the resource"); + return BadRequest("Error in communication with the resource"); } } /// <summary> - /// Tries to establish connection with resource and validates wether the given file/folder exists + /// Tries to establish connection with resource and validates whether the given file/folder exists /// </summary> - private IActionResult CheckResource(string resourceId, out Resource resource) + private IActionResult CheckResource(string resourceId, out Resource resource) { resource = null; if (!Guid.TryParse(resourceId, out Guid resourceGuid)) { - return BadRequest($"{resourceId} is not a guid."); + return BadRequest($"{resourceId} is not a GUID."); } try @@ -388,7 +390,7 @@ namespace Coscine.Api.Blob.Controllers /// Checks if the path is valid /// </summary> /// <param name="path">path</param> - /// <returns>Statuscode 400 if the given path is not valid</returns> + /// <returns>status code 400 if the given path is not valid</returns> public IActionResult CheckPath(string path) { if (string.IsNullOrWhiteSpace(path)) @@ -415,7 +417,7 @@ namespace Coscine.Api.Blob.Controllers /// </summary> /// <param name="user">user</param> /// <param name="resource">resource</param> - /// <returns>Statuscode 403 if the user has no access</returns> + /// <returns>status code 403 if the user has no access</returns> public IActionResult CheckUser(User user, Resource resource) { if (user == null || !_resourceModel.HasAccess(user, resource, UserRoles.Owner, UserRoles.Member)) @@ -442,4 +444,4 @@ namespace Coscine.Api.Blob.Controllers } } } -} +} \ No newline at end of file diff --git a/src/Blob/Program.cs b/src/Blob/Program.cs index 2d16fa9bcf014dac6253a7ac50af673c61a3cc2d..391687893da1af1bbb3803bcdb5068bd90decb2e 100644 --- a/src/Blob/Program.cs +++ b/src/Blob/Program.cs @@ -18,4 +18,4 @@ namespace Coscine.Api.Blob InitializeWebService<Startup>(); } } -} +} \ No newline at end of file diff --git a/src/Blob/Startup.cs b/src/Blob/Startup.cs index bd0ff79b41a2a13b0cfd9d220092204217d8fc7e..3bb1439cb448b2065480cb3b4bb7ff6fd94080cc 100644 --- a/src/Blob/Startup.cs +++ b/src/Blob/Startup.cs @@ -1,5 +1,5 @@ -using Coscine.WaterbutlerHelper.Services; -using Coscine.ApiCommons; +using Coscine.ApiCommons; +using Coscine.WaterbutlerHelper.Services; using Microsoft.Extensions.DependencyInjection; using System; @@ -16,8 +16,9 @@ namespace Coscine.Api.Blob public Startup() { } + /// <summary> - /// Configures custom service extenions + /// Configures custom service extensions /// </summary> /// <param name="services">services</param> public override void ConfigureServicesExtension(IServiceCollection services) @@ -30,6 +31,5 @@ namespace Coscine.Api.Blob client.Timeout = TimeSpan.FromMinutes(30); }); } - } -} +} \ No newline at end of file