Skip to content

Commit

Permalink
Rewrite of MimeTypeMap to support extensions with multiple mimetypes (#…
Browse files Browse the repository at this point in the history
…296)

* Rewrite of MimeTypeMap to support matching file extensions to mimetype aliases.

Example: zip files can be application/zip, application/zip-compressed or application/x-zip-compressed

* Test to cover case where fileextension not found in map

* remove misleading comment

* Fix codeQL warning
  • Loading branch information
tjololo authored Aug 28, 2023
1 parent 9d441d6 commit 0e2e509
Show file tree
Hide file tree
Showing 6 changed files with 856 additions and 711 deletions.
12 changes: 6 additions & 6 deletions src/Altinn.App.Api/Controllers/ResourceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public IActionResult Index(string org, string app, string id)

if (fileContent != null)
{
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(Path.GetExtension(id).ToLower()));
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(Path.GetExtension(id).ToLower()).ToString());
}

if (id == _appSettings.RuleConfigurationJSONFileName || id == _appSettings.RuleHandlerFileName)
Expand All @@ -75,7 +75,7 @@ public IActionResult GetRuntimeResource(string id)

if (fileContent != null)
{
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(Path.GetExtension(id).ToLower()));
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(Path.GetExtension(id).ToLower()).ToString());
}

return StatusCode(404);
Expand All @@ -99,14 +99,14 @@ public IActionResult GetTextResources(string org, string app)
byte[] fileContent = _appResourceService.GetText(org, app, id);
if (fileContent != null)
{
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(Path.GetExtension(id).ToLower()));
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(Path.GetExtension(id).ToLower()).ToString());
}

id = $"resource.{defaultLang}.json";
fileContent = _appResourceService.GetText(org, app, id);
if (fileContent != null)
{
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(Path.GetExtension(id).ToLower()));
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(Path.GetExtension(id).ToLower()).ToString());
}

return StatusCode(404);
Expand Down Expand Up @@ -226,7 +226,7 @@ public ActionResult GetRulehandler(string org, string app, string id)
byte[] fileContent = _appResourceService.GetRuleHandlerForSet(id);
if (fileContent != null)
{
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(".ts"));
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(".ts").ToString());
}

return NoContent();
Expand All @@ -249,7 +249,7 @@ public ActionResult GetRuleConfiguration(string org, string app, string id)
return NoContent();
}

return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(".json"));
return new FileContentResult(fileContent, MimeTypeMap.GetMimeType(".json").ToString());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static (bool Success, List<ValidationIssue> Errors) CompliesWithDataRestr
}

string filetype = splitFilename[splitFilename.Length - 1];
string mimeType = MimeTypeMap.GetMimeType(filetype);
var mimeType = MimeTypeMap.GetMimeType(filetype);

if (!request.Headers.TryGetValue("Content-Type", out StringValues contentType))
{
Expand All @@ -97,7 +97,7 @@ public static (bool Success, List<ValidationIssue> Errors) CompliesWithDataRestr
}

// Verify that file mime type matches content type in request
if (!contentType.Equals("application/octet-stream") && !mimeType.Equals(contentType, StringComparison.InvariantCultureIgnoreCase))
if (!contentType.Equals("application/octet-stream") && !mimeType.Equals(contentType))
{
errors.Add(new ValidationIssue
{
Expand All @@ -110,7 +110,7 @@ public static (bool Success, List<ValidationIssue> Errors) CompliesWithDataRestr
}

// Verify that file mime type is an allowed content-type
if (!dataType.AllowedContentTypes.Contains(mimeType, StringComparer.InvariantCultureIgnoreCase) && !dataType.AllowedContentTypes.Contains("application/octet-stream"))
if (!dataType.AllowedContentTypes.Contains(contentType.ToString(), StringComparer.InvariantCultureIgnoreCase) && !dataType.AllowedContentTypes.Contains("application/octet-stream"))
{
errors.Add(new ValidationIssue
{
Expand Down
Loading

0 comments on commit 0e2e509

Please sign in to comment.