-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from LykkeCity/BufferingHelperFix
Fixed net.core 3.0 compatibility issue for MiddlewareApplicationBuild…
- Loading branch information
Showing
2 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.IO; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.WebUtilities; | ||
|
||
namespace Lykke.Common.ApiLibrary.Middleware | ||
{ | ||
internal static class BufferingHelper | ||
{ | ||
private const int DefaultBufferThreshold = 1024 * 30; | ||
|
||
private static string _tempDirectory; | ||
|
||
public static HttpRequest EnableRewind(this HttpRequest request) | ||
{ | ||
if (request == null) | ||
throw new ArgumentNullException(nameof(request)); | ||
|
||
var body = request.Body; | ||
if (!body.CanSeek) | ||
{ | ||
var fileStream = new FileBufferingReadStream(body, DefaultBufferThreshold, null, GetTempDirectory); | ||
request.Body = fileStream; | ||
request.HttpContext.Response.RegisterForDispose(fileStream); | ||
} | ||
|
||
return request; | ||
} | ||
|
||
private static string GetTempDirectory() | ||
{ | ||
if (_tempDirectory != null) | ||
return _tempDirectory; | ||
|
||
// Look for folders in the following order. | ||
var temp = Environment.GetEnvironmentVariable("ASPNETCORE_TEMP") // ASPNETCORE_TEMP - User set temporary location. | ||
?? Path.GetTempPath(); // Fall back. | ||
|
||
if (!Directory.Exists(temp)) | ||
{ | ||
// TODO: ??? | ||
throw new DirectoryNotFoundException(temp); | ||
} | ||
|
||
_tempDirectory = temp; | ||
|
||
return _tempDirectory; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters