1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-23 17:07:42 +01:00

blob meta data for attachments

This commit is contained in:
Kyle Spearrin 2017-07-10 17:08:50 -04:00
parent c26e679ad9
commit 22f1da8497
4 changed files with 50 additions and 10 deletions

View File

@ -1,4 +1,5 @@
using System;
using Bit.Core.Models.Table;
using System;
using System.IO;
using System.Threading.Tasks;
@ -6,7 +7,7 @@ namespace Bit.Core.Services
{
public interface IAttachmentStorageService
{
Task UploadNewAttachmentAsync(Stream stream, Guid cipherId, string attachmentId);
Task UploadNewAttachmentAsync(Stream stream, Cipher cipher, string attachmentId);
Task UploadShareAttachmentAsync(Stream stream, Guid cipherId, Guid organizationId, string attachmentId);
Task StartShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId);
Task CommitShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId);

View File

@ -3,6 +3,7 @@ using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
using System;
using Bit.Core.Models.Table;
namespace Bit.Core.Services
{
@ -20,14 +21,29 @@ namespace Bit.Core.Services
_blobClient = storageAccount.CreateCloudBlobClient();
}
public async Task UploadNewAttachmentAsync(Stream stream, Guid cipherId, string attachmentId)
public async Task UploadUserAttachmentAsync(Stream stream, Cipher cipher, string attachmentId)
{
await UploadAttachmentAsync(stream, $"{cipherId}/{attachmentId}");
await InitAsync();
var blob = _attachmentsContainer.GetBlockBlobReference($"{cipher.Id}/{attachmentId}");
blob.Metadata.Add("cipherId", cipher.Id.ToString());
if(cipher.UserId.HasValue)
{
blob.Metadata.Add("userId", cipher.UserId.Value.ToString());
}
else
{
blob.Metadata.Add("organizationId", cipher.OrganizationId.Value.ToString());
}
await blob.UploadFromStreamAsync(stream);
}
public async Task UploadShareAttachmentAsync(Stream stream, Guid cipherId, Guid organizationId, string attachmentId)
{
await UploadAttachmentAsync(stream, $"{cipherId}/temp/{organizationId}/{attachmentId}");
await InitAsync();
var blob = _attachmentsContainer.GetBlockBlobReference($"{cipherId}/temp/{organizationId}/{attachmentId}");
blob.Metadata.Add("cipherId", cipherId.ToString());
blob.Metadata.Add("organizationId", organizationId.ToString());
await blob.UploadFromStreamAsync(stream);
}
public async Task StartShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
@ -88,11 +104,33 @@ namespace Bit.Core.Services
await blob.DeleteIfExistsAsync();
}
private async Task UploadAttachmentAsync(Stream stream, string blobName)
public async Task DeleteAttachmentsAsync(Cipher cipher)
{
await InitAsync();
var blob = _attachmentsContainer.GetBlockBlobReference(blobName);
await blob.UploadFromStreamAsync(stream);
var attachments = cipher.GetAttachments();
if(attachments == null)
{
return;
}
foreach(var attachment in attachments)
{
var blobName = $"{cipher.Id}/{attachment.Key}";
var blob = _attachmentsContainer.GetBlockBlobReference(blobName);
await blob.DeleteIfExistsAsync();
}
}
public async Task DeleteAttachmentsForOrganizationAsync(Guid organizationId)
{
await InitAsync();
}
public async Task DeleteAttachmentsForUserAsync(Guid userId)
{
await InitAsync();
}
private async Task InitAsync()

View File

@ -139,7 +139,7 @@ namespace Bit.Core.Services
}
var attachmentId = Utilities.CoreHelpers.SecureRandomString(32, upper: false, special: false);
await _attachmentStorageService.UploadNewAttachmentAsync(stream, cipher.Id, attachmentId);
await _attachmentStorageService.UploadNewAttachmentAsync(stream, cipher, attachmentId);
try
{

View File

@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Bit.Core.Models.Table;
namespace Bit.Core.Services
{
@ -26,7 +27,7 @@ namespace Bit.Core.Services
return Task.FromResult(0);
}
public Task UploadNewAttachmentAsync(Stream stream, Guid cipherId, string attachmentId)
public Task UploadNewAttachmentAsync(Stream stream, Cipher cipher, string attachmentId)
{
return Task.FromResult(0);
}