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-13 12:22:14 +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.Core.Dao;
|
2020-03-02 15:38:31 +00:00
|
|
|
using ASC.CRM.Core.Entities;
|
2020-04-09 21:22:26 +00:00
|
|
|
using ASC.CRM.Resources;
|
2020-03-02 15:38:31 +00:00
|
|
|
using ASC.MessagingSystem;
|
|
|
|
using ASC.Web.Core;
|
|
|
|
using ASC.Web.Core.Files;
|
2021-03-13 12:22:14 +00:00
|
|
|
using ASC.Web.Core.Utility;
|
|
|
|
using ASC.Web.CRM.Classes;
|
2021-05-05 14:09:05 +00:00
|
|
|
using ASC.Web.CRM.Configuration;
|
2020-04-09 21:22:26 +00:00
|
|
|
using ASC.Web.Studio.Core;
|
2021-03-01 17:36:18 +00:00
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2020-04-09 21:22:26 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-03-02 15:38:31 +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-01 17:36:18 +00:00
|
|
|
public class ContactPhotoHandlerMiddleware
|
2020-03-02 15:38:31 +00:00
|
|
|
{
|
2021-03-01 17:36:18 +00:00
|
|
|
public ContactPhotoHandlerMiddleware(RequestDelegate next)
|
2020-04-09 21:22:26 +00:00
|
|
|
{
|
2021-03-01 17:36:18 +00:00
|
|
|
_next = next;
|
2020-04-09 21:22:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 17:36:18 +00:00
|
|
|
private readonly RequestDelegate _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-03-01 17:36:18 +00:00
|
|
|
SetupInfo setupInfo,
|
2021-05-05 14:09:05 +00:00
|
|
|
CrmSecurity crmSecurity,
|
2021-03-01 17:36:18 +00:00
|
|
|
FileSizeComment fileSizeComment,
|
|
|
|
WebItemSecurity webItemSecurity,
|
|
|
|
MessageTarget messageTarget,
|
|
|
|
MessageService messageService,
|
2021-03-13 12:22:14 +00:00
|
|
|
DaoFactory daoFactory,
|
2022-01-11 15:37:19 +00:00
|
|
|
ContactPhotoManager contactPhotoManager){
|
2021-03-01 17:36:18 +00:00
|
|
|
|
2021-05-05 14:09:05 +00:00
|
|
|
if (!webItemSecurity.IsAvailableForMe(ProductEntryPoint.ID))
|
|
|
|
throw crmSecurity.CreateSecurityException();
|
2021-03-01 17:36:18 +00:00
|
|
|
|
|
|
|
context.Request.EnableBuffering();
|
2021-03-19 16:56:26 +00:00
|
|
|
|
2021-03-13 12:22:14 +00:00
|
|
|
var contactId = Convert.ToInt32(context.Request.Form["contactID"]);
|
2021-03-19 16:56:26 +00:00
|
|
|
|
2020-03-02 15:38:31 +00:00
|
|
|
Contact contact = null;
|
2021-03-01 17:36:18 +00:00
|
|
|
|
2020-03-02 15:38:31 +00:00
|
|
|
if (contactId != 0)
|
|
|
|
{
|
2021-03-01 17:36:18 +00:00
|
|
|
contact = daoFactory.GetContactDao().GetByID(contactId);
|
2020-04-22 20:46:49 +00:00
|
|
|
|
2021-05-05 14:09:05 +00:00
|
|
|
if (!crmSecurity.CanEdit(contact))
|
|
|
|
throw crmSecurity.CreateSecurityException();
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 15:37:19 +00:00
|
|
|
return InternalInvoke(context, setupInfo, fileSizeComment, messageTarget, messageService, contactPhotoManager, contact, contactId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async System.Threading.Tasks.Task InternalInvoke(HttpContext context,
|
|
|
|
SetupInfo setupInfo,
|
|
|
|
FileSizeComment fileSizeComment,
|
|
|
|
MessageTarget messageTarget,
|
|
|
|
MessageService messageService,
|
|
|
|
ContactPhotoManager contactPhotoManager,
|
|
|
|
Contact contact,
|
|
|
|
int contactId)
|
|
|
|
{
|
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-01 17:36:18 +00:00
|
|
|
fileUploadResult.Message = fileSizeComment.GetFileImageSizeNote(CRMCommonResource.ErrorMessage_UploadFileSize, false).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
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-13 12:22:14 +00:00
|
|
|
var uploadOnly = Convert.ToBoolean(context.Request.Form["uploadOnly"]);
|
2022-01-11 15:37:10 +00:00
|
|
|
var tmpDirName = context.Request.Form["tmpDirName"];
|
2020-03-02 15:38:31 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ContactPhotoManager.PhotoData photoData;
|
|
|
|
if (contactId != 0)
|
|
|
|
{
|
2022-02-10 18:25:40 +00:00
|
|
|
photoData = await contactPhotoManager.UploadPhotoAsync(context.Request.Form.Files[0].OpenReadStream(), contactId, uploadOnly);
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (String.IsNullOrEmpty(tmpDirName) || tmpDirName == "null")
|
|
|
|
{
|
|
|
|
tmpDirName = Guid.NewGuid().ToString();
|
|
|
|
}
|
2022-02-10 18:25:40 +00:00
|
|
|
photoData = await contactPhotoManager.UploadPhotoToTempAsync(context.Request.Form.Files[0].OpenReadStream(), tmpDirName);
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fileUploadResult.Success = true;
|
|
|
|
fileUploadResult.Data = photoData;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
fileUploadResult.Success = false;
|
|
|
|
fileUploadResult.Message = e.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
|
|
|
}
|
|
|
|
|
|
|
|
if (contact != null)
|
|
|
|
{
|
|
|
|
var messageAction = contact is Company ? MessageAction.CompanyUpdatedPhoto : MessageAction.PersonUpdatedPhoto;
|
2021-03-19 16:56:26 +00:00
|
|
|
|
2021-03-01 17:36:18 +00:00
|
|
|
messageService.Send(messageAction, messageTarget.Create(contact.ID), contact.GetTitle());
|
2021-03-19 16:56:26 +00:00
|
|
|
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 12:22:14 +00:00
|
|
|
await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class ContactPhotoHandlerMiddlewareExtensions
|
|
|
|
{
|
|
|
|
public static IApplicationBuilder UseContactPhotoHandler(this IApplicationBuilder builder)
|
|
|
|
{
|
|
|
|
return builder.UseMiddleware<ContactPhotoHandlerMiddleware>();
|
2020-03-02 15:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|