1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-07 05:58:13 -05:00

attachment cleanup

This commit is contained in:
Kyle Spearrin 2017-07-10 20:48:06 -04:00
parent 22f1da8497
commit de8b2de8e6
4 changed files with 45 additions and 36 deletions

View File

@ -10,8 +10,11 @@ namespace Bit.Core.Services
Task UploadNewAttachmentAsync(Stream stream, Cipher cipher, string attachmentId); Task UploadNewAttachmentAsync(Stream stream, Cipher cipher, string attachmentId);
Task UploadShareAttachmentAsync(Stream stream, Guid cipherId, Guid organizationId, string attachmentId); Task UploadShareAttachmentAsync(Stream stream, Guid cipherId, Guid organizationId, string attachmentId);
Task StartShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId); Task StartShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId);
Task CommitShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId);
Task RollbackShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId); Task RollbackShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId);
Task CleanupAsync(Guid cipherId);
Task DeleteAttachmentAsync(Guid cipherId, string attachmentId); Task DeleteAttachmentAsync(Guid cipherId, string attachmentId);
Task DeleteAttachmentsForCipherAsync(Guid cipherId);
Task DeleteAttachmentsForOrganizationAsync(Guid organizationId);
Task DeleteAttachmentsForUserAsync(Guid userId);
} }
} }

View File

