/// <summary> /// Determines whether [is authorized user] [the specified model]. /// </summary> /// <param name="model">The model.</param> /// <returns> /// <c>true</c> if [is authorized user] [the specified model]; otherwise, <c>false</c>. /// </returns> private bool IsAuthorizedUser(AttachmentDownloadRequest model) { try { if (string.IsNullOrEmpty(model.AccessEmail) && string.IsNullOrEmpty(model.Password)) { return(true); } var securitySettings = m_AttachmentSecuritySettingService.GetSecuritySettings(model.AttachmentId); if (securitySettings == null) { var message = "Security settings Not found for this Attachment."; throw new Exception(message); } if (securitySettings.AccessPassword == model.Password && securitySettings.AccessEmail == model.AccessEmail) { return(true); } return(false); } catch (Exception ex) { var message = string.Format("{0} {1} {2}", ex.InnerException == null ? ex.Message : ex.InnerException.Message, Environment.NewLine, ex.StackTrace); throw new Exception(message); } }
public IActionResult DownloadAttachments([FromBody] AttachmentDownloadRequest entity) { try { var UserEmail = User.Claims.Where(a => a.Type == ClaimTypes.Email).Select(a => a.Value).FirstOrDefault();; var AttachmentResult = this.m_AttachmentsService.DownloadAttachment(entity, UserEmail); if (AttachmentResult == null) { throw new UnauthorizedAccessException("You are not Autherized to access this Attachment."); } if (AttachmentResult.AttachmentBytes == null || AttachmentResult.AttachmentBytes.Length == 0) { return(new NotFoundObjectResult("Attachment not found")); } if (AttachmentResult.FileCount > 1) { HttpContext.Response.ContentType = AttachmentResult.FileType; var result = new FileContentResult(AttachmentResult.AttachmentBytes, AttachmentResult.FileType) { FileDownloadName = "Attachment.zip" }; return(result); } else { HttpContext.Response.ContentType = AttachmentResult.FileType; var result = new FileContentResult(AttachmentResult.AttachmentBytes, "application/octet-stream") { FileDownloadName = AttachmentResult.FileName, }; return(result); } //return File(AttachmentResult.AttachmentBytes, "application/octet-stream"); } catch (Exception ex) { return(new BadRequestObjectResult(ex)); } }
/// <summary> /// Downloads the specified attachment identifier. /// </summary> /// <param name="attachmentId">The attachment identifier.</param> /// <returns></returns> public DownLoadAttachmentModel DownloadAttachment(AttachmentDownloadRequest model, string LoggedInUserEmail) { Download entity = new Download(); DownLoadAttachmentModel DownloadAttachment = new DownLoadAttachmentModel(); byte[] bytes = null; try { var user = LoggedInUser(LoggedInUserEmail); if (IsAuthorizedUser(model)) { var result = this.m_AttachmentRepository.Query <Files>().Where(a => a.AttachmentId == model.AttachmentId).ToList(); if (result.Count == 0) { DownloadAttachment.AttachmentBytes = bytes; DownloadAttachment.FileCount = result.Count; return(DownloadAttachment); } entity.AccessEmail = model.AccessEmail == "" ? null : model.AccessEmail; entity.AccessPassword = model.Password; entity.DownloadDate = DateTime.Now; entity.AttachmentId = model.AttachmentId; entity.UserId = user.Id; m_DownloadRepository.Add(entity); DownloadAttachment = m_FileAssociation.DownloadZipFileFromS3(model.AttachmentId, result); this.m_LogService.LogActivity((int)LogsActivityType.FileUpload, "File Download by User" + user.FirstName + " " + user.LastName + "", (int)model.AttachmentId, "Attachment", user.Id); return(DownloadAttachment); } return(null); } catch (Exception ex) { var message = string.Format("{0} {1} {2}", ex.InnerException == null ? ex.Message : ex.InnerException.Message, Environment.NewLine, ex.StackTrace); throw new Exception(message); } }