/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL 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 more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ASC.Api.Core;
using ASC.Api.Utils;
using ASC.Common;
using ASC.Core;
using ASC.Core.Common.Configuration;
using ASC.Core.Users;
using ASC.FederatedLogin.Helpers;
using ASC.FederatedLogin.LoginProviders;
using ASC.Files.Core;
using ASC.Files.Core.Model;
using ASC.Files.Helpers;
using ASC.Files.Model;
using ASC.MessagingSystem;
using ASC.Web.Api.Routing;
using ASC.Web.Core.Files;
using ASC.Web.Files.Classes;
using ASC.Web.Files.Configuration;
using ASC.Web.Files.Helpers;
using ASC.Web.Files.Services.DocumentService;
using ASC.Web.Files.Services.WCFService;
using ASC.Web.Files.Services.WCFService.FileOperations;
using ASC.Web.Files.Utils;
using ASC.Web.Studio.Core;
using ASC.Web.Studio.Utility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
namespace ASC.Api.Documents
{
///
/// Provides access to documents
///
[Scope]
[DefaultRoute]
[ApiController]
public class FilesController : ControllerBase
{
private readonly FileStorageService FileStorageService;
private FilesControllerHelper FilesControllerHelperString { get; }
private FilesControllerHelper FilesControllerHelperInt { get; }
private FileStorageService FileStorageServiceInt { get; }
private GlobalFolderHelper GlobalFolderHelper { get; }
private FilesSettingsHelper FilesSettingsHelper { get; }
private FilesLinkUtility FilesLinkUtility { get; }
private SecurityContext SecurityContext { get; }
private FolderWrapperHelper FolderWrapperHelper { get; }
private FileOperationWraperHelper FileOperationWraperHelper { get; }
private EntryManager EntryManager { get; }
private UserManager UserManager { get; }
private CoreBaseSettings CoreBaseSettings { get; }
private ThirdpartyConfiguration ThirdpartyConfiguration { get; }
private MessageService MessageService { get; }
private CommonLinkUtility CommonLinkUtility { get; }
private DocumentServiceConnector DocumentServiceConnector { get; }
private WordpressToken WordpressToken { get; }
private WordpressHelper WordpressHelper { get; }
private EasyBibHelper EasyBibHelper { get; }
private ProductEntryPoint ProductEntryPoint { get; }
private TenantManager TenantManager { get; }
private FileUtility FileUtility { get; }
///
///
///
///
public FilesController(
FilesControllerHelper filesControllerHelperString,
FilesControllerHelper filesControllerHelperInt,
FileStorageService fileStorageService,
FileStorageService fileStorageServiceInt,
GlobalFolderHelper globalFolderHelper,
FilesSettingsHelper filesSettingsHelper,
FilesLinkUtility filesLinkUtility,
SecurityContext securityContext,
FolderWrapperHelper folderWrapperHelper,
FileOperationWraperHelper fileOperationWraperHelper,
EntryManager entryManager,
UserManager userManager,
CoreBaseSettings coreBaseSettings,
ThirdpartyConfiguration thirdpartyConfiguration,
MessageService messageService,
CommonLinkUtility commonLinkUtility,
DocumentServiceConnector documentServiceConnector,
WordpressToken wordpressToken,
WordpressHelper wordpressHelper,
ProductEntryPoint productEntryPoint,
TenantManager tenantManager,
FileUtility fileUtility,
ConsumerFactory consumerFactory)
{
FilesControllerHelperString = filesControllerHelperString;
FilesControllerHelperInt = filesControllerHelperInt;
FileStorageService = fileStorageService;
FileStorageServiceInt = fileStorageServiceInt;
GlobalFolderHelper = globalFolderHelper;
FilesSettingsHelper = filesSettingsHelper;
FilesLinkUtility = filesLinkUtility;
SecurityContext = securityContext;
FolderWrapperHelper = folderWrapperHelper;
FileOperationWraperHelper = fileOperationWraperHelper;
EntryManager = entryManager;
UserManager = userManager;
CoreBaseSettings = coreBaseSettings;
ThirdpartyConfiguration = thirdpartyConfiguration;
MessageService = messageService;
CommonLinkUtility = commonLinkUtility;
DocumentServiceConnector = documentServiceConnector;
WordpressToken = wordpressToken;
WordpressHelper = wordpressHelper;
EasyBibHelper = consumerFactory.Get();
ProductEntryPoint = productEntryPoint;
TenantManager = tenantManager;
FileUtility = fileUtility;
}
[Read("info")]
public Module GetModule()
{
ProductEntryPoint.Init();
return new Module(ProductEntryPoint);
}
[Read("@root")]
public IEnumerable> GetRootFolders(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders, bool withoutTrash, bool withoutAdditionalFolder)
{
var IsVisitor = UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor(UserManager);
var IsOutsider = UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsOutsider(UserManager);
var result = new SortedSet();
if (IsOutsider)
{
withoutTrash = true;
withoutAdditionalFolder = true;
}
if (!IsVisitor)
{
result.Add(GlobalFolderHelper.FolderMy);
}
if (!CoreBaseSettings.Personal && !UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsOutsider(UserManager))
{
result.Add(GlobalFolderHelper.FolderShare);
}
if (!IsVisitor && !withoutAdditionalFolder)
{
if (FilesSettingsHelper.FavoritesSection)
{
result.Add(GlobalFolderHelper.FolderFavorites);
}
if (FilesSettingsHelper.RecentSection)
{
result.Add(GlobalFolderHelper.FolderRecent);
}
if (!CoreBaseSettings.Personal && PrivacyRoomSettings.IsAvailable(TenantManager))
{
result.Add(GlobalFolderHelper.FolderPrivacy);
}
}
if (!CoreBaseSettings.Personal)
{
result.Add(GlobalFolderHelper.FolderCommon);
}
if (!IsVisitor
&& !withoutAdditionalFolder
&& FileUtility.ExtsWebTemplate.Any()
&& FilesSettingsHelper.TemplatesSection)
{
result.Add(GlobalFolderHelper.FolderTemplates);
}
if (!withoutTrash)
{
result.Add((int)GlobalFolderHelper.FolderTrash);
}
return result.Select(r => FilesControllerHelperInt.GetFolder(r, userIdOrGroupId, filterType, withsubfolders)).ToList();
}
[Read("@rootAsync")]
public async Task>> GetRootFoldersAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders, bool withoutTrash, bool withoutAdditionalFolder)
{
var IsVisitor = UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor(UserManager);
var IsOutsider = UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsOutsider(UserManager);
var result = new SortedSet();
if (IsOutsider)
{
withoutTrash = true;
withoutAdditionalFolder = true;
}
if (!IsVisitor)
{
result.Add(GlobalFolderHelper.FolderMy);
}
if (!CoreBaseSettings.Personal && !UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsOutsider(UserManager))
{
result.Add(GlobalFolderHelper.FolderShare);
}
if (!IsVisitor && !withoutAdditionalFolder)
{
if (FilesSettingsHelper.FavoritesSection)
{
result.Add(GlobalFolderHelper.FolderFavorites);
}
if (FilesSettingsHelper.RecentSection)
{
result.Add(GlobalFolderHelper.FolderRecent);
}
if (!CoreBaseSettings.Personal && PrivacyRoomSettings.IsAvailable(TenantManager))
{
result.Add(GlobalFolderHelper.FolderPrivacy);
}
}
if (!CoreBaseSettings.Personal)
{
result.Add(GlobalFolderHelper.FolderCommon);
}
if (!IsVisitor
&& !withoutAdditionalFolder
&& FileUtility.ExtsWebTemplate.Any()
&& FilesSettingsHelper.TemplatesSection)
{
result.Add(GlobalFolderHelper.FolderTemplates);
}
if (!withoutTrash)
{
result.Add((int)GlobalFolderHelper.FolderTrash);
}
return await result.ToAsyncEnumerable().SelectAwait(async r => await FilesControllerHelperInt.GetFolderAsync(r, userIdOrGroupId, filterType, withsubfolders)).ToListAsync();
}
[Read("@privacy")]
public FolderContentWrapper GetPrivacyFolder(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
if (!IsAvailablePrivacyRoomSettings()) throw new System.Security.SecurityException();
return FilesControllerHelperInt.GetFolder(GlobalFolderHelper.FolderPrivacy, userIdOrGroupId, filterType, withsubfolders);
}
[Read("@privacyAsync")]
public async Task> GetPrivacyFolderAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
if (!IsAvailablePrivacyRoomSettings()) throw new System.Security.SecurityException();
return await FilesControllerHelperInt.GetFolderAsync(GlobalFolderHelper.FolderPrivacy, userIdOrGroupId, filterType, withsubfolders);
}
[Read("@privacy/available")]
public bool IsAvailablePrivacyRoomSettings()
{
return PrivacyRoomSettings.IsAvailable(TenantManager);
}
///
/// Returns the detailed list of files and folders located in the current user 'My Documents' section
///
///
/// My folder
///
/// Folders
/// My folder contents
[Read("@my")]
public FolderContentWrapper GetMyFolder(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperInt.GetFolder(GlobalFolderHelper.FolderMy, userIdOrGroupId, filterType, withsubfolders);
}
[Read("@myAsync")]
public async Task> GetMyFolderAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return await FilesControllerHelperInt.GetFolderAsync(GlobalFolderHelper.FolderMy, userIdOrGroupId, filterType, withsubfolders);
}
///
/// Returns the detailed list of files and folders located in the current user 'Projects Documents' section
///
///
/// Projects folder
///
/// Folders
/// Projects folder contents
[Read("@projects")]
public FolderContentWrapper GetProjectsFolder(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperString.GetFolder(GlobalFolderHelper.GetFolderProjects(), userIdOrGroupId, filterType, withsubfolders);
}
[Read("@projectsAsync")]
public async Task> GetProjectsFolderAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return await FilesControllerHelperString.GetFolderAsync(GlobalFolderHelper.GetFolderProjects(), userIdOrGroupId, filterType, withsubfolders);
}
///
/// Returns the detailed list of files and folders located in the 'Common Documents' section
///
///
/// Common folder
///
/// Folders
/// Common folder contents
[Read("@common")]
public FolderContentWrapper GetCommonFolder(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperInt.GetFolder(GlobalFolderHelper.FolderCommon, userIdOrGroupId, filterType, withsubfolders);
}
[Read("@commonAsync")]
public async Task> GetCommonFolderAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return await FilesControllerHelperInt.GetFolderAsync(GlobalFolderHelper.FolderCommon, userIdOrGroupId, filterType, withsubfolders);
}
///
/// Returns the detailed list of files and folders located in the 'Shared with Me' section
///
///
/// Shared folder
///
/// Folders
/// Shared folder contents
[Read("@share")]
public FolderContentWrapper GetShareFolder(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperInt.GetFolder(GlobalFolderHelper.FolderShare, userIdOrGroupId, filterType, withsubfolders);
}
[Read("@shareAsync")]
public async Task> GetShareFolderAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return await FilesControllerHelperInt.GetFolderAsync(GlobalFolderHelper.FolderShare, userIdOrGroupId, filterType, withsubfolders);
}
///
/// Returns the detailed list of recent files
///
/// Section Recent
/// Folders
/// Recent contents
[Read("@recent")]
public FolderContentWrapper GetRecentFolder(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperInt.GetFolder(GlobalFolderHelper.FolderRecent, userIdOrGroupId, filterType, withsubfolders);
}
[Read("@recentAsync")]
public async Task> GetRecentFolderAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return await FilesControllerHelperInt.GetFolderAsync(GlobalFolderHelper.FolderRecent, userIdOrGroupId, filterType, withsubfolders);
}
[Create("file/{fileId}/recent", order: int.MaxValue)]
public FileWrapper AddToRecent(string fileId)
{
return FilesControllerHelperString.AddToRecent(fileId);
}
[Create("file/{fileId}/recentAsync", order: int.MaxValue)]
public async Task> AddToRecentAsync(string fileId)
{
return await FilesControllerHelperString.AddToRecentAsync(fileId);
}
[Create("file/{fileId:int}/recent", order: int.MaxValue - 1)]
public FileWrapper AddToRecent(int fileId)
{
return FilesControllerHelperInt.AddToRecent(fileId);
}
[Create("file/{fileId:int}/recentAsync", order: int.MaxValue - 1)]
public async Task> AddToRecentAsync(int fileId)
{
return await FilesControllerHelperInt.AddToRecentAsync(fileId);
}
///
/// Returns the detailed list of favorites files
///
/// Section Favorite
/// Folders
/// Favorites contents
[Read("@favorites")]
public FolderContentWrapper GetFavoritesFolder(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperInt.GetFolder(GlobalFolderHelper.FolderFavorites, userIdOrGroupId, filterType, withsubfolders);
}
[Read("@favoritesAsync")]
public async Task> GetFavoritesFolderAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return await FilesControllerHelperInt.GetFolderAsync(GlobalFolderHelper.FolderFavorites, userIdOrGroupId, filterType, withsubfolders);
}
///
/// Returns the detailed list of templates files
///
/// Section Template
/// Folders
/// Templates contents
[Read("@templates")]
public FolderContentWrapper GetTemplatesFolder(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperInt.GetFolder(GlobalFolderHelper.FolderTemplates, userIdOrGroupId, filterType, withsubfolders);
}
[Read("@templatesAsync")]
public async Task> GetTemplatesFolderAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return await FilesControllerHelperInt.GetFolderAsync(GlobalFolderHelper.FolderTemplates, userIdOrGroupId, filterType, withsubfolders);
}
///
/// Returns the detailed list of files and folders located in the 'Recycle Bin' section
///
///
/// Trash folder
///
/// Folders
/// Trash folder contents
[Read("@trash")]
public FolderContentWrapper GetTrashFolder(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperInt.GetFolder(Convert.ToInt32(GlobalFolderHelper.FolderTrash), userIdOrGroupId, filterType, withsubfolders);
}
[Read("@trashAsync")]
public async Task> GetTrashFolderAsync(Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return await FilesControllerHelperInt.GetFolderAsync(Convert.ToInt32(GlobalFolderHelper.FolderTrash), userIdOrGroupId, filterType, withsubfolders);
}
///
/// Returns the detailed list of files and folders located in the folder with the ID specified in the request
///
///
/// Folder by ID
///
/// Folders
/// Folder ID
/// User or group ID
/// Filter type
/// Folder contents
[Read("{folderId}", order: int.MaxValue, DisableFormat = true)]
public FolderContentWrapper GetFolder(string folderId, Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperString.GetFolder(folderId, userIdOrGroupId, filterType, withsubfolders).NotFoundIfNull();
}
[Read("{folderId}/Async", order: int.MaxValue, DisableFormat = true)]
public async Task> GetFolderAsync(string folderId, Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
var folder = await FilesControllerHelperString.GetFolderAsync(folderId, userIdOrGroupId, filterType, withsubfolders);
return folder.NotFoundIfNull();
}
[Read("{folderId:int}", order: int.MaxValue - 1, DisableFormat = true)]
public FolderContentWrapper GetFolder(int folderId, Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return FilesControllerHelperInt.GetFolder(folderId, userIdOrGroupId, filterType, withsubfolders);
}
[Read("{folderId:int}/async", order: int.MaxValue - 1)]
public async Task> GetFolderAsync(int folderId, Guid userIdOrGroupId, FilterType filterType, bool withsubfolders)
{
return await FilesControllerHelperInt.GetFolderAsync(folderId, userIdOrGroupId, filterType, withsubfolders);
}
[Read("{folderId}/subfolders")]
public IEnumerable GetFolders(string folderId)
{
return FilesControllerHelperString.GetFolders(folderId);
}
[Read("{folderId}/subfoldersAsync")]
public async Task> GetFoldersAsync(string folderId)
{
return await FilesControllerHelperString.GetFoldersAsync(folderId);
}
[Read("{folderId:int}/subfolders")]
public IEnumerable GetFolders(int folderId)
{
return FilesControllerHelperInt.GetFolders(folderId);
}
[Read("{folderId:int}/subfoldersAsync")]
public async Task> GetFoldersAsync(int folderId)
{
return await FilesControllerHelperInt.GetFoldersAsync(folderId);
}
[Read("{folderId}/news")]
public List GetNewItems(string folderId)
{
return FilesControllerHelperString.GetNewItems(folderId);
}
[Read("{folderId}/newsAsync")]
public async Task> GetNewItemsAsync(string folderId)
{
return await FilesControllerHelperString.GetNewItemsAsync(folderId);
}
[Read("{folderId:int}/news")]
public List GetNewItems(int folderId)
{
return FilesControllerHelperInt.GetNewItems(folderId);
}
[Read("{folderId:int}/newsAsync")]
public async Task> GetNewItemsAsync(int folderId)
{
return await FilesControllerHelperInt.GetNewItemsAsync(folderId);
}
///
/// Uploads the file specified with single file upload or standart multipart/form-data method to 'My Documents' section
///
/// Upload to My
/// Uploads
///
///
/// Single file upload. You should set Content-Type & Content-Disposition header to specify filename and content type, and send file in request body
/// Using standart multipart/form-data method
/// ]]>
///
/// Request Input stream
/// Content-Type Header
/// Content-Disposition Header
/// List of files when posted as multipart/form-data
/// Uploaded file
[Create("@my/upload")]
public object UploadFileToMy([ModelBinder(BinderType = typeof(UploadModelBinder))] UploadModel uploadModel)
{
uploadModel.CreateNewIfExist = false;
return FilesControllerHelperInt.UploadFile(GlobalFolderHelper.FolderMy, uploadModel);
}
[Create("@my/uploadAsync")]
public async Task