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

305 lines
11 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.IO;
using System.Linq;
using System.Threading;
2020-02-17 08:58:14 +00:00
using System.Threading.Tasks;
using ASC.Common;
2019-06-04 14:43:20 +00:00
using ASC.Common.Caching;
using ASC.Common.Logging;
using ASC.Common.Threading.Progress;
using ASC.Core;
2020-02-17 08:58:14 +00:00
using ASC.Core.Common.Settings;
2019-06-04 14:43:20 +00:00
using ASC.Core.Tenants;
2020-02-17 08:58:14 +00:00
using ASC.Data.Storage.Configuration;
2019-11-06 15:03:09 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
2019-06-04 14:43:20 +00:00
namespace ASC.Data.Storage
{
public class StaticUploader
{
private static readonly TaskScheduler Scheduler;
private static readonly CancellationTokenSource TokenSource;
private static readonly ICache Cache;
2019-11-06 15:03:09 +00:00
private static readonly object Locker;
public IServiceProvider ServiceProvider { get; }
2020-02-17 08:58:14 +00:00
public TenantManager TenantManager { get; }
public SettingsManager SettingsManager { get; }
public StorageSettingsHelper StorageSettingsHelper { get; }
2019-06-04 14:43:20 +00:00
static StaticUploader()
{
Scheduler = new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, 4).ConcurrentScheduler;
2019-06-04 14:43:20 +00:00
Cache = AscCache.Memory;
Locker = new object();
TokenSource = new CancellationTokenSource();
}
2019-11-06 15:03:09 +00:00
2020-02-17 08:58:14 +00:00
public StaticUploader(
IServiceProvider serviceProvider,
TenantManager tenantManager,
SettingsManager settingsManager,
StorageSettingsHelper storageSettingsHelper)
2019-11-06 15:03:09 +00:00
{
ServiceProvider = serviceProvider;
2020-02-17 08:58:14 +00:00
TenantManager = tenantManager;
SettingsManager = settingsManager;
StorageSettingsHelper = storageSettingsHelper;
2019-11-06 15:03:09 +00:00
}
2019-06-04 14:43:20 +00:00
2019-09-13 11:18:27 +00:00
public string UploadFile(string relativePath, string mappedPath, Action<string> onComplete = null)
2019-06-04 14:43:20 +00:00
{
if (TokenSource.Token.IsCancellationRequested) return null;
if (!CanUpload()) return null;
if (!File.Exists(mappedPath)) return null;
2019-09-17 08:07:46 +00:00
var tenantId = TenantManager.GetCurrentTenant().TenantId;
2019-06-04 14:43:20 +00:00
UploadOperation uploadOperation;
var key = GetCacheKey(tenantId.ToString(), relativePath);
lock (Locker)
{
uploadOperation = Cache.Get<UploadOperation>(key);
if (uploadOperation != null)
{
return !string.IsNullOrEmpty(uploadOperation.Result) ? uploadOperation.Result : string.Empty;
}
2019-09-13 11:18:27 +00:00
uploadOperation = new UploadOperation(ServiceProvider, tenantId, relativePath, mappedPath);
2019-06-04 14:43:20 +00:00
Cache.Insert(key, uploadOperation, DateTime.MaxValue);
}
2019-11-06 15:03:09 +00:00
uploadOperation.DoJob();
2019-08-15 15:08:40 +00:00
onComplete?.Invoke(uploadOperation.Result);
2019-06-04 14:43:20 +00:00
return uploadOperation.Result;
}
2019-09-13 11:18:27 +00:00
public Task<string> UploadFileAsync(string relativePath, string mappedPath, Action<string> onComplete = null)
2019-06-04 14:43:20 +00:00
{
2019-09-17 08:07:46 +00:00
var tenantId = TenantManager.GetCurrentTenant().TenantId;
2019-06-04 14:43:20 +00:00
var task = new Task<string>(() =>
2019-11-06 15:03:09 +00:00
{
using var scope = ServiceProvider.CreateScope();
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
tenantManager.SetCurrentTenant(tenantId);
2019-09-17 08:07:46 +00:00
var staticUploader = scope.ServiceProvider.GetService<StaticUploader>();
return staticUploader.UploadFile(relativePath, mappedPath, onComplete);
2019-06-04 14:43:20 +00:00
}, TaskCreationOptions.LongRunning);
task.ConfigureAwait(false);
task.Start(Scheduler);
return task;
}
2019-09-09 12:56:33 +00:00
public async void UploadDir(string relativePath, string mappedPath)
2019-06-04 14:43:20 +00:00
{
if (!CanUpload()) return;
if (!Directory.Exists(mappedPath)) return;
2019-09-17 08:07:46 +00:00
var tenant = TenantManager.GetCurrentTenant();
2019-06-04 14:43:20 +00:00
var key = typeof(UploadOperationProgress).FullName + tenant.TenantId;
UploadOperationProgress uploadOperation;
lock (Locker)
{
uploadOperation = Cache.Get<UploadOperationProgress>(key);
2019-11-06 15:03:09 +00:00
if (uploadOperation != null) return;
2019-06-04 14:43:20 +00:00
2019-09-13 11:18:27 +00:00
uploadOperation = new UploadOperationProgress(this, relativePath, mappedPath);
2019-06-04 14:43:20 +00:00
Cache.Insert(key, uploadOperation, DateTime.MaxValue);
}
tenant.SetStatus(TenantStatus.Migrating);
2019-09-17 08:07:46 +00:00
TenantManager.SaveTenant(tenant);
2019-06-04 14:43:20 +00:00
await uploadOperation.RunJobAsync();
tenant.SetStatus(Core.Tenants.TenantStatus.Active);
2019-09-17 08:07:46 +00:00
TenantManager.SaveTenant(tenant);
2019-06-04 14:43:20 +00:00
}
2019-09-13 11:18:27 +00:00
public bool CanUpload()
2019-06-04 14:43:20 +00:00
{
var current = StorageSettingsHelper.DataStoreConsumer(SettingsManager.Load<CdnStorageSettings>());
2019-06-04 14:43:20 +00:00
if (current == null || !current.IsSet || (string.IsNullOrEmpty(current["cnamessl"]) && string.IsNullOrEmpty(current["cname"])))
{
return false;
}
return true;
}
public static void Stop()
{
TokenSource.Cancel();
}
public static UploadOperationProgress GetProgress(int tenantId)
{
lock (Locker)
{
var key = typeof(UploadOperationProgress).FullName + tenantId;
return Cache.Get<UploadOperationProgress>(key);
}
}
private static string GetCacheKey(string tenantId, string path)
{
return typeof(UploadOperation).FullName + tenantId + path;
}
}
public class UploadOperation
{
2019-10-17 15:55:35 +00:00
private readonly ILog Log;
2019-06-04 14:43:20 +00:00
private readonly int tenantId;
private readonly string path;
private readonly string mappedPath;
2019-11-06 15:03:09 +00:00
public string Result { get; private set; }
public IServiceProvider ServiceProvider { get; }
2019-09-09 12:56:33 +00:00
public UploadOperation(IServiceProvider serviceProvider, int tenantId, string path, string mappedPath)
2019-11-06 15:03:09 +00:00
{
ServiceProvider = serviceProvider;
Log = ServiceProvider.GetService<IOptionsMonitor<ILog>>().CurrentValue;
2019-06-04 14:43:20 +00:00
this.tenantId = tenantId;
this.path = path.TrimStart('/');
this.mappedPath = mappedPath;
Result = string.Empty;
}
public string DoJob()
{
try
2019-11-06 15:03:09 +00:00
{
using var scope = ServiceProvider.CreateScope();
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
2019-09-17 08:07:46 +00:00
var tenant = tenantManager.GetTenant(tenantId);
2019-11-06 15:03:09 +00:00
tenantManager.SetCurrentTenant(tenant);
2020-02-17 08:58:14 +00:00
var SecurityContext = scope.ServiceProvider.GetService<SecurityContext>();
var SettingsManager = scope.ServiceProvider.GetService<SettingsManager>();
var StorageSettingsHelper = scope.ServiceProvider.GetService<StorageSettingsHelper>();
2019-09-20 15:53:27 +00:00
SecurityContext.AuthenticateMe(tenant.OwnerId);
2019-06-04 14:43:20 +00:00
var dataStore = StorageSettingsHelper.DataStore(SettingsManager.Load<CdnStorageSettings>());
2019-06-04 14:43:20 +00:00
if (File.Exists(mappedPath))
{
if (!dataStore.IsFile(path))
{
2019-11-06 15:03:09 +00:00
using var stream = File.OpenRead(mappedPath);
2019-08-15 15:08:40 +00:00
dataStore.Save(path, stream);
2019-06-04 14:43:20 +00:00
}
Result = dataStore.GetInternalUri("", path, TimeSpan.Zero, null).AbsoluteUri.ToLower();
2019-10-17 15:55:35 +00:00
Log.DebugFormat("UploadFile {0}", Result);
2019-06-04 14:43:20 +00:00
return Result;
}
}
catch (Exception e)
{
Log.Error(e);
}
return null;
}
}
public class UploadOperationProgress : ProgressBase
{
private readonly string relativePath;
private readonly string mappedPath;
2019-11-06 15:03:09 +00:00
private readonly IEnumerable<string> directoryFiles;
public IServiceProvider ServiceProvider { get; }
public StaticUploader StaticUploader { get; }
2019-09-13 11:18:27 +00:00
public UploadOperationProgress(StaticUploader staticUploader, string relativePath, string mappedPath)
2019-11-06 15:03:09 +00:00
{
StaticUploader = staticUploader;
2019-06-04 14:43:20 +00:00
this.relativePath = relativePath;
this.mappedPath = mappedPath;
var extensions = ".png|.jpeg|.jpg|.gif|.ico|.swf|.mp3|.ogg|.eot|.svg|.ttf|.woff|.woff2|.css|.less|.js";
var extensionsArray = extensions.Split('|');
directoryFiles = Directory.GetFiles(mappedPath, "*", SearchOption.AllDirectories)
.Where(r => extensionsArray.Contains(Path.GetExtension(r)))
.ToList();
StepCount = directoryFiles.Count();
}
protected override async Task DoJobAsync()
{
var tasks = new List<Task>();
foreach (var file in directoryFiles)
{
var filePath = file.Substring(mappedPath.TrimEnd('/').Length);
2019-09-13 11:18:27 +00:00
tasks.Add(StaticUploader.UploadFileAsync(Path.Combine(relativePath, filePath), file, (res) => StepDone()));
2019-06-04 14:43:20 +00:00
}
await Task.WhenAll(tasks);
}
protected override void DoJob()
{
foreach (var file in directoryFiles)
{
var filePath = file.Substring(mappedPath.TrimEnd('/').Length);
2019-09-13 11:18:27 +00:00
StaticUploader.UploadFileAsync(Path.Combine(relativePath, filePath), file, (res) => StepDone()).Wait();
2019-06-04 14:43:20 +00:00
}
}
2019-11-06 15:03:09 +00:00
}
public static class StaticUploaderExtension
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddStaticUploaderService(this DIHelper services)
2019-11-06 15:03:09 +00:00
{
2020-07-17 10:52:28 +00:00
if (services.TryAddScoped<StaticUploader>())
{
return services
.AddTenantManagerService()
.AddCdnStorageSettingsService();
}
return services;
2019-11-06 15:03:09 +00:00
}
2019-06-04 14:43:20 +00:00
}
}