mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 16:42:50 -05:00
share login with attachments
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
using Microsoft.WindowsAzure.Storage;
|
||||
using Microsoft.WindowsAzure.Storage.Blob;
|
||||
using System.IO;
|
||||
using System;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
@ -19,20 +20,80 @@ namespace Bit.Core.Services
|
||||
_blobClient = storageAccount.CreateCloudBlobClient();
|
||||
}
|
||||
|
||||
public async Task UploadAttachmentAsync(Stream stream, string name)
|
||||
public async Task UploadNewAttachmentAsync(Stream stream, Guid cipherId, string attachmentId)
|
||||
{
|
||||
await InitAsync();
|
||||
var blob = _attachmentsContainer.GetBlockBlobReference(name);
|
||||
await blob.UploadFromStreamAsync(stream);
|
||||
await UploadAttachmentAsync(stream, $"{cipherId}/{attachmentId}");
|
||||
}
|
||||
|
||||
public async Task DeleteAttachmentAsync(string name)
|
||||
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();
|
||||
var blob = _attachmentsContainer.GetBlockBlobReference(name);
|
||||
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();
|
||||
}
|
||||
|
||||
public async Task RollbackShareAttachmentAsync(Guid cipherId, Guid organizationId, string attachmentId)
|
||||
{
|
||||
await InitAsync();
|
||||
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();
|
||||
}
|
||||
|
||||
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)
|
||||
|
Reference in New Issue
Block a user