Skip to content

Adding File and Virtual Path File helpers #4

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 1 commit into from
Apr 4, 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
11 changes: 11 additions & 0 deletions src/Azure.WebJobs.Extensions.HttpApi/FunctionEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace Azure.WebJobs.Extensions.HttpApi
{
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;
}
}
22 changes: 22 additions & 0 deletions src/Azure.WebJobs.Extensions.HttpApi/HttpFunctionBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Security.Claims;
using System.Text;

Expand All @@ -12,6 +13,7 @@
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Net.Http.Headers;

namespace Azure.WebJobs.Extensions.HttpApi
Expand All @@ -28,6 +30,8 @@ protected HttpFunctionBase(IHttpContextAccessor httpContextAccessor)
private IUrlHelper _url;
private ProblemDetailsFactory _problemDetailsFactory;

private static readonly IFileProvider _functionFileProvider = new PhysicalFileProvider(FunctionEnvironment.RootPath);

protected HttpContext HttpContext => _httpContextAccessor.HttpContext;
protected HttpRequest Request => HttpContext?.Request;
protected HttpResponse Response => HttpContext?.Response;
Expand Down Expand Up @@ -74,6 +78,24 @@ protected ContentResult Content(string content, MediaTypeHeaderValue contentType
protected OkResult Ok() => new OkResult();
protected OkObjectResult Ok(object value) => new OkObjectResult(value);

protected FileContentResult File(byte[] fileContents, string contentType)
=> File(fileContents, contentType, null);

protected FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName)
=> new FileContentResult(fileContents, contentType) { FileDownloadName = fileDownloadName };

protected FileStreamResult File(Stream fileStream, string contentType)
=> File(fileStream, contentType, null);

protected FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName)
=> new FileStreamResult(fileStream, contentType) { FileDownloadName = fileDownloadName };

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 };

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

Expand Down