Exemple #1
0
        /// <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);
            }
        }
Exemple #2
0
        /// <summary>
        /// Gets the object from s3.
        /// </summary>
        /// <param name="AttachmentId">The attachment identifier.</param>
        /// <param name="files">The files.</param>
        /// <returns></returns>
        public DownLoadAttachmentModel DownloadZipFileFromS3(int AttachmentId, List <Files> files)
        {
            try
            {
                DownLoadAttachmentModel      downloadAttachment = new DownLoadAttachmentModel();
                List <byte[]>                bytes      = new List <byte[]>();
                IDictionary <string, byte[]> dictoinary = new Dictionary <string, byte[]>();

                //If Attachment has only 1 file then return the file as it is.
                if (files.Count == 1)
                {
                    var attachmentBytes = GetAttachmentFromS3(AttachmentId, files[0]);
                    downloadAttachment.FileType        = files[0].FileType;
                    downloadAttachment.AttachmentBytes = attachmentBytes;
                    downloadAttachment.FileCount       = files.Count;
                    downloadAttachment.FileName        = files[0].FileName;
                    return(downloadAttachment);
                }

                //If Attachment has more then 1 file the create zip and return the zip as attachment.
                foreach (var file in files)
                {
                    var attachmentBytes = GetAttachmentFromS3(AttachmentId, file);
                    dictoinary.Add(file.FileName, attachmentBytes);
                    bytes.Add(attachmentBytes);
                }
                downloadAttachment.FileType        = "application/zip";
                downloadAttachment.FileCount       = files.Count;
                downloadAttachment.AttachmentBytes = CreateZip(dictoinary);
                return(downloadAttachment);
            }
            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);
            }
        }