// (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.Api; public class OperationController : ApiControllerBase { private readonly FileOperationDtoHelper _fileOperationDtoHelper; private readonly FileStorageService _fileStorageServiceString; private readonly FileStorageService _fileStorageService; public OperationController( FileOperationDtoHelper fileOperationDtoHelper, FileStorageService fileStorageServiceString, FolderDtoHelper folderDtoHelper, FileDtoHelper fileDtoHelper, FileStorageService fileStorageService) : base(folderDtoHelper, fileDtoHelper) { _fileOperationDtoHelper = fileOperationDtoHelper; _fileStorageServiceString = fileStorageServiceString; _fileStorageService = fileStorageService; } /// /// Start downlaod process of files and folders with ID /// /// Finish file operations /// File ID list for download with convert to format /// File ID list /// Folder ID list /// File operations /// Operation result [HttpPut("fileops/bulkdownload")] public async IAsyncEnumerable BulkDownload(DownloadRequestDto inDto) { var folders = new Dictionary(); var files = new Dictionary(); foreach (var fileId in inDto.FileConvertIds.Where(fileId => !files.ContainsKey(fileId.Key))) { files.Add(fileId.Key, fileId.Value); } foreach (var fileId in inDto.FileIds.Where(fileId => !files.ContainsKey(fileId))) { files.Add(fileId, string.Empty); } foreach (var folderId in inDto.FolderIds.Where(folderId => !folders.ContainsKey(folderId))) { folders.Add(folderId, string.Empty); } foreach (var e in _fileStorageServiceString.BulkDownload(folders, files)) { yield return await _fileOperationDtoHelper.GetAsync(e); } } /// /// Copies all the selected files and folders to the folder with the ID specified in the request /// /// Copy to folder /// File operations /// Destination folder ID /// Folder ID list /// File ID list /// Overwriting behavior: skip(0), overwrite(1) or duplicate(2) /// Delete after finished /// Operation result [HttpPut("fileops/copy")] public async IAsyncEnumerable CopyBatchItems(BatchRequestDto inDto) { foreach (var e in _fileStorageServiceString.MoveOrCopyItems(inDto.FolderIds.ToList(), inDto.FileIds.ToList(), inDto.DestFolderId, inDto.ConflictResolveType, true, inDto.DeleteAfter)) { yield return await _fileOperationDtoHelper.GetAsync(e); } } /// /// Deletes the files and folders with the IDs specified in the request /// /// Folder ID list /// File ID list /// Delete after finished /// Don't move to the Recycle Bin /// Delete files and folders /// File operations /// Operation result [HttpPut("fileops/delete")] public async IAsyncEnumerable DeleteBatchItems(DeleteBatchRequestDto inDto) { var tasks = _fileStorageServiceString.DeleteItems("delete", inDto.FileIds.ToList(), inDto.FolderIds.ToList(), false, inDto.DeleteAfter, inDto.Immediately); foreach (var e in tasks) { yield return await _fileOperationDtoHelper.GetAsync(e); } } /// /// Deletes all files and folders from the recycle bin /// /// Clear recycle bin /// File operations /// Operation result [HttpPut("fileops/emptytrash")] public async IAsyncEnumerable EmptyTrashAsync() { var emptyTrash = await _fileStorageService.EmptyTrashAsync(); foreach (var e in emptyTrash) { yield return await _fileOperationDtoHelper.GetAsync(e); } } /// /// Returns the list of all active file operations /// /// Get file operations list /// File operations /// Operation result [HttpGet("fileops")] public async IAsyncEnumerable GetOperationStatuses() { foreach (var e in _fileStorageServiceString.GetTasksStatuses()) { yield return await _fileOperationDtoHelper.GetAsync(e); } } /// /// Marks all files and folders as read /// /// Mark as read /// File operations /// Operation result [HttpPut("fileops/markasread")] public async IAsyncEnumerable MarkAsRead(BaseBatchRequestDto inDto) { foreach (var e in _fileStorageServiceString.MarkAsRead(inDto.FolderIds.ToList(), inDto.FileIds.ToList())) { yield return await _fileOperationDtoHelper.GetAsync(e); } } /// /// Moves all the selected files and folders to the folder with the ID specified in the request /// /// Move to folder /// File operations /// Destination folder ID /// Folder ID list /// File ID list /// Overwriting behavior: skip(0), overwrite(1) or duplicate(2) /// Delete after finished /// Operation result [HttpPut("fileops/move")] public async IAsyncEnumerable MoveBatchItems(BatchRequestDto inDto) { foreach (var e in _fileStorageServiceString.MoveOrCopyItems(inDto.FolderIds.ToList(), inDto.FileIds.ToList(), inDto.DestFolderId, inDto.ConflictResolveType, false, inDto.DeleteAfter)) { yield return await _fileOperationDtoHelper.GetAsync(e); } } /// /// Checking for conflicts /// /// File operations /// Destination folder ID /// Folder ID list /// File ID list /// Conflicts file ids [HttpGet("fileops/move")] public async IAsyncEnumerable MoveOrCopyBatchCheckAsync([ModelBinder(BinderType = typeof(BatchModelBinder))] BatchRequestDto inDto) { List checkedFiles; List checkedFolders; if (inDto.DestFolderId.ValueKind == JsonValueKind.Number) { (checkedFiles, checkedFolders) = await _fileStorageServiceString.MoveOrCopyFilesCheckAsync(inDto.FileIds.ToList(), inDto.FolderIds.ToList(), inDto.DestFolderId.GetInt32()); } else { (checkedFiles, checkedFolders) = await _fileStorageServiceString.MoveOrCopyFilesCheckAsync(inDto.FileIds.ToList(), inDto.FolderIds.ToList(), inDto.DestFolderId.GetString()); } var entries = await _fileStorageServiceString.GetItemsAsync(checkedFiles.OfType().Select(Convert.ToInt32), checkedFiles.OfType().Select(Convert.ToInt32), FilterType.FilesOnly, false, "", ""); entries.AddRange(await _fileStorageServiceString.GetItemsAsync(checkedFiles.OfType(), checkedFiles.OfType(), FilterType.FilesOnly, false, "", "")); foreach (var e in entries) { yield return await GetFileEntryWrapperAsync(e); } } /// /// Finishes all the active file operations /// /// Finish all /// File operations /// Operation result [HttpPut("fileops/terminate")] public async IAsyncEnumerable TerminateTasks() { var tasks = _fileStorageServiceString.TerminateTasks(); foreach (var e in tasks) { yield return await _fileOperationDtoHelper.GetAsync(e); } } }