Merge branch 'develop' into bugfix/room-logo

This commit is contained in:
Alexey Safronov 2022-08-23 18:05:14 +03:00 committed by GitHub
commit 0e9099017a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 91 additions and 168 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

@ -67,7 +67,7 @@ public class FileShareDtoHelper
_employeeWraperFullHelper = employeeWraperFullHelper;
}
public FileShareDto Get(AceWrapper aceWrapper)
public async Task<FileShareDto> Get(AceWrapper aceWrapper)
{
var result = new FileShareDto
{
@ -93,7 +93,7 @@ public class FileShareDtoHelper
}
else
{
result.SharedTo = _employeeWraperFullHelper.GetFull(_userManager.GetUsers(aceWrapper.SubjectId));
result.SharedTo = await _employeeWraperFullHelper.GetFull(_userManager.GetUsers(aceWrapper.SubjectId));
}
result.Access = aceWrapper.Share;

View File

@ -24,104 +24,51 @@
// 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
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;
_options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
}
public async Task Init(CancellationToken cancellationToken)
public async Task<Stream> Get(int id)
{
await _semaphoreSlim.WaitAsync();
try
{
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();
}
}
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 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);
}
return File.OpenRead(filePath);
var streamResponse = await httpClient.GetAsync(file.Attributes.Url);
return await streamResponse.Content.ReadAsStreamAsync();
}
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

@ -88,9 +88,12 @@ public abstract class SecutiryController<T> : ApiControllerBase
/// <param name="fileId">File ID</param>
/// <returns>Shared file information</returns>
[HttpGet("file/{fileId}/share")]
public Task<IEnumerable<FileShareDto>> GetFileSecurityInfoAsync(T fileId)
public async IAsyncEnumerable<FileShareDto> GetFileSecurityInfoAsync(T fileId)
{
return _securityControllerHelper.GetFileSecurityInfoAsync(fileId);
await foreach (var s in _securityControllerHelper.GetFileSecurityInfoAsync(fileId))
{
yield return s;
}
}
/// <summary>
@ -101,9 +104,12 @@ public abstract class SecutiryController<T> : ApiControllerBase
/// <category>Sharing</category>
/// <returns>Shared folder information</returns>
[HttpGet("folder/{folderId}/share")]
public Task<IEnumerable<FileShareDto>> GetFolderSecurityInfoAsync(T folderId)
public async IAsyncEnumerable<FileShareDto> GetFolderSecurityInfoAsync(T folderId)
{
return _securityControllerHelper.GetFolderSecurityInfoAsync(folderId);
await foreach (var s in _securityControllerHelper.GetFolderSecurityInfoAsync(folderId))
{
yield return s;
}
}
[HttpPut("{fileId}/setacelink")]
@ -126,9 +132,12 @@ public abstract class SecutiryController<T> : ApiControllerBase
/// </remarks>
/// <returns>Shared file information</returns>
[HttpPut("file/{fileId}/share")]
public Task<IEnumerable<FileShareDto>> SetFileSecurityInfoAsync(T fileId, SecurityInfoRequestDto inDto)
public async IAsyncEnumerable<FileShareDto> SetFileSecurityInfoAsync(T fileId, SecurityInfoRequestDto inDto)
{
return _securityControllerHelper.SetFileSecurityInfoAsync(fileId, inDto.Share, inDto.Notify, inDto.SharingMessage);
await foreach (var s in _securityControllerHelper.SetSecurityInfoAsync(new List<T> { fileId }, new List<T>(), inDto.Share, inDto.Notify, inDto.SharingMessage))
{
yield return s;
}
}
/// <summary>
@ -145,9 +154,12 @@ public abstract class SecutiryController<T> : ApiControllerBase
/// <category>Sharing</category>
/// <returns>Shared folder information</returns>
[HttpPut("folder/{folderId}/share")]
public Task<IEnumerable<FileShareDto>> SetFolderSecurityInfoAsync(T folderId, SecurityInfoRequestDto inDto)
public async IAsyncEnumerable<FileShareDto> SetFolderSecurityInfoAsync(T folderId, SecurityInfoRequestDto inDto)
{
return _securityControllerHelper.SetFolderSecurityInfoAsync(folderId, inDto.Share, inDto.Notify, inDto.SharingMessage);
await foreach (var s in _securityControllerHelper.SetSecurityInfoAsync(new List<T>(), new List<T> { folderId }, inDto.Share, inDto.Notify, inDto.SharingMessage))
{
yield return s;
}
}
[HttpGet("file/{fileId}/publickeys")]
@ -201,16 +213,18 @@ public class SecutiryControllerCommon : ApiControllerBase
}
[HttpPost("share")]
public async Task<IEnumerable<FileShareDto>> GetSecurityInfoAsync(BaseBatchRequestDto inDto)
public async IAsyncEnumerable<FileShareDto> GetSecurityInfoAsync(BaseBatchRequestDto inDto)
{
var (folderIntIds, folderStringIds) = FileOperationsManager.GetIds(inDto.FolderIds);
var (fileIntIds, fileStringIds) = FileOperationsManager.GetIds(inDto.FileIds);
var result = new List<FileShareDto>();
result.AddRange(await _securityControllerHelperInt.GetSecurityInfoAsync(fileIntIds, folderIntIds));
result.AddRange(await _securityControllerHelperString.GetSecurityInfoAsync(fileStringIds, folderStringIds));
return result;
var internalIds = _securityControllerHelperInt.GetSecurityInfoAsync(fileIntIds, folderIntIds);
var thirdpartyIds = _securityControllerHelperString.GetSecurityInfoAsync(fileStringIds, folderStringIds);
await foreach (var r in internalIds.Concat(thirdpartyIds))
{
yield return r;
}
}
/// <summary>
@ -235,15 +249,17 @@ public class SecutiryControllerCommon : ApiControllerBase
[HttpPut("share")]
public async Task<IEnumerable<FileShareDto>> SetSecurityInfoAsync(SecurityInfoRequestDto inDto)
public async IAsyncEnumerable<FileShareDto> SetSecurityInfoAsync(SecurityInfoRequestDto inDto)
{
var (folderIntIds, folderStringIds) = FileOperationsManager.GetIds(inDto.FolderIds);
var (fileIntIds, fileStringIds) = FileOperationsManager.GetIds(inDto.FileIds);
var result = new List<FileShareDto>();
result.AddRange(await _securityControllerHelperInt.SetSecurityInfoAsync(fileIntIds, folderIntIds, inDto.Share, inDto.Notify, inDto.SharingMessage));
result.AddRange(await _securityControllerHelperString.SetSecurityInfoAsync(fileStringIds, folderStringIds, inDto.Share, inDto.Notify, inDto.SharingMessage));
var internalIds = _securityControllerHelperInt.SetSecurityInfoAsync(fileIntIds, folderIntIds, inDto.Share, inDto.Notify, inDto.SharingMessage);
var thirdpartyIds = _securityControllerHelperString.SetSecurityInfoAsync(fileStringIds, folderStringIds, inDto.Share, inDto.Notify, inDto.SharingMessage);
return result;
await foreach (var s in internalIds.Concat(thirdpartyIds))
{
yield return s;
}
}
}

