DocSpace-client/products/ASC.Files/Server/Helpers/FilesControllerHelper.cs

1185 lines
52 KiB
C#
Raw Normal View History

2020-03-18 13:19:37 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
2020-05-27 15:23:50 +00:00
using System.Text.Json;
2020-03-18 13:19:37 +00:00
using System.Text.RegularExpressions;
2021-11-15 11:31:47 +00:00
using System.Threading.Tasks;
2020-03-18 13:19:37 +00:00
using System.Web;
using ASC.Api.Core;
using ASC.Api.Documents;
using ASC.Api.Utils;
using ASC.Common;
2020-07-03 11:48:56 +00:00
using ASC.Common.Logging;
2020-03-18 13:19:37 +00:00
using ASC.Common.Web;
using ASC.Core;
2020-12-29 13:04:50 +00:00
using ASC.Core.Common.Settings;
2020-03-18 13:19:37 +00:00
using ASC.FederatedLogin.Helpers;
using ASC.Files.Core;
2021-09-29 16:08:30 +00:00
using ASC.Files.Core.Model;
2020-03-18 13:19:37 +00:00
using ASC.Files.Model;
using ASC.Web.Core.Files;
using ASC.Web.Files.Classes;
2020-12-29 13:04:50 +00:00
using ASC.Web.Files.Core.Entries;
2020-03-18 13:19:37 +00:00
using ASC.Web.Files.Services.DocumentService;
using ASC.Web.Files.Services.WCFService;
using ASC.Web.Files.Utils;
2020-12-29 13:04:50 +00:00
using ASC.Web.Studio.Core;
2020-03-18 13:19:37 +00:00
using Microsoft.AspNetCore.Http;
2020-07-03 11:48:56 +00:00
using Microsoft.Extensions.Options;
2020-03-18 13:19:37 +00:00
using Newtonsoft.Json.Linq;
using static ASC.Api.Documents.FilesController;
using FileShare = ASC.Files.Core.Security.FileShare;
using MimeMapping = ASC.Common.Web.MimeMapping;
using SortedByType = ASC.Files.Core.SortedByType;
namespace ASC.Files.Helpers
{
2020-10-19 15:53:15 +00:00
[Scope]
2020-03-18 13:19:37 +00:00
public class FilesControllerHelper<T>
{
private readonly ApiContext ApiContext;
private readonly FileStorageService<T> FileStorageService;
2020-08-12 09:58:08 +00:00
private FileWrapperHelper FileWrapperHelper { get; }
private FilesSettingsHelper FilesSettingsHelper { get; }
private FilesLinkUtility FilesLinkUtility { get; }
private FileUploader FileUploader { get; }
private DocumentServiceHelper DocumentServiceHelper { get; }
private TenantManager TenantManager { get; }
private SecurityContext SecurityContext { get; }
private FolderWrapperHelper FolderWrapperHelper { get; }
private FileOperationWraperHelper FileOperationWraperHelper { get; }
private FileShareWrapperHelper FileShareWrapperHelper { get; }
private FileShareParamsHelper FileShareParamsHelper { get; }
private EntryManager EntryManager { get; }
private FolderContentWrapperHelper FolderContentWrapperHelper { get; }
private ChunkedUploadSessionHelper ChunkedUploadSessionHelper { get; }
private DocumentServiceTrackerHelper DocumentServiceTracker { get; }
2020-12-29 13:04:50 +00:00
private SettingsManager SettingsManager { get; }
private EncryptionKeyPairHelper EncryptionKeyPairHelper { get; }
2021-09-27 19:28:49 +00:00
private IHttpContextAccessor HttpContextAccessor { get; }
private ILog Logger { get; set; }
2020-03-18 13:19:37 +00:00
/// <summary>
/// </summary>
/// <param name="context"></param>
/// <param name="fileStorageService"></param>
public FilesControllerHelper(
ApiContext context,
FileStorageService<T> fileStorageService,
FileWrapperHelper fileWrapperHelper,
FilesSettingsHelper filesSettingsHelper,
FilesLinkUtility filesLinkUtility,
FileUploader fileUploader,
DocumentServiceHelper documentServiceHelper,
TenantManager tenantManager,
SecurityContext securityContext,
FolderWrapperHelper folderWrapperHelper,
FileOperationWraperHelper fileOperationWraperHelper,
FileShareWrapperHelper fileShareWrapperHelper,
FileShareParamsHelper fileShareParamsHelper,
EntryManager entryManager,
FolderContentWrapperHelper folderContentWrapperHelper,
ChunkedUploadSessionHelper chunkedUploadSessionHelper,
DocumentServiceTrackerHelper documentServiceTracker,
2020-12-29 13:04:50 +00:00
IOptionsMonitor<ILog> optionMonitor,
SettingsManager settingsManager,
2021-09-27 19:28:49 +00:00
EncryptionKeyPairHelper encryptionKeyPairHelper,
IHttpContextAccessor httpContextAccessor)
2020-03-18 13:19:37 +00:00
{
ApiContext = context;
FileStorageService = fileStorageService;
FileWrapperHelper = fileWrapperHelper;
FilesSettingsHelper = filesSettingsHelper;
FilesLinkUtility = filesLinkUtility;
FileUploader = fileUploader;
DocumentServiceHelper = documentServiceHelper;
TenantManager = tenantManager;
SecurityContext = securityContext;
FolderWrapperHelper = folderWrapperHelper;
FileOperationWraperHelper = fileOperationWraperHelper;
FileShareWrapperHelper = fileShareWrapperHelper;
FileShareParamsHelper = fileShareParamsHelper;
EntryManager = entryManager;
FolderContentWrapperHelper = folderContentWrapperHelper;
ChunkedUploadSessionHelper = chunkedUploadSessionHelper;
DocumentServiceTracker = documentServiceTracker;
2020-12-29 13:04:50 +00:00
SettingsManager = settingsManager;
EncryptionKeyPairHelper = encryptionKeyPairHelper;
2021-09-27 19:28:49 +00:00
HttpContextAccessor = httpContextAccessor;
2020-07-03 11:48:56 +00:00
Logger = optionMonitor.Get("ASC.Files");
2020-03-18 13:19:37 +00:00
}
2020-03-19 13:18:35 +00:00
public FolderContentWrapper<T> GetFolder(T folderId, Guid userIdOrGroupId, FilterType filterType, bool withSubFolders)
2020-03-18 13:19:37 +00:00
{
2020-03-19 13:18:35 +00:00
return ToFolderContentWrapper(folderId, userIdOrGroupId, filterType, withSubFolders).NotFoundIfNull();
2020-03-18 13:19:37 +00:00
}
2021-11-15 11:31:47 +00:00
public async Task<FolderContentWrapper<T>> GetFolderAsync(T folderId, Guid userIdOrGroupId, FilterType filterType, bool withSubFolders)
{
var folderContentWrapper = ToFolderContentWrapperAsync(folderId, userIdOrGroupId, filterType, withSubFolders);
return await folderContentWrapper.NotFoundIfNull();
}
2021-09-27 19:28:49 +00:00
public object UploadFile(T folderId, UploadModel uploadModel)
2020-03-18 13:19:37 +00:00
{
if (uploadModel.StoreOriginalFileFlag.HasValue)
{
FilesSettingsHelper.StoreOriginalFiles = uploadModel.StoreOriginalFileFlag.Value;
}
2021-09-27 19:28:49 +00:00
IEnumerable<IFormFile> files = HttpContextAccessor.HttpContext.Request.Form.Files;
if (files == null || !files.Any())
2020-03-18 13:19:37 +00:00
{
2021-09-27 19:28:49 +00:00
files = uploadModel.Files;
}
if (files != null && files.Any())
{
if (files.Count() == 1)
2020-03-18 13:19:37 +00:00
{
//Only one file. return it
2021-09-27 19:28:49 +00:00
var postedFile = files.First();
return InsertFile(folderId, postedFile.OpenReadStream(), postedFile.FileName, uploadModel.CreateNewIfExist, uploadModel.KeepConvertStatus);
2020-03-18 13:19:37 +00:00
}
2021-09-27 19:28:49 +00:00
2020-03-18 13:19:37 +00:00
//For case with multiple files
return uploadModel.Files.Select(postedFile => InsertFile(folderId, postedFile.OpenReadStream(), postedFile.FileName, uploadModel.CreateNewIfExist, uploadModel.KeepConvertStatus)).ToList();
}
2021-09-27 19:28:49 +00:00
2020-03-18 13:19:37 +00:00
if (uploadModel.File != null)
{
var fileName = "file" + MimeMapping.GetExtention(uploadModel.ContentType.MediaType);
if (uploadModel.ContentDisposition != null)
{
fileName = uploadModel.ContentDisposition.FileName;
}
return new List<FileWrapper<T>>
{
2021-09-27 19:28:49 +00:00
InsertFile(folderId, uploadModel.File.OpenReadStream(), fileName, uploadModel.CreateNewIfExist, uploadModel.KeepConvertStatus)
2020-03-18 13:19:37 +00:00
};
}
2021-09-27 19:28:49 +00:00
2020-03-18 13:19:37 +00:00
throw new InvalidOperationException("No input files");
}
public async Task<object> UploadFileAsync(T folderId, UploadModel uploadModel)
{
if (uploadModel.StoreOriginalFileFlag.HasValue)
{
FilesSettingsHelper.StoreOriginalFiles = uploadModel.StoreOriginalFileFlag.Value;
}
IEnumerable<IFormFile> files = HttpContextAccessor.HttpContext.Request.Form.Files;
if (files == null || !files.Any())
{
files = uploadModel.Files;
}
if (files != null && files.Any())
{
if (files.Count() == 1)
{
//Only one file. return it
var postedFile = files.First();
return await InsertFileAsync(folderId, postedFile.OpenReadStream(), postedFile.FileName, uploadModel.CreateNewIfExist, uploadModel.KeepConvertStatus);
2020-03-18 13:19:37 +00:00
}
2020-03-18 13:19:37 +00:00
//For case with multiple files
return uploadModel.Files.Select(postedFile => InsertFile(folderId, postedFile.OpenReadStream(), postedFile.FileName, uploadModel.CreateNewIfExist, uploadModel.KeepConvertStatus)).ToList();
}
2020-03-18 13:19:37 +00:00
if (uploadModel.File != null)
{
var fileName = "file" + MimeMapping.GetExtention(uploadModel.ContentType.MediaType);
if (uploadModel.ContentDisposition != null)
{
fileName = uploadModel.ContentDisposition.FileName;
}
return new List<FileWrapper<T>>
{
await InsertFileAsync(folderId, uploadModel.File.OpenReadStream(), fileName, uploadModel.CreateNewIfExist, uploadModel.KeepConvertStatus)
2020-03-18 13:19:37 +00:00
};
}
2020-03-18 13:19:37 +00:00
throw new InvalidOperationException("No input files");
}
public FileWrapper<T> InsertFile(T folderId, Stream file, string title, bool? createNewIfExist, bool keepConvertStatus = false)
{
try
{
var resultFile = FileUploader.Exec(folderId, title, file.Length, file, createNewIfExist ?? !FilesSettingsHelper.UpdateIfExist, !keepConvertStatus);
return FileWrapperHelper.Get(resultFile);
}
catch (FileNotFoundException e)
{
throw new ItemNotFoundException("File not found", e);
}
catch (DirectoryNotFoundException e)
{
throw new ItemNotFoundException("Folder not found", e);
}
}
public async Task<FileWrapper<T>> InsertFileAsync(T folderId, Stream file, string title, bool? createNewIfExist, bool keepConvertStatus = false)
{
try
{
var resultFile = await FileUploader.ExecAsync(folderId, title, file.Length, file, createNewIfExist ?? !FilesSettingsHelper.UpdateIfExist, !keepConvertStatus);
return FileWrapperHelper.Get(resultFile);
}
catch (FileNotFoundException e)
{
throw new ItemNotFoundException("File not found", e);
}
catch (DirectoryNotFoundException e)
{
throw new ItemNotFoundException("Folder not found", e);
}
}
2020-09-22 14:17:17 +00:00
public FileWrapper<T> UpdateFileStream(Stream file, T fileId, bool encrypted = false, bool forcesave = false)
2020-03-18 13:19:37 +00:00
{
try
{
2020-09-22 14:17:17 +00:00
var resultFile = FileStorageService.UpdateFileStream(fileId, file, encrypted, forcesave);
2020-03-18 13:19:37 +00:00
return FileWrapperHelper.Get(resultFile);
}
catch (FileNotFoundException e)
{
throw new ItemNotFoundException("File not found", e);
}
}
public FileWrapper<T> SaveEditing(T fileId, string fileExtension, string downloadUri, Stream stream, string doc, bool forcesave)
{
return FileWrapperHelper.Get(FileStorageService.SaveEditing(fileId, fileExtension, downloadUri, stream, doc, forcesave));
}
public async Task<FileWrapper<T>> SaveEditingAsync(T fileId, string fileExtension, string downloadUri, Stream stream, string doc, bool forcesave)
{
return FileWrapperHelper.Get(await FileStorageService.SaveEditingAsync(fileId, fileExtension, downloadUri, stream, doc, forcesave));
}
2020-03-18 13:19:37 +00:00
public string StartEdit(T fileId, bool editingAlone, string doc)
{
return FileStorageService.StartEdit(fileId, editingAlone, doc);
}
2021-11-15 11:31:47 +00:00
public async Task<string> StartEditAsync(T fileId, bool editingAlone, string doc)
{
return await FileStorageService.StartEditAsync(fileId, editingAlone, doc);
}
2020-03-18 13:19:37 +00:00
public KeyValuePair<bool, string> TrackEditFile(T fileId, Guid tabId, string docKeyForTrack, string doc, bool isFinish)
{
return FileStorageService.TrackEditFile(fileId, tabId, docKeyForTrack, doc, isFinish);
}
public async Task<KeyValuePair<bool, string>> TrackEditFileAsync(T fileId, Guid tabId, string docKeyForTrack, string doc, bool isFinish)
{
return await FileStorageService.TrackEditFileAsync(fileId, tabId, docKeyForTrack, doc, isFinish);
}
public Configuration<T> OpenEdit(T fileId, int version, string doc, bool view)
2020-03-18 13:19:37 +00:00
{
DocumentServiceHelper.GetParams(fileId, version, doc, true, !view, true, out var configuration);
2020-05-20 09:58:40 +00:00
configuration.EditorType = EditorType.External;
if (configuration.EditorConfig.ModeWrite)
{
2021-09-16 11:49:44 +00:00
configuration.EditorConfig.CallbackUrl = DocumentServiceTracker.GetCallbackUrl(configuration.Document.Info.GetFile().ID.ToString());
}
2020-12-29 13:04:50 +00:00
2021-09-16 11:49:44 +00:00
if (configuration.Document.Info.GetFile().RootFolderType == FolderType.Privacy && PrivacyRoomSettings.GetEnabled(SettingsManager))
2020-12-29 13:04:50 +00:00
{
var keyPair = EncryptionKeyPairHelper.GetKeyPair();
if (keyPair != null)
{
configuration.EditorConfig.EncryptionKeys = new EncryptionKeysConfig
{
PrivateKeyEnc = keyPair.PrivateKeyEnc,
PublicKey = keyPair.PublicKey,
};
}
}
2021-09-16 11:49:44 +00:00
if (!configuration.Document.Info.GetFile().Encrypted && !configuration.Document.Info.GetFile().ProviderEntry) EntryManager.MarkAsRecent(configuration.Document.Info.GetFile());
2020-12-29 13:04:50 +00:00
2020-03-18 13:19:37 +00:00
configuration.Token = DocumentServiceHelper.GetSignature(configuration);
return configuration;
}
public async Task<Configuration<T>> OpenEditAsync(T fileId, int version, string doc, bool view)
2020-03-18 13:19:37 +00:00
{
var docParams = await DocumentServiceHelper.GetParamsAsync(fileId, version, doc, true, !view, true);
var configuration = docParams.Configuration;
2020-05-20 09:58:40 +00:00
configuration.EditorType = EditorType.External;
if (configuration.EditorConfig.ModeWrite)
{
configuration.EditorConfig.CallbackUrl = DocumentServiceTracker.GetCallbackUrl(configuration.Document.Info.GetFile().ID.ToString());
}
2020-12-29 13:04:50 +00:00
if (configuration.Document.Info.GetFile().RootFolderType == FolderType.Privacy && PrivacyRoomSettings.GetEnabled(SettingsManager))
2020-12-29 13:04:50 +00:00
{
var keyPair = EncryptionKeyPairHelper.GetKeyPair();
if (keyPair != null)
{
configuration.EditorConfig.EncryptionKeys = new EncryptionKeysConfig
{
PrivateKeyEnc = keyPair.PrivateKeyEnc,
PublicKey = keyPair.PublicKey,
};
}
}
if (!configuration.Document.Info.GetFile().Encrypted && !configuration.Document.Info.GetFile().ProviderEntry) EntryManager.MarkAsRecent(configuration.Document.Info.GetFile());
2020-12-29 13:04:50 +00:00
2020-03-18 13:19:37 +00:00
configuration.Token = DocumentServiceHelper.GetSignature(configuration);
return configuration;
}
public object CreateUploadSession(T folderId, string fileName, long fileSize, string relativePath, bool encrypted)
{
var file = FileUploader.VerifyChunkedUpload(folderId, fileName, fileSize, FilesSettingsHelper.UpdateIfExist, relativePath);
if (FilesLinkUtility.IsLocalFileUploader)
{
2020-04-16 08:50:52 +00:00
var session = FileUploader.InitiateUpload(file.FolderID, (file.ID ?? default), file.Title, file.ContentLength, encrypted);
2020-03-18 13:19:37 +00:00
2020-04-20 08:08:04 +00:00
var responseObject = ChunkedUploadSessionHelper.ToResponseObject(session, true);
2020-03-18 13:19:37 +00:00
return new
{
success = true,
2020-04-20 08:08:04 +00:00
data = responseObject
2020-03-18 13:19:37 +00:00
};
}
var createSessionUrl = FilesLinkUtility.GetInitiateUploadSessionUrl(TenantManager.GetCurrentTenant().TenantId, file.FolderID, file.ID, file.Title, file.ContentLength, encrypted, SecurityContext);
var request = (HttpWebRequest)WebRequest.Create(createSessionUrl);
request.Method = "POST";
request.ContentLength = 0;
// hack for uploader.onlyoffice.com in api requests
2020-05-17 13:08:20 +00:00
var rewriterHeader = ApiContext.HttpContextAccessor.HttpContext.Request.Headers[HttpRequestExtensions.UrlRewriterHeader];
2020-03-18 13:19:37 +00:00
if (!string.IsNullOrEmpty(rewriterHeader))
{
request.Headers[HttpRequestExtensions.UrlRewriterHeader] = rewriterHeader;
}
// hack. http://ubuntuforums.org/showthread.php?t=1841740
if (WorkContext.IsMono)
{
ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
}
2020-04-20 08:08:04 +00:00
using var response = request.GetResponse();
using var responseStream = response.GetResponseStream();
using var streamReader = new StreamReader(responseStream);
return JObject.Parse(streamReader.ReadToEnd()); //result is json string
2020-03-18 13:19:37 +00:00
}
public async Task<object> CreateUploadSessionAsync(T folderId, string fileName, long fileSize, string relativePath, bool encrypted)
{
var file = await FileUploader.VerifyChunkedUploadAsync(folderId, fileName, fileSize, FilesSettingsHelper.UpdateIfExist, relativePath);
if (FilesLinkUtility.IsLocalFileUploader)
{
var session = await FileUploader.InitiateUploadAsync(file.FolderID, (file.ID ?? default), file.Title, file.ContentLength, encrypted);
var responseObject = await ChunkedUploadSessionHelper.ToResponseObjectAsync(session, true);
return new
{
success = true,
data = responseObject
};
}
var createSessionUrl = FilesLinkUtility.GetInitiateUploadSessionUrl(TenantManager.GetCurrentTenant().TenantId, file.FolderID, file.ID, file.Title, file.ContentLength, encrypted, SecurityContext);
var request = (HttpWebRequest)WebRequest.Create(createSessionUrl);
request.Method = "POST";
request.ContentLength = 0;
// hack for uploader.onlyoffice.com in api requests
var rewriterHeader = ApiContext.HttpContextAccessor.HttpContext.Request.Headers[HttpRequestExtensions.UrlRewriterHeader];
if (!string.IsNullOrEmpty(rewriterHeader))
{
request.Headers[HttpRequestExtensions.UrlRewriterHeader] = rewriterHeader;
}
// hack. http://ubuntuforums.org/showthread.php?t=1841740
if (WorkContext.IsMono)
{
ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
}
using var response = request.GetResponse();
using var responseStream = response.GetResponseStream();
using var streamReader = new StreamReader(responseStream);
return JObject.Parse(streamReader.ReadToEnd()); //result is json string
}
2020-03-18 13:19:37 +00:00
public FileWrapper<T> CreateTextFile(T folderId, string title, string content)
{
if (title == null) throw new ArgumentNullException("title");
//Try detect content
var extension = ".txt";
if (!string.IsNullOrEmpty(content))
{
if (Regex.IsMatch(content, @"<([^\s>]*)(\s[^<]*)>"))
{
extension = ".html";
}
}
return CreateFile(folderId, title, content, extension);
}
public async Task<FileWrapper<T>> CreateTextFileAsync(T folderId, string title, string content)
{
if (title == null) throw new ArgumentNullException("title");
//Try detect content
var extension = ".txt";
if (!string.IsNullOrEmpty(content))
{
if (Regex.IsMatch(content, @"<([^\s>]*)(\s[^<]*)>"))
{
extension = ".html";
}
}
return await CreateFileAsync(folderId, title, content, extension);
}
2020-03-18 13:19:37 +00:00
private FileWrapper<T> CreateFile(T folderId, string title, string content, string extension)
{
2020-09-29 09:16:07 +00:00
using var memStream = new MemoryStream(Encoding.UTF8.GetBytes(content));
var file = FileUploader.Exec(folderId,
title.EndsWith(extension, StringComparison.OrdinalIgnoreCase) ? title : (title + extension),
memStream.Length, memStream);
return FileWrapperHelper.Get(file);
2020-03-18 13:19:37 +00:00
}
private async Task<FileWrapper<T>> CreateFileAsync(T folderId, string title, string content, string extension)
{
using var memStream = new MemoryStream(Encoding.UTF8.GetBytes(content));
var file = await FileUploader.ExecAsync(folderId,
title.EndsWith(extension, StringComparison.OrdinalIgnoreCase) ? title : (title + extension),
memStream.Length, memStream);
return FileWrapperHelper.Get(file);
}
2020-03-18 13:19:37 +00:00
public FileWrapper<T> CreateHtmlFile(T folderId, string title, string content)
{
if (title == null) throw new ArgumentNullException("title");
return CreateFile(folderId, title, content, ".html");
}
2021-11-22 13:17:27 +00:00
public async Task<FileWrapper<T>> CreateHtmlFileAsync(T folderId, string title, string content)
{
if (title == null) throw new ArgumentNullException("title");
return await CreateFileAsync(folderId, title, content, ".html");
}
2020-03-18 13:19:37 +00:00
public FolderWrapper<T> CreateFolder(T folderId, string title)
{
var folder = FileStorageService.CreateNewFolder(folderId, title);
return FolderWrapperHelper.Get(folder);
}
2021-11-22 13:17:27 +00:00
public async Task<FolderWrapper<T>> CreateFolderAsync(T folderId, string title)
{
var folder = await FileStorageService.CreateNewFolderAsync(folderId, title);
return FolderWrapperHelper.Get(folder);
}
public FileWrapper<T> CreateFile(T folderId, string title, T templateId, bool enableExternalExt = false)
2020-03-18 13:19:37 +00:00
{
var file = FileStorageService.CreateNewFile(new FileModel<T> { ParentId = folderId, Title = title, TemplateId = templateId }, enableExternalExt);
2020-03-18 13:19:37 +00:00
return FileWrapperHelper.Get(file);
}
2021-11-22 13:17:27 +00:00
public async Task<FileWrapper<T>> CreateFileAsync(T folderId, string title, T templateId, bool enableExternalExt = false)
{
var file = await FileStorageService.CreateNewFileAsync(new FileModel<T> { ParentId = folderId, Title = title, TemplateId = templateId }, enableExternalExt);
return FileWrapperHelper.Get(file);
}
2020-03-18 13:19:37 +00:00
public FolderWrapper<T> RenameFolder(T folderId, string title)
{
var folder = FileStorageService.FolderRename(folderId, title);
return FolderWrapperHelper.Get(folder);
}
2021-11-22 13:17:27 +00:00
public async Task<FolderWrapper<T>> RenameFolderAsync(T folderId, string title)
{
var folder = await FileStorageService.FolderRenameAsync(folderId, title);
return FolderWrapperHelper.Get(folder);
}
2020-03-18 13:19:37 +00:00
public FolderWrapper<T> GetFolderInfo(T folderId)
{
var folder = FileStorageService.GetFolder(folderId).NotFoundIfNull("Folder not found");
return FolderWrapperHelper.Get(folder);
}
2021-11-22 13:17:27 +00:00
public async Task<FolderWrapper<T>> GetFolderInfoAsync(T folderId)
{
var folder = await FileStorageService.GetFolderAsync(folderId).NotFoundIfNull("Folder not found");
return await FolderWrapperHelper.GetAsync(folder);
}
2020-11-06 12:10:27 +00:00
public IEnumerable<FileEntryWrapper> GetFolderPath(T folderId)
2020-03-18 13:19:37 +00:00
{
2020-12-01 19:59:42 +00:00
return EntryManager.GetBreadCrumbs(folderId).Select(GetFileEntryWrapper);
2020-03-18 13:19:37 +00:00
}
2021-11-22 13:17:27 +00:00
public async IAsyncEnumerable<FileEntryWrapper> GetFolderPathAsync(T folderId)
{
var breadCrumbs = await EntryManager.GetBreadCrumbsAsync(folderId);
foreach (var e in breadCrumbs)
{
yield return await GetFileEntryWrapperAsync(e);
}
}
2020-03-18 13:19:37 +00:00
public FileWrapper<T> GetFileInfo(T fileId, int version = -1)
{
var file = FileStorageService.GetFile(fileId, version).NotFoundIfNull("File not found");
return FileWrapperHelper.Get(file);
}
2021-11-15 11:31:47 +00:00
public async Task<FileWrapper<T>> GetFileInfoAsync(T fileId, int version = -1)
{
2021-11-22 13:17:27 +00:00
var file = await FileStorageService.GetFileAsync(fileId, version);
file = file.NotFoundIfNull("File not found");
return await FileWrapperHelper.GetAsync(file);
2021-11-15 11:31:47 +00:00
}
2020-10-07 14:31:17 +00:00
public FileWrapper<T> AddToRecent(T fileId, int version = -1)
{
var file = FileStorageService.GetFile(fileId, version).NotFoundIfNull("File not found");
EntryManager.MarkAsRecent(file);
return FileWrapperHelper.Get(file);
}
2021-11-15 11:31:47 +00:00
public async Task<FileWrapper<T>> AddToRecentAsync(T fileId, int version = -1)
{
var file = await FileStorageService.GetFileAsync(fileId, version).NotFoundIfNull("File not found");
EntryManager.MarkAsRecent(file);
return FileWrapperHelper.Get(file);
}
2020-06-25 17:17:24 +00:00
public List<FileEntryWrapper> GetNewItems(T folderId)
{
return FileStorageService.GetNewItems(folderId)
2020-12-01 19:59:42 +00:00
.Select(GetFileEntryWrapper)
2020-06-25 17:17:24 +00:00
.ToList();
}
public async Task<List<FileEntryWrapper>> GetNewItemsAsync(T folderId)
{
var newItems = await FileStorageService.GetNewItemsAsync(folderId);
return newItems.Select(GetFileEntryWrapper).ToList();
}
2020-03-18 13:19:37 +00:00
public FileWrapper<T> UpdateFile(T fileId, string title, int lastVersion)
{
2021-10-12 07:24:51 +00:00
File<T> newFile = null;
2020-03-18 13:19:37 +00:00
if (!string.IsNullOrEmpty(title))
2021-10-12 07:24:51 +00:00
{
newFile = FileStorageService.FileRename(fileId, title);
}
2020-03-18 13:19:37 +00:00
if (lastVersion > 0)
2021-10-12 07:24:51 +00:00
{
var pair = FileStorageService.UpdateToVersion(fileId, lastVersion);
newFile = pair.Key;
}
2020-03-18 13:19:37 +00:00
2021-10-12 07:24:51 +00:00
return newFile != null ? FileWrapperHelper.Get(newFile) : GetFileInfo(fileId);
2020-03-18 13:19:37 +00:00
}
2021-11-15 11:31:47 +00:00
public async Task<FileWrapper<T>> UpdateFileAsync(T fileId, string title, int lastVersion)
{
if (!string.IsNullOrEmpty(title))
await FileStorageService.FileRenameAsync(fileId, title);
if (lastVersion > 0)
2021-11-22 13:17:27 +00:00
await FileStorageService.UpdateToVersionAsync(fileId, lastVersion);
2021-11-15 11:31:47 +00:00
return await GetFileInfoAsync(fileId);
}
2020-03-24 12:30:16 +00:00
public IEnumerable<FileOperationWraper> DeleteFile(T fileId, bool deleteAfter, bool immediately)
2020-03-18 13:19:37 +00:00
{
2020-03-20 14:35:51 +00:00
return FileStorageService.DeleteFile("delete", fileId, false, deleteAfter, immediately)
2020-03-24 12:30:16 +00:00
.Select(FileOperationWraperHelper.Get);
2020-03-18 13:19:37 +00:00
}
2021-08-11 14:32:40 +00:00
public IEnumerable<ConversationResult<T>> StartConversion(T fileId, bool sync = false)
2020-03-18 13:19:37 +00:00
{
2021-08-11 14:32:40 +00:00
return CheckConversion(fileId, true, sync);
2020-03-18 13:19:37 +00:00
}
2021-11-22 13:17:27 +00:00
public IAsyncEnumerable<ConversationResult<T>> StartConversionAsync(T fileId, bool sync = false)
{
return CheckConversionAsync(fileId, true, sync);
}
2021-08-11 14:32:40 +00:00
public IEnumerable<ConversationResult<T>> CheckConversion(T fileId, bool start, bool sync = false)
2020-03-18 13:19:37 +00:00
{
2021-06-18 12:56:15 +00:00
return FileStorageService.CheckConversion(new List<List<string>>
2020-03-18 13:19:37 +00:00
{
2021-06-18 12:56:15 +00:00
new List<string> { fileId.ToString(), "0", start.ToString() }
2021-08-11 14:32:40 +00:00
}, sync)
2020-03-18 13:19:37 +00:00
.Select(r =>
{
var o = new ConversationResult<T>
{
Id = r.Id,
Error = r.Error,
OperationType = r.OperationType,
Processed = r.Processed,
Progress = r.Progress,
Source = r.Source,
};
if (!string.IsNullOrEmpty(r.Result))
{
2020-07-03 11:48:56 +00:00
try
{
2021-01-13 10:49:52 +00:00
var options = new JsonSerializerOptions
{
AllowTrailingCommas = true,
PropertyNameCaseInsensitive = true
};
var jResult = JsonSerializer.Deserialize<FileJsonSerializerData<T>>(r.Result, options);
2020-07-03 14:47:01 +00:00
o.File = GetFileInfo(jResult.Id, jResult.Version);
2020-07-03 11:48:56 +00:00
}
catch (Exception e)
{
Logger.Error(e);
}
2020-03-18 13:19:37 +00:00
}
return o;
});
}
2021-11-22 13:17:27 +00:00
public async IAsyncEnumerable<ConversationResult<T>> CheckConversionAsync(T fileId, bool start, bool sync = false)
2021-11-15 11:31:47 +00:00
{
2021-11-22 13:17:27 +00:00
var checkConversaion = await FileStorageService.CheckConversionAsync(new List<List<string>>
2021-11-15 11:31:47 +00:00
{
new List<string> { fileId.ToString(), "0", start.ToString() }
2021-11-22 13:17:27 +00:00
}, sync);
foreach(var r in checkConversaion)
2021-11-15 11:31:47 +00:00
{
var o = new ConversationResult<T>
{
Id = r.Id,
Error = r.Error,
OperationType = r.OperationType,
Processed = r.Processed,
Progress = r.Progress,
Source = r.Source,
};
2021-11-22 13:17:27 +00:00
2021-11-15 11:31:47 +00:00
if (!string.IsNullOrEmpty(r.Result))
{
try
{
var options = new JsonSerializerOptions
{
AllowTrailingCommas = true,
PropertyNameCaseInsensitive = true
};
var jResult = JsonSerializer.Deserialize<FileJsonSerializerData<T>>(r.Result, options);
o.File = await GetFileInfoAsync(jResult.Id, jResult.Version);
}
catch (Exception e)
{
Logger.Error(e);
}
}
2021-11-22 13:17:27 +00:00
yield return o;
}
2021-11-15 11:31:47 +00:00
}
2020-03-24 12:30:16 +00:00
public IEnumerable<FileOperationWraper> DeleteFolder(T folderId, bool deleteAfter, bool immediately)
2020-03-18 13:19:37 +00:00
{
2020-04-03 11:22:14 +00:00
return FileStorageService.DeleteFolder("delete", folderId, false, deleteAfter, immediately)
2021-04-13 09:46:25 +00:00
.Select(FileOperationWraperHelper.Get)
.ToList();
2020-03-18 13:19:37 +00:00
}
2020-03-24 12:30:16 +00:00
public IEnumerable<FileEntryWrapper> MoveOrCopyBatchCheck(BatchModel batchModel)
2020-03-18 13:19:37 +00:00
{
2021-04-15 18:43:12 +00:00
var checkedFiles = new List<object>();
var checkedFolders = new List<object>();
if (batchModel.DestFolderId.ValueKind == JsonValueKind.Number)
{
2021-09-24 18:40:29 +00:00
(checkedFiles, checkedFolders) = FileStorageService.MoveOrCopyFilesCheck(batchModel.FileIds.ToList(), batchModel.FolderIds.ToList(), batchModel.DestFolderId.GetInt32());
2021-04-15 18:43:12 +00:00
}
else
{
2021-09-24 18:40:29 +00:00
(checkedFiles, checkedFolders) = FileStorageService.MoveOrCopyFilesCheck(batchModel.FileIds.ToList(), batchModel.FolderIds.ToList(), batchModel.DestFolderId.GetString());
2021-04-15 18:43:12 +00:00
}
2020-05-27 15:23:50 +00:00
var entries = FileStorageService.GetItems(checkedFiles.OfType<int>().Select(Convert.ToInt32), checkedFiles.OfType<int>().Select(Convert.ToInt32), FilterType.FilesOnly, false, "", "");
2020-03-18 13:19:37 +00:00
2020-03-24 12:30:16 +00:00
entries.AddRange(FileStorageService.GetItems(checkedFiles.OfType<string>(), checkedFiles.OfType<string>(), FilterType.FilesOnly, false, "", ""));
2020-03-18 13:19:37 +00:00
2021-04-13 09:46:25 +00:00
return entries.Select(GetFileEntryWrapper).ToList();
2020-03-18 13:19:37 +00:00
}
2021-11-22 13:17:27 +00:00
public async IAsyncEnumerable<FileEntryWrapper> MoveOrCopyBatchCheckAsync(BatchModel batchModel)
{
var checkedFiles = new List<object>();
var checkedFolders = new List<object>();
if (batchModel.DestFolderId.ValueKind == JsonValueKind.Number)
{
(checkedFiles, checkedFolders) = await FileStorageService.MoveOrCopyFilesCheckAsync(batchModel.FileIds.ToList(), batchModel.FolderIds.ToList(), batchModel.DestFolderId.GetInt32());
}
else
{
(checkedFiles, checkedFolders) = await FileStorageService.MoveOrCopyFilesCheckAsync(batchModel.FileIds.ToList(), batchModel.FolderIds.ToList(), batchModel.DestFolderId.GetString());
}
var entries = await FileStorageService.GetItemsAsync(checkedFiles.OfType<int>().Select(Convert.ToInt32), checkedFiles.OfType<int>().Select(Convert.ToInt32), FilterType.FilesOnly, false, "", "");
entries.AddRange(await FileStorageService.GetItemsAsync(checkedFiles.OfType<string>(), checkedFiles.OfType<string>(), FilterType.FilesOnly, false, "", ""));
foreach(var e in entries)
{
yield return await GetFileEntryWrapperAsync(e);
}
}
2020-03-24 12:30:16 +00:00
public IEnumerable<FileOperationWraper> MoveBatchItems(BatchModel batchModel)
2020-03-18 13:19:37 +00:00
{
2021-09-24 18:40:29 +00:00
return FileStorageService.MoveOrCopyItems(batchModel.FolderIds.ToList(), batchModel.FileIds.ToList(), batchModel.DestFolderId, batchModel.ConflictResolveType, false, batchModel.DeleteAfter)
2021-04-13 09:46:25 +00:00
.Select(FileOperationWraperHelper.Get)
.ToList();
2020-03-18 13:19:37 +00:00
}
2020-03-24 12:30:16 +00:00
public IEnumerable<FileOperationWraper> CopyBatchItems(BatchModel batchModel)
2020-03-18 13:19:37 +00:00
{
2021-09-24 18:40:29 +00:00
return FileStorageService.MoveOrCopyItems(batchModel.FolderIds.ToList(), batchModel.FileIds.ToList(), batchModel.DestFolderId, batchModel.ConflictResolveType, true, batchModel.DeleteAfter)
2021-04-13 09:46:25 +00:00
.Select(FileOperationWraperHelper.Get)
.ToList();
2020-03-18 13:19:37 +00:00
}
2021-09-30 06:52:46 +00:00
public IEnumerable<FileOperationWraper> MarkAsRead(BaseBatchModel model)
2020-03-18 13:19:37 +00:00
{
2021-09-24 18:40:29 +00:00
return FileStorageService.MarkAsRead(model.FolderIds.ToList(), model.FileIds.ToList()).Select(FileOperationWraperHelper.Get).ToList();
2020-03-18 13:19:37 +00:00
}
2020-03-24 12:30:16 +00:00
public IEnumerable<FileOperationWraper> TerminateTasks()
2020-03-18 13:19:37 +00:00
{
2020-03-24 12:30:16 +00:00
return FileStorageService.TerminateTasks().Select(FileOperationWraperHelper.Get);
2020-03-18 13:19:37 +00:00
}
2020-03-24 12:30:16 +00:00
public IEnumerable<FileOperationWraper> GetOperationStatuses()
2020-03-18 13:19:37 +00:00
{
2020-03-24 12:30:16 +00:00
return FileStorageService.GetTasksStatuses().Select(FileOperationWraperHelper.Get);
2020-03-18 13:19:37 +00:00
}
2020-03-25 14:04:02 +00:00
public IEnumerable<FileOperationWraper> BulkDownload(DownloadModel model)
2020-03-18 13:19:37 +00:00
{
2020-05-27 15:23:50 +00:00
var folders = new Dictionary<JsonElement, string>();
var files = new Dictionary<JsonElement, string>();
2020-03-18 13:19:37 +00:00
2020-03-25 14:04:02 +00:00
foreach (var fileId in model.FileConvertIds.Where(fileId => !files.ContainsKey(fileId.Key)))
2020-03-18 13:19:37 +00:00
{
2020-03-25 14:04:02 +00:00
files.Add(fileId.Key, fileId.Value);
2020-03-18 13:19:37 +00:00
}
2020-03-25 14:04:02 +00:00
foreach (var fileId in model.FileIds.Where(fileId => !files.ContainsKey(fileId)))
2020-03-18 13:19:37 +00:00
{
2020-03-25 14:04:02 +00:00
files.Add(fileId, string.Empty);
2020-03-18 13:19:37 +00:00
}
2020-03-25 14:04:02 +00:00
foreach (var folderId in model.FolderIds.Where(folderId => !folders.ContainsKey(folderId)))
2020-03-18 13:19:37 +00:00
{
2020-03-25 14:04:02 +00:00
folders.Add(folderId, string.Empty);
2020-03-18 13:19:37 +00:00
}
2021-04-13 09:46:25 +00:00
return FileStorageService.BulkDownload(folders, files).Select(FileOperationWraperHelper.Get).ToList();
2020-03-18 13:19:37 +00:00
}
2020-03-24 12:30:16 +00:00
public IEnumerable<FileOperationWraper> EmptyTrash()
2020-03-18 13:19:37 +00:00
{
2021-04-13 09:46:25 +00:00
return FileStorageService.EmptyTrash().Select(FileOperationWraperHelper.Get).ToList();
2020-03-18 13:19:37 +00:00
}
public IEnumerable<FileWrapper<T>> GetFileVersionInfo(T fileId)
{
var files = FileStorageService.GetFileHistory(fileId);
return files.Select(r => FileWrapperHelper.Get(r)).ToList();
2020-03-18 13:19:37 +00:00
}
2021-11-22 13:17:27 +00:00
public async Task<IEnumerable<FileWrapper<T>>> GetFileVersionInfoAsync(T fileId)
{
var files = await FileStorageService.GetFileHistoryAsync(fileId);
return files.Select(r => FileWrapperHelper.Get(r)).ToList();
}
2020-03-18 13:19:37 +00:00
public IEnumerable<FileWrapper<T>> ChangeHistory(T fileId, int version, bool continueVersion)
{
var history = FileStorageService.CompleteVersion(fileId, version, continueVersion).Value;
return history.Select(r => FileWrapperHelper.Get(r)).ToList();
2020-03-18 13:19:37 +00:00
}
2021-11-22 13:17:27 +00:00
public async Task<IEnumerable<FileWrapper<T>>> ChangeHistoryAsync(T fileId, int version, bool continueVersion)
{
var pair = await FileStorageService.CompleteVersionAsync(fileId, version, continueVersion);
var history = pair.Value;
return history.Select(r => FileWrapperHelper.Get(r)).ToList();
}
2020-08-12 15:13:52 +00:00
public FileWrapper<T> LockFile(T fileId, bool lockFile)
{
var result = FileStorageService.LockFile(fileId, lockFile);
return FileWrapperHelper.Get(result);
}
2021-11-22 13:17:27 +00:00
public async Task<FileWrapper<T>> LockFileAsync(T fileId, bool lockFile)
{
var result = await FileStorageService.LockFileAsync(fileId, lockFile);
return await FileWrapperHelper.GetAsync(result);
}
2021-08-11 15:04:23 +00:00
public DocumentService.FileLink GetPresignedUri(T fileId)
{
return FileStorageService.GetPresignedUri(fileId);
}
2021-11-22 13:17:27 +00:00
public async Task<DocumentService.FileLink> GetPresignedUriAsync(T fileId)
{
return await FileStorageService.GetPresignedUriAsync(fileId);
}
public string UpdateComment(T fileId, int version, string comment)
{
return FileStorageService.UpdateComment(fileId, version, comment);
}
2021-11-22 13:17:27 +00:00
public async Task<string> UpdateCommentAsync(T fileId, int version, string comment)
{
return await FileStorageService.UpdateCommentAsync(fileId, version, comment);
}
2020-03-18 13:19:37 +00:00
public IEnumerable<FileShareWrapper> GetFileSecurityInfo(T fileId)
{
2020-12-03 14:43:33 +00:00
return GetSecurityInfo(new List<T> { fileId }, new List<T> { });
2020-03-18 13:19:37 +00:00
}
2021-11-22 13:17:27 +00:00
public async Task<IEnumerable<FileShareWrapper>> GetFileSecurityInfoAsync(T fileId)
{
return await GetSecurityInfoAsync(new List<T> { fileId }, new List<T> { });
}
2020-03-18 13:19:37 +00:00
public IEnumerable<FileShareWrapper> GetFolderSecurityInfo(T folderId)
{
2020-12-03 14:43:33 +00:00
return GetSecurityInfo(new List<T> { }, new List<T> { folderId });
2020-12-02 13:17:43 +00:00
}
2021-11-22 13:17:27 +00:00
public async Task<IEnumerable<FileShareWrapper>> GetFolderSecurityInfoAsync(T folderId)
{
return await GetSecurityInfoAsync(new List<T> { }, new List<T> { folderId });
}
2021-06-11 11:24:01 +00:00
public IEnumerable<FileEntryWrapper> GetFolders(T folderId)
{
return FileStorageService.GetFolders(folderId)
.Select(GetFileEntryWrapper)
.ToList();
}
public async Task<IEnumerable<FileEntryWrapper>> GetFoldersAsync(T folderId)
{
var folders = await FileStorageService.GetFoldersAsync(folderId);
return folders.Select(GetFileEntryWrapper).ToList();
}
2020-12-02 13:17:43 +00:00
public IEnumerable<FileShareWrapper> GetSecurityInfo(IEnumerable<T> fileIds, IEnumerable<T> folderIds)
{
var fileShares = FileStorageService.GetSharedInfo(fileIds, folderIds);
2021-04-13 09:46:25 +00:00
return fileShares.Select(FileShareWrapperHelper.Get).ToList();
2020-03-18 13:19:37 +00:00
}
2021-11-22 13:17:27 +00:00
public async Task<IEnumerable<FileShareWrapper>> GetSecurityInfoAsync(IEnumerable<T> fileIds, IEnumerable<T> folderIds)
{
var fileShares = await FileStorageService.GetSharedInfoAsync(fileIds, folderIds);
return fileShares.Select(FileShareWrapperHelper.Get).ToList();
}
2020-03-18 13:19:37 +00:00
public IEnumerable<FileShareWrapper> SetFileSecurityInfo(T fileId, IEnumerable<FileShareParams> share, bool notify, string sharingMessage)
{
2020-12-03 14:43:33 +00:00
return SetSecurityInfo(new List<T> { fileId }, new List<T>(), share, notify, sharingMessage);
2020-03-18 13:19:37 +00:00
}
2021-11-22 13:17:27 +00:00
public async Task<IEnumerable<FileShareWrapper>> SetFileSecurityInfoAsync(T fileId, IEnumerable<FileShareParams> share, bool notify, string sharingMessage)
{
return await SetSecurityInfoAsync(new List<T> { fileId }, new List<T>(), share, notify, sharingMessage);
}
2020-03-18 13:19:37 +00:00
public IEnumerable<FileShareWrapper> SetFolderSecurityInfo(T folderId, IEnumerable<FileShareParams> share, bool notify, string sharingMessage)
2020-12-03 14:43:33 +00:00
{
return SetSecurityInfo(new List<T>(), new List<T> { folderId }, share, notify, sharingMessage);
2020-12-03 14:43:33 +00:00
}
2021-11-22 13:17:27 +00:00
public async Task<IEnumerable<FileShareWrapper>> SetFolderSecurityInfoAsync(T folderId, IEnumerable<FileShareParams> share, bool notify, string sharingMessage)
{
return await SetSecurityInfoAsync(new List<T>(), new List<T> { folderId }, share, notify, sharingMessage);
}
2020-12-03 14:43:33 +00:00
public IEnumerable<FileShareWrapper> SetSecurityInfo(IEnumerable<T> fileIds, IEnumerable<T> folderIds, IEnumerable<FileShareParams> share, bool notify, string sharingMessage)
2020-03-18 13:19:37 +00:00
{
if (share != null && share.Any())
{
2021-06-18 12:56:15 +00:00
var list = new List<AceWrapper>(share.Select(FileShareParamsHelper.ToAceObject));
2020-12-02 13:17:43 +00:00
var aceCollection = new AceCollection<T>
2020-03-18 13:19:37 +00:00
{
2020-12-03 14:43:33 +00:00
Files = fileIds,
Folders = folderIds,
2020-03-18 13:19:37 +00:00
Aces = list,
Message = sharingMessage
};
FileStorageService.SetAceObject(aceCollection, notify);
}
2020-12-03 14:43:33 +00:00
return GetSecurityInfo(fileIds, folderIds);
2020-03-18 13:19:37 +00:00
}
2021-11-22 13:17:27 +00:00
public async Task<IEnumerable<FileShareWrapper>> SetSecurityInfoAsync(IEnumerable<T> fileIds, IEnumerable<T> folderIds, IEnumerable<FileShareParams> share, bool notify, string sharingMessage)
{
if (share != null && share.Any())
{
var list = new List<AceWrapper>(share.Select(FileShareParamsHelper.ToAceObject));
var aceCollection = new AceCollection<T>
{
Files = fileIds,
Folders = folderIds,
Aces = list,
Message = sharingMessage
};
await FileStorageService.SetAceObjectAsync(aceCollection, notify);
}
return await GetSecurityInfoAsync(fileIds, folderIds);
}
2020-05-15 15:12:04 +00:00
public bool RemoveSecurityInfo(List<T> fileIds, List<T> folderIds)
2020-03-18 13:19:37 +00:00
{
2020-05-15 15:12:04 +00:00
FileStorageService.RemoveAce(fileIds, folderIds);
2020-03-18 13:19:37 +00:00
return true;
}
2021-11-22 13:17:27 +00:00
public async Task<bool> RemoveSecurityInfoAsync(List<T> fileIds, List<T> folderIds)
{
await FileStorageService.RemoveAceAsync(fileIds, folderIds);
return true;
}
2020-03-18 13:19:37 +00:00
public string GenerateSharedLink(T fileId, FileShare share)
{
var file = GetFileInfo(fileId);
2020-12-02 13:17:43 +00:00
var sharedInfo = FileStorageService.GetSharedInfo(new List<T> { fileId }, new List<T> { }).Find(r => r.SubjectId == FileConstant.ShareLinkId);
2020-03-18 13:19:37 +00:00
if (sharedInfo == null || sharedInfo.Share != share)
{
2021-06-18 12:56:15 +00:00
var list = new List<AceWrapper>
2020-03-18 13:19:37 +00:00
{
new AceWrapper
{
SubjectId = FileConstant.ShareLinkId,
SubjectGroup = true,
Share = share
}
};
2020-12-02 13:17:43 +00:00
var aceCollection = new AceCollection<T>
2020-03-18 13:19:37 +00:00
{
2020-12-02 13:17:43 +00:00
Files = new List<T> { fileId },
2020-03-18 13:19:37 +00:00
Aces = list
};
FileStorageService.SetAceObject(aceCollection, false);
2020-12-02 13:17:43 +00:00
sharedInfo = FileStorageService.GetSharedInfo(new List<T> { fileId }, new List<T> { }).Find(r => r.SubjectId == FileConstant.ShareLinkId);
2020-03-18 13:19:37 +00:00
}
return sharedInfo.Link;
}
2021-11-15 11:31:47 +00:00
public async Task<string> GenerateSharedLinkAsync(T fileId, FileShare share)
{
var file = await GetFileInfoAsync(fileId);
var sharedInfo = FileStorageService.GetSharedInfo(new List<T> { fileId }, new List<T> { }).Find(r => r.SubjectId == FileConstant.ShareLinkId);
if (sharedInfo == null || sharedInfo.Share != share)
{
var list = new List<AceWrapper>
{
new AceWrapper
{
SubjectId = FileConstant.ShareLinkId,
SubjectGroup = true,
Share = share
}
};
var aceCollection = new AceCollection<T>
{
Files = new List<T> { fileId },
Aces = list
};
FileStorageService.SetAceObject(aceCollection, false);
sharedInfo = FileStorageService.GetSharedInfo(new List<T> { fileId }, new List<T> { }).Find(r => r.SubjectId == FileConstant.ShareLinkId);
}
return sharedInfo.Link;
}
public bool SetAceLink(T fileId, FileShare share)
{
return FileStorageService.SetAceLink(fileId, share);
}
2021-11-22 13:17:27 +00:00
public async Task<bool> SetAceLinkAsync(T fileId, FileShare share)
{
return await FileStorageService.SetAceLinkAsync(fileId, share);
}
2020-03-18 13:19:37 +00:00
///// <summary>
/////
///// </summary>
///// <param name="query"></param>
///// <returns></returns>
//[Read(@"@search/{query}")]
//public IEnumerable<FileEntryWrapper> Search(string query)
//{
// var searcher = new SearchHandler();
// var files = searcher.SearchFiles(query).Select(r => (FileEntryWrapper)FileWrapperHelper.Get(r));
// var folders = searcher.SearchFolders(query).Select(f => (FileEntryWrapper)FolderWrapperHelper.Get(f));
// return files.Concat(folders);
//}
2020-03-19 13:18:35 +00:00
private FolderContentWrapper<T> ToFolderContentWrapper(T folderId, Guid userIdOrGroupId, FilterType filterType, bool withSubFolders)
2020-03-18 13:19:37 +00:00
{
2021-05-23 16:11:25 +00:00
OrderBy orderBy = null;
if (Enum.TryParse(ApiContext.SortBy, true, out SortedByType sortBy))
2020-03-18 13:19:37 +00:00
{
2021-05-23 16:11:25 +00:00
orderBy = new OrderBy(sortBy, !ApiContext.SortDescending);
2020-03-18 13:19:37 +00:00
}
var startIndex = Convert.ToInt32(ApiContext.StartIndex);
return FolderContentWrapperHelper.Get(FileStorageService.GetFolderItems(folderId,
startIndex,
2020-09-28 10:41:48 +00:00
Convert.ToInt32(ApiContext.Count),
2020-03-18 13:19:37 +00:00
filterType,
filterType == FilterType.ByUser,
userIdOrGroupId.ToString(),
ApiContext.FilterValue,
false,
2021-11-15 11:31:47 +00:00
withSubFolders,
orderBy),
startIndex);
}
private async Task<FolderContentWrapper<T>> ToFolderContentWrapperAsync(T folderId, Guid userIdOrGroupId, FilterType filterType, bool withSubFolders)
{
OrderBy orderBy = null;
if (Enum.TryParse(ApiContext.SortBy, true, out SortedByType sortBy))
{
orderBy = new OrderBy(sortBy, !ApiContext.SortDescending);
}
var startIndex = Convert.ToInt32(ApiContext.StartIndex);
return FolderContentWrapperHelper.Get(await FileStorageService.GetFolderItemsAsync(folderId,
startIndex,
Convert.ToInt32(ApiContext.Count),
filterType,
filterType == FilterType.ByUser,
userIdOrGroupId.ToString(),
ApiContext.FilterValue,
false,
2020-03-19 13:18:35 +00:00
withSubFolders,
2021-05-23 16:11:25 +00:00
orderBy),
2020-03-18 13:19:37 +00:00
startIndex);
}
2020-12-01 19:59:42 +00:00
internal FileEntryWrapper GetFileEntryWrapper(FileEntry r)
{
FileEntryWrapper wrapper = null;
if (r is Folder<int> fol1)
{
wrapper = FolderWrapperHelper.Get(fol1);
}
else if (r is Folder<string> fol2)
{
wrapper = FolderWrapperHelper.Get(fol2);
}
else if (r is File<int> file1)
{
wrapper = FileWrapperHelper.Get(file1);
}
else if (r is File<string> file2)
{
wrapper = FileWrapperHelper.Get(file2);
}
return wrapper;
}
2021-09-29 16:08:30 +00:00
2021-11-22 13:17:27 +00:00
internal async Task<FileEntryWrapper> GetFileEntryWrapperAsync(FileEntry r)
{
FileEntryWrapper wrapper = null;
if (r is Folder<int> fol1)
{
wrapper = await FolderWrapperHelper.GetAsync(fol1);
}
else if (r is Folder<string> fol2)
{
wrapper = await FolderWrapperHelper.GetAsync(fol2);
}
else if (r is File<int> file1)
{
wrapper = await FileWrapperHelper.GetAsync(file1);
}
else if (r is File<string> file2)
{
wrapper = await FileWrapperHelper.GetAsync(file2);
}
return wrapper;
}
2021-09-29 16:08:30 +00:00
internal IFormFile GetFileFromRequest(IModelWithFile model)
{
IEnumerable<IFormFile> files = HttpContextAccessor.HttpContext.Request.Form.Files;
if (files != null && files.Any())
{
return files.First();
}
return model.File;
}
2020-03-18 13:19:37 +00:00
}
}