mirror of
https://github.com/bitwarden/server.git
synced 2025-07-06 02:22:49 -05:00
database adjustments and storage for attachments
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
|
||||
namespace Bit.Core.Models.Data
|
||||
{
|
||||
@ -12,7 +13,23 @@ namespace Bit.Core.Models.Data
|
||||
|
||||
public class MetaData
|
||||
{
|
||||
public long Size { get; set; }
|
||||
private long _size;
|
||||
|
||||
[JsonIgnore]
|
||||
public long Size
|
||||
{
|
||||
get { return _size; }
|
||||
set { _size = value; }
|
||||
}
|
||||
|
||||
// We serialize Size as a string since JSON (or Javascript) doesn't support full precision for long numbers
|
||||
[JsonProperty("Size")]
|
||||
public string SizeString
|
||||
{
|
||||
get { return _size.ToString(); }
|
||||
set { _size = Convert.ToInt64(value); }
|
||||
}
|
||||
|
||||
public string FileName { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ namespace Bit.Core.Models.Table
|
||||
public short? MaxCollections { get; set; }
|
||||
public bool UseGroups { get; set; }
|
||||
public bool UseDirectory { get; set; }
|
||||
public long? Storage { get; set; }
|
||||
public short? MaxStorageGb { get; set; }
|
||||
public string StripeCustomerId { get; set; }
|
||||
public string StripeSubscriptionId { get; set; }
|
||||
public bool Enabled { get; set; } = true;
|
||||
@ -29,5 +31,21 @@ namespace Bit.Core.Models.Table
|
||||
Id = CoreHelpers.GenerateComb();
|
||||
}
|
||||
}
|
||||
|
||||
public long StorageBytesRemaining()
|
||||
{
|
||||
if(!MaxStorageGb.HasValue)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var maxStorageBytes = MaxStorageGb.Value * 1073741824L;
|
||||
if(!Storage.HasValue)
|
||||
{
|
||||
return maxStorageBytes;
|
||||
}
|
||||
|
||||
return maxStorageBytes - Storage.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,9 @@ namespace Bit.Core.Models.Table
|
||||
public string Key { get; set; }
|
||||
public string PublicKey { get; set; }
|
||||
public string PrivateKey { get; set; }
|
||||
public bool Premium { get; set; }
|
||||
public long? Storage { get; set; }
|
||||
public short? MaxStorageGb { get; set; }
|
||||
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
|
||||
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
|
||||
|
||||
@ -99,5 +102,21 @@ namespace Bit.Core.Models.Table
|
||||
|
||||
return providers[provider];
|
||||
}
|
||||
|
||||
public long StorageBytesRemaining()
|
||||
{
|
||||
if(!MaxStorageGb.HasValue)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var maxStorageBytes = MaxStorageGb.Value * 1073741824L;
|
||||
if(!Storage.HasValue)
|
||||
{
|
||||
return maxStorageBytes;
|
||||
}
|
||||
|
||||
return maxStorageBytes - Storage.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ namespace Bit.Core.Services
|
||||
{
|
||||
Task SaveAsync(Cipher cipher, Guid savingUserId, bool orgAdmin = false);
|
||||
Task SaveDetailsAsync(CipherDetails cipher, Guid savingUserId);
|
||||
Task AttachAsync(Cipher cipher, Stream stream, string fileName, long requestLength, Guid savingUserId,
|
||||
Task CreateAttachmentAsync(Cipher cipher, Stream stream, string fileName, long requestLength, Guid savingUserId,
|
||||
bool orgAdmin = false);
|
||||
Task DeleteAsync(Cipher cipher, Guid deletingUserId, bool orgAdmin = false);
|
||||
Task DeleteManyAsync(IEnumerable<Guid> cipherIds, Guid deletingUserId);
|
||||
|
@ -92,7 +92,7 @@ namespace Bit.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
public async Task AttachAsync(Cipher cipher, Stream stream, string fileName, long requestLength,
|
||||
public async Task CreateAttachmentAsync(Cipher cipher, Stream stream, string fileName, long requestLength,
|
||||
Guid savingUserId, bool orgAdmin = false)
|
||||
{
|
||||
if(!orgAdmin && !(await UserCanEditAsync(cipher, savingUserId)))
|
||||
@ -102,30 +102,55 @@ namespace Bit.Core.Services
|
||||
|
||||
if(requestLength < 1)
|
||||
{
|
||||
throw new BadRequestException("No data.");
|
||||
throw new BadRequestException("No data to attach.");
|
||||
}
|
||||
|
||||
// TODO: check available space against requestLength
|
||||
var storageBytesRemaining = 0L;
|
||||
if(cipher.UserId.HasValue)
|
||||
{
|
||||
var user = await _userRepository.GetByIdAsync(cipher.UserId.Value);
|
||||
storageBytesRemaining = user.StorageBytesRemaining();
|
||||
}
|
||||
else if(cipher.OrganizationId.HasValue)
|
||||
{
|
||||
var org = await _organizationRepository.GetByIdAsync(cipher.OrganizationId.Value);
|
||||
storageBytesRemaining = org.StorageBytesRemaining();
|
||||
}
|
||||
|
||||
if(storageBytesRemaining < requestLength)
|
||||
{
|
||||
throw new BadRequestException("Not enough storage available.");
|
||||
}
|
||||
|
||||
var attachmentId = Utilities.CoreHelpers.SecureRandomString(32, upper: false, special: false);
|
||||
await _attachmentStorageService.UploadAttachmentAsync(stream, $"{cipher.Id}/{attachmentId}");
|
||||
var storageId = $"{cipher.Id}/{attachmentId}";
|
||||
await _attachmentStorageService.UploadAttachmentAsync(stream, storageId);
|
||||
|
||||
var data = new CipherAttachment.MetaData
|
||||
try
|
||||
{
|
||||
FileName = fileName,
|
||||
Size = stream.Length
|
||||
};
|
||||
var data = new CipherAttachment.MetaData
|
||||
{
|
||||
FileName = fileName,
|
||||
Size = stream.Length
|
||||
};
|
||||
|
||||
var attachment = new CipherAttachment
|
||||
var attachment = new CipherAttachment
|
||||
{
|
||||
Id = cipher.Id,
|
||||
UserId = cipher.UserId,
|
||||
OrganizationId = cipher.OrganizationId,
|
||||
AttachmentId = attachmentId,
|
||||
AttachmentData = JsonConvert.SerializeObject(data)
|
||||
};
|
||||
|
||||
await _cipherRepository.UpdateAttachmentAsync(attachment);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Id = cipher.Id,
|
||||
UserId = cipher.UserId,
|
||||
OrganizationId = cipher.OrganizationId,
|
||||
AttachmentId = attachmentId,
|
||||
AttachmentData = JsonConvert.SerializeObject(data)
|
||||
};
|
||||
|
||||
await _cipherRepository.UpdateAttachmentAsync(attachment);
|
||||
// Clean up since this is not transactional
|
||||
await _attachmentStorageService.DeleteAttachmentAsync(storageId);
|
||||
throw;
|
||||
}
|
||||
|
||||
// push
|
||||
await _pushService.PushSyncCipherUpdateAsync(cipher);
|
||||
|
Reference in New Issue
Block a user