Files: removed oformservice

This commit is contained in:
pavelbannov 2022-08-23 16:34:54 +03:00
parent 53ee8e9e15
commit 34232bc735
5 changed files with 21 additions and 117 deletions

View File

@ -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"
}
},

View File

@ -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<OFormRequestManager> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly TempPath _tempPath;
private readonly SemaphoreSlim _semaphoreSlim;
private OFromRequestData _data;
private readonly JsonSerializerOptions _options;
public OFormRequestManager(
ILogger<OFormRequestManager> logger,
ConfigurationExtension configuration,
IHttpClientFactory httpClientFactory,
TempPath tempPath)
{
_semaphoreSlim = new SemaphoreSlim(1);
_configuration = configuration.GetSetting<OFormSettings>("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<OFromRequestData>(await response.Content.ReadAsStringAsync(combined.Token), options);
}
catch (Exception)
{
}
finally
{
_semaphoreSlim.Release();
}
PropertyNameCaseInsensitive = true
};
}
public async Task<FileStream> 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<OFromRequestData>(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();
}
}

View File

@ -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<OFormSettings>("files:oform").Period);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await _oFormRequestManager.Init(stoppingToken);
await Task.Delay(_formPeriod, stoppingToken);
}
}
}

View File

@ -28,5 +28,5 @@ namespace ASC.Files.Core.Services.OFormService;
public class OFromRequestData
{
public IEnumerable<OFromData> Data { get; set; }
public OFromData Data { get; set; }
}

View File

@ -53,9 +53,7 @@ public class Startup : BaseStartup
DIHelper.TryAdd<ChunkedUploaderHandlerService>();
DIHelper.TryAdd<DocuSignHandlerService>();
DIHelper.TryAdd<ThirdPartyAppHandlerService>();
DIHelper.TryAdd<OFormService>();
services.AddHostedService<OFormService>();
NotifyConfigurationExtension.Register(DIHelper);
}