2020-01-27 11:15:18 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-01-31 07:56:40 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Security;
|
|
|
|
using System.Threading;
|
|
|
|
|
2020-02-26 10:01:12 +00:00
|
|
|
using ASC.Common;
|
2020-01-31 07:56:40 +00:00
|
|
|
using ASC.Core;
|
2020-01-27 11:15:18 +00:00
|
|
|
using ASC.Core.Users;
|
|
|
|
using ASC.Files.Core;
|
2020-02-26 10:01:12 +00:00
|
|
|
using ASC.Files.Core.Data;
|
2020-02-06 11:55:11 +00:00
|
|
|
using ASC.Files.Core.Security;
|
2020-02-18 08:39:28 +00:00
|
|
|
using ASC.Files.Resources;
|
2020-01-27 11:15:18 +00:00
|
|
|
using ASC.MessagingSystem;
|
|
|
|
using ASC.Web.Core.Files;
|
|
|
|
using ASC.Web.Files.Classes;
|
|
|
|
using ASC.Web.Files.Helpers;
|
|
|
|
using ASC.Web.Studio.Core;
|
2020-01-31 07:56:40 +00:00
|
|
|
using ASC.Web.Studio.UserControls.Statistics;
|
|
|
|
using ASC.Web.Studio.Utility;
|
|
|
|
|
2020-02-10 16:04:54 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
2020-01-27 11:15:18 +00:00
|
|
|
using File = ASC.Files.Core.File;
|
|
|
|
|
|
|
|
namespace ASC.Web.Files.Utils
|
|
|
|
{
|
2020-01-31 07:56:40 +00:00
|
|
|
public class FileUploader
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
2020-01-31 07:56:40 +00:00
|
|
|
public FilesSettingsHelper FilesSettingsHelper { get; }
|
|
|
|
public FileUtility FileUtility { get; }
|
|
|
|
public UserManager UserManager { get; }
|
|
|
|
public TenantManager TenantManager { get; }
|
|
|
|
public AuthContext AuthContext { get; }
|
|
|
|
public SetupInfo SetupInfo { get; }
|
|
|
|
public TenantExtra TenantExtra { get; }
|
|
|
|
public TenantStatisticsProvider TenantStatisticsProvider { get; }
|
2020-02-06 11:55:11 +00:00
|
|
|
public FileMarker FileMarker { get; }
|
|
|
|
public FileConverter FileConverter { get; }
|
|
|
|
public IDaoFactory DaoFactory { get; }
|
|
|
|
public Global Global { get; }
|
|
|
|
public FilesLinkUtility FilesLinkUtility { get; }
|
|
|
|
public FilesMessageService FilesMessageService { get; }
|
|
|
|
public FileSecurity FileSecurity { get; }
|
|
|
|
public EntryManager EntryManager { get; }
|
2020-02-10 16:04:54 +00:00
|
|
|
public IServiceProvider ServiceProvider { get; }
|
2020-02-11 15:10:30 +00:00
|
|
|
public ChunkedUploadSessionHolder ChunkedUploadSessionHolder { get; }
|
2020-01-31 07:56:40 +00:00
|
|
|
|
|
|
|
public FileUploader(
|
|
|
|
FilesSettingsHelper filesSettingsHelper,
|
|
|
|
FileUtility fileUtility,
|
|
|
|
UserManager userManager,
|
|
|
|
TenantManager tenantManager,
|
|
|
|
AuthContext authContext,
|
|
|
|
SetupInfo setupInfo,
|
|
|
|
TenantExtra tenantExtra,
|
2020-02-06 11:55:11 +00:00
|
|
|
TenantStatisticsProvider tenantStatisticsProvider,
|
|
|
|
FileMarker fileMarker,
|
|
|
|
FileConverter fileConverter,
|
|
|
|
IDaoFactory daoFactory,
|
|
|
|
Global global,
|
|
|
|
FilesLinkUtility filesLinkUtility,
|
|
|
|
FilesMessageService filesMessageService,
|
|
|
|
FileSecurity fileSecurity,
|
2020-02-10 16:04:54 +00:00
|
|
|
EntryManager entryManager,
|
2020-02-11 15:10:30 +00:00
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
ChunkedUploadSessionHolder chunkedUploadSessionHolder)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
2020-01-31 07:56:40 +00:00
|
|
|
FilesSettingsHelper = filesSettingsHelper;
|
|
|
|
FileUtility = fileUtility;
|
|
|
|
UserManager = userManager;
|
|
|
|
TenantManager = tenantManager;
|
|
|
|
AuthContext = authContext;
|
|
|
|
SetupInfo = setupInfo;
|
|
|
|
TenantExtra = tenantExtra;
|
|
|
|
TenantStatisticsProvider = tenantStatisticsProvider;
|
2020-02-06 11:55:11 +00:00
|
|
|
FileMarker = fileMarker;
|
|
|
|
FileConverter = fileConverter;
|
|
|
|
DaoFactory = daoFactory;
|
|
|
|
Global = global;
|
|
|
|
FilesLinkUtility = filesLinkUtility;
|
|
|
|
FilesMessageService = filesMessageService;
|
|
|
|
FileSecurity = fileSecurity;
|
|
|
|
EntryManager = entryManager;
|
2020-02-10 16:04:54 +00:00
|
|
|
ServiceProvider = serviceProvider;
|
2020-02-11 15:10:30 +00:00
|
|
|
ChunkedUploadSessionHolder = chunkedUploadSessionHolder;
|
2020-01-27 11:15:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 07:56:40 +00:00
|
|
|
public File Exec(string folderId, string title, long contentLength, Stream data)
|
|
|
|
{
|
|
|
|
return Exec(folderId, title, contentLength, data, !FilesSettingsHelper.UpdateIfExist);
|
|
|
|
}
|
|
|
|
|
|
|
|
public File Exec(string folderId, string title, long contentLength, Stream data, bool createNewIfExist, bool deleteConvertStatus = true)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
|
|
|
if (contentLength <= 0)
|
|
|
|
throw new Exception(FilesCommonResource.ErrorMassage_EmptyFile);
|
|
|
|
|
|
|
|
var file = VerifyFileUpload(folderId, title, contentLength, !createNewIfExist);
|
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
var dao = DaoFactory.FileDao;
|
|
|
|
file = dao.SaveFile(file, data);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
|
|
|
FileMarker.MarkAsNew(file);
|
|
|
|
|
|
|
|
if (FileConverter.EnableAsUploaded && FileConverter.MustConvert(file))
|
|
|
|
FileConverter.ExecAsync(file, deleteConvertStatus);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-01-31 07:56:40 +00:00
|
|
|
public File VerifyFileUpload(string folderId, string fileName, bool updateIfExists, string relativePath = null)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
|
|
|
fileName = Global.ReplaceInvalidCharsAndTruncate(fileName);
|
|
|
|
|
|
|
|
if (Global.EnableUploadFilter && !FileUtility.ExtsUploadable.Contains(FileUtility.GetFileExtension(fileName)))
|
|
|
|
throw new NotSupportedException(FilesCommonResource.ErrorMassage_NotSupportedFormat);
|
|
|
|
|
|
|
|
folderId = GetFolderId(folderId, string.IsNullOrEmpty(relativePath) ? null : relativePath.Split('/').ToList());
|
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
var fileDao = DaoFactory.FileDao;
|
|
|
|
var file = fileDao.GetFile(folderId, fileName);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
if (updateIfExists && CanEdit(file))
|
|
|
|
{
|
|
|
|
file.Title = fileName;
|
|
|
|
file.ConvertedType = null;
|
|
|
|
file.Comment = FilesCommonResource.CommentUpload;
|
|
|
|
file.Version++;
|
|
|
|
file.VersionGroup++;
|
|
|
|
file.Encrypted = false;
|
|
|
|
|
|
|
|
return file;
|
2020-01-27 11:15:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 16:04:54 +00:00
|
|
|
var newFile = ServiceProvider.GetService<File>();
|
|
|
|
newFile.FolderID = folderId;
|
|
|
|
newFile.Title = fileName;
|
|
|
|
return newFile;
|
2020-01-27 11:15:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 07:56:40 +00:00
|
|
|
public File VerifyFileUpload(string folderId, string fileName, long fileSize, bool updateIfExists)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
|
|
|
if (fileSize <= 0)
|
|
|
|
throw new Exception(FilesCommonResource.ErrorMassage_EmptyFile);
|
|
|
|
|
|
|
|
var maxUploadSize = GetMaxFileSize(folderId);
|
|
|
|
|
|
|
|
if (fileSize > maxUploadSize)
|
|
|
|
throw FileSizeComment.GetFileSizeException(maxUploadSize);
|
|
|
|
|
|
|
|
var file = VerifyFileUpload(folderId, fileName, updateIfExists);
|
|
|
|
file.ContentLength = fileSize;
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-01-31 07:56:40 +00:00
|
|
|
private bool CanEdit(File file)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
|
|
|
return file != null
|
2020-02-06 11:55:11 +00:00
|
|
|
&& FileSecurity.CanEdit(file)
|
2020-01-31 07:56:40 +00:00
|
|
|
&& !UserManager.GetUsers(AuthContext.CurrentAccount.ID).IsVisitor(UserManager)
|
2020-01-27 11:15:18 +00:00
|
|
|
&& !EntryManager.FileLockedForMe(file.ID)
|
|
|
|
&& !FileTracker.IsEditing(file.ID)
|
|
|
|
&& file.RootFolderType != FolderType.TRASH
|
|
|
|
&& !file.Encrypted;
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
private string GetFolderId(object folderId, IList<string> relativePath)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
2020-02-06 11:55:11 +00:00
|
|
|
var folderDao = DaoFactory.FolderDao;
|
|
|
|
var folder = folderDao.GetFolder(folderId);
|
|
|
|
|
|
|
|
if (folder == null)
|
|
|
|
throw new DirectoryNotFoundException(FilesCommonResource.ErrorMassage_FolderNotFound);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
if (!FileSecurity.CanCreate(folder))
|
|
|
|
throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_Create);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
if (relativePath != null && relativePath.Any())
|
|
|
|
{
|
|
|
|
var subFolderTitle = Global.ReplaceInvalidCharsAndTruncate(relativePath.FirstOrDefault());
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
if (!string.IsNullOrEmpty(subFolderTitle))
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
2020-02-06 11:55:11 +00:00
|
|
|
folder = folderDao.GetFolder(subFolderTitle, folder.ID);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
if (folder == null)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
2020-02-11 11:51:45 +00:00
|
|
|
var newFolder = ServiceProvider.GetService<Folder>();
|
|
|
|
newFolder.Title = subFolderTitle;
|
|
|
|
newFolder.ParentFolderID = folderId;
|
|
|
|
|
|
|
|
folderId = folderDao.SaveFolder(newFolder);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
folder = folderDao.GetFolder(folderId);
|
|
|
|
FilesMessageService.Send(folder, MessageAction.FolderCreated, folder.Title);
|
|
|
|
}
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
folderId = folder.ID;
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
relativePath.RemoveAt(0);
|
|
|
|
folderId = GetFolderId(folderId, relativePath);
|
2020-01-27 11:15:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return folderId.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
#region chunked upload
|
|
|
|
|
2020-01-31 07:56:40 +00:00
|
|
|
public File VerifyChunkedUpload(string folderId, string fileName, long fileSize, bool updateIfExists, string relativePath = null)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
|
|
|
var maxUploadSize = GetMaxFileSize(folderId, true);
|
|
|
|
|
|
|
|
if (fileSize > maxUploadSize)
|
|
|
|
throw FileSizeComment.GetFileSizeException(maxUploadSize);
|
|
|
|
|
|
|
|
var file = VerifyFileUpload(folderId, fileName, updateIfExists, relativePath);
|
|
|
|
file.ContentLength = fileSize;
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-01-31 07:56:40 +00:00
|
|
|
public ChunkedUploadSession InitiateUpload(string folderId, string fileId, string fileName, long contentLength, bool encrypted)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(folderId))
|
|
|
|
folderId = null;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(fileId))
|
|
|
|
fileId = null;
|
|
|
|
|
2020-02-10 16:04:54 +00:00
|
|
|
var file = ServiceProvider.GetService<File>();
|
|
|
|
file.ID = fileId;
|
|
|
|
file.FolderID = folderId;
|
|
|
|
file.Title = fileName;
|
|
|
|
file.ContentLength = contentLength;
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
var dao = DaoFactory.FileDao;
|
|
|
|
var uploadSession = dao.CreateUploadSession(file, contentLength);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
uploadSession.Expired = uploadSession.Created + ChunkedUploadSessionHolder.SlidingExpiration;
|
|
|
|
uploadSession.Location = FilesLinkUtility.GetUploadChunkLocationUrl(uploadSession.Id);
|
|
|
|
uploadSession.TenantId = TenantManager.GetCurrentTenant().TenantId;
|
|
|
|
uploadSession.UserId = AuthContext.CurrentAccount.ID;
|
|
|
|
uploadSession.FolderId = folderId;
|
|
|
|
uploadSession.CultureName = Thread.CurrentThread.CurrentUICulture.Name;
|
|
|
|
uploadSession.Encrypted = encrypted;
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
ChunkedUploadSessionHolder.StoreSession(uploadSession);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
return uploadSession;
|
2020-01-27 11:15:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 07:56:40 +00:00
|
|
|
public ChunkedUploadSession UploadChunk(string uploadId, Stream stream, long chunkLength)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
|
|
|
var uploadSession = ChunkedUploadSessionHolder.GetSession(uploadId);
|
|
|
|
uploadSession.Expired = DateTime.UtcNow + ChunkedUploadSessionHolder.SlidingExpiration;
|
|
|
|
|
|
|
|
if (chunkLength <= 0)
|
|
|
|
{
|
|
|
|
throw new Exception(FilesCommonResource.ErrorMassage_EmptyFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunkLength > SetupInfo.ChunkUploadSize)
|
|
|
|
{
|
2020-01-31 07:56:40 +00:00
|
|
|
throw FileSizeComment.GetFileSizeException(SetupInfo.MaxUploadSize(TenantExtra, TenantStatisticsProvider));
|
2020-01-27 11:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var maxUploadSize = GetMaxFileSize(uploadSession.FolderId, uploadSession.BytesTotal > 0);
|
|
|
|
|
|
|
|
if (uploadSession.BytesUploaded + chunkLength > maxUploadSize)
|
|
|
|
{
|
|
|
|
AbortUpload(uploadSession);
|
|
|
|
throw FileSizeComment.GetFileSizeException(maxUploadSize);
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
var dao = DaoFactory.FileDao;
|
|
|
|
dao.UploadChunk(uploadSession, stream, chunkLength);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
|
|
|
if (uploadSession.BytesUploaded == uploadSession.BytesTotal)
|
|
|
|
{
|
|
|
|
FileMarker.MarkAsNew(uploadSession.File);
|
|
|
|
ChunkedUploadSessionHolder.RemoveSession(uploadSession);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ChunkedUploadSessionHolder.StoreSession(uploadSession);
|
|
|
|
}
|
|
|
|
|
|
|
|
return uploadSession;
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
public void AbortUpload(string uploadId)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
|
|
|
AbortUpload(ChunkedUploadSessionHolder.GetSession(uploadId));
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
private void AbortUpload(ChunkedUploadSession uploadSession)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
2020-02-06 11:55:11 +00:00
|
|
|
DaoFactory.FileDao.AbortUploadSession(uploadSession);
|
2020-01-27 11:15:18 +00:00
|
|
|
|
|
|
|
ChunkedUploadSessionHolder.RemoveSession(uploadSession);
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:55:11 +00:00
|
|
|
private long GetMaxFileSize(object folderId, bool chunkedUpload = false)
|
2020-01-27 11:15:18 +00:00
|
|
|
{
|
2020-02-06 11:55:11 +00:00
|
|
|
var folderDao = DaoFactory.FolderDao;
|
|
|
|
return folderDao.GetMaxUploadSize(folderId, chunkedUpload);
|
2020-01-27 11:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
2020-02-26 10:01:12 +00:00
|
|
|
|
|
|
|
public static class FileUploaderExtention
|
|
|
|
{
|
|
|
|
public static DIHelper AddFileUploaderService(this DIHelper services)
|
|
|
|
{
|
|
|
|
services.TryAddScoped<FileUploader>();
|
|
|
|
|
|
|
|
return services
|
|
|
|
.AddChunkedUploadSessionHolderService()
|
|
|
|
.AddEntryManagerService()
|
|
|
|
.AddFileSecurityService()
|
|
|
|
.AddFilesLinkUtilityService()
|
|
|
|
.AddFilesMessageService()
|
|
|
|
.AddGlobalService()
|
|
|
|
.AddDaoFactoryService()
|
|
|
|
.AddFileConverterService()
|
|
|
|
.AddFileMarkerService()
|
|
|
|
.AddTenantStatisticsProviderService()
|
|
|
|
.AddTenantExtraService()
|
|
|
|
.AddUserManagerService()
|
|
|
|
.AddTenantManagerService()
|
|
|
|
.AddAuthContextService()
|
|
|
|
.AddSetupInfo()
|
|
|
|
.AddFileUtilityService()
|
|
|
|
.AddFilesSettingsHelperService()
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
2020-01-27 11:15:18 +00:00
|
|
|
}
|