Skip to content
Snippets Groups Projects
Commit 3ea1375b authored by Benedikt Heinrichs's avatar Benedikt Heinrichs Committed by Petar Hristov
Browse files

Update: Extend with a Mimetype attribute

parent d61ee1d6
No related branches found
No related tags found
2 merge requests!73Release: Sprint/2022 10 :robot:,!71Update: Extend with a Mimetype attribute
......@@ -24,7 +24,6 @@ using System.Threading.Tasks;
using System.Web;
using VDS.RDF;
using VDS.RDF.Parsing;
using VDS.RDF.Writing;
namespace Coscine.Api.Tree.Controllers
{
......@@ -101,9 +100,10 @@ namespace Coscine.Api.Tree.Controllers
/// </summary>
/// <param name="resourceId"> Id of a resource</param>
/// <param name="path">Path to the file</param>
/// <param name="mimeType">Requested MimeType of the metadata</param>
/// <returns> JSON Object with the metadata if OK, otherwise status code 400 or 401 or 404</returns>
[HttpGet("[controller]/{resourceId}/")]
public async Task<IActionResult> GetMetadataWithParameter(string resourceId, [FromQuery] string path = "")
public async Task<IActionResult> GetMetadataWithParameter(string resourceId, [FromQuery] string path = "", [FromQuery] string mimeType = "application/rdf+json")
{
// Strip the first slash, to reuse the previous implementation.
if (path.StartsWith("/"))
......@@ -111,7 +111,7 @@ namespace Coscine.Api.Tree.Controllers
path = path[1..];
}
return await GetMetadata(resourceId, path);
return await GetMetadata(resourceId, path, mimeType);
}
/// <summary>
......@@ -119,8 +119,9 @@ namespace Coscine.Api.Tree.Controllers
/// </summary>
/// <param name="resourceId"> Id of a resource</param>
/// <param name="path">Path to the file</param>
/// <param name="mimeType">Requested MimeType of the metadata</param>
/// <returns> JSON Object with the metadata if OK, otherwise status code 400 or 401 or 404</returns>
public async Task<IActionResult> GetMetadata(string resourceId, string path = "")
public async Task<IActionResult> GetMetadata(string resourceId, string path = "", string mimeType = "application/rdf+json")
{
if (path.Contains("%2F") || path.Contains("%2f"))
{
......@@ -156,7 +157,7 @@ namespace Coscine.Api.Tree.Controllers
var resourceTypeInformation = resourceTypeDefinition.GetResourceTypeInformation().Result;
var metadataInfos = new List<ResourceEntry>(fileInfos);
var graphs = new List<JToken>();
var graphs = new List<object>();
int metadataCount = 0;
foreach (var info in metadataInfos)
{
......@@ -165,7 +166,20 @@ namespace Coscine.Api.Tree.Controllers
{
var graph = _rdfStoreConnector.GetGraph(id);
metadataCount = graph.Triples.Count;
graphs.Add(JToken.Parse(StringWriter.Write(graph, new RdfJsonWriter())));
var writer = MimeTypesHelper.GetWriter(new List<string>() { mimeType });
var parsedRdf = VDS.RDF.Writing.StringWriter.Write(graph, writer);
// Legacy Support
if (mimeType == "application/rdf+json")
{
graphs.Add(JToken.Parse(parsedRdf));
}
else
{
graphs.Add(new JObject
{
[id.AbsoluteUri] = parsedRdf
});
}
}
}
......@@ -278,9 +292,10 @@ namespace Coscine.Api.Tree.Controllers
/// </summary>
/// <param name="resourceId">Id of the resource</param>
/// <param name="path">Path to the file</param>
/// <param name="mimeType">Requested MimeType of the metadata</param>
/// <returns>If OK status code 204, otherwise status code 400 or 401</returns>
[HttpPut("[controller]/{resourceId}/")]
public IActionResult StoreMetadataForFileWithParameter(string resourceId, [FromQuery] string path = "")
public IActionResult StoreMetadataForFileWithParameter(string resourceId, [FromQuery] string path = "", [FromQuery] string mimeType = "application/rdf+json")
{
// Strip the first slash, to reuse the previous implementation.
if (path.StartsWith("/"))
......@@ -288,7 +303,7 @@ namespace Coscine.Api.Tree.Controllers
path = path[1..];
}
return StoreMetadataForFile(resourceId, path);
return StoreMetadataForFile(resourceId, path, mimeType);
}
/// <summary>
......@@ -296,8 +311,9 @@ namespace Coscine.Api.Tree.Controllers
/// </summary>
/// <param name="resourceId">Id of the resource</param>
/// <param name="path">Path to the file</param>
/// <param name="mimeType">Requested MimeType of the metadata</param>
/// <returns>If OK status code 204, otherwise status code 400 or 401</returns>
public IActionResult StoreMetadataForFile(string resourceId, string path)
public IActionResult StoreMetadataForFile(string resourceId, string path, string mimeType = "application/rdf+json")
{
path = $"/{path}";
if (path.Contains("%2F") || path.Contains("%2f"))
......@@ -305,13 +321,34 @@ namespace Coscine.Api.Tree.Controllers
return BadRequest("Path can not contain the sequence %2F.");
}
var innerBlock = ObjectFactory<JToken>.DeserializeFromStream(Request.Body);
// Ducktape solution for supporting multiple mimetypes
var metadataObject = ObjectFactory<JToken>.DeserializeFromStream(Request.Body);
var graphNameUri = GenerateId(resourceId, path);
var json = new JObject
JObject json;
// Legacy Support
if (mimeType == "application/rdf+json")
{
json = new JObject
{
[graphNameUri.AbsoluteUri] = innerBlock
[graphNameUri.AbsoluteUri] = metadataObject
};
}
else
{
var tempGraph = new Graph();
StringParser.Parse(tempGraph, metadataObject.Value<string>("metadata").ToString(), MimeTypesHelper.GetParser(mimeType));
var triplesList = tempGraph.Triples.ToArray();
var subjectNode = tempGraph.CreateUriNode(graphNameUri);
foreach (var triple in triplesList)
{
tempGraph.Retract(triple);
tempGraph.Assert(new Triple(subjectNode, triple.Predicate, triple.Object));
}
json = JObject.Parse(VDS.RDF.Writing.StringWriter.Write(tempGraph, MimeTypesHelper.GetWriter("application/rdf+json")));
}
var user = _authenticator.GetUser();
var resource = _resourceModel.GetById(Guid.Parse(resourceId));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment