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-08-12 10:53:12 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
namespace ASC.Web.Studio.UserControls.CustomNavigation;
|
|
|
|
|
|
|
|
//internal class LogoUploader : IFileUploadHandler
|
|
|
|
//{
|
|
|
|
// public FileUploadResult ProcessUpload(HttpContext context)
|
|
|
|
// {
|
|
|
|
// var result = new FileUploadResult();
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
// SecurityContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
// var width = Convert.ToInt32(context.Request["size"]);
|
|
|
|
// var size = new Size(width, width);
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
// if (context.Request.Files.Count != 0)
|
|
|
|
// {
|
|
|
|
// const string imgContentType = @"image";
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
// var logo = context.Request.Files[0];
|
|
|
|
// if (!logo.ContentType.StartsWith(imgContentType))
|
|
|
|
// {
|
|
|
|
// throw new Exception(WhiteLabelResource.ErrorFileNotImage);
|
|
|
|
// }
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
// var data = new byte[logo.InputStream.Length];
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
// var reader = new BinaryReader(logo.InputStream);
|
|
|
|
// reader.Read(data, 0, (int) logo.InputStream.Length);
|
|
|
|
// reader.Close();
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
// using (var stream = new MemoryStream(data))
|
|
|
|
// using (var image = Image.FromStream(stream))
|
|
|
|
// {
|
|
|
|
// var actualSize = image.Size;
|
|
|
|
// if (actualSize.Height != size.Height || actualSize.Width != size.Width)
|
|
|
|
// {
|
|
|
|
// throw new ImageSizeLimitException();
|
|
|
|
// }
|
|
|
|
// }
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
// result.Success = true;
|
|
|
|
// result.Message = UserPhotoManager.SaveTempPhoto(data, SetupInfo.MaxImageUploadSize, size.Width,
|
|
|
|
// size.Height);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// result.Success = false;
|
|
|
|
// result.Message = Resource.ErrorEmptyUploadFileSelected;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// catch (ImageWeightLimitException)
|
|
|
|
// {
|
|
|
|
// result.Success = false;
|
|
|
|
// result.Message = Resource.ErrorImageWeightLimit;
|
|
|
|
// }
|
|
|
|
// catch (ImageSizeLimitException)
|
|
|
|
// {
|
|
|
|
// result.Success = false;
|
|
|
|
// result.Message = WhiteLabelResource.ErrorImageSize;
|
|
|
|
// }
|
|
|
|
// catch (Exception ex)
|
|
|
|
// {
|
|
|
|
// result.Success = false;
|
|
|
|
// result.Message = ex.Message.HtmlEncode();
|
|
|
|
// }
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
// return result;
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
public class StorageHelper
|
|
|
|
{
|
2022-03-25 16:26:06 +00:00
|
|
|
private const string StorageName = "customnavigation";
|
|
|
|
private const string Base64Start = "data:image/png;base64,";
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-22 10:31:41 +00:00
|
|
|
private readonly UserPhotoManager _userPhotoManager;
|
|
|
|
private readonly StorageFactory _storageFactory;
|
|
|
|
private readonly TenantManager _tenantManager;
|
2022-05-26 09:01:54 +00:00
|
|
|
private readonly ILogger<StorageHelper> _logger;
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-04-26 17:53:48 +00:00
|
|
|
public StorageHelper(UserPhotoManager userPhotoManager, StorageFactory storageFactory, TenantManager tenantManager, ILogger<StorageHelper> logger)
|
2022-03-17 15:13:38 +00:00
|
|
|
{
|
2022-03-22 10:31:41 +00:00
|
|
|
_userPhotoManager = userPhotoManager;
|
|
|
|
_storageFactory = storageFactory;
|
|
|
|
_tenantManager = tenantManager;
|
2022-04-26 17:53:48 +00:00
|
|
|
_logger = logger;
|
2022-03-17 15:13:38 +00:00
|
|
|
}
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
public string SaveTmpLogo(string tmpLogoPath)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(tmpLogoPath))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
byte[] data;
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-25 16:26:06 +00:00
|
|
|
if (tmpLogoPath.StartsWith(Base64Start))
|
2022-03-17 15:01:39 +00:00
|
|
|
{
|
2022-03-25 16:26:06 +00:00
|
|
|
data = Convert.FromBase64String(tmpLogoPath.Substring(Base64Start.Length));
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
return SaveLogo(Guid.NewGuid() + ".png", data);
|
2022-03-17 15:01:39 +00:00
|
|
|
}
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
var fileName = Path.GetFileName(tmpLogoPath);
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-22 10:31:41 +00:00
|
|
|
data = _userPhotoManager.GetTempPhotoData(fileName);
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-22 10:31:41 +00:00
|
|
|
_userPhotoManager.RemoveTempPhoto(fileName);
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
return SaveLogo(fileName, data);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2022-05-13 14:48:49 +00:00
|
|
|
_logger.ErrorSaveTmpLogo(ex);
|
2022-03-17 15:13:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
public void DeleteLogo(string logoPath)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(logoPath))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
try
|
|
|
|
{
|
2022-03-25 16:26:06 +00:00
|
|
|
var store = _storageFactory.GetStorage(_tenantManager.GetCurrentTenant().Id.ToString(CultureInfo.InvariantCulture), StorageName);
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
var fileName = Path.GetFileName(logoPath);
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
if (store.IsFileAsync(fileName).Result)
|
2022-03-17 15:01:39 +00:00
|
|
|
{
|
2022-03-17 15:13:38 +00:00
|
|
|
store.DeleteAsync(fileName).Wait();
|
2022-03-17 15:01:39 +00:00
|
|
|
}
|
2022-03-17 15:13:38 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2022-05-13 14:48:49 +00:00
|
|
|
_logger.ErrorDeleteLogo(e);
|
2022-03-17 15:13:38 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
private string SaveLogo(string fileName, byte[] data)
|
|
|
|
{
|
2022-03-25 16:26:06 +00:00
|
|
|
var store = _storageFactory.GetStorage(_tenantManager.GetCurrentTenant().Id.ToString(CultureInfo.InvariantCulture), StorageName);
|
2022-03-22 10:20:43 +00:00
|
|
|
|
2022-03-17 15:13:38 +00:00
|
|
|
using var stream = new MemoryStream(data);
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
return store.SaveAsync(fileName, stream).Result.ToString();
|
|
|
|
}
|
|
|
|
}
|