Skip to content

Commit

Permalink
Change streaming method (#40)
Browse files Browse the repository at this point in the history
* Update SettlementReportsController.cs

* Change streaming
  • Loading branch information
FirestarJes authored Sep 30, 2024
1 parent 83dd591 commit ff316e6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ namespace Energinet.DataHub.SettlementReport.Application.Handlers;

public interface ISettlementReportJobsDownloadHandler
{
Task DownloadReportAsync(SettlementReportRequestId requestId, Func<Stream> outputStreamProvider, Guid actorId, bool isMultitenancy);
Task<Stream> DownloadReportAsync(SettlementReportRequestId requestId, Guid actorId, bool isMultitenancy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public SettlementReportJobsDownloadHandler(
_repository = repository;
}

public async Task DownloadReportAsync(
public async Task<Stream> DownloadReportAsync(
SettlementReportRequestId requestId,
Func<Stream> outputStreamProvider,
Guid actorId,
bool isMultitenancy)
{
Expand All @@ -48,8 +47,8 @@ public async Task DownloadReportAsync(
if (string.IsNullOrEmpty(report.BlobFileName))
throw new InvalidOperationException("Report does not have a Blob file name.");

await _fileRepository
.DownloadAsync(report.BlobFileName, outputStreamProvider())
return await _fileRepository
.DownloadAsync(report.BlobFileName)
.ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ namespace Energinet.DataHub.SettlementReport.Application.SettlementReports_v2;

public interface ISettlementReportJobsFileRepository
{
Task DownloadAsync(string blobFileName, Stream downloadStream);
Task<Stream> DownloadAsync(string blobFileName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public SettlementReportJobsFileBlobStorage(BlobContainerClient blobContainerClie
_blobContainerClient = blobContainerClient;
}

public async Task DownloadAsync(string fileName, Stream downloadStream)
public async Task<Stream> DownloadAsync(string fileName)
{
var blobName = GetBlobName(fileName);
var blobClient = _blobContainerClient.GetBlobClient(blobName);
await blobClient.DownloadToAsync(downloadStream).ConfigureAwait(false);
return await blobClient.OpenReadAsync().ConfigureAwait(false);
}

private static string GetBlobName(string fileName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Net;
using System.Net.Mime;
using Azure;
using Energinet.DataHub.Core.App.Common.Abstractions.Users;
Expand Down Expand Up @@ -123,22 +124,20 @@ public async Task<IEnumerable<RequestedSettlementReportDto>> ListSettlementRepor
[Route("download")]
[Authorize]
[Produces("application/octet-stream")]
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK)]
[EnableRevision(activityName: "DownloadSettlementReportAPI", entityType: typeof(RequestedSettlementReportDto))]
public async Task<ActionResult> DownloadFileAsync([FromBody]SettlementReportRequestId requestId)
{
try
{
var stream = new MemoryStream();
await _downloadHandler
var stream = await _downloadHandler
.DownloadReportAsync(
requestId,
() => stream,
_userContext.CurrentUser.Actor.ActorId,
_userContext.CurrentUser.MultiTenancy)
.ConfigureAwait(false);

return File(stream.GetBuffer(), MediaTypeNames.Application.Octet);
return new FileStreamResult(stream, MediaTypeNames.Application.Octet);
}
catch (Exception ex) when (ex is InvalidOperationException or RequestFailedException)
{
Expand Down

0 comments on commit ff316e6

Please sign in to comment.