Files
mixtape/zero.Backoffice/Extensions/ZeroBackofficeControllerExtensions.cs
T

51 lines
1.4 KiB
C#
Raw Normal View History

2021-11-23 15:43:21 +01:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using System.IO;
2021-12-12 15:41:51 +01:00
using zero.Api.Abstractions;
2021-11-23 15:43:21 +01:00
2021-12-03 14:45:49 +01:00
namespace zero.Backoffice.Extensions;
2021-11-23 15:43:21 +01:00
public static class ZeroBackofficeControllerExtensions
{
/// <summary>
/// Provides a file stream for download in the browser
/// </summary>
2021-11-29 18:25:50 +01:00
public static IActionResult DownloadFile(this ZeroApiController controller, Stream stream, string filename)
2021-11-23 15:43:21 +01:00
{
if (stream == null)
{
// TODO add success property + return error response
}
if (filename.Contains("{date}"))
{
filename = filename.Replace("{date}", DateTimeOffset.Now.ToString("yyyy-MM-dd"));
}
var provider = new FileExtensionContentTypeProvider();
if (filename == null || !provider.TryGetContentType(Path.GetFileName(filename), out string contentType))
{
contentType = "application/octet-stream";
}
controller.Response.Headers.Add("zero-filename", filename);
return controller.File(stream, contentType, filename, true);
}
/// <summary>
/// Provides a file stream to the browser
/// </summary>
2021-11-29 18:25:50 +01:00
public static IActionResult File(this ZeroApiController controller, FileStorage.FileResult file)
2021-11-23 15:43:21 +01:00
{
if (file == null)
{
return controller.NotFound();
}
FileStream stream = System.IO.File.OpenRead(file.Path);
return controller.File(stream, file.ContentType, file.Filename);
}
}