1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 16:12:49 -05:00

post, upload, and save cipher attachment

This commit is contained in:
Kyle Spearrin
2017-06-30 11:15:58 -04:00
parent 71f755dd44
commit 6cea556ae1
18 changed files with 158 additions and 25 deletions

View File

@ -12,23 +12,38 @@ namespace Bit.Api.Utilities
{
private static readonly FormOptions _defaultFormOptions = new FormOptions();
public static async Task GetFilesAsync(this HttpRequest request, Func<Stream, string, Task> callback)
public static async Task GetFileAsync(this HttpRequest request, Func<Stream, string, Task> callback)
{
await request.GetFilesAsync(1, callback);
}
private static async Task GetFilesAsync(this HttpRequest request, int? fileCount, Func<Stream, string, Task> callback)
{
var boundary = GetBoundary(MediaTypeHeaderValue.Parse(request.ContentType),
_defaultFormOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, request.Body);
var section = await reader.ReadNextSectionAsync();
while(section != null)
var fileNumber = 1;
while(section != null && fileNumber <= fileCount)
{
ContentDispositionHeaderValue content;
if(ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out content) &&
HasFileContentDisposition(content))
{
await callback(section.Body, HeaderUtilities.RemoveQuotes(content.FileName));
var fileName = HeaderUtilities.RemoveQuotes(content.FileName) ?? string.Empty;
await callback(section.Body, fileName);
}
section = await reader.ReadNextSectionAsync();
if(fileNumber >= fileCount)
{
section = null;
}
else
{
section = await reader.ReadNextSectionAsync();
fileNumber++;
}
}
}