mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
Send APIs (#979)
* send work * fix sql proj file * update * updates * access id * delete job * fix delete job * local send storage * update sprocs for null checks
This commit is contained in:
@ -6,6 +6,8 @@ using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Bit.Core.Models.Api;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Api.Utilities
|
||||
{
|
||||
@ -33,7 +35,7 @@ namespace Bit.Api.Utilities
|
||||
await callback(firstSection.Body, fileName, null);
|
||||
}
|
||||
}
|
||||
else if (HasKeyDisposition(firstContent))
|
||||
else if (HasDispositionName(firstContent, "key"))
|
||||
{
|
||||
// New style with key, then data
|
||||
string key = null;
|
||||
@ -64,6 +66,49 @@ namespace Bit.Api.Utilities
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task GetSendFileAsync(this HttpRequest request, Func<Stream, string,
|
||||
SendRequestModel, Task> callback)
|
||||
{
|
||||
var boundary = GetBoundary(MediaTypeHeaderValue.Parse(request.ContentType),
|
||||
_defaultFormOptions.MultipartBoundaryLengthLimit);
|
||||
var reader = new MultipartReader(boundary, request.Body);
|
||||
|
||||
var firstSection = await reader.ReadNextSectionAsync();
|
||||
if (firstSection != null)
|
||||
{
|
||||
if (ContentDispositionHeaderValue.TryParse(firstSection.ContentDisposition, out _))
|
||||
{
|
||||
// Request model json, then data
|
||||
string requestModelJson = null;
|
||||
using (var sr = new StreamReader(firstSection.Body))
|
||||
{
|
||||
requestModelJson = await sr.ReadToEndAsync();
|
||||
}
|
||||
|
||||
var secondSection = await reader.ReadNextSectionAsync();
|
||||
if (secondSection != null)
|
||||
{
|
||||
if (ContentDispositionHeaderValue.TryParse(secondSection.ContentDisposition,
|
||||
out var secondContent) && HasFileContentDisposition(secondContent))
|
||||
{
|
||||
var fileName = HeaderUtilities.RemoveQuotes(secondContent.FileName).ToString();
|
||||
using (secondSection.Body)
|
||||
{
|
||||
var model = JsonConvert.DeserializeObject<SendRequestModel>(requestModelJson);
|
||||
await callback(secondSection.Body, fileName, model);
|
||||
}
|
||||
}
|
||||
|
||||
secondSection = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
firstSection = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static string GetBoundary(MediaTypeHeaderValue contentType, int lengthLimit)
|
||||
{
|
||||
var boundary = HeaderUtilities.RemoveQuotes(contentType.Boundary);
|
||||
@ -87,10 +132,10 @@ namespace Bit.Api.Utilities
|
||||
(!StringSegment.IsNullOrEmpty(content.FileName) || !StringSegment.IsNullOrEmpty(content.FileNameStar));
|
||||
}
|
||||
|
||||
private static bool HasKeyDisposition(ContentDispositionHeaderValue content)
|
||||
private static bool HasDispositionName(ContentDispositionHeaderValue content, string name)
|
||||
{
|
||||
// Content-Disposition: form-data; name="key";
|
||||
return content != null && content.DispositionType.Equals("form-data") && content.Name == "key";
|
||||
return content != null && content.DispositionType.Equals("form-data") && content.Name == name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user