mirror of
https://github.com/bitwarden/server.git
synced 2025-04-15 18:18:12 -05:00
blob meta data for attachments
This commit is contained in:
parent
c26e679ad9
commit
22f1da8497
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using Bit.Core.Models.Table;
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -6,7 +7,7 @@ namespace Bit.Core.Services
|
|||||||
{
|
{
|
||||||
public interface IAttachmentStorageService
|
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 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 CommitShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId);
|
||||||
|
@ -3,6 +3,7 @@ using Microsoft.WindowsAzure.Storage;
|
|||||||
using Microsoft.WindowsAzure.Storage.Blob;
|
using Microsoft.WindowsAzure.Storage.Blob;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System;
|
using System;
|
||||||
|
using Bit.Core.Models.Table;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
@ -20,14 +21,29 @@ namespace Bit.Core.Services
|
|||||||
_blobClient = storageAccount.CreateCloudBlobClient();
|
_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)
|
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)
|
public async Task StartShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
|
||||||
@ -88,11 +104,33 @@ namespace Bit.Core.Services
|
|||||||
await blob.DeleteIfExistsAsync();
|
await blob.DeleteIfExistsAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task UploadAttachmentAsync(Stream stream, string blobName)
|
public async Task DeleteAttachmentsAsync(Cipher cipher)
|
||||||
{
|
{
|
||||||
await InitAsync();
|
await InitAsync();
|
||||||
|
var attachments = cipher.GetAttachments();
|
||||||
|
if(attachments == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(var attachment in attachments)
|
||||||
|
{
|
||||||
|
var blobName = $"{cipher.Id}/{attachment.Key}";
|
||||||
var blob = _attachmentsContainer.GetBlockBlobReference(blobName);
|
var blob = _attachmentsContainer.GetBlockBlobReference(blobName);
|
||||||
await blob.UploadFromStreamAsync(stream);
|
await blob.DeleteIfExistsAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAttachmentsForOrganizationAsync(Guid organizationId)
|
||||||
|
{
|
||||||
|
await InitAsync();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAttachmentsForUserAsync(Guid userId)
|
||||||
|
{
|
||||||
|
await InitAsync();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task InitAsync()
|
private async Task InitAsync()
|
||||||
|
@ -139,7 +139,7 @@ namespace Bit.Core.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
var attachmentId = Utilities.CoreHelpers.SecureRandomString(32, upper: false, special: false);
|
var attachmentId = Utilities.CoreHelpers.SecureRandomString(32, upper: false, special: false);
|
||||||
await _attachmentStorageService.UploadNewAttachmentAsync(stream, cipher.Id, attachmentId);
|
await _attachmentStorageService.UploadNewAttachmentAsync(stream, cipher, attachmentId);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core.Models.Table;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
@ -26,7 +27,7 @@ namespace Bit.Core.Services
|
|||||||
return Task.FromResult(0);
|
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);
|
return Task.FromResult(0);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user