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

408 lines
16 KiB
C#
Raw Normal View History

2019-06-04 14:43:20 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
2022-01-13 11:19:39 +00:00
using System.Net.Http;
2021-05-21 13:26:42 +00:00
using System.Threading.Tasks;
2019-06-04 14:43:20 +00:00
using System.Web;
2021-05-17 11:35:00 +00:00
using ASC.Common;
2019-10-17 15:55:35 +00:00
using ASC.Common.Logging;
using ASC.Common.Utils;
2019-06-04 14:43:20 +00:00
using ASC.Core;
using ASC.Data.Storage.Configuration;
using ASC.Security.Cryptography;
2019-10-18 08:48:27 +00:00
using Microsoft.AspNetCore.Http;
2019-10-17 15:55:35 +00:00
using Microsoft.Extensions.Options;
2019-06-04 14:43:20 +00:00
namespace ASC.Data.Storage
{
public abstract class BaseStorage : IDataStore
{
2019-10-17 15:55:35 +00:00
protected ILog Log { get; set; }
2021-05-17 11:35:00 +00:00
protected TempStream TempStream { get; }
protected TenantManager TenantManager { get; }
protected PathUtils PathUtils { get; }
protected EmailValidationKeyProvider EmailValidationKeyProvider { get; }
protected IHttpContextAccessor HttpContextAccessor { get; }
protected IOptionsMonitor<ILog> Options { get; }
2022-01-13 11:19:39 +00:00
protected IHttpClientFactory ClientFactory { get; }
2022-01-24 09:57:01 +00:00
protected BaseStorage(
2021-05-17 11:35:00 +00:00
TempStream tempStream,
2019-10-17 15:55:35 +00:00
TenantManager tenantManager,
PathUtils pathUtils,
EmailValidationKeyProvider emailValidationKeyProvider,
IHttpContextAccessor httpContextAccessor,
2022-01-13 11:19:39 +00:00
IOptionsMonitor<ILog> options,
IHttpClientFactory clientFactory)
2019-09-20 14:04:06 +00:00
{
2021-05-17 11:35:00 +00:00
TempStream = tempStream;
2019-09-20 14:04:06 +00:00
TenantManager = tenantManager;
2019-09-21 16:39:17 +00:00
PathUtils = pathUtils;
2019-09-23 12:20:08 +00:00
EmailValidationKeyProvider = emailValidationKeyProvider;
2019-10-18 08:48:27 +00:00
Options = options;
2019-11-06 15:03:09 +00:00
Log = options.CurrentValue;
2020-12-22 12:51:04 +00:00
HttpContextAccessor = httpContextAccessor;
2022-01-13 11:19:39 +00:00
ClientFactory = clientFactory;
2020-12-22 12:51:04 +00:00
}
2019-06-04 14:43:20 +00:00
#region IDataStore Members
internal string _modulename;
internal DataList _dataList;
internal string _tenant;
internal Dictionary<string, TimeSpan> _domainsExpires = new Dictionary<string, TimeSpan>();
public IQuotaController QuotaController { get; set; }
public TimeSpan GetExpire(string domain)
{
return _domainsExpires.ContainsKey(domain) ? _domainsExpires[domain] : _domainsExpires[string.Empty];
}
2022-01-25 09:29:11 +00:00
public Task<Uri> GetUriAsync(string path)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return GetUriAsync(string.Empty, path);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public Task<Uri> GetUriAsync(string domain, string path)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return GetPreSignedUriAsync(domain, path, TimeSpan.MaxValue, null);
}
2019-06-04 14:43:20 +00:00
2022-01-25 09:29:11 +00:00
public Task<Uri> GetPreSignedUriAsync(string domain, string path, TimeSpan expire, IEnumerable<string> headers)
2019-06-04 14:43:20 +00:00
{
if (path == null)
{
2022-01-24 10:49:00 +00:00
throw new ArgumentNullException(nameof(path));
2019-06-04 14:43:20 +00:00
}
if (string.IsNullOrEmpty(_tenant) && IsSupportInternalUri)
{
2022-01-25 09:29:11 +00:00
return GetInternalUriAsync(domain, path, expire, headers);
2019-06-04 14:43:20 +00:00
}
var headerAttr = string.Empty;
if (headers != null)
{
headerAttr = string.Join("&", headers.Select(HttpUtility.UrlEncode));
}
if (expire == TimeSpan.Zero || expire == TimeSpan.MinValue || expire == TimeSpan.MaxValue)
{
expire = GetExpire(domain);
}
var query = string.Empty;
if (expire != TimeSpan.Zero && expire != TimeSpan.MinValue && expire != TimeSpan.MaxValue)
{
var expireString = expire.TotalMinutes.ToString(CultureInfo.InvariantCulture);
int currentTenantId;
2019-09-20 14:04:06 +00:00
var currentTenant = TenantManager.GetCurrentTenant(false);
2019-06-04 14:43:20 +00:00
if (currentTenant != null)
{
currentTenantId = currentTenant.TenantId;
}
2020-01-21 12:44:05 +00:00
else if (!TenantPath.TryGetTenant(_tenant, out currentTenantId))
2019-06-04 14:43:20 +00:00
{
currentTenantId = 0;
}
var auth = EmailValidationKeyProvider.GetEmailKey(currentTenantId, path.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + "." + headerAttr + "." + expireString);
2022-01-14 13:12:37 +00:00
query = $"{(path.IndexOf('?') >= 0 ? "&" : "?")}{Constants.QUERY_EXPIRE}={expireString}&{Constants.QUERY_AUTH}={auth}";
2019-06-04 14:43:20 +00:00
}
if (!string.IsNullOrEmpty(headerAttr))
{
2022-01-14 13:12:37 +00:00
query += $"{(query.IndexOf('?') >= 0 ? "&" : "?")}{Constants.QUERY_HEADER}={HttpUtility.UrlEncode(headerAttr)}";
2019-06-04 14:43:20 +00:00
}
var tenant = _tenant.Trim('/');
var vpath = PathUtils.ResolveVirtualPath(_modulename, domain);
vpath = PathUtils.ResolveVirtualPath(vpath, false);
vpath = string.Format(vpath, tenant);
var virtualPath = new Uri(vpath + "/", UriKind.RelativeOrAbsolute);
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);
2022-01-25 09:29:11 +00:00
return Task.FromResult<Uri>(uri);
2019-06-04 14:43:20 +00:00
}
public virtual bool IsSupportInternalUri
{
get { return true; }
}
2022-01-25 09:29:11 +00:00
public virtual Task<Uri> GetInternalUriAsync(string domain, string path, TimeSpan expire, IEnumerable<string> headers)
{
return null;
}
public abstract Task<Stream> GetReadStreamAsync(string domain, string path);
2021-05-21 13:26:42 +00:00
public abstract Task<Stream> GetReadStreamAsync(string domain, string path, int offset);
2019-06-04 14:43:20 +00:00
2022-01-25 09:29:11 +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-01-25 09:29:11 +00:00
public Task<Uri> SaveAsync(string domain, string path, Stream stream, string attachmentFileName)
2019-06-04 14:43:20 +00:00
{
if (!string.IsNullOrEmpty(attachmentFileName))
{
2022-01-25 09:29:11 +00:00
return SaveWithAutoAttachmentAsync(domain, path, stream, attachmentFileName);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
return SaveAsync(domain, path, stream);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
protected abstract Task<Uri> SaveWithAutoAttachmentAsync(string domain, string path, Stream stream, string attachmentFileName);
2019-06-04 14:43:20 +00:00
2022-01-25 09:29:11 +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
public virtual bool IsSupportedPreSignedUri
{
get
{
return true;
}
}
#region chunking
2022-01-25 09:29:11 +00:00
public virtual Task<string> InitiateChunkedUploadAsync(string domain, string path)
2019-06-04 14:43:20 +00:00
{
throw new NotImplementedException();
}
2022-01-25 09:29:11 +00:00
public virtual Task<string> UploadChunkAsync(string domain, string path, string uploadId, Stream stream, long defaultChunkSize, int chunkNumber, long chunkLength)
2019-06-04 14:43:20 +00:00
{
throw new NotImplementedException();
}
2022-01-25 09:29:11 +00:00
public virtual Task<Uri> FinalizeChunkedUploadAsync(string domain, string path, string uploadId, Dictionary<int, string> eTags)
2019-06-04 14:43:20 +00:00
{
throw new NotImplementedException();
}
2022-01-25 09:29:11 +00:00
public virtual Task AbortChunkedUploadAsync(string domain, string path, string uploadId)
2019-06-04 14:43:20 +00:00
{
throw new NotImplementedException();
}
public virtual bool IsSupportChunking { get { return false; } }
#endregion
2022-01-25 09:29:11 +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);
2021-05-21 13:26:42 +00:00
public abstract Task<bool> IsFileAsync(string domain, string path);
2022-01-25 09:29:11 +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);
2019-06-04 14:43:20 +00:00
2022-01-25 09:29:11 +00:00
public Task<Stream> GetReadStreamAsync(string path)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return GetReadStreamAsync(string.Empty, path);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public Task<Uri> SaveAsync(string path, Stream stream, string attachmentFileName)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return SaveAsync(string.Empty, path, stream, attachmentFileName);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public Task<Uri> SaveAsync(string path, Stream stream)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return SaveAsync(string.Empty, path, stream);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public async Task DeleteAsync(string path)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
await DeleteAsync(string.Empty, path);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public async Task DeleteFilesAsync(string folderPath, string pattern, bool recursive)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
await DeleteFilesAsync(string.Empty, folderPath, pattern, recursive);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public Task<Uri> MoveAsync(string srcpath, string newdomain, string newpath)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return MoveAsync(string.Empty, srcpath, newdomain, newpath);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public Task<Uri> SaveTempAsync(out string assignedPath, Stream stream)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return SaveTempAsync(string.Empty, out assignedPath, stream);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public IAsyncEnumerable<string> ListDirectoriesRelativeAsync(string path, bool recursive)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return ListDirectoriesRelativeAsync(string.Empty, path, recursive);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public IAsyncEnumerable<Uri> ListFilesAsync(string path, string pattern, bool recursive)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return ListFilesAsync(string.Empty, path, pattern, recursive);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public async IAsyncEnumerable<Uri> ListFilesAsync(string domain, string path, string pattern, bool recursive)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
var filePaths = ListFilesRelativeAsync(domain, path, pattern, recursive);
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-01-25 09:29:11 +00:00
public Task<bool> IsFileAsync(string path)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return IsFileAsync(string.Empty, path);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public Task<bool> IsDirectoryAsync(string path)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return IsDirectoryAsync(string.Empty, path);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public async Task DeleteDirectoryAsync(string path)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
await DeleteDirectoryAsync(string.Empty, path);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public Task<long> GetFileSizeAsync(string path)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return GetFileSizeAsync(string.Empty, path);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public Task<long> GetDirectorySizeAsync(string path)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return GetDirectorySizeAsync(string.Empty, path);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public Task<Uri> CopyAsync(string path, string newdomain, string newpath)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
return CopyAsync(string.Empty, path, newdomain, newpath);
2019-06-04 14:43:20 +00:00
}
2022-01-25 09:29:11 +00:00
public async Task CopyDirectoryAsync(string dir, string newdomain, string newdir)
2019-06-04 14:43:20 +00:00
{
2022-01-25 09:29:11 +00:00
await CopyDirectoryAsync(string.Empty, dir, newdomain, newdir);
2019-06-04 14:43:20 +00:00
}
2019-09-20 14:04:06 +00:00
public virtual IDataStore Configure(string tenant, Handler handlerConfig, Module moduleConfig, IDictionary<string, string> props)
2019-06-04 14:43:20 +00:00
{
return this;
}
public IDataStore SetQuotaController(IQuotaController controller)
{
QuotaController = controller;
return this;
}
2022-01-25 09:29:11 +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
public abstract string GetUploadForm(string domain, string directoryPath, string redirectTo, long maxUploadSize,
string contentType, string contentDisposition, string submitLabel);
2022-01-25 09:29:11 +00:00
public abstract Task<string> GetUploadedUrlAsync(string domain, string directoryPath);
2019-06-04 14:43:20 +00:00
public abstract string GetUploadUrl();
public abstract string GetPostParams(string domain, string directoryPath, long maxUploadSize, string contentType,
string contentDisposition);
#endregion
2021-05-21 13:26:42 +00:00
internal void QuotaUsedAdd(string domain, long size, bool quotaCheckFileSize = true)
2019-06-04 14:43:20 +00:00
{
if (QuotaController != null)
{
2021-05-21 13:26:42 +00:00
QuotaController.QuotaUsedAdd(_modulename, domain, _dataList.GetData(domain), size, quotaCheckFileSize);
2019-06-04 14:43:20 +00:00
}
}
internal void QuotaUsedDelete(string domain, long size)
{
if (QuotaController != null)
{
QuotaController.QuotaUsedDelete(_modulename, domain, _dataList.GetData(domain), size);
}
}
internal static string EnsureLeadingSlash(string str)
{
return "/" + str.TrimStart('/');
}
internal class MonoUri : Uri
{
public MonoUri(Uri baseUri, string relativeUri)
: base(baseUri, relativeUri)
{
}
public MonoUri(string uriString, UriKind uriKind)
: base(uriString, uriKind)
{
}
public override string ToString()
{
var s = base.ToString();
if (WorkContext.IsMono && s.StartsWith(UriSchemeFile + SchemeDelimiter))
{
return s.Substring(7);
}
return s;
}
}
}
}