Skip to content

Automatically determine the Content-Type from the extension #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ internal static class FunctionEnvironment
{
public static bool IsAvailable => !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME"));

public static string RootPath { get; } = IsAvailable ? Environment.ExpandEnvironmentVariables(@"%HOME%\site\wwwroot") : Environment.CurrentDirectory;
public static string RootPath => IsAvailable ? Environment.ExpandEnvironmentVariables(@"%HOME%\site\wwwroot") : Environment.CurrentDirectory;
}
}
11 changes: 9 additions & 2 deletions src/Azure.WebJobs.Extensions.HttpApi/HttpFunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Net.Http.Headers;
Expand All @@ -30,7 +31,10 @@ protected HttpFunctionBase(IHttpContextAccessor httpContextAccessor)
private IUrlHelper _url;
private ProblemDetailsFactory _problemDetailsFactory;

private static readonly IFileProvider _functionFileProvider = new PhysicalFileProvider(FunctionEnvironment.RootPath);
private const string DefaultContentType = "application/octet-stream";

private static readonly IFileProvider _fileProvider = new PhysicalFileProvider(FunctionEnvironment.RootPath);
private static readonly IContentTypeProvider _contentTypeProvider = new FileExtensionContentTypeProvider();

protected HttpContext HttpContext => _httpContextAccessor.HttpContext;
protected HttpRequest Request => HttpContext?.Request;
Expand Down Expand Up @@ -90,11 +94,14 @@ protected FileStreamResult File(Stream fileStream, string contentType)
protected FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName)
=> new FileStreamResult(fileStream, contentType) { FileDownloadName = fileDownloadName };

protected VirtualFileResult File(string virtualPath)
=> File(virtualPath, _contentTypeProvider.TryGetContentType(virtualPath, out var contentType) ? contentType : DefaultContentType);

protected VirtualFileResult File(string virtualPath, string contentType)
=> File(virtualPath, contentType, null);

protected VirtualFileResult File(string virtualPath, string contentType, string fileDownloadName)
=> new VirtualFileResult(virtualPath, contentType) { FileDownloadName = fileDownloadName, FileProvider = _functionFileProvider };
=> new VirtualFileResult(virtualPath, contentType) { FileDownloadName = fileDownloadName, FileProvider = _fileProvider };

protected UnauthorizedResult Unauthorized() => new UnauthorizedResult();
protected UnauthorizedObjectResult Unauthorized(object value) => new UnauthorizedObjectResult(value);
Expand Down