View File

@ -320,16 +320,24 @@ public abstract class VirtualRoomsController<T> : ApiControllerBase
/// Room security info
/// </returns>
[HttpPut("rooms/{id}/share")]
public Task<IEnumerable<FileShareDto>> SetRoomSecurityAsync(T id, SecurityInfoRequestDto inDto)
public async IAsyncEnumerable<FileShareDto> SetRoomSecurityAsync(T id, SecurityInfoRequestDto inDto)
{
ErrorIfNotDocSpace();
IAsyncEnumerable<FileShareDto> result;
if (!string.IsNullOrEmpty(inDto.Key))
{
return SetRoomSecurityByLinkAsync(id, _authContext.CurrentAccount.ID, inDto.Access, inDto.Key);
result = SetRoomSecurityByLinkAsync(id, _authContext.CurrentAccount.ID, inDto.Access, inDto.Key);
}
else
{
result = _securityControllerHelper.SetFolderSecurityInfoAsync(id, inDto.Share, inDto.Notify, inDto.SharingMessage);
}
await foreach (var r in result)
{
yield return r;
}
return _securityControllerHelper.SetFolderSecurityInfoAsync(id, inDto.Share, inDto.Notify, inDto.SharingMessage);
}
/// <summary>
@ -568,7 +576,7 @@ public abstract class VirtualRoomsController<T> : ApiControllerBase
}
}
private async Task<IEnumerable<FileShareDto>> SetRoomSecurityByLinkAsync(T id, Guid userId, FileShare access, string key)
private async IAsyncEnumerable<FileShareDto> SetRoomSecurityByLinkAsync(T id, Guid userId, FileShare access, string key)
{
var result = _emailValidationKeyProvider.ValidateEmailKey(string.Empty + ConfirmType.LinkInvite + ((int)EmployeeType.User + (int)access + id.ToString()), key,
_emailValidationKeyProvider.ValidEmailKeyInterval);
@ -584,7 +592,10 @@ public abstract class VirtualRoomsController<T> : ApiControllerBase
Access = access
};
return await _securityControllerHelper.SetFolderSecurityInfoAsync(id, new[] { share }, false, null, true);
await foreach (var s in _securityControllerHelper.SetFolderSecurityInfoAsync(id, new[] { share }, false, null, true))
{
yield return s;
}
}
private async Task ErrorIfNotRights(T id, FileShare share)

