diff --git a/src/Api/Controllers/SendsController.cs b/src/Api/Controllers/SendsController.cs index d39fae52c4..38fd1214d6 100644 --- a/src/Api/Controllers/SendsController.cs +++ b/src/Api/Controllers/SendsController.cs @@ -160,7 +160,7 @@ namespace Bit.Api.Controllers var userId = _userService.GetProperUserId(User).Value; var (madeSend, madeData) = model.ToSend(userId, fileName, _sendService); send = madeSend; - await _sendService.CreateSendAsync(send, madeData, stream, Request.ContentLength.GetValueOrDefault(0)); + await _sendService.CreateSendAsync(send, madeData, stream, model.FileLength.GetValueOrDefault(0)); }); return new SendResponseModel(send, _globalSettings); diff --git a/src/Core/Models/Api/Request/SendRequestModel.cs b/src/Core/Models/Api/Request/SendRequestModel.cs index ccb27b0cc5..a0cca1a1fc 100644 --- a/src/Core/Models/Api/Request/SendRequestModel.cs +++ b/src/Core/Models/Api/Request/SendRequestModel.cs @@ -13,6 +13,7 @@ namespace Bit.Core.Models.Api public class SendRequestModel { public SendType Type { get; set; } + public long? FileLength { get; set; } [EncryptedString] [EncryptedStringLength(1000)] public string Name { get; set; } diff --git a/src/Core/Services/Implementations/SendService.cs b/src/Core/Services/Implementations/SendService.cs index eb6f73113f..2727ae1bba 100644 --- a/src/Core/Services/Implementations/SendService.cs +++ b/src/Core/Services/Implementations/SendService.cs @@ -128,11 +128,24 @@ namespace Bit.Core.Services try { data.Id = fileId; - data.Size = stream.Length; send.Data = JsonConvert.SerializeObject(data, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); await SaveSendAsync(send); await _sendFileStorageService.UploadNewFileAsync(stream, send, fileId); + // Need to save length of stream since that isn't available until it is read + if (stream.Length <= requestLength) + { + data.Size = stream.Length; + send.Data = JsonConvert.SerializeObject(data, + new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); + await SaveSendAsync(send); + } + else + { + await DeleteSendAsync(send); + throw new BadRequestException("Content-Length header is smaller than file received."); + } + } catch {