DocSpace-buildtools/common/ASC.Data.Storage/BaseStorage.cs

371 lines
15 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2022-02-10 11:24:16 +00:00
namespace ASC.Data.Storage;
public abstract class BaseStorage : IDataStore
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
public IQuotaController QuotaController { get; set; }
public virtual bool IsSupportInternalUri => true;
public virtual bool IsSupportedPreSignedUri => true;
public virtual bool IsSupportChunking => false;
2022-02-11 10:04:06 +00:00
internal string Modulename { get; set; }
internal DataList DataList { get; set; }
internal string Tenant { get; set; }
internal Dictionary<string, TimeSpan> DomainsExpires { get; set; }
= new Dictionary<string, TimeSpan>();
protected ILogger Logger { get; set; }
2022-02-10 11:24:16 +00:00
2022-03-25 16:26:06 +00:00
protected readonly TempStream _tempStream;
protected readonly TenantManager _tenantManager;
protected readonly PathUtils _tpathUtils;
protected readonly EmailValidationKeyProvider _temailValidationKeyProvider;
protected readonly IHttpContextAccessor _httpContextAccessor;
protected readonly ILoggerProvider _options;
2022-03-25 16:26:06 +00:00
protected readonly IHttpClientFactory _clientFactory;
2022-02-10 11:24:16 +00:00
public BaseStorage(
TempStream tempStream,
TenantManager tenantManager,
PathUtils pathUtils,
EmailValidationKeyProvider emailValidationKeyProvider,
IHttpContextAccessor httpContextAccessor,
ILoggerProvider options,
ILogger logger,
IHttpClientFactory clientFactory)
2022-02-10 11:24:16 +00:00
{
2022-03-25 16:26:06 +00:00
_tempStream = tempStream;
_tenantManager = tenantManager;
_tpathUtils = pathUtils;
_temailValidationKeyProvider = emailValidationKeyProvider;
_options = options;
_clientFactory = clientFactory;
Logger = logger;
2022-03-25 16:26:06 +00:00
_httpContextAccessor = httpContextAccessor;
2022-02-10 11:24:16 +00:00
}
2020-12-22 12:51:04 +00:00
2022-02-10 11:24:16 +00:00
public TimeSpan GetExpire(string domain)
{
2022-02-11 10:04:06 +00:00
return DomainsExpires.ContainsKey(domain) ? DomainsExpires[domain] : DomainsExpires[string.Empty];
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<Uri> GetUriAsync(string path)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return GetUriAsync(string.Empty, path);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<Uri> GetUriAsync(string domain, string path)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return GetPreSignedUriAsync(domain, path, TimeSpan.MaxValue, null);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<Uri> GetPreSignedUriAsync(string domain, string path, TimeSpan expire, IEnumerable<string> headers)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
ArgumentNullException.ThrowIfNull(path);
2019-06-04 14:43:20 +00:00
2022-02-11 10:04:06 +00:00
if (string.IsNullOrEmpty(Tenant) && IsSupportInternalUri)
2019-06-04 14:43:20 +00:00
{
2022-03-09 17:15:51 +00:00
return GetInternalUriAsync(domain, path, expire, headers);
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
var headerAttr = string.Empty;
if (headers != null)
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
headerAttr = string.Join("&", headers.Select(HttpUtility.UrlEncode));
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
if (expire == TimeSpan.Zero || expire == TimeSpan.MinValue || expire == TimeSpan.MaxValue)
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
expire = GetExpire(domain);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
var query = string.Empty;
if (expire != TimeSpan.Zero && expire != TimeSpan.MinValue && expire != TimeSpan.MaxValue)
{
var expireString = expire.TotalMinutes.ToString(CultureInfo.InvariantCulture);
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
int currentTenantId;
2022-03-25 16:26:06 +00:00
var currentTenant = _tenantManager.GetCurrentTenant(false);
2022-03-09 17:15:51 +00:00
if (currentTenant != null)
{
currentTenantId = currentTenant.Id;
}
else if (!TenantPath.TryGetTenant(Tenant, out currentTenantId))
{
currentTenantId = 0;
}
2019-06-04 14:43:20 +00:00
2022-03-25 16:26:06 +00:00
var auth = _temailValidationKeyProvider.GetEmailKey(currentTenantId, path.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + "." + headerAttr + "." + expireString);
query = $"{(path.IndexOf('?') >= 0 ? "&" : "?")}{Constants.QueryExpire}={expireString}&{Constants.QueryAuth}={auth}";
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
if (!string.IsNullOrEmpty(headerAttr))
{
query += $"{(query.IndexOf('?') >= 0 ? "&" : "?")}{Constants.QueryHeader}={HttpUtility.UrlEncode(headerAttr)}";
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-02-11 10:04:06 +00:00
var tenant = Tenant.Trim('/');
2022-03-25 16:26:06 +00:00
var vpath = _tpathUtils.ResolveVirtualPath(Modulename, domain);
vpath = _tpathUtils.ResolveVirtualPath(vpath, false);
2022-02-10 11:24:16 +00:00
vpath = string.Format(vpath, tenant);
var virtualPath = new Uri(vpath + "/", UriKind.RelativeOrAbsolute);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
var uri = virtualPath.IsAbsoluteUri ?
new MonoUri(virtualPath, virtualPath.LocalPath.TrimEnd('/') + EnsureLeadingSlash(path.Replace('\\', '/')) + query) :
new MonoUri(virtualPath.ToString().TrimEnd('/') + EnsureLeadingSlash(path.Replace('\\', '/')) + query, UriKind.Relative);
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
return Task.FromResult<Uri>(uri);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public virtual Task<Uri> GetInternalUriAsync(string domain, string path, TimeSpan expire, IEnumerable<string> headers)
2022-02-10 11:24:16 +00:00
{
return null;
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public abstract Task<Stream> GetReadStreamAsync(string domain, string path);
2022-02-10 11:24:16 +00:00
public abstract Task<Stream> GetReadStreamAsync(string domain, string path, int offset);
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public abstract Task<Uri> SaveAsync(string domain, string path, Stream stream);
public abstract Task<Uri> SaveAsync(string domain, string path, Stream stream, ACL acl);
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<Uri> SaveAsync(string domain, string path, Stream stream, string attachmentFileName)
2022-02-10 11:24:16 +00:00
{
if (!string.IsNullOrEmpty(attachmentFileName))
2019-06-04 14:43:20 +00:00
{
2022-03-09 17:15:51 +00:00
return SaveWithAutoAttachmentAsync(domain, path, stream, attachmentFileName);
2019-06-04 14:43:20 +00:00
}
2022-03-09 17:15:51 +00:00
return SaveAsync(domain, path, stream);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
protected abstract Task<Uri> SaveWithAutoAttachmentAsync(string domain, string path, Stream stream, string attachmentFileName);
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public abstract Task<Uri> SaveAsync(string domain, string path, Stream stream, string contentType,
string contentDisposition);
public abstract Task<Uri> SaveAsync(string domain, string path, Stream stream, string contentEncoding, int cacheDays);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
#region chunking
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public virtual Task<string> InitiateChunkedUploadAsync(string domain, string path)
2022-02-10 11:24:16 +00:00
{
throw new NotImplementedException();
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public virtual Task<string> UploadChunkAsync(string domain, string path, string uploadId, Stream stream, long defaultChunkSize, int chunkNumber, long chunkLength)
2022-02-10 11:24:16 +00:00
{
throw new NotImplementedException();
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public virtual Task<Uri> FinalizeChunkedUploadAsync(string domain, string path, string uploadId, Dictionary<int, string> eTags)
2022-02-10 11:24:16 +00:00
{
throw new NotImplementedException();
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public virtual Task AbortChunkedUploadAsync(string domain, string path, string uploadId)
2022-02-10 11:24:16 +00:00
{
throw new NotImplementedException();
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
#endregion
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public abstract Task DeleteAsync(string domain, string path);
public abstract Task DeleteFilesAsync(string domain, string folderPath, string pattern, bool recursive);
public abstract Task DeleteFilesAsync(string domain, List<string> paths);
public abstract Task DeleteFilesAsync(string domain, string folderPath, DateTime fromDate, DateTime toDate);
public abstract Task MoveDirectoryAsync(string srcdomain, string srcdir, string newdomain, string newdir);
public abstract Task<Uri> MoveAsync(string srcdomain, string srcpath, string newdomain, string newpath, bool quotaCheckFileSize = true);
public abstract Task<Uri> SaveTempAsync(string domain, out string assignedPath, Stream stream);
public abstract IAsyncEnumerable<string> ListDirectoriesRelativeAsync(string domain, string path, bool recursive);
public abstract IAsyncEnumerable<string> ListFilesRelativeAsync(string domain, string path, string pattern, bool recursive);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public abstract Task<bool> IsFileAsync(string domain, string path);
2022-03-09 17:15:51 +00:00
public abstract Task<bool> IsDirectoryAsync(string domain, string path);
public abstract Task DeleteDirectoryAsync(string domain, string path);
public abstract Task<long> GetFileSizeAsync(string domain, string path);
public abstract Task<long> GetDirectorySizeAsync(string domain, string path);
public abstract Task<long> ResetQuotaAsync(string domain);
public abstract Task<long> GetUsedQuotaAsync(string domain);
public abstract Task<Uri> CopyAsync(string srcdomain, string path, string newdomain, string newpath);
public abstract Task CopyDirectoryAsync(string srcdomain, string dir, string newdomain, string newdir);
public Task<Stream> GetReadStreamAsync(string path)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return GetReadStreamAsync(string.Empty, path);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<Uri> SaveAsync(string path, Stream stream, string attachmentFileName)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return SaveAsync(string.Empty, path, stream, attachmentFileName);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<Uri> SaveAsync(string path, Stream stream)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return SaveAsync(string.Empty, path, stream);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public async Task DeleteAsync(string path)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
await DeleteAsync(string.Empty, path);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public async Task DeleteFilesAsync(string folderPath, string pattern, bool recursive)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
await DeleteFilesAsync(string.Empty, folderPath, pattern, recursive);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<Uri> MoveAsync(string srcpath, string newdomain, string newpath)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return MoveAsync(string.Empty, srcpath, newdomain, newpath);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<Uri> SaveTempAsync(out string assignedPath, Stream stream)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return SaveTempAsync(string.Empty, out assignedPath, stream);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public IAsyncEnumerable<string> ListDirectoriesRelativeAsync(string path, bool recursive)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return ListDirectoriesRelativeAsync(string.Empty, path, recursive);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public IAsyncEnumerable<Uri> ListFilesAsync(string path, string pattern, bool recursive)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return ListFilesAsync(string.Empty, path, pattern, recursive);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public async IAsyncEnumerable<Uri> ListFilesAsync(string domain, string path, string pattern, bool recursive)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
var filePaths = ListFilesRelativeAsync(domain, path, pattern, recursive);
2022-01-25 09:29:11 +00:00
2022-03-09 17:15:51 +00:00
await foreach (var paths in filePaths)
{
yield return await GetUriAsync(domain, CrossPlatform.PathCombine(PathUtils.Normalize(path), paths));
2019-06-04 14:43:20 +00:00
}
2022-03-09 17:15:51 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<bool> IsFileAsync(string path)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return IsFileAsync(string.Empty, path);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<bool> IsDirectoryAsync(string path)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return IsDirectoryAsync(string.Empty, path);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public async Task DeleteDirectoryAsync(string path)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
await DeleteDirectoryAsync(string.Empty, path);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<long> GetFileSizeAsync(string path)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return GetFileSizeAsync(string.Empty, path);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<long> GetDirectorySizeAsync(string path)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return GetDirectorySizeAsync(string.Empty, path);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public Task<Uri> CopyAsync(string path, string newdomain, string newpath)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
return CopyAsync(string.Empty, path, newdomain, newpath);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public async Task CopyDirectoryAsync(string dir, string newdomain, string newdir)
2022-02-10 11:24:16 +00:00
{
2022-03-09 17:15:51 +00:00
await CopyDirectoryAsync(string.Empty, dir, newdomain, newdir);
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public virtual IDataStore Configure(string tenant, Handler handlerConfig, Module moduleConfig, IDictionary<string, string> props)
{
return this;
}
2022-02-10 11:24:16 +00:00
public IDataStore SetQuotaController(IQuotaController controller)
{
QuotaController = controller;
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
return this;
}
2022-03-09 17:15:51 +00:00
public abstract Task<string> SavePrivateAsync(string domain, string path, Stream stream, DateTime expires);
public abstract Task DeleteExpiredAsync(string domain, string path, TimeSpan oldThreshold);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public abstract string GetUploadForm(string domain, string directoryPath, string redirectTo, long maxUploadSize,
string contentType, string contentDisposition, string submitLabel);
2019-06-04 14:43:20 +00:00
2022-03-09 17:15:51 +00:00
public abstract Task<string> GetUploadedUrlAsync(string domain, string directoryPath);
2022-02-10 11:24:16 +00:00
public abstract string GetUploadUrl();
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public abstract string GetPostParams(string domain, string directoryPath, long maxUploadSize, string contentType,
string contentDisposition);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
internal void QuotaUsedAdd(string domain, long size, bool quotaCheckFileSize = true)
{
if (QuotaController != null)
2019-06-04 14:43:20 +00:00
{
2022-02-11 10:04:06 +00:00
QuotaController.QuotaUsedAdd(Modulename, domain, DataList.GetData(domain), size, quotaCheckFileSize);
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
internal void QuotaUsedDelete(string domain, long size)
{
if (QuotaController != null)
2019-06-04 14:43:20 +00:00
{
2022-02-11 10:04:06 +00:00
QuotaController.QuotaUsedDelete(Modulename, domain, DataList.GetData(domain), size);
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
internal static string EnsureLeadingSlash(string str)
{
return "/" + str.TrimStart('/');
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
internal class MonoUri : Uri
{
public MonoUri(Uri baseUri, string relativeUri)
: base(baseUri, relativeUri) { }
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public MonoUri(string uriString, UriKind uriKind)
: base(uriString, uriKind) { }
2022-02-10 11:24:16 +00:00
public override string ToString()
{
var s = base.ToString();
if (WorkContext.IsMono && s.StartsWith(UriSchemeFile + SchemeDelimiter))
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
return s.Substring(7);
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
return s;
2019-06-04 14:43:20 +00:00
}
}
2022-02-10 11:24:16 +00:00
}