View File

@ -95,21 +95,24 @@ public class SecurityControllerHelper<T> : FilesHelperBase<T>
return sharedInfo.Link;
}
public Task<IEnumerable<FileShareDto>> GetFileSecurityInfoAsync(T fileId)
public IAsyncEnumerable<FileShareDto> GetFileSecurityInfoAsync(T fileId)
{
return GetSecurityInfoAsync(new List<T> { fileId }, new List<T> { });
}
public Task<IEnumerable<FileShareDto>> GetFolderSecurityInfoAsync(T folderId)
public IAsyncEnumerable<FileShareDto> GetFolderSecurityInfoAsync(T folderId)
{
return GetSecurityInfoAsync(new List<T> { }, new List<T> { folderId });
}
public async Task<IEnumerable<FileShareDto>> GetSecurityInfoAsync(IEnumerable<T> fileIds, IEnumerable<T> folderIds, bool invite = false)
public async IAsyncEnumerable<FileShareDto> GetSecurityInfoAsync(IEnumerable<T> fileIds, IEnumerable<T> folderIds, bool invite = false)
{
var fileShares = await _fileStorageService.GetSharedInfoAsync(fileIds, folderIds, invite);
return fileShares.Select(_fileShareDtoHelper.Get).ToList();
foreach (var fileShareDto in fileShares)
{
yield return await _fileShareDtoHelper.Get(fileShareDto);
}
}
public async Task<bool> RemoveSecurityInfoAsync(List<T> fileIds, List<T> folderIds)
@ -119,17 +122,12 @@ public class SecurityControllerHelper<T> : FilesHelperBase<T>
return true;
}
public Task<IEnumerable<FileShareDto>> SetFileSecurityInfoAsync(T fileId, IEnumerable<FileShareParams> share, bool notify, string sharingMessage)
{
return SetSecurityInfoAsync(new List<T> { fileId }, new List<T>(), share, notify, sharingMessage);
}
public Task<IEnumerable<FileShareDto>> SetFolderSecurityInfoAsync(T folderId, IEnumerable<FileShareParams> share, bool notify, string sharingMessage, bool invite = false)
public IAsyncEnumerable<FileShareDto> SetFolderSecurityInfoAsync(T folderId, IEnumerable<FileShareParams> share, bool notify, string sharingMessage, bool invite = false)
{
return SetSecurityInfoAsync(new List<T>(), new List<T> { folderId }, share, notify, sharingMessage, invite);
}
public async Task<IEnumerable<FileShareDto>> SetSecurityInfoAsync(IEnumerable<T> fileIds, IEnumerable<T> folderIds, IEnumerable<FileShareParams> share, bool notify, string sharingMessage, bool invite = false)
public async IAsyncEnumerable<FileShareDto> SetSecurityInfoAsync(IEnumerable<T> fileIds, IEnumerable<T> folderIds, IEnumerable<FileShareParams> share, bool notify, string sharingMessage, bool invite = false)
{
if (share != null && share.Any())
{
@ -146,6 +144,9 @@ public class SecurityControllerHelper<T> : FilesHelperBase<T>
await _fileStorageService.SetAceObjectAsync(aceCollection, notify, invite);
}
return await GetSecurityInfoAsync(fileIds, folderIds, invite);
await foreach (var s in GetSecurityInfoAsync(fileIds, folderIds, invite))
{
yield return s;
}
}
}

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);
}