2020-05-20 15:14:44 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2020
|
|
|
|
*
|
|
|
|
* 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-06-01 08:52:01 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
|
2020-06-10 12:35:10 +00:00
|
|
|
using ASC.Common;
|
2020-05-20 15:14:44 +00:00
|
|
|
using ASC.Core;
|
|
|
|
using ASC.Data.Storage;
|
2020-05-29 14:48:27 +00:00
|
|
|
using ASC.Files.Core;
|
2021-05-17 18:27:39 +00:00
|
|
|
using ASC.Web.Studio.Core;
|
2020-06-10 12:35:10 +00:00
|
|
|
//using File = ASC.Files.Core.File;
|
2020-06-01 08:52:01 +00:00
|
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-05-20 15:14:44 +00:00
|
|
|
|
|
|
|
namespace ASC.Data.Backup.Storage
|
2020-10-19 15:53:15 +00:00
|
|
|
{
|
|
|
|
[Scope]
|
2020-05-20 15:14:44 +00:00
|
|
|
public class DocumentsBackupStorage : IBackupStorage
|
|
|
|
{
|
2020-06-01 08:52:01 +00:00
|
|
|
private int TenantId { get; set; }
|
2021-05-17 18:27:39 +00:00
|
|
|
private string WebConfigPath { get; set; }
|
|
|
|
private SetupInfo SetupInfo { get; }
|
2020-06-01 08:52:01 +00:00
|
|
|
private TenantManager TenantManager { get; set; }
|
|
|
|
private SecurityContext SecurityContext { get; set; }
|
|
|
|
private IDaoFactory DaoFactory { get; set; }
|
|
|
|
private StorageFactory StorageFactory { get; set; }
|
2020-08-12 09:58:08 +00:00
|
|
|
private IServiceProvider ServiceProvider { get; }
|
2020-06-01 08:52:01 +00:00
|
|
|
|
|
|
|
public DocumentsBackupStorage(
|
2021-05-17 18:27:39 +00:00
|
|
|
SetupInfo setupInfo,
|
2020-06-01 08:52:01 +00:00
|
|
|
TenantManager tenantManager,
|
|
|
|
SecurityContext securityContext,
|
|
|
|
IDaoFactory daoFactory,
|
|
|
|
StorageFactory storageFactory,
|
|
|
|
IServiceProvider serviceProvider)
|
2021-05-17 18:27:39 +00:00
|
|
|
{
|
|
|
|
SetupInfo = setupInfo;
|
2020-06-01 08:52:01 +00:00
|
|
|
TenantManager = tenantManager;
|
|
|
|
SecurityContext = securityContext;
|
|
|
|
DaoFactory = daoFactory;
|
|
|
|
StorageFactory = storageFactory;
|
|
|
|
ServiceProvider = serviceProvider;
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2020-06-28 11:46:16 +00:00
|
|
|
public void Init(int tenantId, string webConfigPath)
|
2020-06-10 12:35:10 +00:00
|
|
|
{
|
|
|
|
TenantId = tenantId;
|
|
|
|
WebConfigPath = webConfigPath;
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
public string Upload(string folderId, string localPath, Guid userId)
|
2020-06-01 08:52:01 +00:00
|
|
|
{
|
|
|
|
TenantManager.SetCurrentTenant(TenantId);
|
|
|
|
if (!userId.Equals(Guid.Empty))
|
|
|
|
{
|
2021-08-18 14:04:16 +00:00
|
|
|
SecurityContext.AuthenticateMeWithoutCookie(userId);
|
2020-06-01 08:52:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var tenant = TenantManager.GetTenant(TenantId);
|
2021-08-18 14:04:16 +00:00
|
|
|
SecurityContext.AuthenticateMeWithoutCookie(tenant.OwnerId);
|
2020-06-01 08:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (int.TryParse(folderId, out var fId))
|
|
|
|
{
|
|
|
|
return Upload(fId, localPath).ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Upload(folderId, localPath);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Download(string fileId, string targetLocalPath)
|
2020-06-01 08:52:01 +00:00
|
|
|
{
|
|
|
|
TenantManager.SetCurrentTenant(TenantId);
|
|
|
|
|
|
|
|
if (int.TryParse(fileId, out var fId))
|
|
|
|
{
|
|
|
|
DownloadDao(fId, targetLocalPath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadDao(fileId, targetLocalPath);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(string fileId)
|
2020-06-01 08:52:01 +00:00
|
|
|
{
|
|
|
|
TenantManager.SetCurrentTenant(TenantId);
|
|
|
|
|
|
|
|
if (int.TryParse(fileId, out var fId))
|
|
|
|
{
|
|
|
|
DeleteDao(fId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteDao(fileId);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsExists(string fileId)
|
2020-06-01 08:52:01 +00:00
|
|
|
{
|
|
|
|
TenantManager.SetCurrentTenant(TenantId);
|
|
|
|
if (int.TryParse(fileId, out var fId))
|
|
|
|
{
|
|
|
|
return IsExistsDao(fId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return IsExistsDao(fileId);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetPublicLink(string fileId)
|
|
|
|
{
|
|
|
|
return string.Empty;
|
2020-06-01 08:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private T Upload<T>(T folderId, string localPath)
|
|
|
|
{
|
|
|
|
var folderDao = GetFolderDao<T>();
|
|
|
|
var fileDao = GetFileDao<T>();
|
|
|
|
|
2021-12-23 11:46:21 +00:00
|
|
|
var folder = folderDao.GetFolderAsync(folderId).Result;
|
2020-06-01 08:52:01 +00:00
|
|
|
if (folder == null)
|
|
|
|
{
|
|
|
|
throw new FileNotFoundException("Folder not found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
using var source = File.OpenRead(localPath);
|
|
|
|
var newFile = ServiceProvider.GetService<File<T>>();
|
|
|
|
newFile.Title = Path.GetFileName(localPath);
|
|
|
|
newFile.FolderID = folder.ID;
|
|
|
|
newFile.ContentLength = source.Length;
|
|
|
|
|
2021-05-17 18:27:39 +00:00
|
|
|
File<T> file = null;
|
|
|
|
var buffer = new byte[SetupInfo.ChunkUploadSize];
|
2021-12-23 11:46:21 +00:00
|
|
|
var chunkedUploadSession = fileDao.CreateUploadSessionAsync(newFile, source.Length).Result;
|
2021-05-17 18:27:39 +00:00
|
|
|
chunkedUploadSession.CheckQuota = false;
|
|
|
|
|
2022-02-11 21:51:48 +00:00
|
|
|
int bytesRead;
|
2021-05-17 18:27:39 +00:00
|
|
|
|
|
|
|
while ((bytesRead = source.Read(buffer, 0, (int)SetupInfo.ChunkUploadSize)) > 0)
|
|
|
|
{
|
|
|
|
using (var theMemStream = new MemoryStream())
|
|
|
|
{
|
|
|
|
theMemStream.Write(buffer, 0, bytesRead);
|
|
|
|
theMemStream.Position = 0;
|
2021-12-23 11:46:21 +00:00
|
|
|
file = fileDao.UploadChunkAsync(chunkedUploadSession, theMemStream, bytesRead).Result;
|
2021-05-17 18:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-01 08:52:01 +00:00
|
|
|
|
|
|
|
return file.ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DownloadDao<T>(T fileId, string targetLocalPath)
|
|
|
|
{
|
|
|
|
TenantManager.SetCurrentTenant(TenantId);
|
|
|
|
var fileDao = GetFileDao<T>();
|
2021-11-22 08:30:38 +00:00
|
|
|
var file = fileDao.GetFileAsync(fileId).Result;
|
2020-06-01 08:52:01 +00:00
|
|
|
if (file == null)
|
|
|
|
{
|
|
|
|
throw new FileNotFoundException("File not found.");
|
|
|
|
}
|
|
|
|
|
2021-11-22 08:30:38 +00:00
|
|
|
using var source = fileDao.GetFileStreamAsync(file).Result;
|
2020-06-01 08:52:01 +00:00
|
|
|
using var destination = File.OpenWrite(targetLocalPath);
|
|
|
|
source.CopyTo(destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DeleteDao<T>(T fileId)
|
|
|
|
{
|
|
|
|
var fileDao = GetFileDao<T>();
|
2021-11-15 11:31:47 +00:00
|
|
|
fileDao.DeleteFileAsync(fileId).Wait();
|
2020-06-01 08:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsExistsDao<T>(T fileId)
|
|
|
|
{
|
|
|
|
var fileDao = GetFileDao<T>();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
2021-11-22 08:30:38 +00:00
|
|
|
var file = fileDao.GetFileAsync(fileId).Result;
|
|
|
|
return file != null && file.RootFolderType != FolderType.TRASH;
|
2020-06-01 08:52:01 +00:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private IFolderDao<T> GetFolderDao<T>()
|
|
|
|
{
|
|
|
|
return DaoFactory.GetFolderDao<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private IFileDao<T> GetFileDao<T>()
|
|
|
|
{
|
|
|
|
// hack: create storage using webConfigPath and put it into DataStoreCache
|
|
|
|
// FileDao will use this storage and will not try to create the new one from service config
|
|
|
|
StorageFactory.GetStorage(WebConfigPath, TenantId.ToString(), "files");
|
|
|
|
return DaoFactory.GetFileDao<T>();
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|