Skip to content
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

Add ReturnFileAsync method #1177

Merged
merged 2 commits into from
Oct 20, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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