1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-03 00:52:49 -05:00

attachment apis and azure storage service

This commit is contained in:
Kyle Spearrin
2017-06-15 15:34:12 -04:00
parent 94be5bc1dd
commit 06ca566be1
9 changed files with 232 additions and 0 deletions

View File

@ -10,6 +10,7 @@
public virtual MailSettings Mail { get; set; } = new MailSettings();
public virtual PushSettings Push { get; set; } = new PushSettings();
public virtual StorageSettings Storage { get; set; } = new StorageSettings();
public virtual AttachmentSettings Attachment { get; set; } = new AttachmentSettings();
public virtual IdentityServerSettings IdentityServer { get; set; } = new IdentityServerSettings();
public virtual DataProtectionSettings DataProtection { get; set; } = new DataProtectionSettings();
public virtual DocumentDbSettings DocumentDb { get; set; } = new DocumentDbSettings();
@ -26,6 +27,12 @@
public string ConnectionString { get; set; }
}
public class AttachmentSettings
{
public string ConnectionString { get; set; }
public string BaseUrl { get; set; }
}
public class MailSettings
{
public string ReplyToEmail { get; set; }

View File

@ -0,0 +1,11 @@
using System.IO;
using System.Threading.Tasks;
namespace Bit.Core.Services
{
public interface IAttachmentStorageService
{
Task UploadAttachmentAsync(Stream stream, string name);
Task DeleteAttachmentAsync(string name);
}
}

View File

@ -0,0 +1,45 @@
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
namespace Bit.Core.Services
{
public class AzureAttachmentStorageService : IAttachmentStorageService
{
private const string AttchmentContainerName = "attachments";
private readonly CloudBlobClient _blobClient;
private CloudBlobContainer _attachmentsContainer;
public AzureAttachmentStorageService(
GlobalSettings globalSettings)
{
var storageAccount = CloudStorageAccount.Parse(globalSettings.Storage.ConnectionString);
_blobClient = storageAccount.CreateCloudBlobClient();
}
public async Task UploadAttachmentAsync(Stream stream, string name)
{
await InitAsync();
var blob = _attachmentsContainer.GetBlockBlobReference(name);
await blob.UploadFromStreamAsync(stream);
}
public async Task DeleteAttachmentAsync(string name)
{
await InitAsync();
var blob = _attachmentsContainer.GetBlockBlobReference(name);
await blob.DeleteIfExistsAsync();
}
private async Task InitAsync()
{
if(_attachmentsContainer == null)
{
_attachmentsContainer = _blobClient.GetContainerReference(AttchmentContainerName);
await _attachmentsContainer.CreateIfNotExistsAsync();
}
}
}
}

View File

@ -0,0 +1,18 @@
using System.IO;
using System.Threading.Tasks;
namespace Bit.Core.Services
{
public class NoopAttachmentStorageService : IAttachmentStorageService
{
public Task DeleteAttachmentAsync(string name)
{
return Task.FromResult(0);
}
public Task UploadAttachmentAsync(Stream stream, string name)
{
return Task.FromResult(0);
}
}
}

View File

@ -53,6 +53,8 @@ namespace Bit.Core.Utilities
services.AddSingleton<IPushNotificationService, NotificationHubPushNotificationService>();
services.AddSingleton<IBlockIpService, AzureQueueBlockIpService>();
services.AddSingleton<IPushRegistrationService, NotificationHubPushRegistrationService>();
// noop for now
services.AddSingleton<IAttachmentStorageService, NoopAttachmentStorageService>();
}
public static void AddNoopServices(this IServiceCollection services)
@ -61,6 +63,7 @@ namespace Bit.Core.Utilities
services.AddSingleton<IPushNotificationService, NoopPushNotificationService>();
services.AddSingleton<IBlockIpService, NoopBlockIpService>();
services.AddSingleton<IPushRegistrationService, NoopPushRegistrationService>();
services.AddSingleton<IAttachmentStorageService, NoopAttachmentStorageService>();
}
public static IdentityBuilder AddCustomIdentityServices(