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

398 lines
14 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.
*
*/
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;
protected ILog Logger { get; set; }
internal string _modulename;
internal DataList _dataList;
internal string _tenant;
internal Dictionary<string, TimeSpan> _domainsExpires = new Dictionary<string, TimeSpan>();
protected readonly TempStream _tempStream;
protected readonly TenantManager _tenantManager;
protected readonly PathUtils _pathUtils;
protected readonly EmailValidationKeyProvider _emailValidationKeyProvider;
protected readonly IHttpContextAccessor _httpContextAccessor;
protected readonly IOptionsMonitor<ILog> _options;
public BaseStorage(
TempStream tempStream,
TenantManager tenantManager,
PathUtils pathUtils,
EmailValidationKeyProvider emailValidationKeyProvider,
IHttpContextAccessor httpContextAccessor,
IOptionsMonitor<ILog> options)
{
2022-02-10 11:24:16 +00:00
_tempStream = tempStream;
_tenantManager = tenantManager;
_pathUtils = pathUtils;
_emailValidationKeyProvider = emailValidationKeyProvider;
_options = options;
Logger = options.CurrentValue;
_httpContextAccessor = httpContextAccessor;
}
2020-12-22 12:51:04 +00:00
2022-02-10 11:24:16 +00:00
public TimeSpan GetExpire(string domain)
{
return _domainsExpires.ContainsKey(domain) ? _domainsExpires[domain] : _domainsExpires[string.Empty];
}
public Uri GetUri(string path)
{
return GetUri(string.Empty, path);
}
public Uri GetUri(string domain, string path)
{
return GetPreSignedUri(domain, path, TimeSpan.MaxValue, null);
}
public Uri GetPreSignedUri(string domain, string path, TimeSpan expire, IEnumerable<string> headers)
{
if (path == null)
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
throw new ArgumentNullException(nameof(path));
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
if (string.IsNullOrEmpty(_tenant) && IsSupportInternalUri)
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
return GetInternalUri(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-02-10 11:24:16 +00:00
int currentTenantId;
var currentTenant = _tenantManager.GetCurrentTenant(false);
if (currentTenant != null)
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
currentTenantId = currentTenant.TenantId;
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
else if (!TenantPath.TryGetTenant(_tenant, out currentTenantId))
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
currentTenantId = 0;
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
var auth = _emailValidationKeyProvider.GetEmailKey(currentTenantId, path.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + "." + headerAttr + "." + expireString);
query = string.Format("{0}{1}={2}&{3}={4}",
path.Contains("?") ? "&" : "?",
Constants.QueryExpire,
expireString,
Constants.QueryAuth,
auth);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
if (!string.IsNullOrEmpty(headerAttr))
{
query += string.Format("{0}{1}={2}",
query.Contains("?") ? "&" : "?",
Constants.QueryHeader,
HttpUtility.UrlEncode(headerAttr));
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +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);
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-02-10 11:24:16 +00:00
return uri;
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public virtual Uri GetInternalUri(string domain, string path, TimeSpan expire, IEnumerable<string> headers)
{
return null;
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public abstract Stream GetReadStream(string domain, string path);
2022-02-10 11:24:16 +00:00
public abstract Stream GetReadStream(string domain, string path, int offset);
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-02-10 11:24:16 +00:00
public abstract Uri Save(string domain, string path, Stream stream);
2022-02-10 11:24:16 +00:00
public abstract Uri Save(string domain, string path, Stream stream, ACL acl);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public Uri Save(string domain, string path, Stream stream, string attachmentFileName)
{
if (!string.IsNullOrEmpty(attachmentFileName))
2019-06-04 14:43:20 +00:00
{
2022-02-10 11:24:16 +00:00
return SaveWithAutoAttachment(domain, path, stream, attachmentFileName);
2019-06-04 14:43:20 +00:00
}
2022-02-10 11:24:16 +00:00
return Save(domain, path, stream);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
protected abstract Uri SaveWithAutoAttachment(string domain, string path, Stream stream, string attachmentFileName);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public abstract Uri Save(string domain, string path, Stream stream, string contentType,
string contentDisposition);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public abstract Uri Save(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-02-10 11:24:16 +00:00
public virtual string InitiateChunkedUpload(string domain, string path)
{
throw new NotImplementedException();
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public virtual string UploadChunk(string domain, string path, string uploadId, Stream stream, long defaultChunkSize, int chunkNumber, long chunkLength)
{
throw new NotImplementedException();
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public virtual Uri FinalizeChunkedUpload(string domain, string path, string uploadId, Dictionary<int, string> eTags)
{
throw new NotImplementedException();
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public virtual void AbortChunkedUpload(string domain, string path, string uploadId)
{
throw new NotImplementedException();
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
#endregion
2022-02-10 11:24:16 +00:00
public abstract void Delete(string domain, string path);
2022-02-10 11:24:16 +00:00
public abstract void DeleteFiles(string domain, string folderPath, string pattern, bool recursive);
2022-02-10 11:24:16 +00:00
public abstract void DeleteFiles(string domain, List<string> paths);
2022-02-10 11:24:16 +00:00
public abstract void DeleteFiles(string domain, string folderPath, DateTime fromDate, DateTime toDate);
2022-02-10 11:24:16 +00:00
public abstract void MoveDirectory(string srcdomain, string srcdir, string newdomain, string newdir);
2022-02-10 11:24:16 +00:00
public abstract Uri Move(string srcdomain, string srcpath, string newdomain, string newpath, bool quotaCheckFileSize = true);
2022-02-10 11:24:16 +00:00
public abstract Uri SaveTemp(string domain, out string assignedPath, Stream stream);
2022-02-10 11:24:16 +00:00
public abstract string[] ListDirectoriesRelative(string domain, string path, bool recursive);
2022-02-10 11:24:16 +00:00
public abstract string[] ListFilesRelative(string domain, string path, string pattern, bool recursive);
2022-02-10 11:24:16 +00:00
public abstract bool IsFile(string domain, string path);
2022-02-10 11:24:16 +00:00
public abstract Task<bool> IsFileAsync(string domain, string path);
2022-02-10 11:24:16 +00:00
public abstract bool IsDirectory(string domain, string path);
2022-02-10 11:24:16 +00:00
public abstract void DeleteDirectory(string domain, string path);
2022-02-10 11:24:16 +00:00
public abstract long GetFileSize(string domain, string path);
2022-02-10 11:24:16 +00:00
public abstract long GetDirectorySize(string domain, string path);
2022-02-10 11:24:16 +00:00
public abstract long ResetQuota(string domain);
2022-02-10 11:24:16 +00:00
public abstract long GetUsedQuota(string domain);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public abstract Uri Copy(string srcdomain, string path, string newdomain, string newpath);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public abstract void CopyDirectory(string srcdomain, string dir, string newdomain, string newdir);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public Stream GetReadStream(string path)
{
return GetReadStream(string.Empty, path);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public Uri Save(string path, Stream stream, string attachmentFileName)
{
return Save(string.Empty, path, stream, attachmentFileName);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public Uri Save(string path, Stream stream)
{
return Save(string.Empty, path, stream);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public void Delete(string path)
{
Delete(string.Empty, path);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public void DeleteFiles(string folderPath, string pattern, bool recursive)
{
DeleteFiles(string.Empty, folderPath, pattern, recursive);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public Uri Move(string srcpath, string newdomain, string newpath)
{
return Move(string.Empty, srcpath, newdomain, newpath);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public Uri SaveTemp(out string assignedPath, Stream stream)
{
return SaveTemp(string.Empty, out assignedPath, stream);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public string[] ListDirectoriesRelative(string path, bool recursive)
{
return ListDirectoriesRelative(string.Empty, path, recursive);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public Uri[] ListFiles(string path, string pattern, bool recursive)
{
return ListFiles(string.Empty, path, pattern, recursive);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public Uri[] ListFiles(string domain, string path, string pattern, bool recursive)
{
var filePaths = ListFilesRelative(domain, path, pattern, recursive);
return Array.ConvertAll(
filePaths,
x => GetUri(domain, CrossPlatform.PathCombine(PathUtils.Normalize(path), x)));
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public bool IsFile(string path)
{
return IsFile(string.Empty, path);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public bool IsDirectory(string path)
{
return IsDirectory(string.Empty, path);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public void DeleteDirectory(string path)
{
DeleteDirectory(string.Empty, path);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public long GetFileSize(string path)
{
return GetFileSize(string.Empty, path);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public long GetDirectorySize(string path)
{
return GetDirectorySize(string.Empty, path);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public Uri Copy(string path, string newdomain, string newpath)
{
return Copy(string.Empty, path, newdomain, newpath);
}
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public void CopyDirectory(string dir, string newdomain, string newdir)
{
CopyDirectory(string.Empty, dir, newdomain, newdir);
}
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-02-10 11:24:16 +00:00
public abstract string SavePrivate(string domain, string path, Stream stream, DateTime expires);
2019-06-04 14:43:20 +00:00
2022-02-10 11:24:16 +00:00
public abstract void DeleteExpired(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-02-10 11:24:16 +00:00
public abstract string GetUploadedUrl(string domain, string directoryPath);
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-10 11:24:16 +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-10 11:24:16 +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))
{
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
}