From 853d4d005d0acc9853348b79cf896abcec78be1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Standa=20Luke=C5=A1?= Date: Sat, 16 Oct 2021 13:29:54 +0000 Subject: [PATCH] Add ReturnFileAsync method resolves #1164 --- .../Hosting/DotvvmRequestContextExtensions.cs | 17 +++++++++++++++-- .../Storage/FileSystemReturnedFileStorage.cs | 11 ++++++++--- .../Tests/Tests/Feature/ReturnedFileTests.cs | 2 +- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Framework/Framework/Hosting/DotvvmRequestContextExtensions.cs b/src/Framework/Framework/Hosting/DotvvmRequestContextExtensions.cs index 2d1b71cabf..41b1cc7d7c 100644 --- a/src/Framework/Framework/Hosting/DotvvmRequestContextExtensions.cs +++ b/src/Framework/Framework/Hosting/DotvvmRequestContextExtensions.cs @@ -209,13 +209,26 @@ public static string TranslateVirtualPath(string virtualUrl, IHttpContext httpCo /// /// Redirects the client to the specified file. /// + [Obsolete("Use ReturnFileAsync() instead")] public static void ReturnFile(this IDotvvmRequestContext context, byte[] bytes, string fileName, string mimeType, IEnumerable>? additionalHeaders = null, string? attachmentDispositionType = null) => context.ReturnFile(new MemoryStream(bytes), fileName, mimeType, additionalHeaders, attachmentDispositionType); /// /// Redirects the client to the specified file. /// - public static void ReturnFile(this IDotvvmRequestContext context, Stream stream, string fileName, string mimeType, IEnumerable>? additionalHeaders = null, string? attachmentDispositionType = null) + [Obsolete("Use ReturnFileAsync() instead")] + public static void ReturnFile(this IDotvvmRequestContext context, Stream stream, string fileName, string mimeType, IEnumerable>? additionalHeaders = null, string? attachmentDispositionType = null) => + context.ReturnFileAsync(stream, fileName, mimeType, additionalHeaders, attachmentDispositionType).GetAwaiter().GetResult(); + /// + /// Redirects the client to the specified file. + /// + public static Task ReturnFileAsync(this IDotvvmRequestContext context, byte[] bytes, string fileName, string mimeType, IEnumerable>? additionalHeaders = null, string? attachmentDispositionType = null) => + context.ReturnFileAsync(new MemoryStream(bytes), fileName, mimeType, additionalHeaders, attachmentDispositionType); + + /// + /// Redirects the client to the specified file. + /// + public static async Task ReturnFileAsync(this IDotvvmRequestContext context, Stream stream, string fileName, string mimeType, IEnumerable>? additionalHeaders = null, string? attachmentDispositionType = null) { var returnedFileStorage = context.Services.GetService(); @@ -233,7 +246,7 @@ public static void ReturnFile(this IDotvvmRequestContext context, Stream stream, AttachmentDispositionType = attachmentDispositionType ?? "attachment" }; - var generatedFileId = returnedFileStorage.StoreFileAsync(stream, metadata).Result; + var generatedFileId = await returnedFileStorage.StoreFileAsync(stream, metadata).ConfigureAwait(false); context.SetRedirectResponse(context.TranslateVirtualPath("~/dotvvmReturnedFile?id=" + generatedFileId)); throw new DotvvmInterruptRequestExecutionException(InterruptReason.ReturnFile, fileName); } diff --git a/src/Framework/Framework/Storage/FileSystemReturnedFileStorage.cs b/src/Framework/Framework/Storage/FileSystemReturnedFileStorage.cs index 4eaa81d2f7..a6c928e083 100644 --- a/src/Framework/Framework/Storage/FileSystemReturnedFileStorage.cs +++ b/src/Framework/Framework/Storage/FileSystemReturnedFileStorage.cs @@ -79,15 +79,20 @@ public async Task StoreFileAsync(Stream stream, ReturnedFileMetadata metad await stream.CopyToAsync(fs).ConfigureAwait(false); } - StoreMetadata(id, metadata); + await StoreMetadata(id, metadata); return id; } - private void StoreMetadata(Guid id, ReturnedFileMetadata metadata) + private async Task StoreMetadata(Guid id, ReturnedFileMetadata metadata) { var metadataFilePath = GetMetadataFilePath(id); var settings = DefaultSerializerSettingsProvider.Instance.Settings; - File.WriteAllText(metadataFilePath, JsonConvert.SerializeObject(metadata, settings), Encoding.UTF8); +#if DotNetCore + await File.WriteAllTextAsync( +#else + File.WriteAllText( +#endif + metadataFilePath, JsonConvert.SerializeObject(metadata, settings), Encoding.UTF8); } private string GetDataFilePath(Guid id) diff --git a/src/Samples/Tests/Tests/Feature/ReturnedFileTests.cs b/src/Samples/Tests/Tests/Feature/ReturnedFileTests.cs index b76c2e09a0..4bc56af0c1 100644 --- a/src/Samples/Tests/Tests/Feature/ReturnedFileTests.cs +++ b/src/Samples/Tests/Tests/Feature/ReturnedFileTests.cs @@ -62,7 +62,7 @@ private void ReturnedFileDownload(IBrowserWrapper browser, string fileContent) browser.First("input").SendKeys(Keys.Enter); browser.WaitForPostback(); var downloadURL = (string)jsexec.ExecuteScript("return window.downloadURL;"); - Assert.False(string.IsNullOrEmpty(downloadURL)); + Assert.NotEmpty(downloadURL); string returnedFile; using (var client = new WebClient())