mirror of
https://github.com/bitwarden/server.git
synced 2026-02-05 08:33:10 +08:00
* Get limited life attachment download URL This change limits url download to a 1min lifetime. This requires moving to a new container to allow for non-public blob access. Clients will have to call GetAttachmentData api function to receive the download URL. For backwards compatibility, attachment URLs are still present, but will not work for attachments stored in non-public access blobs. * Make GlobalSettings interface for testing * Test LocalAttachmentStorageService equivalence * Remove comment * Add missing globalSettings using * Simplify default attachment container * Default to attachments containe for existing methods A new upload method will be made for uploading to attachments-v2. For compatibility for clients which don't use these new methods, we need to still use the old container. The new container will be used only for new uploads * Remove Default MetaData fixture. * Keep attachments container blob-level security for all instances * Close unclosed FileStream * Favor default value for noop services
179 lines
6.4 KiB
C#
179 lines
6.4 KiB
C#
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using System;
|
|
using Bit.Core.Models.Table;
|
|
using Bit.Core.Models.Data;
|
|
using Bit.Core.Settings;
|
|
|
|
namespace Bit.Core.Services
|
|
{
|
|
public class LocalAttachmentStorageService : IAttachmentStorageService
|
|
{
|
|
private readonly string _baseAttachmentUrl;
|
|
private readonly string _baseDirPath;
|
|
private readonly string _baseTempDirPath;
|
|
|
|
public LocalAttachmentStorageService(
|
|
IGlobalSettings globalSettings)
|
|
{
|
|
_baseDirPath = globalSettings.Attachment.BaseDirectory;
|
|
_baseTempDirPath = $"{_baseDirPath}/temp";
|
|
_baseAttachmentUrl = globalSettings.Attachment.BaseUrl;
|
|
}
|
|
|
|
public async Task<string> GetAttachmentDownloadUrlAsync(Cipher cipher, CipherAttachment.MetaData attachmentData)
|
|
{
|
|
await InitAsync();
|
|
return $"{_baseAttachmentUrl}/{cipher.Id}/{attachmentData.AttachmentId}";
|
|
}
|
|
|
|
public async Task UploadNewAttachmentAsync(Stream stream, Cipher cipher, CipherAttachment.MetaData attachmentData)
|
|
{
|
|
await InitAsync();
|
|
var cipherDirPath = CipherDirectoryPath(cipher.Id, temp: false);
|
|
CreateDirectoryIfNotExists(cipherDirPath);
|
|
|
|
using (var fs = File.Create(AttachmentFilePath(cipherDirPath, attachmentData.AttachmentId)))
|
|
{
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
await stream.CopyToAsync(fs);
|
|
}
|
|
}
|
|
|
|
public async Task UploadShareAttachmentAsync(Stream stream, Guid cipherId, Guid organizationId, CipherAttachment.MetaData attachmentData)
|
|
{
|
|
await InitAsync();
|
|
var tempCipherOrgDirPath = OrganizationDirectoryPath(cipherId, organizationId, temp: true);
|
|
CreateDirectoryIfNotExists(tempCipherOrgDirPath);
|
|
|
|
using (var fs = File.Create(AttachmentFilePath(tempCipherOrgDirPath, attachmentData.AttachmentId)))
|
|
{
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
await stream.CopyToAsync(fs);
|
|
}
|
|
}
|
|
|
|
public async Task StartShareAttachmentAsync(Guid cipherId, Guid organizationId, CipherAttachment.MetaData attachmentData)
|
|
{
|
|
await InitAsync();
|
|
var sourceFilePath = AttachmentFilePath(attachmentData.AttachmentId, cipherId, organizationId, temp: true);
|
|
if (!File.Exists(sourceFilePath))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var destFilePath = AttachmentFilePath(attachmentData.AttachmentId, cipherId, temp: false);
|
|
if (!File.Exists(destFilePath))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var originalFilePath = AttachmentFilePath(attachmentData.AttachmentId, cipherId, temp: true);
|
|
DeleteFileIfExists(originalFilePath);
|
|
|
|
File.Move(destFilePath, originalFilePath);
|
|
DeleteFileIfExists(destFilePath);
|
|
|
|
File.Move(sourceFilePath, destFilePath);
|
|
}
|
|
|
|
public async Task RollbackShareAttachmentAsync(Guid cipherId, Guid organizationId, CipherAttachment.MetaData attachmentData, string originalContainer)
|
|
{
|
|
await InitAsync();
|
|
DeleteFileIfExists(AttachmentFilePath(attachmentData.AttachmentId, cipherId, organizationId, temp: true));
|
|
|
|
var originalFilePath = AttachmentFilePath(attachmentData.AttachmentId, cipherId, temp: true);
|
|
if (!File.Exists(originalFilePath))
|
|
{
|
|
return;
|
|
}
|
|
|
|
var destFilePath = AttachmentFilePath(attachmentData.AttachmentId, cipherId, temp: false);
|
|
DeleteFileIfExists(destFilePath);
|
|
|
|
File.Move(originalFilePath, destFilePath);
|
|
DeleteFileIfExists(originalFilePath);
|
|
}
|
|
|
|
public async Task DeleteAttachmentAsync(Guid cipherId, CipherAttachment.MetaData attachmentData)
|
|
{
|
|
await InitAsync();
|
|
DeleteFileIfExists(AttachmentFilePath(attachmentData.AttachmentId, cipherId, temp: false));
|
|
}
|
|
|
|
public async Task CleanupAsync(Guid cipherId)
|
|
{
|
|
await InitAsync();
|
|
DeleteDirectoryIfExists(CipherDirectoryPath(cipherId, temp: true));
|
|
}
|
|
|
|
public async Task DeleteAttachmentsForCipherAsync(Guid cipherId)
|
|
{
|
|
await InitAsync();
|
|
DeleteDirectoryIfExists(CipherDirectoryPath(cipherId, temp: false));
|
|
}
|
|
|
|
public async Task DeleteAttachmentsForOrganizationAsync(Guid organizationId)
|
|
{
|
|
await InitAsync();
|
|
}
|
|
|
|
public async Task DeleteAttachmentsForUserAsync(Guid userId)
|
|
{
|
|
await InitAsync();
|
|
}
|
|
|
|
private void DeleteFileIfExists(string path)
|
|
{
|
|
if (File.Exists(path))
|
|
{
|
|
File.Delete(path);
|
|
}
|
|
}
|
|
|
|
private void DeleteDirectoryIfExists(string path)
|
|
{
|
|
if (Directory.Exists(path))
|
|
{
|
|
Directory.Delete(path, true);
|
|
}
|
|
}
|
|
|
|
private void CreateDirectoryIfNotExists(string path)
|
|
{
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
}
|
|
|
|
private Task InitAsync()
|
|
{
|
|
if (!Directory.Exists(_baseDirPath))
|
|
{
|
|
Directory.CreateDirectory(_baseDirPath);
|
|
}
|
|
|
|
if (!Directory.Exists(_baseTempDirPath))
|
|
{
|
|
Directory.CreateDirectory(_baseTempDirPath);
|
|
}
|
|
|
|
return Task.FromResult(0);
|
|
}
|
|
|
|
private string CipherDirectoryPath(Guid cipherId, bool temp = false) =>
|
|
Path.Combine(temp ? _baseTempDirPath : _baseDirPath, cipherId.ToString());
|
|
private string OrganizationDirectoryPath(Guid cipherId, Guid organizationId, bool temp = false) =>
|
|
Path.Combine(temp ? _baseTempDirPath : _baseDirPath, cipherId.ToString(), organizationId.ToString());
|
|
|
|
private string AttachmentFilePath(string dir, string attachmentId) => Path.Combine(dir, attachmentId);
|
|
private string AttachmentFilePath(string attachmentId, Guid cipherId, Guid? organizationId = null,
|
|
bool temp = false) =>
|
|
organizationId.HasValue ?
|
|
AttachmentFilePath(OrganizationDirectoryPath(cipherId, organizationId.Value, temp), attachmentId) :
|
|
AttachmentFilePath(CipherDirectoryPath(cipherId, temp), attachmentId);
|
|
|
|
}
|
|
}
|