@ -21,7 +21,7 @@ namespace Bit.Core.Services
_blobClient = storageAccount.CreateCloudBlobClient(); _blobClient = storageAccount.CreateCloudBlobClient();
} }
public async Task UploadUserAttachmentAsync(Stream stream, Cipher cipher, string attachmentId) public async Task UploadNewAttachmentAsync(Stream stream, Cipher cipher, string attachmentId)
{ {
await InitAsync(); await InitAsync();
var blob = _attachmentsContainer.GetBlockBlobReference($"{cipher.Id}/{attachmentId}"); var blob = _attachmentsContainer.GetBlockBlobReference($"{cipher.Id}/{attachmentId}");
@ -40,7 +40,7 @@ namespace Bit.Core.Services
public async Task UploadShareAttachmentAsync(Stream stream, Guid cipherId, Guid organizationId, string attachmentId) public async Task UploadShareAttachmentAsync(Stream stream, Guid cipherId, Guid organizationId, string attachmentId)
{ {
await InitAsync(); await InitAsync();
var blob = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{organizationId}/{attachmentId}"); var blob = _attachmentsContainer.GetBlockBlobReference($"temp/{cipherId}/{organizationId}/{attachmentId}");
blob.Metadata.Add("cipherId", cipherId.ToString()); blob.Metadata.Add("cipherId", cipherId.ToString());
blob.Metadata.Add("organizationId", organizationId.ToString()); blob.Metadata.Add("organizationId", organizationId.ToString());
await blob.UploadFromStreamAsync(stream); await blob.UploadFromStreamAsync(stream);
@ -49,7 +49,7 @@ namespace Bit.Core.Services
public async Task StartShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId) public async Task StartShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
{ {
await InitAsync(); await InitAsync();
var source = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{organizationId}/{attachmentId}"); var source = _attachmentsContainer.GetBlockBlobReference($"temp/{cipherId}/{organizationId}/{attachmentId}");
if(!(await source.ExistsAsync())) if(!(await source.ExistsAsync()))
{ {
return; return;
@ -61,7 +61,7 @@ namespace Bit.Core.Services
return; return;
} }
var original = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{attachmentId}"); var original = _attachmentsContainer.GetBlockBlobReference($"temp/{cipherId}/{attachmentId}");
await original.DeleteIfExistsAsync(); await original.DeleteIfExistsAsync();
await original.StartCopyAsync(dest); await original.StartCopyAsync(dest);
@ -69,22 +69,13 @@ namespace Bit.Core.Services
await dest.StartCopyAsync(source); await dest.StartCopyAsync(source);
} }
public async Task CommitShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
{
await InitAsync();
var source = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{organizationId}/{attachmentId}");
var original = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{attachmentId}");
await original.DeleteIfExistsAsync();
await source.DeleteIfExistsAsync();
}
public async Task RollbackShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId) public async Task RollbackShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
{ {
await InitAsync(); await InitAsync();
var source = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{organizationId}/{attachmentId}"); var source = _attachmentsContainer.GetBlockBlobReference($"temp/{cipherId}/{organizationId}/{attachmentId}");
await source.DeleteIfExistsAsync(); await source.DeleteIfExistsAsync();
var original = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{attachmentId}"); var original = _attachmentsContainer.GetBlockBlobReference($"temp/{cipherId}/{attachmentId}");
if(!(await original.ExistsAsync())) if(!(await original.ExistsAsync()))
{ {
return; return;
@ -104,33 +95,38 @@ namespace Bit.Core.Services
await blob.DeleteIfExistsAsync(); await blob.DeleteIfExistsAsync();
} }
public async Task DeleteAttachmentsAsync(Cipher cipher) public async Task CleanupAsync(Guid cipherId)
{ {
await InitAsync(); await InitAsync();
var attachments = cipher.GetAttachments(); foreach(var blob in _attachmentsContainer.ListBlobs($"temp/{cipherId}", true))
if(attachments == null)
{ {
return; if(blob is CloudBlockBlob blockBlob)
{
await blockBlob.DeleteIfExistsAsync();
}
}
} }
foreach(var attachment in attachments) public async Task DeleteAttachmentsForCipherAsync(Guid cipherId)
{ {
var blobName = $"{cipher.Id}/{attachment.Key}"; await InitAsync();
var blob = _attachmentsContainer.GetBlockBlobReference(blobName); foreach(var blob in _attachmentsContainer.ListBlobs(cipherId.ToString(), true))
await blob.DeleteIfExistsAsync(); {
if(blob is CloudBlockBlob blockBlob)
{
await blockBlob.DeleteIfExistsAsync();
}
} }
} }
public async Task DeleteAttachmentsForOrganizationAsync(Guid organizationId) public async Task DeleteAttachmentsForOrganizationAsync(Guid organizationId)
{ {
await InitAsync(); await InitAsync();
} }
public async Task DeleteAttachmentsForUserAsync(Guid userId) public async Task DeleteAttachmentsForUserAsync(Guid userId)
{ {
await InitAsync(); await InitAsync();
} }
private async Task InitAsync() private async Task InitAsync()

View File

@ -208,11 +208,7 @@ namespace Bit.Core.Services
} }
catch catch
{ {
foreach(var attachment in cipher.GetAttachments()) await _attachmentStorageService.CleanupAsync(cipher.Id);
{
await _attachmentStorageService.RollbackShareAttachmentAsync(cipher.Id, organizationId, attachment.Key);
}
throw; throw;
} }
} }
@ -225,6 +221,7 @@ namespace Bit.Core.Services
} }
await _cipherRepository.DeleteAsync(cipher); await _cipherRepository.DeleteAsync(cipher);
await _attachmentStorageService.DeleteAttachmentsForCipherAsync(cipher.Id);
// push // push
await _pushService.PushSyncCipherDeleteAsync(cipher); await _pushService.PushSyncCipherDeleteAsync(cipher);
@ -379,14 +376,12 @@ namespace Bit.Core.Services
await _attachmentStorageService.RollbackShareAttachmentAsync(cipher.Id, organizationId, attachment.Key); await _attachmentStorageService.RollbackShareAttachmentAsync(cipher.Id, organizationId, attachment.Key);
} }
await _attachmentStorageService.CleanupAsync(cipher.Id);
throw; throw;
} }
// commit attachment migration // commit attachment migration
foreach(var attachment in attachments) await _attachmentStorageService.CleanupAsync(cipher.Id);
{
await _attachmentStorageService.CommitShareAttachmentAsync(cipher.Id, organizationId, attachment.Key);
}
// push // push
await _pushService.PushSyncCipherUpdateAsync(cipher); await _pushService.PushSyncCipherUpdateAsync(cipher);

View File

@ -7,7 +7,7 @@ namespace Bit.Core.Services
{ {
public class NoopAttachmentStorageService : IAttachmentStorageService public class NoopAttachmentStorageService : IAttachmentStorageService
{ {
public Task CommitShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId) public Task CleanupAsync(Guid cipherId)
{ {
return Task.FromResult(0); return Task.FromResult(0);
} }
@ -17,6 +17,21 @@ namespace Bit.Core.Services
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task DeleteAttachmentsForCipherAsync(Guid cipherId)
{
return Task.FromResult(0);
}
public Task DeleteAttachmentsForOrganizationAsync(Guid organizationId)
{
return Task.FromResult(0);
}
public Task DeleteAttachmentsForUserAsync(Guid userId)
{
return Task.FromResult(0);
}
public Task RollbackShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId) public Task RollbackShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
{ {
return Task.FromResult(0); return Task.FromResult(0);