1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

recompute full storage each time

This commit is contained in:
Kyle Spearrin 2017-07-10 22:08:52 -04:00
parent de8b2de8e6
commit 8684b9c8e5
10 changed files with 70 additions and 53 deletions

View File

@ -8,6 +8,6 @@ namespace Bit.Core.Repositories
public interface IOrganizationRepository : IRepository<Organization, Guid>
{
Task<ICollection<Organization>> GetManyByUserIdAsync(Guid userId);
Task UpdateStorageAsync(Guid id, long storageIncrease);
Task UpdateStorageAsync(Guid id);
}
}

View File

@ -9,6 +9,6 @@ namespace Bit.Core.Repositories
Task<User> GetByEmailAsync(string email);
Task<string> GetPublicKeyAsync(Guid id);
Task<DateTime> GetAccountRevisionDateAsync(Guid id);
Task UpdateStorageAsync(Guid id, long storageIncrease);
Task UpdateStorageAsync(Guid id);
}
}

View File

@ -32,13 +32,13 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task UpdateStorageAsync(Guid id, long storageIncrease)
public async Task UpdateStorageAsync(Guid id)
{
using(var connection = new SqlConnection(ConnectionString))
{
await connection.ExecuteAsync(
"[dbo].[Organization_UpdateStorage]",
new { Id = id, StorageIncrease = storageIncrease },
new { Id = id },
commandType: CommandType.StoredProcedure,
commandTimeout: 180);
}

View File

@ -79,13 +79,13 @@ namespace Bit.Core.Repositories.SqlServer
}
}
public async Task UpdateStorageAsync(Guid id, long storageIncrease)
public async Task UpdateStorageAsync(Guid id)
{
using(var connection = new SqlConnection(ConnectionString))
{
await connection.ExecuteAsync(
$"[{Schema}].[{Table}_UpdateStorage]",
new { Id = id, StorageIncrease = storageIncrease },
new { Id = id },
commandType: CommandType.StoredProcedure,
commandTimeout: 180);
}

View File

@ -302,7 +302,6 @@ namespace Bit.Core.Services
{
var attachments = cipher.GetAttachments();
var hasAttachments = (attachments?.Count ?? 0) > 0;
var storageAdjustment = attachments?.Sum(a => a.Value.Size) ?? 0;
var updatedCipher = false;
var migratedAttachments = false;
@ -329,8 +328,8 @@ namespace Bit.Core.Services
throw new BadRequestException("This organization cannot use attachments.");
}
var storageBytesRemaining = org.StorageBytesRemaining();
if(storageBytesRemaining < storageAdjustment)
var storageAdjustment = attachments?.Sum(a => a.Value.Size) ?? 0;
if(org.StorageBytesRemaining() < storageAdjustment)
{
throw new BadRequestException("Not enough storage available for this organization.");
}
@ -367,8 +366,8 @@ namespace Bit.Core.Services
if(updatedCipher)
{
await _userRepository.UpdateStorageAsync(sharingUserId, storageAdjustment);
await _organizationRepository.UpdateStorageAsync(organizationId, -1 * storageAdjustment);
await _userRepository.UpdateStorageAsync(sharingUserId);
await _organizationRepository.UpdateStorageAsync(organizationId);
}
foreach(var attachment in attachments)

View File

@ -8,21 +8,16 @@ BEGIN
DECLARE @AttachmentIdKey VARCHAR(50) = CONCAT('"', @AttachmentId, '"')
DECLARE @AttachmentIdPath VARCHAR(50) = CONCAT('$.', @AttachmentIdKey)
DECLARE @Attachments NVARCHAR(MAX)
DECLARE @UserId UNIQUEIDENTIFIER
DECLARE @OrganizationId UNIQUEIDENTIFIER
SELECT
@UserId = [UserId],
@OrganizationId = [OrganizationId],
@Attachments = [Attachments]
@OrganizationId = [OrganizationId]
FROM
[dbo].[Cipher]
WHERE [Id] = @Id
DECLARE @AttachmentData NVARCHAR(MAX) = JSON_QUERY(@Attachments, @AttachmentIdPath)
DECLARE @Size BIGINT = (CAST(JSON_VALUE(@AttachmentData, '$.Size') AS BIGINT) * -1)
UPDATE
[dbo].[Cipher]
SET
@ -32,12 +27,12 @@ BEGIN
IF @OrganizationId IS NOT NULL
BEGIN
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId, @Size
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
END
ELSE IF @UserId IS NOT NULL
BEGIN
EXEC [dbo].[User_UpdateStorage] @UserId, @Size
EXEC [dbo].[User_UpdateStorage] @UserId
EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
END
END

View File

@ -10,7 +10,6 @@ BEGIN
DECLARE @AttachmentIdKey VARCHAR(50) = CONCAT('"', @AttachmentId, '"')
DECLARE @AttachmentIdPath VARCHAR(50) = CONCAT('$.', @AttachmentIdKey)
DECLARE @Size BIGINT = CAST(JSON_VALUE(@AttachmentData, '$.Size') AS BIGINT)
UPDATE
[dbo].[Cipher]
@ -27,12 +26,12 @@ BEGIN
IF @OrganizationId IS NOT NULL
BEGIN
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId, @Size
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
END
ELSE IF @UserId IS NOT NULL
BEGIN
EXEC [dbo].[User_UpdateStorage] @UserId, @Size
EXEC [dbo].[User_UpdateStorage] @UserId
EXEC [dbo].[User_BumpAccountRevisionDate] @UserId
END
END

View File

@ -14,26 +14,6 @@ AS
BEGIN
SET NOCOUNT ON
DECLARE @CipherAttachments NVARCHAR(MAX)
SELECT
@CipherAttachments = [Attachments]
FROM
[dbo].[Cipher]
WHERE [Id] = @Id
DECLARE @Size BIGINT
DECLARE @SizeDec BIGINT
IF @CipherAttachments IS NOT NULL
BEGIN
SELECT
@Size = SUM(CAST(JSON_VALUE(value,'$.Size') AS BIGINT))
FROM
OPENJSON(@CipherAttachments)
SET @SizeDec = @Size * -1
END
UPDATE
[dbo].[Cipher]
SET
@ -87,10 +67,10 @@ BEGIN
WHERE
[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE])
IF ISNULL(@Size, 0) > 0
IF @Attachments IS NOT NULL
BEGIN
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId, @Size
EXEC [dbo].[User_UpdateStorage] @UserId, @SizeDec
EXEC [dbo].[Organization_UpdateStorage] @OrganizationId
EXEC [dbo].[User_UpdateStorage] @UserId
END
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId

