DocSpace-buildtools/products/ASC.CRM/Server/Middlewares/OrganisationLogoHandlerMiddleware.cs

132 lines
5.3 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.
*
*/
2021-03-19 16:56:26 +00:00
using System;
using System.Text.Json;
2020-03-02 15:38:31 +00:00
using ASC.CRM.Core;
2020-04-09 21:22:26 +00:00
using ASC.CRM.Resources;
2020-03-02 15:38:31 +00:00
using ASC.Web.Core.Files;
using ASC.Web.Core.Utility;
2021-03-13 12:22:14 +00:00
using ASC.Web.CRM.Classes;
2020-03-02 15:38:31 +00:00
using ASC.Web.Studio.Core;
2021-03-01 17:36:18 +00:00
2021-03-13 12:22:14 +00:00
using Microsoft.AspNetCore.Builder;
2020-04-09 21:22:26 +00:00
using Microsoft.AspNetCore.Http;
2021-03-01 17:36:18 +00:00
2021-03-13 12:22:14 +00:00
namespace ASC.Web.CRM.HttpHandlers
2020-03-02 15:38:31 +00:00
{
2021-03-13 12:22:14 +00:00
public class OrganisationLogoHandlerMiddleware
2020-03-02 15:38:31 +00:00
{
2021-03-13 12:22:14 +00:00
private readonly RequestDelegate _next;
public OrganisationLogoHandlerMiddleware(
RequestDelegate next
)
2020-04-09 21:22:26 +00:00
{
2021-03-13 12:22:14 +00:00
_next = next;
2020-04-09 21:22:26 +00:00
}
2022-01-11 15:37:19 +00:00
public System.Threading.Tasks.Task Invoke(HttpContext context,
2021-05-05 14:09:05 +00:00
CrmSecurity crmSecurity,
2021-03-13 12:22:14 +00:00
SetupInfo setupInfo,
FileSizeComment fileSizeComment,
ContactPhotoManager contactPhotoManager,
OrganisationLogoManager organisationLogoManager)
2020-03-02 15:38:31 +00:00
{
2021-03-13 12:22:14 +00:00
context.Request.EnableBuffering();
2021-04-06 17:33:28 +00:00
if (!crmSecurity.IsAdmin)
throw crmSecurity.CreateSecurityException();
2020-03-02 15:38:31 +00:00
2022-01-11 15:37:19 +00:00
return InternalInvoke(context, crmSecurity, setupInfo, fileSizeComment, contactPhotoManager, organisationLogoManager);
}
private async System.Threading.Tasks.Task InternalInvoke(HttpContext context,
CrmSecurity crmSecurity,
SetupInfo setupInfo,
FileSizeComment fileSizeComment,
ContactPhotoManager contactPhotoManager,
OrganisationLogoManager organisationLogoManager)
{
2020-03-02 15:38:31 +00:00
var fileUploadResult = new FileUploadResult();
2021-03-13 12:22:14 +00:00
if (context.Request.Form.Files.Count == 0)
{
await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult));
}
2020-03-02 15:38:31 +00:00
2021-03-13 12:22:14 +00:00
var fileName = context.Request.Form.Files[0].FileName;
var contentLength = context.Request.Form.Files[0].Length;
2020-03-02 15:38:31 +00:00
2021-03-13 12:22:14 +00:00
if (String.IsNullOrEmpty(fileName) || contentLength == 0)
2020-03-02 15:38:31 +00:00
throw new InvalidOperationException(CRMErrorsResource.InvalidFile);
2021-03-13 12:22:14 +00:00
if (0 < setupInfo.MaxImageUploadSize && setupInfo.MaxImageUploadSize < contentLength)
2020-03-02 15:38:31 +00:00
{
fileUploadResult.Success = false;
2021-03-13 12:22:14 +00:00
fileUploadResult.Message = fileSizeComment.GetFileImageSizeNote(CRMCommonResource.ErrorMessage_UploadFileSize, false).HtmlEncode();
await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult));
2020-03-02 15:38:31 +00:00
}
2021-03-13 12:22:14 +00:00
if (FileUtility.GetFileTypeByFileName(fileName) != FileType.Image)
2020-03-02 15:38:31 +00:00
{
fileUploadResult.Success = false;
fileUploadResult.Message = CRMJSResource.ErrorMessage_NotImageSupportFormat.HtmlEncode();
2021-03-13 12:22:14 +00:00
await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult));
2020-03-02 15:38:31 +00:00
}
try
{
2021-03-13 12:22:14 +00:00
var imageData = Global.ToByteArray(context.Request.Form.Files[0].OpenReadStream());
var imageFormat = contactPhotoManager.CheckImgFormat(imageData);
2022-02-10 18:25:40 +00:00
var photoUri = await organisationLogoManager.UploadLogoAsync(imageData, imageFormat);
2020-03-02 15:38:31 +00:00
fileUploadResult.Success = true;
fileUploadResult.Data = photoUri;
2021-03-13 12:22:14 +00:00
await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult));
2020-03-02 15:38:31 +00:00
}
catch (Exception exception)
{
fileUploadResult.Success = false;
fileUploadResult.Message = exception.Message.HtmlEncode();
2021-03-13 12:22:14 +00:00
await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult));
2020-03-02 15:38:31 +00:00
}
}
}
2021-03-13 12:22:14 +00:00
public static class OrganisationLogoHandlerMiddlewareExtensions
{
public static IApplicationBuilder UseOrganisationLogoHandler(this IApplicationBuilder builder)
{
return builder.UseMiddleware<OrganisationLogoHandlerMiddleware>();
}
}
2020-03-02 15:38:31 +00:00
}