Files
server/src/Core/Services/Implementations/AzureAttachmentStorageService.cs

107 lines
4.2 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
2017-07-10 14:30:12 -04:00
using System;
namespace Bit.Core.Services
{
public class AzureAttachmentStorageService : IAttachmentStorageService
{
private const string AttchmentContainerName = "attachments";
private readonly CloudBlobClient _blobClient;
private CloudBlobContainer _attachmentsContainer;
public AzureAttachmentStorageService(
GlobalSettings globalSettings)
{
2017-06-30 23:01:41 -04:00
var storageAccount = CloudStorageAccount.Parse(globalSettings.Attachment.ConnectionString);
_blobClient = storageAccount.CreateCloudBlobClient();
}
2017-07-10 14:30:12 -04:00
public async Task UploadNewAttachmentAsync(Stream stream, Guid cipherId, string attachmentId)
{
await UploadAttachmentAsync(stream, $"{cipherId}/{attachmentId}");
}
public async Task UploadShareAttachmentAsync(Stream stream, Guid cipherId, Guid organizationId, string attachmentId)
{
await UploadAttachmentAsync(stream, $"{cipherId}/share/{organizationId}/{attachmentId}");
}
public async Task StartShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
{
await InitAsync();
2017-07-10 14:30:12 -04:00
var source = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/share/{organizationId}/{attachmentId}");
if(!await source.ExistsAsync())
{
return;
}
var dest = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/{attachmentId}");
if(!await dest.ExistsAsync())
{
return;
}
var original = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{attachmentId}");
await original.DeleteIfExistsAsync();
await original.StartCopyAsync(dest);
await dest.DeleteIfExistsAsync();
await dest.StartCopyAsync(source);
}
public async Task CommitShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
{
await InitAsync();
var source = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/share/{organizationId}/{attachmentId}");
var original = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{attachmentId}");
await original.DeleteIfExistsAsync();
await source.DeleteIfExistsAsync();
}
2017-07-10 14:30:12 -04:00
public async Task RollbackShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
{
await InitAsync();
2017-07-10 14:30:12 -04:00
var source = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/share/{organizationId}/{attachmentId}");
var dest = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/{attachmentId}");
var original = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{attachmentId}");
if(!await original.ExistsAsync())
{
return;
}
await dest.DeleteIfExistsAsync();
await dest.StartCopyAsync(original);
await original.DeleteIfExistsAsync();
await source.DeleteIfExistsAsync();
}
public async Task DeleteAttachmentAsync(Guid cipherId, string attachmentId)
{
await InitAsync();
var blobName = $"{cipherId}/{attachmentId}";
var blob = _attachmentsContainer.GetBlockBlobReference(blobName);
await blob.DeleteIfExistsAsync();
}
2017-07-10 14:30:12 -04:00
private async Task UploadAttachmentAsync(Stream stream, string blobName)
{
await InitAsync();
var blob = _attachmentsContainer.GetBlockBlobReference(blobName);
await blob.UploadFromStreamAsync(stream);
}
private async Task InitAsync()
{
if(_attachmentsContainer == null)
{
_attachmentsContainer = _blobClient.GetContainerReference(AttchmentContainerName);
2017-06-30 23:01:41 -04:00
await _attachmentsContainer.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Blob, null, null);
}
}
}
}