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

174 lines
6.7 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.DiscStorage;
2022-01-11 15:37:19 +00:00
2022-02-10 11:24:16 +00:00
public class StorageHandler
{
private readonly string _path;
private readonly string _module;
private readonly string _domain;
private readonly bool _checkAuth;
2022-03-25 09:23:28 +00:00
public StorageHandler(string path, string module, string domain, bool checkAuth = true)
2022-02-10 11:24:16 +00:00
{
_path = path;
_module = module;
_domain = domain;
_checkAuth = checkAuth;
}
2022-03-25 09:23:28 +00:00
public Task Invoke(HttpContext context, TenantManager tenantManager, SecurityContext securityContext, StorageFactory storageFactory, EmailValidationKeyProvider emailValidationKeyProvider)
2022-02-10 11:24:16 +00:00
{
if (_checkAuth && !securityContext.IsAuthenticated)
{
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
return Task.CompletedTask;
}
2022-02-10 11:24:16 +00:00
2022-10-28 12:23:51 +00:00
var storage = storageFactory.GetStorage(tenantManager.GetCurrentTenant().Id, _module);
var path = CrossPlatform.PathCombine(_path, GetRouteValue("pathInfo", context).Replace('/', Path.DirectorySeparatorChar));
2022-02-10 11:24:16 +00:00
var header = context.Request.Query[Constants.QueryHeader].FirstOrDefault() ?? "";
var auth = context.Request.Query[Constants.QueryAuth].FirstOrDefault() ?? "";
var storageExpire = storage.GetExpire(_domain);
if (storageExpire != TimeSpan.Zero && storageExpire != TimeSpan.MinValue && storageExpire != TimeSpan.MaxValue || !string.IsNullOrEmpty(auth))
{
var expire = context.Request.Query[Constants.QueryExpire];
2022-03-17 15:01:39 +00:00
if (string.IsNullOrEmpty(expire))
{
expire = storageExpire.TotalMinutes.ToString(CultureInfo.InvariantCulture);
}
2022-02-10 11:24:16 +00:00
var validateResult = emailValidationKeyProvider.ValidateEmailKey(path + "." + header + "." + expire, auth ?? "", TimeSpan.FromMinutes(Convert.ToDouble(expire)));
if (validateResult != EmailValidationKeyProvider.ValidationResult.Ok)
2021-09-05 19:59:14 +00:00
{
2022-02-10 11:24:16 +00:00
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
2022-01-11 15:37:19 +00:00
return Task.CompletedTask;
2021-09-05 19:59:14 +00:00
}
2022-02-10 11:24:16 +00:00
}
2022-03-25 09:23:28 +00:00
return InternalInvoke(context, storage, path, header);
}
2022-02-16 12:57:37 +00:00
2022-03-25 09:23:28 +00:00
private async Task InternalInvoke(HttpContext context, IDataStore storage, string path, string header)
{
if (!await storage.IsFileAsync(_domain, path))
{
2022-02-10 11:24:16 +00:00
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
2022-03-25 09:23:28 +00:00
return;
2022-02-10 11:24:16 +00:00
}
var headers = header.Length > 0 ? header.Split('&').Select(HttpUtility.UrlDecode) : Array.Empty<string>();
2022-02-10 11:24:16 +00:00
if (storage.IsSupportInternalUri)
{
2022-03-25 09:23:28 +00:00
var uri = await storage.GetInternalUriAsync(_domain, path, TimeSpan.FromMinutes(15), headers);
2022-02-10 11:24:16 +00:00
//TODO
//context.Response.Cache.SetAllowResponseInBrowserHistory(false);
//context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Redirect(uri.ToString());
2022-03-25 09:23:28 +00:00
return;
2022-02-10 11:24:16 +00:00
}
2021-08-03 14:05:49 +00:00
2022-02-10 11:24:16 +00:00
string encoding = null;
2022-03-25 09:23:28 +00:00
if (storage is DiscDataStore && await storage.IsFileAsync(_domain, path + ".gz"))
2022-02-10 11:24:16 +00:00
{
path += ".gz";
encoding = "gzip";
2022-01-11 15:37:19 +00:00
}
2022-02-10 11:24:16 +00:00
var headersToCopy = new List<string> { "Content-Disposition", "Cache-Control", "Content-Encoding", "Content-Language", "Content-Type", "Expires" };
foreach (var h in headers)
2022-01-11 15:37:19 +00:00
{
2022-02-10 11:24:16 +00:00
var toCopy = headersToCopy.Find(x => h.StartsWith(x));
if (string.IsNullOrEmpty(toCopy))
2022-02-10 11:06:37 +00:00
{
2022-02-10 11:24:16 +00:00
continue;
2021-08-03 14:05:49 +00:00
}
2022-02-10 11:24:16 +00:00
context.Response.Headers[toCopy] = h.Substring(toCopy.Length + 1);
2022-01-11 15:37:19 +00:00
}
2022-02-10 11:24:16 +00:00
try
{
context.Response.ContentType = MimeMapping.GetMimeMapping(path);
}
catch (Exception)
2022-01-11 15:37:19 +00:00
{
2020-10-28 12:26:27 +00:00
2022-02-10 11:24:16 +00:00
}
if (encoding != null)
2020-07-29 21:57:58 +00:00
{
2022-02-10 11:24:16 +00:00
context.Response.Headers["Content-Encoding"] = encoding;
2020-08-24 18:41:06 +00:00
}
2020-07-29 21:57:58 +00:00
Merge branch 'develop' into feature/backend-refactor # Conflicts: # common/ASC.Common/Caching/ICacheNotify.cs # common/ASC.Common/Caching/KafkaCache.cs # common/ASC.Common/Caching/MemoryCacheNotify.cs # common/ASC.Common/Threading/DistributedTaskProgress.cs # common/ASC.Common/Threading/DistributedTaskQueue.cs # common/ASC.Core.Common/Notify/Signalr/SignalrServiceClient.cs # common/ASC.Data.Backup.Core/Core/FileBackupProvider.cs # common/ASC.Data.Backup.Core/Storage/BackupRepository.cs # common/ASC.Data.Backup.Core/Storage/ConsumerBackupStorage.cs # common/ASC.Data.Backup.Core/Storage/DataStoreBackupStorage.cs # common/ASC.Data.Backup.Core/Storage/DocumentsBackupStorage.cs # common/ASC.Data.Backup.Core/Tasks/BackupPortalTask.cs # common/ASC.Data.Backup.Core/Tasks/DeletePortalTask.cs # common/ASC.Data.Backup.Core/Tasks/PortalTaskBase.cs # common/ASC.Data.Backup.Core/Tasks/RestorePortalTask.cs # common/ASC.Data.Backup.Core/Tasks/TransferPortalTask.cs # common/ASC.Data.Reassigns/RemoveProgressItem.cs # common/ASC.Data.Storage/BaseStorage.cs # common/ASC.Data.Storage/ChunkedUploader/CommonChunkedUploadSessionHolder.cs # common/ASC.Data.Storage/CrossModuleTransferUtility.cs # common/ASC.Data.Storage/DiscStorage/DiscDataStore.cs # common/ASC.Data.Storage/Encryption/EncryptionOperation.cs # common/ASC.Data.Storage/Extensions.cs # common/ASC.Data.Storage/GoogleCloud/GoogleCloudStorage.cs # common/ASC.Data.Storage/IDataStore.cs # common/ASC.Data.Storage/RackspaceCloud/RackspaceCloudStorage.cs # common/ASC.Data.Storage/S3/S3Storage.cs # common/ASC.Data.Storage/S3/S3UploadGuard.cs # common/ASC.Data.Storage/StaticUploader.cs # common/ASC.Data.Storage/StorageHandler.cs # common/ASC.Data.Storage/StorageUploader.cs # common/ASC.Data.Storage/WebPath.cs # common/services/ASC.ApiSystem/Controllers/PortalController.cs # common/services/ASC.AuditTrail/AuditEventsRepository.cs # common/services/ASC.AuditTrail/AuditReportCreator.cs # common/services/ASC.AuditTrail/LoginEventsRepository.cs # products/ASC.Files/Core/Configuration/FilesSpaceUsageStatManager.cs # products/ASC.Files/Core/Core/Dao/Interfaces/IFolderDao.cs # products/ASC.Files/Core/Core/Dao/Interfaces/ILinkDao.cs # products/ASC.Files/Core/Core/Dao/Interfaces/IProviderDao.cs # products/ASC.Files/Core/Core/Dao/Interfaces/IProviderInfo.cs # products/ASC.Files/Core/Core/Dao/Interfaces/ITagDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/AbstractDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/LinkDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/SecurityDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs # products/ASC.Files/Core/Core/Entries/ChunkedUploadSession.cs # products/ASC.Files/Core/Core/Entries/EncryptionKeyPair.cs # products/ASC.Files/Core/Core/FileStorageService.cs # products/ASC.Files/Core/Core/FilesIntegration.cs # products/ASC.Files/Core/Core/Security/FileSecurity.cs # products/ASC.Files/Core/Core/Security/IFileSecurity.cs # products/ASC.Files/Core/Core/Security/ISecurityDao.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxFileDao.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxFolderDao.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxProviderInfo.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxStorage.cs # products/ASC.Files/Core/Core/Thirdparty/CrossDao.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxFileDao.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxFolderDao.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxProviderInfo.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxStorage.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveFileDao.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveFolderDao.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveProviderInfo.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveStorage.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveFileDao.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveFolderDao.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveProviderInfo.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveStorage.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFileDao.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderFolderDao.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderTagDao.cs # products/ASC.Files/Core/Core/Thirdparty/RegexDaoSelectorBase.cs # products/ASC.Files/Core/Core/Thirdparty/SharePoint/SharePointDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/SharePoint/SharePointFileDao.cs # products/ASC.Files/Core/Core/Thirdparty/SharePoint/SharePointFolderDao.cs # products/ASC.Files/Core/Core/Thirdparty/SharePoint/SharePointProviderInfo.cs # products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxFileDao.cs # products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxFolderDao.cs # products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxProviderInfo.cs # products/ASC.Files/Core/Helpers/DocuSignHelper.cs # products/ASC.Files/Core/Helpers/Global.cs # products/ASC.Files/Core/Helpers/PathProvider.cs # products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/SearchHandler.cs # products/ASC.Files/Core/Model/FileEntryWrapper.cs # products/ASC.Files/Core/Model/FileOperationWraper.cs # products/ASC.Files/Core/Model/FileWrapper.cs # products/ASC.Files/Core/Model/FolderContentWrapper.cs # products/ASC.Files/Core/Model/FolderWrapper.cs # products/ASC.Files/Core/Services/DocumentService/Configuration.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceConnector.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceHelper.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceTracker.cs # products/ASC.Files/Core/Services/NotifyService/NotifyClient.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileDeleteOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileDownloadOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileMarkAsReadOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileMoveCopyOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileOperationsManager.cs # products/ASC.Files/Core/ThirdPartyApp/BoxApp.cs # products/ASC.Files/Core/ThirdPartyApp/GoogleDriveApp.cs # products/ASC.Files/Core/ThirdPartyApp/IThirdPartyApp.cs # products/ASC.Files/Core/Utils/ChunkedUploadSessionHolder.cs # products/ASC.Files/Core/Utils/EntryManager.cs # products/ASC.Files/Core/Utils/FileConverter.cs # products/ASC.Files/Core/Utils/FileMarker.cs # products/ASC.Files/Core/Utils/FileShareLink.cs # products/ASC.Files/Core/Utils/FileSharing.cs # products/ASC.Files/Core/Utils/FileUploader.cs # products/ASC.Files/Core/Utils/MailMergeTask.cs # products/ASC.Files/Core/Utils/SocketManager.cs # products/ASC.Files/Server/Controllers/FilesController.cs # products/ASC.Files/Server/Controllers/PrivacyRoomController.cs # products/ASC.Files/Server/Helpers/FilesControllerHelper.cs # products/ASC.People/Server/Controllers/PeopleController.cs # web/ASC.Web.Api/Controllers/AuthenticationController.cs # web/ASC.Web.Api/Controllers/PortalController.cs # web/ASC.Web.Api/Controllers/SettingsController.cs # web/ASC.Web.Api/Models/BuildVersion.cs # web/ASC.Web.Core/Files/DocumentService.cs # web/ASC.Web.Core/Files/DocumentServiceLicense.cs # web/ASC.Web.Core/Helpers/ApiSystemHelper.cs # web/ASC.Web.Core/Notify/StudioNotifyServiceSender.cs # web/ASC.Web.Core/Notify/StudioPeriodicNotify.cs # web/ASC.Web.Core/Recaptcha.cs # web/ASC.Web.Core/Sms/SmsManager.cs # web/ASC.Web.Core/Sms/SmsProvider.cs # web/ASC.Web.Core/Sms/SmsSender.cs # web/ASC.Web.Core/SpaceUsageStatManager.cs # web/ASC.Web.Core/Utility/UrlShortener.cs
2022-02-23 19:42:34 +00:00
using (var stream = await storage.GetReadStreamAsync(_domain, path))
{
2022-02-10 11:24:16 +00:00
await stream.CopyToAsync(context.Response.Body);
2020-08-24 18:41:06 +00:00
}
2022-02-10 11:24:16 +00:00
await context.Response.Body.FlushAsync();
await context.Response.CompleteAsync();
}
2022-02-10 11:24:16 +00:00
private string GetRouteValue(string name, HttpContext context)
{
return (context.GetRouteValue(name) ?? "").ToString();
2022-02-10 11:24:16 +00:00
}
}
public static class StorageHandlerExtensions
{
public static IEndpointRouteBuilder RegisterStorageHandler(this IEndpointRouteBuilder builder, string module, string domain, bool publicRoute = false)
{
var pathUtils = builder.ServiceProvider.GetService<PathUtils>();
var virtPath = pathUtils.ResolveVirtualPath(module, domain);
virtPath = virtPath.TrimStart('/');
2022-03-25 09:23:28 +00:00
var handler = new StorageHandler(string.Empty, module, domain, !publicRoute);
2022-02-10 11:24:16 +00:00
var url = virtPath + "{*pathInfo}";
if (!builder.DataSources.Any(r => r.Endpoints.Any(e => e.DisplayName == url)))
2020-08-31 08:18:07 +00:00
{
2022-03-25 09:23:28 +00:00
builder.MapGet(url, handler.Invoke);
2022-02-10 11:24:16 +00:00
var newUrl = url.Replace("{0}", "{t1}/{t2}/{t3}");
if (newUrl != url)
{
2022-03-25 09:23:28 +00:00
builder.MapGet(url, handler.Invoke);
2022-02-10 11:24:16 +00:00
}
2020-08-31 08:18:07 +00:00
}
2022-02-10 11:24:16 +00:00
return builder;
}
}