2020-11-02 15:55:49 -05:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Microsoft.Azure.Storage;
|
|
|
|
|
|
using Microsoft.Azure.Storage.Blob;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using Bit.Core.Models.Table;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2020-11-02 15:55:49 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class AzureSendFileStorageService : ISendFileStorageService
|
|
|
|
|
|
{
|
2021-03-01 15:01:04 -06:00
|
|
|
|
public const string FilesContainerName = "sendfiles";
|
2021-02-24 13:03:16 -06:00
|
|
|
|
private static readonly TimeSpan _downloadLinkLiveTime = TimeSpan.FromMinutes(1);
|
2020-11-02 15:55:49 -05:00
|
|
|
|
private readonly CloudBlobClient _blobClient;
|
|
|
|
|
|
private CloudBlobContainer _sendFilesContainer;
|
|
|
|
|
|
|
2021-03-01 15:01:04 -06:00
|
|
|
|
public static string SendIdFromBlobName(string blobName) => blobName.Split('/')[0];
|
|
|
|
|
|
public static string BlobName(Send send, string fileId) => $"{send.Id}/{fileId}";
|
|
|
|
|
|
|
2020-11-02 15:55:49 -05:00
|
|
|
|
public AzureSendFileStorageService(
|
|
|
|
|
|
GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
|
|
|
|
|
var storageAccount = CloudStorageAccount.Parse(globalSettings.Send.ConnectionString);
|
|
|
|
|
|
_blobClient = storageAccount.CreateCloudBlobClient();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UploadNewFileAsync(Stream stream, Send send, string fileId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
2021-03-01 15:01:04 -06:00
|
|
|
|
var blob = _sendFilesContainer.GetBlockBlobReference(BlobName(send, fileId));
|
2020-11-02 15:55:49 -05:00
|
|
|
|
if (send.UserId.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
blob.Metadata.Add("userId", send.UserId.Value.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
blob.Metadata.Add("organizationId", send.OrganizationId.Value.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
blob.Properties.ContentDisposition = $"attachment; filename=\"{fileId}\"";
|
|
|
|
|
|
await blob.UploadFromStreamAsync(stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-01 15:01:04 -06:00
|
|
|
|
public async Task DeleteFileAsync(Send send, string fileId)
|
2020-11-02 15:55:49 -05:00
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
2021-03-01 15:01:04 -06:00
|
|
|
|
var blob = _sendFilesContainer.GetBlockBlobReference(BlobName(send, fileId));
|
2020-11-02 15:55:49 -05:00
|
|
|
|
await blob.DeleteIfExistsAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteFilesForOrganizationAsync(Guid organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteFilesForUserAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-01 15:01:04 -06:00
|
|
|
|
public async Task<string> GetSendFileDownloadUrlAsync(Send send, string fileId)
|
2021-02-24 13:03:16 -06:00
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
2021-03-01 15:01:04 -06:00
|
|
|
|
var blob = _sendFilesContainer.GetBlockBlobReference(BlobName(send, fileId));
|
2021-02-24 13:03:16 -06:00
|
|
|
|
var accessPolicy = new SharedAccessBlobPolicy()
|
|
|
|
|
|
{
|
|
|
|
|
|
SharedAccessExpiryTime = DateTime.UtcNow.Add(_downloadLinkLiveTime),
|
2021-03-01 15:01:04 -06:00
|
|
|
|
Permissions = SharedAccessBlobPermissions.Read,
|
2021-02-24 13:03:16 -06:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return blob.Uri + blob.GetSharedAccessSignature(accessPolicy);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-02 15:55:49 -05:00
|
|
|
|
private async Task InitAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_sendFilesContainer == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_sendFilesContainer = _blobClient.GetContainerReference(FilesContainerName);
|
2021-02-24 13:03:16 -06:00
|
|
|
|
await _sendFilesContainer.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Off, null, null);
|
2020-11-02 15:55:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|