Skip to content
Snippets Groups Projects
Commit e26d7e9d authored by Marcel Nellesen's avatar Marcel Nellesen
Browse files

Merge branch 'Product/1440-largerFiles' into 'Sprint/2021-07'

Product/1440 larger files

See merge request !32
parents 97799e8f a343f9c5
No related branches found
No related tags found
2 merge requests!32Product/1440 larger files,!31Sprint/2021 07
......@@ -9,6 +9,7 @@ using Coscine.ResourceLoader;
using Coscine.ResourceTypeBase;
using Coscine.WaterbutlerHelper.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Logging;
......@@ -16,7 +17,6 @@ using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
......@@ -180,15 +180,84 @@ namespace Coscine.Api.Blob.Controllers
}
}
/// <summary>
/// This method returns the download url for a resource and path
/// </summary>
/// <param name="resourceId">Id of the resource</param>
/// <param name="path">Path to the to downloaded file</param>
/// <returns>Uri of the download url in the form of: "{ data: { url: url } }"</returns>
[HttpGet("[controller]/downloadUrl/{resourceId}/{*path}")]
public async Task<IActionResult> GetDownloadUrl(string resourceId, string path)
{
return await GetUrl(resourceId, path,
(resourceTypeDefinition, path, version, resourceTypeOptions)
=> resourceTypeDefinition.GetEntryDownloadUrl(path, version, resourceTypeOptions));
}
/// <summary>
/// This method returns the upload url for a resource and path
/// </summary>
/// <param name="resourceId">Id of the resource</param>
/// <param name="path">Path to the to uploaded file</param>
/// <returns>Uri of the upload url in the form of: "{ data: { url: url } }"</returns>
[HttpGet("[controller]/uploadUrl/{resourceId}/{*path}")]
public async Task<IActionResult> GetUploadUrl(string resourceId, string path)
{
return await GetUrl(resourceId, path,
(resourceTypeDefinition, path, version, resourceTypeOptions)
=> resourceTypeDefinition.GetEntryStoreUrl(path, version, resourceTypeOptions));
}
private async Task<IActionResult> GetUrl(string resourceId, string path, Func<ResourceTypeDefinition, string, string, Dictionary<string, string>, Task<Uri>> receiver)
{
var rawPath = path;
var user = _authenticator.GetUser();
path = $"/{path}";
var checkPath = CheckPath(path);
if (checkPath != null)
{
return checkPath;
}
var checkResourceId = CheckResource(resourceId, out Resource resource);
if (checkResourceId != null)
{
return checkResourceId;
}
var checkUser = CheckUser(user, resource);
if (checkUser != null)
{
return checkUser;
}
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 entryUrl = await receiver(resourceTypeDefinition, rawPath, null, resourceTypeOptions);
if (entryUrl == null)
{
var hostUrl = await _configuration.GetStringAsync("coscine/local/api/additional/url");
var apiPath = $"/coscine/api/{((System.Reflection.Assembly.GetEntryAssembly() != null) ? System.Reflection.Assembly.GetEntryAssembly().GetName().Name : System.Reflection.Assembly.GetExecutingAssembly().GetName().Name)}";
entryUrl = new Uri($"{hostUrl}{apiPath}/Blob/{resourceId}/{rawPath}");
}
return Ok($"{{ \"data\": {{ \"url\": \"{entryUrl}\" }} }}");
}
/// <summary>
/// This method uploads a given File
/// </summary>
/// <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>
[HttpPut("[controller]/{resourceId}/{*path}")]
[DisableRequestSizeLimit]
public async Task<IActionResult> UploadFile(string resourceId, string path)
[RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
[HttpPut("[controller]/{resourceId}/{*path}")]
public async Task<IActionResult> UploadFile(string resourceId, string path, List<IFormFile> files)
{
var user = _authenticator.GetUser();
path = $"/{path}";
......@@ -207,6 +276,12 @@ namespace Coscine.Api.Blob.Controllers
{
return checkUser;
}
if (files.Count != 1)
{
return BadRequest($"Only one file can be uploaded per request.");
}
var id = GenerateId(resourceId, path);
if (!_rdfStoreConnector.HasGraph(id.AbsoluteUri))
{
......@@ -217,7 +292,8 @@ namespace Coscine.Api.Blob.Controllers
try
{
var resourceTypeOptions = _resourceModel.GetResourceTypeOptions(resource.Id);
resourceTypeOptions.Add("ContentLength", Request.ContentLength.ToString());
var stream = files[0].OpenReadStream();
resourceTypeOptions.Add("ContentLength", stream.Length.ToString());
var resourceTypeDefinition = ResourceTypeFactory.CreateResourceTypeObject(resource.Type.DisplayName, _configuration);
if (resourceTypeDefinition == null)
{
......@@ -232,11 +308,11 @@ namespace Coscine.Api.Blob.Controllers
{
// do nothing
}
await resourceTypeDefinition.StoreEntry(resource.Id.ToString(), path, Request.Body, resourceTypeOptions);
await resourceTypeDefinition.StoreEntry(resource.Id.ToString(), path, stream, resourceTypeOptions);
LogAnalytics(infos == null ? "Upload File" : "Update File", resourceId, path, user);
return NoContent();
}
catch
catch (Exception e)
{
return BadRequest($"Error in communication with the resource");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment