2020-03-02 15:38:31 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
2022-02-10 18:25:40 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2021-03-19 16:56:26 +00:00
|
|
|
|
2020-04-14 18:59:32 +00:00
|
|
|
using ASC.Common;
|
2020-03-02 15:38:31 +00:00
|
|
|
using ASC.CRM.Core.Dao;
|
|
|
|
using ASC.Data.Storage;
|
|
|
|
using ASC.Web.Core;
|
|
|
|
using ASC.Web.Core.Utility.Skins;
|
|
|
|
using ASC.Web.CRM.Configuration;
|
2021-03-19 16:56:26 +00:00
|
|
|
|
2022-05-31 15:34:04 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-03-02 15:38:31 +00:00
|
|
|
|
2021-10-18 11:24:05 +00:00
|
|
|
using SixLabors.ImageSharp;
|
|
|
|
using SixLabors.ImageSharp.Formats;
|
|
|
|
|
2020-03-02 15:38:31 +00:00
|
|
|
namespace ASC.Web.CRM.Classes
|
|
|
|
{
|
2021-03-02 16:29:07 +00:00
|
|
|
[Scope]
|
2020-04-14 07:59:07 +00:00
|
|
|
public class OrganisationLogoManager
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
2021-05-28 17:20:12 +00:00
|
|
|
private DaoFactory _daoFactory;
|
2022-05-31 15:34:04 +00:00
|
|
|
private ILogger _logger;
|
2021-05-28 17:20:12 +00:00
|
|
|
private Global _global;
|
|
|
|
private WebImageSupplier _webImageSupplier;
|
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
public OrganisationLogoManager(WebImageSupplier webImageSupplier,
|
|
|
|
Global global,
|
2022-05-31 15:34:04 +00:00
|
|
|
ILogger logger,
|
2020-04-14 14:35:52 +00:00
|
|
|
DaoFactory daoFactory)
|
2020-04-14 07:59:07 +00:00
|
|
|
{
|
2021-05-28 17:20:12 +00:00
|
|
|
_webImageSupplier = webImageSupplier;
|
|
|
|
_global = global;
|
2022-05-31 15:34:04 +00:00
|
|
|
_logger = logger;
|
2021-05-28 17:20:12 +00:00
|
|
|
_daoFactory = daoFactory;
|
2020-04-14 07:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-02 15:38:31 +00:00
|
|
|
#region Members
|
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
public readonly String OrganisationLogoBaseDirName = "organisationlogo";
|
2020-03-02 15:38:31 +00:00
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
public readonly String OrganisationLogoImgName = "logo";
|
2020-03-02 15:38:31 +00:00
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
public readonly String OrganisationLogoSrcFormat = "data:image/jpeg;base64,{0}";
|
2020-03-02 15:38:31 +00:00
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
public readonly Size OrganisationLogoSize = new Size(200, 150);
|
|
|
|
|
|
|
|
private readonly Object _synchronizedObj = new Object();
|
2020-03-02 15:38:31 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Private Methods
|
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
private String BuildFileDirectory()
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
return String.Concat(OrganisationLogoBaseDirName, "/");
|
|
|
|
}
|
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
private String BuildFilePath(String imageExtension)
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
return String.Concat(BuildFileDirectory(), OrganisationLogoImgName, imageExtension);
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:25:40 +00:00
|
|
|
private async Task<String> ExecResizeImageAsync(byte[] imageData, Size fotoSize, IDataStore dataStore, String photoPath)
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
var data = imageData;
|
|
|
|
using (var stream = new MemoryStream(data))
|
2021-10-18 11:24:05 +00:00
|
|
|
using (var img = Image.Load(stream, out var format))
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
2021-10-18 11:24:05 +00:00
|
|
|
var imgFormat = format;
|
|
|
|
if (fotoSize != img.Size())
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
using (var img2 = CommonPhotoManager.DoThumbnail(img, fotoSize, false, false, false))
|
|
|
|
{
|
2021-10-18 11:24:05 +00:00
|
|
|
data = CommonPhotoManager.SaveToBytes(img2, imgFormat);
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-21 13:23:26 +00:00
|
|
|
data = CommonPhotoManager.SaveToBytes(img, imgFormat);
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using (var fileStream = new MemoryStream(data))
|
|
|
|
{
|
2022-02-10 18:25:40 +00:00
|
|
|
var uri = await dataStore.SaveAsync(photoPath, fileStream);
|
|
|
|
var photoUri = uri.ToString();
|
2020-03-02 15:38:31 +00:00
|
|
|
photoUri = String.Format("{0}?cd={1}", photoUri, DateTime.UtcNow.Ticks);
|
|
|
|
return photoUri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 14:35:52 +00:00
|
|
|
|
2020-03-02 15:38:31 +00:00
|
|
|
#endregion
|
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
public String GetDefaultLogoUrl()
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
2021-05-28 17:20:12 +00:00
|
|
|
return _webImageSupplier.GetAbsoluteWebPath("org_logo_default.png", ProductEntryPoint.ID);
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
public String GetOrganisationLogoBase64(int logoID)
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
if (logoID <= 0) { return ""; }
|
2020-04-14 14:35:52 +00:00
|
|
|
|
2021-05-28 17:20:12 +00:00
|
|
|
return _daoFactory.GetInvoiceDao().GetOrganisationLogoBase64(logoID);
|
2020-04-14 14:35:52 +00:00
|
|
|
|
|
|
|
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
2021-03-19 16:56:26 +00:00
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
public String GetOrganisationLogoSrc(int logoID)
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
var bytestring = GetOrganisationLogoBase64(logoID);
|
|
|
|
return String.IsNullOrEmpty(bytestring) ? "" : String.Format(OrganisationLogoSrcFormat, bytestring);
|
|
|
|
}
|
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
public void DeletePhoto(bool recursive)
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
var photoDirectory = BuildFileDirectory();
|
2021-05-28 17:20:12 +00:00
|
|
|
var store = _global.GetStore();
|
2020-03-02 15:38:31 +00:00
|
|
|
|
|
|
|
lock (_synchronizedObj)
|
|
|
|
{
|
2022-02-10 18:25:40 +00:00
|
|
|
if (store.IsDirectoryAsync(photoDirectory).Result)
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
2022-02-10 18:25:40 +00:00
|
|
|
store.DeleteFilesAsync(photoDirectory, "*", recursive).Wait();
|
2020-03-02 15:38:31 +00:00
|
|
|
if (recursive)
|
|
|
|
{
|
2022-02-10 18:25:40 +00:00
|
|
|
store.DeleteDirectoryAsync(photoDirectory).Wait();
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:25:40 +00:00
|
|
|
public async Task<int> TryUploadOrganisationLogoFromTmpAsync(DaoFactory factory)
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
var directoryPath = BuildFileDirectory();
|
2021-05-28 17:20:12 +00:00
|
|
|
var dataStore = _global.GetStore();
|
2020-03-02 15:38:31 +00:00
|
|
|
|
2022-02-10 18:25:40 +00:00
|
|
|
if (!await dataStore.IsDirectoryAsync(directoryPath))
|
2020-03-02 15:38:31 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-02-10 18:25:40 +00:00
|
|
|
var photoPaths = await _global.GetStore().ListFilesRelativeAsync("", directoryPath, OrganisationLogoImgName + "*", false).ToArrayAsync();
|
2020-03-02 15:38:31 +00:00
|
|
|
if (photoPaths.Length == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
byte[] bytes;
|
2022-02-10 18:25:40 +00:00
|
|
|
using (var photoTmpStream = await dataStore.GetReadStreamAsync(Path.Combine(directoryPath, photoPaths[0])))
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
bytes = Global.ToByteArray(photoTmpStream);
|
|
|
|
}
|
|
|
|
|
2020-04-14 07:59:07 +00:00
|
|
|
var logoID = factory.GetInvoiceDao().SaveOrganisationLogo(bytes);
|
2022-02-10 18:25:40 +00:00
|
|
|
await dataStore.DeleteFilesAsync(directoryPath, "*", false);
|
2020-03-02 15:38:31 +00:00
|
|
|
return logoID;
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2022-05-31 15:34:04 +00:00
|
|
|
_logger.LogError("TryUploadOrganisationLogoFromTmp failed with error: {0}", ex.ToString());
|
2020-04-14 07:59:07 +00:00
|
|
|
|
2020-03-02 15:38:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:25:40 +00:00
|
|
|
public Task<String> UploadLogoAsync(byte[] imageData, IImageFormat imageFormat)
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
|
|
|
var photoPath = BuildFilePath("." + Global.GetImgFormatName(imageFormat));
|
2020-04-14 14:35:52 +00:00
|
|
|
|
2022-02-10 18:25:40 +00:00
|
|
|
return ExecResizeImageAsync(imageData, OrganisationLogoSize, _global.GetStore(), photoPath);
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
2021-03-19 16:56:26 +00:00
|
|
|
}
|
2020-04-14 18:59:32 +00:00
|
|
|
}
|