DocSpace-buildtools/products/ASC.CRM/Server/Classes/OrganisationLogoManager.cs

199 lines
6.8 KiB
C#
Raw Normal View History

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.Drawing;
using System.Drawing.Imaging;
using System.IO;
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.Common.Logging;
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
2020-04-14 07:59:07 +00:00
using Microsoft.Extensions.Options;
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;
private ILog _logger;
private Global _global;
private WebImageSupplier _webImageSupplier;
2020-04-14 07:59:07 +00:00
public OrganisationLogoManager(WebImageSupplier webImageSupplier,
Global global,
2020-04-14 14:35:52 +00:00
IOptionsMonitor<ILog> logger,
DaoFactory daoFactory)
2020-04-14 07:59:07 +00:00
{
2021-05-28 17:20:12 +00:00
_webImageSupplier = webImageSupplier;
_global = global;
_logger = logger.Get("ASC.CRM");
_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);
}
2020-04-14 07:59:07 +00:00
private String ExecResizeImage(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))
using (var img = new Bitmap(stream))
{
var imgFormat = img.RawFormat;
if (fotoSize != img.Size)
{
using (var img2 = CommonPhotoManager.DoThumbnail(img, fotoSize, false, false, false))
{
data = CommonPhotoManager.SaveToBytes(img2, Global.GetImgFormatName(imgFormat));
}
}
else
{
data = Global.SaveToBytes(img);
}
using (var fileStream = new MemoryStream(data))
{
var photoUri = dataStore.Save(photoPath, fileStream).ToString();
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)
{
if (store.IsDirectory(photoDirectory))
{
store.DeleteFiles(photoDirectory, "*", recursive);
if (recursive)
{
store.DeleteDirectory(photoDirectory);
}
}
}
}
2020-04-14 07:59:07 +00:00
public int TryUploadOrganisationLogoFromTmp(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
if (!dataStore.IsDirectory(directoryPath))
return 0;
try
{
2021-05-28 17:20:12 +00:00
var photoPaths = _global.GetStore().ListFilesRelative("", directoryPath, OrganisationLogoImgName + "*", false);
2020-03-02 15:38:31 +00:00
if (photoPaths.Length == 0)
return 0;
byte[] bytes;
using (var photoTmpStream = dataStore.GetReadStream(Path.Combine(directoryPath, photoPaths[0])))
{
bytes = Global.ToByteArray(photoTmpStream);
}
2020-04-14 07:59:07 +00:00
var logoID = factory.GetInvoiceDao().SaveOrganisationLogo(bytes);
2020-03-02 15:38:31 +00:00
dataStore.DeleteFiles(directoryPath, "*", false);
return logoID;
}
catch (Exception ex)
{
2021-05-28 17:20:12 +00:00
_logger.ErrorFormat("TryUploadOrganisationLogoFromTmp failed with error: {0}", ex);
2020-04-14 07:59:07 +00:00
2020-03-02 15:38:31 +00:00
return 0;
}
}
2020-04-14 07:59:07 +00:00
public String UploadLogo(byte[] imageData, ImageFormat imageFormat)
2020-03-02 15:38:31 +00:00
{
var photoPath = BuildFilePath("." + Global.GetImgFormatName(imageFormat));
2020-04-14 14:35:52 +00:00
2021-05-28 17:20:12 +00:00
return ExecResizeImage(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
}