View File

@ -1,15 +1,37 @@
CREATE PROCEDURE [dbo].[Organization_UpdateStorage]
@Id UNIQUEIDENTIFIER,
@StorageIncrease BIGINT
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
DECLARE @Storage BIGINT
;WITH [CTE] AS (
SELECT
[Id],
(
SELECT
SUM(CAST(JSON_VALUE(value,'$.Size') AS BIGINT))
FROM
OPENJSON([Attachments])
) [Size]
FROM
[dbo].[Cipher]
)
SELECT
@Storage = SUM([CTE].[Size])
FROM
[dbo].[Cipher] C
LEFT JOIN
[CTE] ON C.[Id] = [CTE].[Id]
WHERE
C.[OrganizationId] = @Id
AND C.[Attachments] IS NOT NULL
UPDATE
[dbo].[Organization]
SET
[Storage] = ISNULL([Storage], 0) + @StorageIncrease,
[Storage] = @Storage,
[RevisionDate] = GETUTCDATE()
WHERE
[Id] = @Id

View File

@ -1,15 +1,37 @@
CREATE PROCEDURE [dbo].[User_UpdateStorage]
@Id UNIQUEIDENTIFIER,
@StorageIncrease BIGINT
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
DECLARE @Storage BIGINT
;WITH [CTE] AS (
SELECT
[Id],
(
SELECT
SUM(CAST(JSON_VALUE(value,'$.Size') AS BIGINT))
FROM
OPENJSON([Attachments])
) [Size]
FROM
[dbo].[Cipher]
)
SELECT
@Storage = SUM([CTE].[Size])
FROM
[dbo].[Cipher] C
LEFT JOIN
[CTE] ON C.[Id] = [CTE].[Id]
WHERE
C.[UserId] = @Id
AND C.[Attachments] IS NOT NULL
UPDATE
[dbo].[User]
SET
[Storage] = ISNULL([Storage], 0) + @StorageIncrease,
[Storage] = @Storage,
[RevisionDate] = GETUTCDATE()
WHERE
[Id] = @Id