Skip to content

Commit

Permalink
Add ReturnFileAsync method
Browse files Browse the repository at this point in the history
resolves #1164
  • Loading branch information
exyi committed Oct 16, 2021
1 parent d53c8d9 commit 853d4d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/Framework/Framework/Hosting/DotvvmRequestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,26 @@ public static string TranslateVirtualPath(string virtualUrl, IHttpContext httpCo
/// <summary>
/// Redirects the client to the specified file.
/// </summary>
[Obsolete("Use ReturnFileAsync() instead")]
public static void ReturnFile(this IDotvvmRequestContext context, byte[] bytes, string fileName, string mimeType, IEnumerable<KeyValuePair<string, string>>? additionalHeaders = null, string? attachmentDispositionType = null) =>
context.ReturnFile(new MemoryStream(bytes), fileName, mimeType, additionalHeaders, attachmentDispositionType);

/// <summary>
/// Redirects the client to the specified file.
/// </summary>
public static void ReturnFile(this IDotvvmRequestContext context, Stream stream, string fileName, string mimeType, IEnumerable<KeyValuePair<string, string>>? additionalHeaders = null, string? attachmentDispositionType = null)
[Obsolete("Use ReturnFileAsync() instead")]
public static void ReturnFile(this IDotvvmRequestContext context, Stream stream, string fileName, string mimeType, IEnumerable<KeyValuePair<string, string>>? additionalHeaders = null, string? attachmentDispositionType = null) =>
context.ReturnFileAsync(stream, fileName, mimeType, additionalHeaders, attachmentDispositionType).GetAwaiter().GetResult();
/// <summary>
/// Redirects the client to the specified file.
/// </summary>
public static Task ReturnFileAsync(this IDotvvmRequestContext context, byte[] bytes, string fileName, string mimeType, IEnumerable<KeyValuePair<string, string>>? additionalHeaders = null, string? attachmentDispositionType = null) =>
context.ReturnFileAsync(new MemoryStream(bytes), fileName, mimeType, additionalHeaders, attachmentDispositionType);

/// <summary>
/// Redirects the client to the specified file.
/// </summary>
public static async Task ReturnFileAsync(this IDotvvmRequestContext context, Stream stream, string fileName, string mimeType, IEnumerable<KeyValuePair<string, string>>? additionalHeaders = null, string? attachmentDispositionType = null)
{
var returnedFileStorage = context.Services.GetService<IReturnedFileStorage>();

Expand All @@ -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);
}
Expand Down
11 changes: 8 additions & 3 deletions src/Framework/Framework/Storage/FileSystemReturnedFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,20 @@ public async Task<Guid> 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)
Expand Down
2 changes: 1 addition & 1 deletion src/Samples/Tests/Tests/Feature/ReturnedFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 853d4d0

Please sign in to comment.