DocSpace-client/web/ASC.Web.Core/WhiteLabel/TenantLogoManager.cs

218 lines
8.4 KiB
C#
Raw Normal View History

2019-06-07 08:59:07 +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;
2019-08-15 12:04:42 +00:00
using System.Linq;
2019-06-07 08:59:07 +00:00
using ASC.Common.Caching;
2019-10-11 15:03:03 +00:00
using ASC.Core;
using ASC.Core.Common.Settings;
2019-06-07 08:59:07 +00:00
using Microsoft.AspNetCore.Http;
2019-09-23 12:20:08 +00:00
using Microsoft.Extensions.Configuration;
2019-10-31 13:54:43 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
2019-06-07 08:59:07 +00:00
namespace ASC.Web.Core.WhiteLabel
{
public class TenantLogoManager
2019-10-31 13:54:43 +00:00
{
2019-09-16 14:51:39 +00:00
private string CacheKey
2019-06-07 08:59:07 +00:00
{
2019-09-16 14:51:39 +00:00
get { return "letterlogodata" + TenantManager.GetCurrentTenant().TenantId; }
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public bool WhiteLabelEnabled
2019-06-07 08:59:07 +00:00
{
get;
private set;
2019-10-11 15:03:03 +00:00
}
2019-09-13 11:18:27 +00:00
2019-10-11 15:03:03 +00:00
public ICache Cache { get; }
public ICacheNotify<TenantLogoCacheItem> CacheNotify { get; }
2019-09-23 12:20:08 +00:00
2019-10-11 15:03:03 +00:00
public TenantLogoManager(
TenantWhiteLabelSettingsHelper tenantWhiteLabelSettingsHelper,
SettingsManager settingsManager,
TenantInfoSettingsHelper tenantInfoSettingsHelper,
2019-10-11 15:03:03 +00:00
TenantManager tenantManager,
IConfiguration configuration,
ICacheNotify<TenantLogoCacheItem> cacheNotify)
2019-09-13 11:18:27 +00:00
{
TenantWhiteLabelSettingsHelper = tenantWhiteLabelSettingsHelper;
SettingsManager = settingsManager;
TenantInfoSettingsHelper = tenantInfoSettingsHelper;
2019-09-16 14:51:39 +00:00
TenantManager = tenantManager;
2019-09-23 12:20:08 +00:00
Configuration = configuration;
var hideSettings = (Configuration["web:hide-settings"] ?? "").Split(new[] { ',', ';', ' ' });
WhiteLabelEnabled = !hideSettings.Contains("WhiteLabel", StringComparer.CurrentCultureIgnoreCase);
2019-10-11 15:03:03 +00:00
Cache = AscCache.Memory;
CacheNotify = cacheNotify;
2019-09-13 11:18:27 +00:00
}
2019-06-07 08:59:07 +00:00
2019-09-13 11:18:27 +00:00
public string GetFavicon(bool general, bool timeParam)
2019-06-07 08:59:07 +00:00
{
string faviconPath;
var tenantWhiteLabelSettings = SettingsManager.Load<TenantWhiteLabelSettings>();
2019-06-07 08:59:07 +00:00
if (WhiteLabelEnabled)
{
faviconPath = TenantWhiteLabelSettingsHelper.GetAbsoluteLogoPath(tenantWhiteLabelSettings, WhiteLabelLogoTypeEnum.Favicon, general);
2019-08-15 12:04:42 +00:00
if (timeParam)
{
2019-06-07 08:59:07 +00:00
var now = DateTime.Now;
2019-08-15 13:16:39 +00:00
faviconPath = string.Format("{0}?t={1}", faviconPath, now.Ticks);
2019-06-07 08:59:07 +00:00
}
}
else
{
faviconPath = TenantWhiteLabelSettingsHelper.GetAbsoluteDefaultLogoPath(WhiteLabelLogoTypeEnum.Favicon, general);
2019-08-15 12:04:42 +00:00
}
2019-06-07 08:59:07 +00:00
return faviconPath;
}
2019-09-13 11:18:27 +00:00
public string GetTopLogo(bool general)//LogoLightSmall
{
var tenantWhiteLabelSettings = SettingsManager.Load<TenantWhiteLabelSettings>();
2019-06-07 08:59:07 +00:00
if (WhiteLabelEnabled)
{
return TenantWhiteLabelSettingsHelper.GetAbsoluteLogoPath(tenantWhiteLabelSettings, WhiteLabelLogoTypeEnum.LightSmall, general);
2019-06-07 08:59:07 +00:00
}
return TenantWhiteLabelSettingsHelper.GetAbsoluteDefaultLogoPath(WhiteLabelLogoTypeEnum.LightSmall, general);
2019-06-07 08:59:07 +00:00
}
2019-09-13 11:18:27 +00:00
public string GetLogoDark(bool general)
2019-08-15 12:04:42 +00:00
{
2019-06-07 08:59:07 +00:00
if (WhiteLabelEnabled)
{
var tenantWhiteLabelSettings = SettingsManager.Load<TenantWhiteLabelSettings>();
return TenantWhiteLabelSettingsHelper.GetAbsoluteLogoPath(tenantWhiteLabelSettings, WhiteLabelLogoTypeEnum.Dark, general);
2019-06-07 08:59:07 +00:00
}
/*** simple scheme ***/
return TenantInfoSettingsHelper.GetAbsoluteCompanyLogoPath(SettingsManager.Load<TenantInfoSettings>());
2019-06-07 08:59:07 +00:00
/***/
}
2019-09-13 11:18:27 +00:00
public string GetLogoDocsEditor(bool general)
{
var tenantWhiteLabelSettings = SettingsManager.Load<TenantWhiteLabelSettings>();
2019-06-07 08:59:07 +00:00
if (WhiteLabelEnabled)
{
return TenantWhiteLabelSettingsHelper.GetAbsoluteLogoPath(tenantWhiteLabelSettings, WhiteLabelLogoTypeEnum.DocsEditor, general);
2019-06-07 08:59:07 +00:00
}
return TenantWhiteLabelSettingsHelper.GetAbsoluteDefaultLogoPath(WhiteLabelLogoTypeEnum.DocsEditor, general);
2019-06-07 08:59:07 +00:00
}
2019-09-13 11:18:27 +00:00
public string GetLogoText()
2019-06-07 08:59:07 +00:00
{
if (WhiteLabelEnabled)
{
var tenantWhiteLabelSettings = SettingsManager.LoadForDefaultTenant<TenantWhiteLabelSettings>();
2019-06-07 08:59:07 +00:00
return tenantWhiteLabelSettings.GetLogoText(SettingsManager) ?? TenantWhiteLabelSettings.DefaultLogoText;
2019-06-07 08:59:07 +00:00
}
return TenantWhiteLabelSettings.DefaultLogoText;
}
2019-09-13 11:18:27 +00:00
public bool IsRetina(HttpRequest request)
2019-06-07 08:59:07 +00:00
{
var isRetina = false;
if (request != null)
{
var cookie = request.Cookies["is_retina"];
2019-08-15 13:16:39 +00:00
if (cookie != null && !string.IsNullOrEmpty(cookie))
2019-06-07 08:59:07 +00:00
{
2019-08-15 13:16:39 +00:00
if (bool.TryParse(cookie, out var result))
2019-06-07 08:59:07 +00:00
{
isRetina = result;
}
}
}
return isRetina;
2019-08-15 12:04:42 +00:00
}
2019-09-16 14:51:39 +00:00
public bool WhiteLabelPaid
2019-06-07 08:59:07 +00:00
{
get
{
2019-09-16 14:51:39 +00:00
return TenantManager.GetTenantQuota(TenantManager.GetCurrentTenant().TenantId).WhiteLabel;
2019-06-07 08:59:07 +00:00
}
2019-09-13 11:18:27 +00:00
}
public TenantWhiteLabelSettingsHelper TenantWhiteLabelSettingsHelper { get; }
public SettingsManager SettingsManager { get; }
public TenantInfoSettingsHelper TenantInfoSettingsHelper { get; }
2019-09-16 14:51:39 +00:00
public TenantManager TenantManager { get; }
2019-09-23 12:20:08 +00:00
public IConfiguration Configuration { get; }
2019-09-13 11:18:27 +00:00
2019-06-07 08:59:07 +00:00
/// <summary>
/// Get logo stream or null in case of default logo
/// </summary>
2019-09-13 11:18:27 +00:00
public Stream GetWhitelabelMailLogo()
2019-06-07 08:59:07 +00:00
{
if (WhiteLabelEnabled)
{
var tenantWhiteLabelSettings = SettingsManager.Load<TenantWhiteLabelSettings>();
return TenantWhiteLabelSettingsHelper.GetWhitelabelLogoData(tenantWhiteLabelSettings, WhiteLabelLogoTypeEnum.Dark, true);
2019-06-07 08:59:07 +00:00
}
/*** simple scheme ***/
return TenantInfoSettingsHelper.GetStorageLogoData(SettingsManager.Load<TenantInfoSettings>());
2019-06-07 08:59:07 +00:00
/***/
}
2019-09-16 14:51:39 +00:00
public byte[] GetMailLogoDataFromCache()
2019-06-07 08:59:07 +00:00
{
return Cache.Get<byte[]>(CacheKey);
}
2019-09-16 14:51:39 +00:00
public void InsertMailLogoDataToCache(byte[] data)
2019-06-07 08:59:07 +00:00
{
Cache.Insert(CacheKey, data, DateTime.UtcNow.Add(TimeSpan.FromDays(1)));
}
2019-09-16 14:51:39 +00:00
public void RemoveMailLogoDataFromCache()
{
CacheNotify.Publish(new TenantLogoCacheItem() { Key = CacheKey }, CacheNotifyAction.Remove);
2019-06-07 08:59:07 +00:00
}
2019-10-31 13:54:43 +00:00
}
public static class TenantLogoManagerExtension
2019-10-31 13:54:43 +00:00
{
public static IServiceCollection AddTenantLogoManagerService(this IServiceCollection services)
{
services.TryAddScoped<TenantLogoManager>();
return services
.AddTenantWhiteLabelSettingsService()
.AddTenantInfoSettingsService()
.AddTenantManagerService();
}
2019-06-07 08:59:07 +00:00
}
}