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 9a81873
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 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);
context.SetRedirectResponse(context.TranslateVirtualPath("~/dotvvmReturnedFile?id=" + generatedFileId));
throw new DotvvmInterruptRequestExecutionException(InterruptReason.ReturnFile, fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ 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);
await File.WriteAllTextAsync(metadataFilePath, JsonConvert.SerializeObject(metadata, settings), Encoding.UTF8);
}

private string GetDataFilePath(Guid id)
Expand Down

0 comments on commit 9a81873

Please sign in to comment.