diff --git a/config/appsettings.json b/config/appsettings.json index c914bff490..404a717870 100644 --- a/config/appsettings.json +++ b/config/appsettings.json @@ -95,8 +95,7 @@ "viewed-media": [ ".aac", ".flac", ".m4a", ".mp3", ".oga", ".ogg", ".wav", ".f4v", ".m4v", ".mov", ".mp4", ".ogv", ".webm" ], "index": [ ".pptx", ".xlsx", ".docx" ], "oform": { - "url": "https://cmsoforms.onlyoffice.com/api/oforms?populate=*&locale=all", - "period": 60, + "url": "https://cmsoforms.onlyoffice.com/api/oforms/", "ext": ".oform" } }, diff --git a/products/ASC.Files/Core/Services/OFormService/OFormRequestManager.cs b/products/ASC.Files/Core/Services/OFormService/OFormRequestManager.cs index c15f797d98..4dfe9f2497 100644 --- a/products/ASC.Files/Core/Services/OFormService/OFormRequestManager.cs +++ b/products/ASC.Files/Core/Services/OFormService/OFormRequestManager.cs @@ -29,99 +29,55 @@ using File = System.IO.File; namespace ASC.Files.Core.Services.OFormService; [Singletone] -public class OFormRequestManager : IDisposable +public class OFormRequestManager { private readonly OFormSettings _configuration; + private readonly ILogger _logger; private readonly IHttpClientFactory _httpClientFactory; private readonly TempPath _tempPath; - private readonly SemaphoreSlim _semaphoreSlim; - private OFromRequestData _data; + private readonly JsonSerializerOptions _options; public OFormRequestManager( + ILogger logger, ConfigurationExtension configuration, IHttpClientFactory httpClientFactory, TempPath tempPath) { - _semaphoreSlim = new SemaphoreSlim(1); _configuration = configuration.GetSetting("files:oform"); + _logger = logger; _httpClientFactory = httpClientFactory; _tempPath = tempPath; - } - public async Task Init(CancellationToken cancellationToken) - { - await _semaphoreSlim.WaitAsync(); - - try + _options = new JsonSerializerOptions { - using var httpClient = _httpClientFactory.CreateClient(); - using var response = await httpClient.GetAsync(_configuration.Url); - - if (response.StatusCode != HttpStatusCode.OK) - { - return; - } - - var options = new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true - }; - - using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); - using var combined = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cts.Token); - _data = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(combined.Token), options); - } - catch (Exception) - { - - } - finally - { - _semaphoreSlim.Release(); - } - + PropertyNameCaseInsensitive = true + }; } public async Task Get(int id) { - await _semaphoreSlim.WaitAsync(TimeSpan.FromSeconds(_configuration.Period)); - try { - if (_data == null) throw new Exception("not found"); - - var item = _data.Data.FirstOrDefault(r => r.Id == id); - if (item == null) throw new Exception("not found"); - - var file = item.Attributes.File.Data.FirstOrDefault(f => f.Attributes.Ext == _configuration.Ext); - if (file == null) throw new Exception("not found"); - + using var httpClient = _httpClientFactory.CreateClient(); + using var response = await httpClient.GetAsync($"{_configuration.Url}{id}?populate[file_oform][fields]=url&populate[file_oform][fields]=name&populate[file_oform][fields]=ext&populate[file_oform][filters][url][$endsWith]={_configuration.Ext}"); + var data = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), _options); + var file = data.Data.Attributes.File.Data.FirstOrDefault(f => f.Attributes.Ext == _configuration.Ext); var filePath = Path.Combine(_tempPath.GetTempPath(), file.Attributes.Name); if (!File.Exists(filePath)) { - await DownloadAndSave(file, filePath); + using var streamResponse = await httpClient.GetAsync(file.Attributes.Url); + using var stream = await streamResponse.Content.ReadAsStreamAsync(); + using var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite, System.IO.FileShare.Read); + await stream.CopyToAsync(fileStream); } return File.OpenRead(filePath); } - finally + catch (Exception e) { - _semaphoreSlim.Release(); + _logger.ErrorWithException(e); + throw; } } - - private async Task DownloadAndSave(OFromFileData fileData, string filePath) - { - using var httpClient = _httpClientFactory.CreateClient(); - using var response = await httpClient.GetAsync(fileData.Attributes.Url); - using var stream = await response.Content.ReadAsStreamAsync(); - using var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite, System.IO.FileShare.Read); - await stream.CopyToAsync(fileStream); - } - - public void Dispose() - { - _semaphoreSlim?.Dispose(); - } } diff --git a/products/ASC.Files/Core/Services/OFormService/OFormService.cs b/products/ASC.Files/Core/Services/OFormService/OFormService.cs deleted file mode 100644 index 22fb40596d..0000000000 --- a/products/ASC.Files/Core/Services/OFormService/OFormService.cs +++ /dev/null @@ -1,49 +0,0 @@ -// (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 - -namespace ASC.Files.Core.Services.OFormService; - -[Singletone] -public sealed class OFormService : BackgroundService -{ - private readonly TimeSpan _formPeriod; - private readonly OFormRequestManager _oFormRequestManager; - - public OFormService(OFormRequestManager oFormRequestManager, ConfigurationExtension configurationExtension) - { - _oFormRequestManager = oFormRequestManager; - _formPeriod = TimeSpan.FromSeconds(configurationExtension.GetSetting("files:oform").Period); - } - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - while (!stoppingToken.IsCancellationRequested) - { - await _oFormRequestManager.Init(stoppingToken); - await Task.Delay(_formPeriod, stoppingToken); - } - } -} diff --git a/products/ASC.Files/Core/Services/OFormService/OFromRequestData.cs b/products/ASC.Files/Core/Services/OFormService/OFromRequestData.cs index 192bc8470b..0f5488b9dc 100644 --- a/products/ASC.Files/Core/Services/OFormService/OFromRequestData.cs +++ b/products/ASC.Files/Core/Services/OFormService/OFromRequestData.cs @@ -28,5 +28,5 @@ namespace ASC.Files.Core.Services.OFormService; public class OFromRequestData { - public IEnumerable Data { get; set; } + public OFromData Data { get; set; } } diff --git a/products/ASC.Files/Server/Startup.cs b/products/ASC.Files/Server/Startup.cs index a609dc80f9..7138633c48 100644 --- a/products/ASC.Files/Server/Startup.cs +++ b/products/ASC.Files/Server/Startup.cs @@ -53,9 +53,7 @@ public class Startup : BaseStartup DIHelper.TryAdd(); DIHelper.TryAdd(); DIHelper.TryAdd(); - DIHelper.TryAdd(); - services.AddHostedService(); NotifyConfigurationExtension.Register(DIHelper); }