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

282 lines
9.5 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;
2020-09-29 13:02:47 +00:00
2022-04-12 19:04:10 +00:00
[Scope]
2022-02-10 11:24:16 +00:00
public class StaticUploader
{
2022-03-25 16:26:06 +00:00
protected readonly DistributedTaskQueue _queue;
2022-03-17 15:07:17 +00:00
private readonly ICache _cache;
2022-04-14 19:42:15 +00:00
public const string CUSTOM_DISTRIBUTED_TASK_QUEUE_NAME = "static_upload"; private static readonly CancellationTokenSource _tokenSource;
2022-02-10 12:34:26 +00:00
private static readonly object _locker;
2022-02-10 11:24:16 +00:00
private readonly IServiceProvider _serviceProvider;
private readonly TenantManager _tenantManager;
private readonly SettingsManager _settingsManager;
private readonly StorageSettingsHelper _storageSettingsHelper;
2022-03-25 09:23:28 +00:00
private readonly UploadOperation _uploadOperation;
2021-05-18 18:24:22 +00:00
2022-02-10 11:24:16 +00:00
static StaticUploader()
{
2022-02-10 12:34:26 +00:00
_locker = new object();
_tokenSource = new CancellationTokenSource();
2022-02-10 11:24:16 +00:00
}
2022-02-10 11:06:37 +00:00
2022-02-10 11:24:16 +00:00
public StaticUploader(
IServiceProvider serviceProvider,
TenantManager tenantManager,
SettingsManager settingsManager,
StorageSettingsHelper storageSettingsHelper,
2022-03-25 09:23:28 +00:00
UploadOperation uploadOperation,
2022-02-10 11:24:16 +00:00
ICache cache,
2022-03-22 12:10:05 +00:00
IDistributedTaskQueueFactory queueFactory)
2022-02-10 11:24:16 +00:00
{
_cache = cache;
_serviceProvider = serviceProvider;
_tenantManager = tenantManager;
_settingsManager = settingsManager;
_storageSettingsHelper = storageSettingsHelper;
Merge branch 'feature/backend-refactor' into feature/warnings # Conflicts: # common/ASC.Common/Threading/DistributedTaskProgress.cs # common/ASC.Common/Threading/DistributedTaskQueue.cs # common/ASC.Core.Common/Configuration/AmiPublicDnsSyncService.cs # common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs # common/ASC.Core.Common/Notify/Model/NotifyClientImpl.cs # common/ASC.Core.Common/Security/EmailValidationKeyProvider.cs # common/ASC.Data.Reassigns/QueueWorker.cs # common/ASC.Data.Reassigns/ReassignProgressItem.cs # common/ASC.Data.Reassigns/RemoveProgressItem.cs # common/ASC.Data.Storage/StaticUploader.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FileDto.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FolderDto.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs # products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxDaoBase.cs # products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/ThirdPartyAppHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/docusignhandler.ashx.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceTracker.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileDownloadOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileOperation.cs # products/ASC.Files/Core/Utils/EntryManager.cs # web/ASC.Web.Core/CollaboratorSettings.cs # web/ASC.Web.Core/CustomNavigationSettings.cs # web/ASC.Web.Core/EmailActivationSettings.cs # web/ASC.Web.Core/Notify/NotifyConfiguration.cs # web/ASC.Web.Core/Notify/SpamEmailSettings.cs # web/ASC.Web.Core/Notify/StudioNotifyHelper.cs # web/ASC.Web.Core/Notify/StudioNotifyService.cs # web/ASC.Web.Core/Notify/StudioNotifyServiceSender.cs # web/ASC.Web.Core/Notify/StudioPeriodicNotify.cs # web/ASC.Web.Core/Notify/StudioWhatsNewNotify.cs # web/ASC.Web.Core/PersonalSettings.cs # web/ASC.Web.Core/PrivacyRoomSettings.cs # web/ASC.Web.Core/PromotionsSettings.cs # web/ASC.Web.Core/QuotaSync.cs # web/ASC.Web.Core/Sms/SmsSender.cs # web/ASC.Web.Core/Sms/StudioSmsNotificationSettings.cs # web/ASC.Web.Core/StudioAdminMessageSettings.cs # web/ASC.Web.Core/StudioDefaultPageSettings.cs # web/ASC.Web.Core/StudioTrustedDomainSettings.cs # web/ASC.Web.Core/TariffSettings.cs # web/ASC.Web.Core/Tfa/TfaAppAuthSettings.cs # web/ASC.Web.Core/Tfa/TfaAppUserSettings.cs # web/ASC.Web.Core/TipsSettings.cs # web/ASC.Web.Core/Users/CustomNamingPeople.cs # web/ASC.Web.Core/Users/UserHelpTourSettings.cs # web/ASC.Web.Core/Users/UserPhotoManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs # web/ASC.Web.Core/Utility/ColorThemesSettings.cs # web/ASC.Web.Core/Utility/PasswordSettings.cs # web/ASC.Web.Core/Utility/Settings/TenantAccessSettings.cs # web/ASC.Web.Core/Utility/Settings/WebItemSettings.cs # web/ASC.Web.Core/Utility/Settings/WizardSettings.cs # web/ASC.Web.Core/Utility/TenantExtra.cs # web/ASC.Web.Core/WhiteLabel/AdditionalWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/CompanyWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs
2022-04-14 12:21:06 +00:00
_queue = queueFactory.CreateQueue(CUSTOM_DISTRIBUTED_TASK_QUEUE_NAME);
2022-03-25 09:23:28 +00:00
_uploadOperation = uploadOperation;
2022-02-10 11:24:16 +00:00
}
2022-02-10 11:06:37 +00:00
2022-03-25 09:23:28 +00:00
public async Task<string> UploadFileAsync(string relativePath, string mappedPath, Action<string> onComplete = null)
2022-02-10 11:24:16 +00:00
{
2022-02-10 12:34:26 +00:00
if (_tokenSource.Token.IsCancellationRequested)
2022-02-10 11:24:16 +00:00
{
return null;
}
2022-02-10 11:06:37 +00:00
2022-02-10 11:24:16 +00:00
if (!CanUpload())
{
return null;
}
if (!File.Exists(mappedPath))
{
return null;
}
2020-09-29 13:02:47 +00:00
var tenantId = _tenantManager.GetCurrentTenant().Id;
2022-02-10 11:24:16 +00:00
var key = GetCacheKey(tenantId.ToString(), relativePath);
2022-02-10 12:34:26 +00:00
lock (_locker)
2022-02-10 11:24:16 +00:00
{
2022-03-25 09:23:28 +00:00
var result = _cache.Get<string>(key);
if (!string.IsNullOrEmpty(result))
2022-02-10 11:06:37 +00:00
{
2022-03-25 09:23:28 +00:00
return result;
2022-02-10 11:06:37 +00:00
}
2022-02-10 11:24:16 +00:00
}
2022-03-25 09:23:28 +00:00
await _uploadOperation.DoJobAsync(tenantId, relativePath, mappedPath);
onComplete?.Invoke(_uploadOperation.Result);
2022-02-10 11:24:16 +00:00
2022-03-25 09:23:28 +00:00
lock (_locker)
2020-09-29 13:02:47 +00:00
{
2022-03-25 09:23:28 +00:00
_cache.Insert(key, _uploadOperation.Result, DateTime.MaxValue);
2022-04-14 19:42:15 +00:00
}
2022-02-10 11:24:16 +00:00
2022-03-25 09:23:28 +00:00
return _uploadOperation.Result;
2022-02-10 11:24:16 +00:00
}
public void UploadDir(string relativePath, string mappedPath)
{
if (!CanUpload())
{
return;
}
if (!Directory.Exists(mappedPath))
{
return;
}
2020-10-02 07:19:02 +00:00
2022-02-10 11:24:16 +00:00
var tenant = _tenantManager.GetCurrentTenant();
var key = typeof(UploadOperationProgress).FullName + tenant.Id;
2022-02-10 11:24:16 +00:00
2022-02-10 12:34:26 +00:00
lock (_locker)
2022-02-10 11:24:16 +00:00
{
Merge branch 'feature/backend-refactor' into feature/warnings # Conflicts: # common/ASC.Common/Threading/DistributedTaskProgress.cs # common/ASC.Common/Threading/DistributedTaskQueue.cs # common/ASC.Core.Common/Configuration/AmiPublicDnsSyncService.cs # common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs # common/ASC.Core.Common/Notify/Model/NotifyClientImpl.cs # common/ASC.Core.Common/Security/EmailValidationKeyProvider.cs # common/ASC.Data.Reassigns/QueueWorker.cs # common/ASC.Data.Reassigns/ReassignProgressItem.cs # common/ASC.Data.Reassigns/RemoveProgressItem.cs # common/ASC.Data.Storage/StaticUploader.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FileDto.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FolderDto.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs # products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxDaoBase.cs # products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/ThirdPartyAppHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/docusignhandler.ashx.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceTracker.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileDownloadOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileOperation.cs # products/ASC.Files/Core/Utils/EntryManager.cs # web/ASC.Web.Core/CollaboratorSettings.cs # web/ASC.Web.Core/CustomNavigationSettings.cs # web/ASC.Web.Core/EmailActivationSettings.cs # web/ASC.Web.Core/Notify/NotifyConfiguration.cs # web/ASC.Web.Core/Notify/SpamEmailSettings.cs # web/ASC.Web.Core/Notify/StudioNotifyHelper.cs # web/ASC.Web.Core/Notify/StudioNotifyService.cs # web/ASC.Web.Core/Notify/StudioNotifyServiceSender.cs # web/ASC.Web.Core/Notify/StudioPeriodicNotify.cs # web/ASC.Web.Core/Notify/StudioWhatsNewNotify.cs # web/ASC.Web.Core/PersonalSettings.cs # web/ASC.Web.Core/PrivacyRoomSettings.cs # web/ASC.Web.Core/PromotionsSettings.cs # web/ASC.Web.Core/QuotaSync.cs # web/ASC.Web.Core/Sms/SmsSender.cs # web/ASC.Web.Core/Sms/StudioSmsNotificationSettings.cs # web/ASC.Web.Core/StudioAdminMessageSettings.cs # web/ASC.Web.Core/StudioDefaultPageSettings.cs # web/ASC.Web.Core/StudioTrustedDomainSettings.cs # web/ASC.Web.Core/TariffSettings.cs # web/ASC.Web.Core/Tfa/TfaAppAuthSettings.cs # web/ASC.Web.Core/Tfa/TfaAppUserSettings.cs # web/ASC.Web.Core/TipsSettings.cs # web/ASC.Web.Core/Users/CustomNamingPeople.cs # web/ASC.Web.Core/Users/UserHelpTourSettings.cs # web/ASC.Web.Core/Users/UserPhotoManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs # web/ASC.Web.Core/Utility/ColorThemesSettings.cs # web/ASC.Web.Core/Utility/PasswordSettings.cs # web/ASC.Web.Core/Utility/Settings/TenantAccessSettings.cs # web/ASC.Web.Core/Utility/Settings/WebItemSettings.cs # web/ASC.Web.Core/Utility/Settings/WizardSettings.cs # web/ASC.Web.Core/Utility/TenantExtra.cs # web/ASC.Web.Core/WhiteLabel/AdditionalWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/CompanyWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs
2022-04-14 12:21:06 +00:00
if (_queue.GetAllTasks().Any(x => x.Id != key))
2022-02-10 11:06:37 +00:00
{
return;
}
2022-03-25 16:15:28 +00:00
var uploadOperation = new UploadOperationProgress(_serviceProvider, key, tenant.Id, relativePath, mappedPath);
Merge branch 'feature/backend-refactor' into feature/warnings # Conflicts: # common/ASC.Common/Threading/DistributedTaskProgress.cs # common/ASC.Common/Threading/DistributedTaskQueue.cs # common/ASC.Core.Common/Configuration/AmiPublicDnsSyncService.cs # common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs # common/ASC.Core.Common/Notify/Model/NotifyClientImpl.cs # common/ASC.Core.Common/Security/EmailValidationKeyProvider.cs # common/ASC.Data.Reassigns/QueueWorker.cs # common/ASC.Data.Reassigns/ReassignProgressItem.cs # common/ASC.Data.Reassigns/RemoveProgressItem.cs # common/ASC.Data.Storage/StaticUploader.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FileDto.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FolderDto.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs # products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxDaoBase.cs # products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/ThirdPartyAppHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/docusignhandler.ashx.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceTracker.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileDownloadOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileOperation.cs # products/ASC.Files/Core/Utils/EntryManager.cs # web/ASC.Web.Core/CollaboratorSettings.cs # web/ASC.Web.Core/CustomNavigationSettings.cs # web/ASC.Web.Core/EmailActivationSettings.cs # web/ASC.Web.Core/Notify/NotifyConfiguration.cs # web/ASC.Web.Core/Notify/SpamEmailSettings.cs # web/ASC.Web.Core/Notify/StudioNotifyHelper.cs # web/ASC.Web.Core/Notify/StudioNotifyService.cs # web/ASC.Web.Core/Notify/StudioNotifyServiceSender.cs # web/ASC.Web.Core/Notify/StudioPeriodicNotify.cs # web/ASC.Web.Core/Notify/StudioWhatsNewNotify.cs # web/ASC.Web.Core/PersonalSettings.cs # web/ASC.Web.Core/PrivacyRoomSettings.cs # web/ASC.Web.Core/PromotionsSettings.cs # web/ASC.Web.Core/QuotaSync.cs # web/ASC.Web.Core/Sms/SmsSender.cs # web/ASC.Web.Core/Sms/StudioSmsNotificationSettings.cs # web/ASC.Web.Core/StudioAdminMessageSettings.cs # web/ASC.Web.Core/StudioDefaultPageSettings.cs # web/ASC.Web.Core/StudioTrustedDomainSettings.cs # web/ASC.Web.Core/TariffSettings.cs # web/ASC.Web.Core/Tfa/TfaAppAuthSettings.cs # web/ASC.Web.Core/Tfa/TfaAppUserSettings.cs # web/ASC.Web.Core/TipsSettings.cs # web/ASC.Web.Core/Users/CustomNamingPeople.cs # web/ASC.Web.Core/Users/UserHelpTourSettings.cs # web/ASC.Web.Core/Users/UserPhotoManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs # web/ASC.Web.Core/Utility/ColorThemesSettings.cs # web/ASC.Web.Core/Utility/PasswordSettings.cs # web/ASC.Web.Core/Utility/Settings/TenantAccessSettings.cs # web/ASC.Web.Core/Utility/Settings/WebItemSettings.cs # web/ASC.Web.Core/Utility/Settings/WizardSettings.cs # web/ASC.Web.Core/Utility/TenantExtra.cs # web/ASC.Web.Core/WhiteLabel/AdditionalWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/CompanyWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs
2022-04-14 12:21:06 +00:00
_queue.EnqueueTask(uploadOperation);
2022-02-10 11:24:16 +00:00
}
}
2022-02-10 11:06:37 +00:00
2022-02-10 11:24:16 +00:00
public bool CanUpload()
{
2022-02-10 11:24:16 +00:00
var current = _storageSettingsHelper.DataStoreConsumer(_settingsManager.Load<CdnStorageSettings>());
if (current == null || !current.IsSet || (string.IsNullOrEmpty(current["cnamessl"]) && string.IsNullOrEmpty(current["cname"])))
{
return false;
}
2022-02-10 11:24:16 +00:00
return true;
}
2021-05-18 18:24:22 +00:00
2022-02-10 11:24:16 +00:00
public static void Stop()
{
2022-02-10 12:34:26 +00:00
_tokenSource.Cancel();
2022-02-10 11:24:16 +00:00
}
2020-09-29 13:02:47 +00:00
2022-02-10 11:24:16 +00:00
public UploadOperationProgress GetProgress(int tenantId)
{
2022-02-10 12:34:26 +00:00
lock (_locker)
2020-09-29 13:02:47 +00:00
{
2022-02-10 11:24:16 +00:00
var key = typeof(UploadOperationProgress).FullName + tenantId;
Merge branch 'feature/backend-refactor' into feature/warnings # Conflicts: # common/ASC.Common/Threading/DistributedTaskProgress.cs # common/ASC.Common/Threading/DistributedTaskQueue.cs # common/ASC.Core.Common/Configuration/AmiPublicDnsSyncService.cs # common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs # common/ASC.Core.Common/Notify/Model/NotifyClientImpl.cs # common/ASC.Core.Common/Security/EmailValidationKeyProvider.cs # common/ASC.Data.Reassigns/QueueWorker.cs # common/ASC.Data.Reassigns/ReassignProgressItem.cs # common/ASC.Data.Reassigns/RemoveProgressItem.cs # common/ASC.Data.Storage/StaticUploader.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FileDto.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FolderDto.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs # products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxDaoBase.cs # products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/ThirdPartyAppHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/docusignhandler.ashx.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceTracker.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileDownloadOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileOperation.cs # products/ASC.Files/Core/Utils/EntryManager.cs # web/ASC.Web.Core/CollaboratorSettings.cs # web/ASC.Web.Core/CustomNavigationSettings.cs # web/ASC.Web.Core/EmailActivationSettings.cs # web/ASC.Web.Core/Notify/NotifyConfiguration.cs # web/ASC.Web.Core/Notify/SpamEmailSettings.cs # web/ASC.Web.Core/Notify/StudioNotifyHelper.cs # web/ASC.Web.Core/Notify/StudioNotifyService.cs # web/ASC.Web.Core/Notify/StudioNotifyServiceSender.cs # web/ASC.Web.Core/Notify/StudioPeriodicNotify.cs # web/ASC.Web.Core/Notify/StudioWhatsNewNotify.cs # web/ASC.Web.Core/PersonalSettings.cs # web/ASC.Web.Core/PrivacyRoomSettings.cs # web/ASC.Web.Core/PromotionsSettings.cs # web/ASC.Web.Core/QuotaSync.cs # web/ASC.Web.Core/Sms/SmsSender.cs # web/ASC.Web.Core/Sms/StudioSmsNotificationSettings.cs # web/ASC.Web.Core/StudioAdminMessageSettings.cs # web/ASC.Web.Core/StudioDefaultPageSettings.cs # web/ASC.Web.Core/StudioTrustedDomainSettings.cs # web/ASC.Web.Core/TariffSettings.cs # web/ASC.Web.Core/Tfa/TfaAppAuthSettings.cs # web/ASC.Web.Core/Tfa/TfaAppUserSettings.cs # web/ASC.Web.Core/TipsSettings.cs # web/ASC.Web.Core/Users/CustomNamingPeople.cs # web/ASC.Web.Core/Users/UserHelpTourSettings.cs # web/ASC.Web.Core/Users/UserPhotoManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs # web/ASC.Web.Core/Utility/ColorThemesSettings.cs # web/ASC.Web.Core/Utility/PasswordSettings.cs # web/ASC.Web.Core/Utility/Settings/TenantAccessSettings.cs # web/ASC.Web.Core/Utility/Settings/WebItemSettings.cs # web/ASC.Web.Core/Utility/Settings/WizardSettings.cs # web/ASC.Web.Core/Utility/TenantExtra.cs # web/ASC.Web.Core/WhiteLabel/AdditionalWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/CompanyWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs
2022-04-14 12:21:06 +00:00
return _queue.PeekTask<UploadOperationProgress>(key);
2022-02-10 11:24:16 +00:00
}
}
2020-10-02 07:19:02 +00:00
2022-02-10 11:24:16 +00:00
private static string GetCacheKey(string tenantId, string path)
{
return typeof(UploadOperation).FullName + tenantId + path;
}
}
2022-03-25 09:23:28 +00:00
[Scope]
2022-02-10 11:24:16 +00:00
public class UploadOperation
{
public string Result { get; private set; }
2022-05-26 09:01:54 +00:00
private readonly ILogger<UploadOperation> _logger;
2022-03-25 09:23:28 +00:00
private readonly TenantManager _tenantManager;
private readonly SecurityContext _securityContext;
private readonly SettingsManager _settingsManager;
private readonly StorageSettingsHelper _storageSettingsHelper;
2022-02-10 11:24:16 +00:00
2022-03-25 09:23:28 +00:00
public UploadOperation(
ILogger<UploadOperation> logger,
2022-03-25 09:23:28 +00:00
TenantManager tenantManager,
SecurityContext securityContext,
SettingsManager settingsManager,
StorageSettingsHelper storageSettingsHelper)
2022-02-10 11:24:16 +00:00
{
_logger = logger;
2022-02-10 11:24:16 +00:00
Result = string.Empty;
2022-03-25 09:23:28 +00:00
_tenantManager = tenantManager;
_securityContext = securityContext;
_settingsManager = settingsManager;
_storageSettingsHelper = storageSettingsHelper;
2022-02-10 11:24:16 +00:00
}
2022-03-25 09:23:28 +00:00
public async Task<string> DoJobAsync(int tenantId, string path, string mappedPath)
2022-02-10 11:24:16 +00:00
{
try
2020-09-29 13:02:47 +00:00
{
2022-03-25 09:23:28 +00:00
path = path.TrimStart('/');
var tenant = _tenantManager.GetTenant(tenantId);
_tenantManager.SetCurrentTenant(tenant);
_securityContext.AuthenticateMeWithoutCookie(tenant.OwnerId);
2020-09-29 13:02:47 +00:00
2022-03-25 09:23:28 +00:00
var dataStore = _storageSettingsHelper.DataStore(_settingsManager.Load<CdnStorageSettings>());
2022-02-10 11:24:16 +00:00
2022-03-25 09:23:28 +00:00
if (File.Exists(mappedPath))
2022-02-10 11:24:16 +00:00
{
2022-03-25 09:23:28 +00:00
if (!await dataStore.IsFileAsync(path))
2022-02-10 11:24:16 +00:00
{
2022-03-25 09:23:28 +00:00
using var stream = File.OpenRead(mappedPath);
await dataStore.SaveAsync(path, stream);
2022-02-10 11:24:16 +00:00
}
2022-03-25 09:23:28 +00:00
var uri = await dataStore.GetInternalUriAsync("", path, TimeSpan.Zero, null);
2022-04-14 19:42:15 +00:00
Result = uri.AbsoluteUri.ToLower();
2022-05-20 15:24:41 +00:00
_logger.DebugUploadFile(Result);
2022-02-10 11:24:16 +00:00
return Result;
2020-09-29 13:02:47 +00:00
}
2022-02-10 11:24:16 +00:00
}
catch (Exception e)
{
2022-05-20 15:24:41 +00:00
_logger.ErrorUploadOperation(e);
2022-02-10 11:24:16 +00:00
}
return null;
}
}
[Transient]
public class UploadOperationProgress : DistributedTaskProgress
{
public int TenantId { get; }
private readonly string _relativePath;
private readonly string _mappedPath;
private readonly IEnumerable<string> _directoryFiles;
private readonly IServiceProvider _serviceProvider;
public UploadOperationProgress(IServiceProvider serviceProvider, string key, int tenantId, string relativePath, string mappedPath)
{
_serviceProvider = serviceProvider;
Id = key;
Status = DistributedTaskStatus.Created;
TenantId = tenantId;
_relativePath = relativePath;
_mappedPath = mappedPath;
2022-04-14 19:42:15 +00:00
const string extensions = ".png|.jpeg|.jpg|.gif|.ico|.swf|.mp3|.ogg|.eot|.svg|.ttf|.woff|.woff2|.css|.less|.js";
2022-02-10 11:24:16 +00:00
var extensionsArray = extensions.Split('|');
_directoryFiles = Directory.GetFiles(mappedPath, "*", SearchOption.AllDirectories)
.Where(r => extensionsArray.Contains(Path.GetExtension(r)))
.ToList();
StepCount = _directoryFiles.Count();
}
protected override void DoJob()
{
using var scope = _serviceProvider.CreateScope();
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
var staticUploader = scope.ServiceProvider.GetService<StaticUploader>();
var tenant = tenantManager.GetTenant(TenantId);
tenantManager.SetCurrentTenant(tenant);
2020-09-29 13:02:47 +00:00
2022-02-10 11:24:16 +00:00
tenant.SetStatus(TenantStatus.Migrating);
tenantManager.SaveTenant(tenant);
PublishChanges();
foreach (var file in _directoryFiles)
{
var filePath = file.Substring(_mappedPath.TrimEnd('/').Length);
2022-03-25 09:23:28 +00:00
staticUploader.UploadFileAsync(CrossPlatform.PathCombine(_relativePath, filePath), file, (res) => StepDone()).Wait();
2020-09-29 13:02:47 +00:00
}
2022-02-10 11:24:16 +00:00
tenant.SetStatus(TenantStatus.Active);
tenantManager.SaveTenant(tenant);
}
2022-02-10 11:24:16 +00:00
public object Clone()
{
return MemberwiseClone();
}
2022-04-12 19:04:10 +00:00
}