public async Task Invoke(HttpContext context, WebItemSecurity webItemSecurity, CrmSecurity crmSecurity, Global global, ImportFromCSV importFromCSV) { if (!webItemSecurity.IsAvailableForMe(ProductEntryPoint.ID)) { throw crmSecurity.CreateSecurityException(); } var fileUploadResult = new FileUploadResult(); if (context.Request.Form.Files.Count == 0) { await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } var fileName = context.Request.Form.Files[0].FileName; var contentLength = context.Request.Form.Files[0].Length; String assignedPath; global.GetStore().SaveTemp("temp", out assignedPath, context.Request.Form.Files[0].OpenReadStream()); var jObject = importFromCSV.GetInfo(context.Request.Form.Files[0].OpenReadStream(), context.Request.Form["importSettings"]); var jsonDocumentAsDictionary = JsonSerializer.Deserialize <Dictionary <string, string> >(jObject.ToString()); jsonDocumentAsDictionary.Add("assignedPath", assignedPath); fileUploadResult.Success = true; fileUploadResult.Data = Global.EncodeTo64(JsonSerializer.Serialize(jsonDocumentAsDictionary)); await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); }
public async System.Threading.Tasks.Task Invoke(HttpContext context, SetupInfo setupInfo, CrmSecurity crmSecurity, FileSizeComment fileSizeComment, WebItemSecurity webItemSecurity, MessageTarget messageTarget, MessageService messageService, DaoFactory daoFactory, ContactPhotoManager contactPhotoManager) { if (!webItemSecurity.IsAvailableForMe(ProductEntryPoint.ID)) { throw crmSecurity.CreateSecurityException(); } context.Request.EnableBuffering(); var contactId = Convert.ToInt32(context.Request.Form["contactID"]); Contact contact = null; if (contactId != 0) { contact = daoFactory.GetContactDao().GetByID(contactId); if (!crmSecurity.CanEdit(contact)) { throw crmSecurity.CreateSecurityException(); } } var fileUploadResult = new FileUploadResult(); if (context.Request.Form.Files.Count == 0) { await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } var fileName = context.Request.Form.Files[0].FileName; var contentLength = context.Request.Form.Files[0].Length; if (String.IsNullOrEmpty(fileName) || contentLength == 0) { throw new InvalidOperationException(CRMErrorsResource.InvalidFile); } if (0 < setupInfo.MaxImageUploadSize && setupInfo.MaxImageUploadSize < contentLength) { fileUploadResult.Success = false; fileUploadResult.Message = fileSizeComment.GetFileImageSizeNote(CRMCommonResource.ErrorMessage_UploadFileSize, false).HtmlEncode(); await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } if (FileUtility.GetFileTypeByFileName(fileName) != FileType.Image) { fileUploadResult.Success = false; fileUploadResult.Message = CRMJSResource.ErrorMessage_NotImageSupportFormat.HtmlEncode(); await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } var uploadOnly = Convert.ToBoolean(context.Request.Form["uploadOnly"]); var tmpDirName = Convert.ToString(context.Request.Form["tmpDirName"]); try { ContactPhotoManager.PhotoData photoData; if (contactId != 0) { photoData = contactPhotoManager.UploadPhoto(context.Request.Form.Files[0].OpenReadStream(), contactId, uploadOnly); } else { if (String.IsNullOrEmpty(tmpDirName) || tmpDirName == "null") { tmpDirName = Guid.NewGuid().ToString(); } photoData = contactPhotoManager.UploadPhotoToTemp(context.Request.Form.Files[0].OpenReadStream(), tmpDirName); } fileUploadResult.Success = true; fileUploadResult.Data = photoData; } catch (Exception e) { fileUploadResult.Success = false; fileUploadResult.Message = e.Message.HtmlEncode(); await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } if (contact != null) { var messageAction = contact is Company ? MessageAction.CompanyUpdatedPhoto : MessageAction.PersonUpdatedPhoto; messageService.Send(messageAction, messageTarget.Create(contact.ID), contact.GetTitle()); } await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); }
public async System.Threading.Tasks.Task Invoke(HttpContext context, CrmSecurity crmSecurity, SetupInfo setupInfo, FileSizeComment fileSizeComment, ContactPhotoManager contactPhotoManager, OrganisationLogoManager organisationLogoManager) { context.Request.EnableBuffering(); if (!crmSecurity.IsAdmin) { throw crmSecurity.CreateSecurityException(); } var fileUploadResult = new FileUploadResult(); if (context.Request.Form.Files.Count == 0) { await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } var fileName = context.Request.Form.Files[0].FileName; var contentLength = context.Request.Form.Files[0].Length; if (String.IsNullOrEmpty(fileName) || contentLength == 0) { throw new InvalidOperationException(CRMErrorsResource.InvalidFile); } if (0 < setupInfo.MaxImageUploadSize && setupInfo.MaxImageUploadSize < contentLength) { fileUploadResult.Success = false; fileUploadResult.Message = fileSizeComment.GetFileImageSizeNote(CRMCommonResource.ErrorMessage_UploadFileSize, false).HtmlEncode(); await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } if (FileUtility.GetFileTypeByFileName(fileName) != FileType.Image) { fileUploadResult.Success = false; fileUploadResult.Message = CRMJSResource.ErrorMessage_NotImageSupportFormat.HtmlEncode(); await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } try { var imageData = Global.ToByteArray(context.Request.Form.Files[0].OpenReadStream()); var imageFormat = contactPhotoManager.CheckImgFormat(imageData); var photoUri = organisationLogoManager.UploadLogo(imageData, imageFormat); fileUploadResult.Success = true; fileUploadResult.Data = photoUri; await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } catch (Exception exception) { fileUploadResult.Success = false; fileUploadResult.Message = exception.Message.HtmlEncode(); await context.Response.WriteAsync(JsonSerializer.Serialize(fileUploadResult)); } }