2020-11-02 15:55:49 -05:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
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 LocalSendStorageService : ISendFileStorageService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly string _baseDirPath;
|
2021-02-24 13:03:16 -06:00
|
|
|
|
private readonly string _baseSendUrl;
|
2020-11-02 15:55:49 -05:00
|
|
|
|
|
|
|
|
|
|
public LocalSendStorageService(
|
|
|
|
|
|
GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
|
|
|
|
|
_baseDirPath = globalSettings.Send.BaseDirectory;
|
2021-02-24 13:03:16 -06:00
|
|
|
|
_baseSendUrl = globalSettings.Send.BaseUrl;
|
2020-11-02 15:55:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UploadNewFileAsync(Stream stream, Send send, string fileId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
|
|
|
|
|
using (var fs = File.Create($"{_baseDirPath}/{fileId}"))
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
await stream.CopyToAsync(fs);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteFileAsync(string fileId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
|
|
|
|
|
DeleteFileIfExists($"{_baseDirPath}/{fileId}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteFilesForOrganizationAsync(Guid organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task DeleteFilesForUserAsync(Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-24 13:03:16 -06:00
|
|
|
|
public async Task<string> GetSendFileDownloadUrlAsync(string fileId)
|
|
|
|
|
|
{
|
|
|
|
|
|
await InitAsync();
|
|
|
|
|
|
return $"{_baseSendUrl}/{fileId}";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-02 15:55:49 -05:00
|
|
|
|
private void DeleteFileIfExists(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Task InitAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Directory.Exists(_baseDirPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(_baseDirPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|