DocSpace-client/web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs

260 lines
9.4 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
namespace ASC.Web.Core.Users;
2022-03-17 15:13:38 +00:00
public static class UserPhotoThumbnailManager
{
2022-08-12 13:26:57 +00:00
public static async Task<List<ThumbnailItem>> SaveThumbnails(UserPhotoManager userPhotoManager, SettingsManager settingsManager, int x, int y, int width, int height, Guid userId)
2022-03-17 15:13:38 +00:00
{
2022-08-12 13:26:57 +00:00
return await SaveThumbnails(userPhotoManager, settingsManager, new UserPhotoThumbnailSettings(x, y, width, height), userId);
2022-03-17 15:13:38 +00:00
}
2022-08-12 13:26:57 +00:00
public static async Task<List<ThumbnailItem>> SaveThumbnails(UserPhotoManager userPhotoManager, SettingsManager settingsManager, Point point, Size size, Guid userId)
2022-03-17 15:13:38 +00:00
{
2022-08-12 13:26:57 +00:00
return await SaveThumbnails(userPhotoManager, settingsManager, new UserPhotoThumbnailSettings(point, size), userId);
2022-03-17 15:13:38 +00:00
}
2022-08-12 13:26:57 +00:00
public static async Task<List<ThumbnailItem>> SaveThumbnails(UserPhotoManager userPhotoManager, SettingsManager settingsManager, UserPhotoThumbnailSettings thumbnailSettings, Guid userId)
2022-03-17 15:13:38 +00:00
{
if (thumbnailSettings.Size.IsEmpty)
{
2022-03-17 15:13:38 +00:00
return null;
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
var thumbnailsData = new ThumbnailsData(userId, userPhotoManager);
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
var resultBitmaps = new List<ThumbnailItem>();
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
using var img = thumbnailsData.MainImgBitmap(out var format);
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
if (img == null)
{
2022-03-17 15:13:38 +00:00
return null;
}
2019-06-07 08:59:07 +00:00
2022-08-12 13:26:57 +00:00
foreach (var thumbnail in await thumbnailsData.ThumbnailList())
2022-03-17 15:13:38 +00:00
{
thumbnail.Image = GetImage(img, thumbnail.Size, thumbnailSettings);
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
resultBitmaps.Add(thumbnail);
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
thumbnailsData.Save(resultBitmaps);
settingsManager.SaveForUser(thumbnailSettings, userId);
2019-06-07 08:59:07 +00:00
2022-08-12 13:26:57 +00:00
return await thumbnailsData.ThumbnailList();
2022-03-17 15:13:38 +00:00
}
2019-06-07 08:59:07 +00:00
2022-03-17 15:13:38 +00:00
public static Image GetImage(Image mainImg, Size size, UserPhotoThumbnailSettings thumbnailSettings)
{
var x = thumbnailSettings.Point.X > 0 ? thumbnailSettings.Point.X : 0;
var y = thumbnailSettings.Point.Y > 0 ? thumbnailSettings.Point.Y : 0;
Merge branch 'feature/backend-refactor' into feature/warnings # Conflicts: # common/ASC.Common/Threading/DistributedTaskProgress.cs # common/ASC.Common/Threading/DistributedTaskQueue.cs # common/ASC.Core.Common/Configuration/AmiPublicDnsSyncService.cs # common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs # common/ASC.Core.Common/Notify/Model/NotifyClientImpl.cs # common/ASC.Core.Common/Security/EmailValidationKeyProvider.cs # common/ASC.Data.Reassigns/QueueWorker.cs # common/ASC.Data.Reassigns/ReassignProgressItem.cs # common/ASC.Data.Reassigns/RemoveProgressItem.cs # common/ASC.Data.Storage/StaticUploader.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FileDto.cs # products/ASC.Files/Core/ApiModels/ResponseDto/FolderDto.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FileDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/FolderDao.cs # products/ASC.Files/Core/Core/Dao/TeamlabDao/TagDao.cs # products/ASC.Files/Core/Core/Thirdparty/Box/BoxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/Dropbox/DropboxDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/GoogleDrive/GoogleDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs # products/ASC.Files/Core/Core/Thirdparty/OneDrive/OneDriveDaoBase.cs # products/ASC.Files/Core/Core/Thirdparty/ProviderDao/ProviderSecutiryDao.cs # products/ASC.Files/Core/Core/Thirdparty/Sharpbox/SharpBoxDaoBase.cs # products/ASC.Files/Core/HttpHandlers/FileHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/ThirdPartyAppHandler.ashx.cs # products/ASC.Files/Core/HttpHandlers/docusignhandler.ashx.cs # products/ASC.Files/Core/Services/DocumentService/DocumentServiceTracker.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileDownloadOperation.cs # products/ASC.Files/Core/Services/WCFService/FileOperations/FileOperation.cs # products/ASC.Files/Core/Utils/EntryManager.cs # web/ASC.Web.Core/CollaboratorSettings.cs # web/ASC.Web.Core/CustomNavigationSettings.cs # web/ASC.Web.Core/EmailActivationSettings.cs # web/ASC.Web.Core/Notify/NotifyConfiguration.cs # web/ASC.Web.Core/Notify/SpamEmailSettings.cs # web/ASC.Web.Core/Notify/StudioNotifyHelper.cs # web/ASC.Web.Core/Notify/StudioNotifyService.cs # web/ASC.Web.Core/Notify/StudioNotifyServiceSender.cs # web/ASC.Web.Core/Notify/StudioPeriodicNotify.cs # web/ASC.Web.Core/Notify/StudioWhatsNewNotify.cs # web/ASC.Web.Core/PersonalSettings.cs # web/ASC.Web.Core/PrivacyRoomSettings.cs # web/ASC.Web.Core/PromotionsSettings.cs # web/ASC.Web.Core/QuotaSync.cs # web/ASC.Web.Core/Sms/SmsSender.cs # web/ASC.Web.Core/Sms/StudioSmsNotificationSettings.cs # web/ASC.Web.Core/StudioAdminMessageSettings.cs # web/ASC.Web.Core/StudioDefaultPageSettings.cs # web/ASC.Web.Core/StudioTrustedDomainSettings.cs # web/ASC.Web.Core/TariffSettings.cs # web/ASC.Web.Core/Tfa/TfaAppAuthSettings.cs # web/ASC.Web.Core/Tfa/TfaAppUserSettings.cs # web/ASC.Web.Core/TipsSettings.cs # web/ASC.Web.Core/Users/CustomNamingPeople.cs # web/ASC.Web.Core/Users/UserHelpTourSettings.cs # web/ASC.Web.Core/Users/UserPhotoManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailManager.cs # web/ASC.Web.Core/Users/UserPhotoThumbnailSettings.cs # web/ASC.Web.Core/Utility/ColorThemesSettings.cs # web/ASC.Web.Core/Utility/PasswordSettings.cs # web/ASC.Web.Core/Utility/Settings/TenantAccessSettings.cs # web/ASC.Web.Core/Utility/Settings/WebItemSettings.cs # web/ASC.Web.Core/Utility/Settings/WizardSettings.cs # web/ASC.Web.Core/Utility/TenantExtra.cs # web/ASC.Web.Core/WhiteLabel/AdditionalWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/CompanyWhiteLabelSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantInfoSettings.cs # web/ASC.Web.Core/WhiteLabel/TenantWhiteLabelSettings.cs
2022-04-14 12:21:06 +00:00
var width = x + thumbnailSettings.Size.Width > mainImg.Width ? mainImg.Width : thumbnailSettings.Size.Width;
2022-03-17 15:13:38 +00:00
var height = y + thumbnailSettings.Size.Height > mainImg.Height ? mainImg.Height : thumbnailSettings.Size.Height;
var rect = new Rectangle(x,
y,
width,
height);
2022-08-12 13:26:57 +00:00
var result = mainImg.Clone(x => x.BackgroundColor(Color.White).Crop(rect).Resize(new ResizeOptions
2019-08-08 09:26:58 +00:00
{
2022-04-14 19:42:15 +00:00
Size = size
2022-03-17 15:13:38 +00:00
}));
2022-08-12 13:26:57 +00:00
return result;
2022-03-17 15:13:38 +00:00
}
2022-06-02 18:41:16 +00:00
public static void CheckImgFormat(byte[] data)
{
IImageFormat imgFormat;
try
{
using var img = Image.Load(data, out var format);
imgFormat = format;
}
catch (OutOfMemoryException)
{
throw new ImageSizeLimitException();
}
catch (ArgumentException error)
{
throw new UnknownImageFormatException(error);
}
if (imgFormat.Name != "PNG" && imgFormat.Name != "JPEG")
{
throw new UnknownImageFormatException();
}
}
public static byte[] TryParseImage(byte[] data, long maxFileSize, Size maxsize, out IImageFormat imgFormat, out int width, out int height)
{
if (data == null || data.Length <= 0)
{
throw new UnknownImageFormatException();
}
if (maxFileSize != -1 && data.Length > maxFileSize)
{
throw new ImageSizeLimitException();
}
//data = ImageHelper.RotateImageByExifOrientationData(data, Log);
try
{
using var img = Image.Load(data, out var format);
imgFormat = format;
width = img.Width;
height = img.Height;
var maxWidth = maxsize.Width;
var maxHeight = maxsize.Height;
if ((maxHeight != -1 && img.Height > maxHeight) || (maxWidth != -1 && img.Width > maxWidth))
{
#region calulate height and width
if (width > maxWidth && height > maxHeight)
{
if (width > height)
{
height = (int)(height * (double)maxWidth / width + 0.5);
width = maxWidth;
}
else
{
width = (int)(width * (double)maxHeight / height + 0.5);
height = maxHeight;
}
}
if (width > maxWidth && height <= maxHeight)
{
height = (int)(height * (double)maxWidth / width + 0.5);
width = maxWidth;
}
if (width <= maxWidth && height > maxHeight)
{
width = (int)(width * (double)maxHeight / height + 0.5);
height = maxHeight;
}
var tmpW = width;
var tmpH = height;
#endregion
using var destRound = img.Clone(x => x.Resize(new ResizeOptions
{
Size = new Size(tmpW, tmpH),
Mode = ResizeMode.Stretch
}));
data = CommonPhotoManager.SaveToBytes(destRound);
}
return data;
}
catch (OutOfMemoryException)
{
throw new ImageSizeLimitException();
}
catch (ArgumentException error)
{
throw new Users.UnknownImageFormatException(error);
}
}
2022-03-17 15:13:38 +00:00
}
2022-03-17 15:13:38 +00:00
public class ThumbnailItem
{
public Size Size { get; set; }
public string ImgUrl { get; set; }
public Image Image { get; set; }
}
2022-03-17 15:13:38 +00:00
public class ThumbnailsData
{
2022-04-15 09:08:06 +00:00
private readonly Guid _userId;
private readonly UserPhotoManager _userPhotoManager;
2022-03-17 15:13:38 +00:00
public ThumbnailsData(Guid userId, UserPhotoManager userPhotoManager)
{
2022-04-15 09:08:06 +00:00
_userId = userId;
_userPhotoManager = userPhotoManager;
2022-03-17 15:13:38 +00:00
}
2022-03-17 15:13:38 +00:00
public Image MainImgBitmap(out IImageFormat format)
{
2022-04-15 09:08:06 +00:00
var img = _userPhotoManager.GetPhotoImage(_userId, out var imageFormat);
2022-03-17 15:13:38 +00:00
format = imageFormat;
return img;
}
2022-08-12 13:26:57 +00:00
public async Task<List<ThumbnailItem>> ThumbnailList()
2022-03-17 15:13:38 +00:00
{
return new List<ThumbnailItem>
2019-08-08 09:26:58 +00:00
{
new ThumbnailItem
{
Size = UserPhotoManager.RetinaFotoSize,
2022-08-12 13:26:57 +00:00
ImgUrl = await _userPhotoManager.GetRetinaPhotoURL(_userId)
2019-08-08 09:26:58 +00:00
},
new ThumbnailItem
{
Size = UserPhotoManager.MaxFotoSize,
2022-08-12 13:26:57 +00:00
ImgUrl = await _userPhotoManager.GetMaxPhotoURL(_userId)
2019-08-08 09:26:58 +00:00
},
new ThumbnailItem
{
Size = UserPhotoManager.BigFotoSize,
2022-08-12 13:26:57 +00:00
ImgUrl = await _userPhotoManager.GetBigPhotoURL(_userId)
2019-08-08 09:26:58 +00:00
},
new ThumbnailItem
{
Size = UserPhotoManager.MediumFotoSize,
2022-08-12 13:26:57 +00:00
ImgUrl = await _userPhotoManager.GetMediumPhotoURL(_userId)
2019-08-08 09:26:58 +00:00
},
new ThumbnailItem
{
Size = UserPhotoManager.SmallFotoSize,
2022-08-12 13:26:57 +00:00
ImgUrl = await _userPhotoManager.GetSmallPhotoURL(_userId)
2019-08-08 09:26:58 +00:00
}
};
2022-03-17 15:13:38 +00:00
}
2022-03-17 15:13:38 +00:00
public void Save(List<ThumbnailItem> bitmaps)
{
foreach (var item in bitmaps)
{
using var mainImgBitmap = MainImgBitmap(out var format);
2022-04-15 09:08:06 +00:00
_userPhotoManager.SaveThumbnail(_userId, item.Image, format);
2022-03-17 15:13:38 +00:00
}
}
}