DocSpace-client/products/ASC.Files/Server/Configuration/FilesSpaceUsageStatManager.cs
2020-02-18 11:39:28 +03:00

181 lines
8.4 KiB
C#

/*
*
* (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.Linq;
using ASC.Common;
using ASC.Core;
using ASC.Core.Common.EF;
using ASC.Core.Common.EF.Context;
using ASC.Core.Users;
using ASC.Files.Core;
using ASC.Files.Resources;
using ASC.Web.Core;
using ASC.Web.Core.Users;
using ASC.Web.Files.Classes;
using ASC.Web.Studio.Utility;
namespace ASC.Web.Files
{
public class FilesSpaceUsageStatManager : SpaceUsageStatManager
{
public ASC.Files.Core.EF.FilesDbContext FilesDbContext { get; }
public TenantManager TenantManager { get; }
public UserManager UserManager { get; }
public UserPhotoManager UserPhotoManager { get; }
public DisplayUserSettingsHelper DisplayUserSettingsHelper { get; }
public CommonLinkUtility CommonLinkUtility { get; }
public GlobalFolderHelper GlobalFolderHelper { get; }
public PathProvider PathProvider { get; }
public FilesSpaceUsageStatManager(
DbContextManager<ASC.Files.Core.EF.FilesDbContext> dbContextManager,
TenantManager tenantManager,
UserManager userManager,
UserPhotoManager userPhotoManager,
DisplayUserSettingsHelper displayUserSettingsHelper,
CommonLinkUtility commonLinkUtility,
GlobalFolderHelper globalFolderHelper,
PathProvider pathProvider)
{
FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId);
TenantManager = tenantManager;
UserManager = userManager;
UserPhotoManager = userPhotoManager;
DisplayUserSettingsHelper = displayUserSettingsHelper;
CommonLinkUtility = commonLinkUtility;
GlobalFolderHelper = globalFolderHelper;
PathProvider = pathProvider;
}
public override List<UsageSpaceStatItem> GetStatData()
{
var myFiles = FilesDbContext.Files
.Join(FilesDbContext.Tree, a => a.FolderId, b => b.FolderId, (file, tree) => new { file, tree })
.Join(FilesDbContext.BunchObjects, a => a.tree.ParentId.ToString(), b => b.LeftNode, (fileTree, bunch) => new { fileTree.file, fileTree.tree, bunch })
.Where(r => r.file.TenantId == r.bunch.TenantId)
.Where(r => r.file.TenantId == TenantManager.GetCurrentTenant().TenantId)
.Where(r => r.bunch.RightNode.StartsWith("files/my/") | r.bunch.RightNode.StartsWith("files/trash/"))
.GroupBy(r => r.file.CreateBy)
.Select(r => new { CreateBy = r.Key, Size = r.Sum(a => a.file.ContentLength) });
var commonFiles = FilesDbContext.Files
.Join(FilesDbContext.Tree, a => a.FolderId, b => b.FolderId, (file, tree) => new { file, tree })
.Join(FilesDbContext.BunchObjects, a => a.tree.ParentId.ToString(), b => b.LeftNode, (fileTree, bunch) => new { fileTree.file, fileTree.tree, bunch })
.Where(r => r.file.TenantId == r.bunch.TenantId)
.Where(r => r.file.TenantId == TenantManager.GetCurrentTenant().TenantId)
.Where(r => r.bunch.RightNode.StartsWith("files/common/"))
.GroupBy(r => Constants.LostUser.ID)
.Select(r => new { CreateBy = Constants.LostUser.ID, Size = r.Sum(a => a.file.ContentLength) });
return myFiles.Union(commonFiles)
.ToList()
.GroupBy(
r => r.CreateBy,
r => r.Size,
(userId, items) =>
{
var user = UserManager.GetUsers(userId);
var item = new UsageSpaceStatItem { SpaceUsage = items.Sum() };
if (user.Equals(Constants.LostUser))
{
item.Name = FilesUCResource.CorporateFiles;
item.ImgUrl = PathProvider.GetImagePath("corporatefiles_big.png");
item.Url = PathProvider.GetFolderUrl(GlobalFolderHelper.FolderCommon);
}
else
{
item.Name = user.DisplayUserName(false, DisplayUserSettingsHelper);
item.ImgUrl = user.GetSmallPhotoURL(UserPhotoManager);
item.Url = user.GetUserProfilePageURL(CommonLinkUtility);
item.Disabled = user.Status == EmployeeStatus.Terminated;
}
return item;
})
.OrderByDescending(i => i.SpaceUsage)
.ToList();
}
}
public class FilesUserSpaceUsage : IUserSpaceUsage
{
public ASC.Files.Core.EF.FilesDbContext FilesDbContext { get; }
public TenantManager TenantManager { get; }
public FilesUserSpaceUsage(
DbContextManager<ASC.Files.Core.EF.FilesDbContext> dbContextManager,
TenantManager tenantManager)
{
FilesDbContext = dbContextManager.Get(FileConstant.DatabaseId);
TenantManager = tenantManager;
}
public long GetUserSpaceUsage(Guid userId)
{
return FilesDbContext.Files
.Join(FilesDbContext.Tree, a => a.FolderId, b => b.FolderId, (file, tree) => new { file, tree })
.Join(FilesDbContext.BunchObjects, a => a.tree.ParentId.ToString(), b => b.LeftNode, (fileTree, bunch) => new { fileTree.file, fileTree.tree, bunch })
.Where(r => r.file.TenantId == r.bunch.TenantId)
.Where(r => r.file.TenantId == TenantManager.GetCurrentTenant().TenantId)
.Where(r => r.file.CreateBy == userId)
.Where(r => r.bunch.RightNode.StartsWith("files/trash/") | r.bunch.RightNode.StartsWith("files/my/"))
.GroupBy(r => r.file.CreateBy)
.Select(r => r.Sum(f => f.file.ContentLength))
.FirstOrDefault();
}
}
public static class FilesSpaceUsageStatManagerExtention
{
public static DIHelper AddFilesSpaceUsageStatManagerService(this DIHelper services)
{
services.TryAddScoped<FilesSpaceUsageStatManager>();
/*
GlobalFolderHelper globalFolderHelper,
*/
return services
.AddTenantManagerService()
.AddUserManagerService()
.AddUserPhotoManagerService()
.AddDisplayUserSettingsService()
.AddCommonLinkUtilityService()
.AddFilesDbContextService()
.AddPathProviderService();
}
public static DIHelper AddFilesUserSpaceUsageService(this DIHelper services)
{
services.TryAddScoped<FilesUserSpaceUsage>();
return services
.AddTenantManagerService()
.AddFilesDbContextService();
}
}
}