Exemplo n.º 1
0
        public List <EmailAttachment> AddAttachments(IFormFile[] files, Guid id, string driveName)
        {
            if (driveName != "Files" && !string.IsNullOrEmpty(driveName))
            {
                throw new EntityOperationException("Component files can only be saved in the Files drive");
            }
            else if (string.IsNullOrEmpty(driveName))
            {
                driveName = "Files";
            }

            var attachments = new List <EmailAttachment>();

            if (files?.Length != 0 && files != null)
            {
                //add files to drive
                string storagePath = Path.Combine(driveName, "Email Attachments", id.ToString());
                var    fileView    = new FileFolderViewModel()
                {
                    StoragePath     = storagePath,
                    FullStoragePath = storagePath,
                    Files           = files,
                    IsFile          = true
                };

                long?size = 0;
                foreach (var file in files)
                {
                    size += file.Length;
                }

                CheckStoragePathExists(fileView, size, id, driveName);
                var fileViewList = _fileManager.AddFileFolder(fileView, driveName);

                foreach (var file in fileViewList)
                {
                    //create email attachment
                    EmailAttachment emailAttachment = new EmailAttachment()
                    {
                        Name                  = file.Name,
                        FileId                = file.Id,
                        ContentType           = file.ContentType,
                        ContentStorageAddress = file.FullStoragePath,
                        SizeInBytes           = file.Size,
                        EmailId               = id,
                        CreatedOn             = DateTime.UtcNow,
                        CreatedBy             = _httpContextAccessor.HttpContext.User.Identity.Name
                    };
                    _emailAttachmentRepository.Add(emailAttachment);
                    attachments.Add(emailAttachment);
                }
            }
            else
            {
                throw new EntityOperationException("No files found to attach");
            }

            return(attachments);
        }
Exemplo n.º 2
0
        public List <EmailAttachment> AddAttachments(IFormFile[] files, Guid id, string driveId)
        {
            driveId = CheckDriveId(driveId);
            var drive = GetDrive(driveId);

            driveId = drive.Id.ToString();

            var attachments = new List <EmailAttachment>();

            if (files?.Length != 0 && files != null)
            {
                //add files to drive
                string storagePath = Path.Combine(drive.Name, "Email Attachments", id.ToString());
                var    fileView    = new FileFolderViewModel()
                {
                    StoragePath     = storagePath,
                    FullStoragePath = storagePath,
                    Files           = files,
                    IsFile          = true
                };

                long?size = 0;
                foreach (var file in files)
                {
                    size += file.Length;
                }

                CheckStoragePathExists(fileView, size, id, driveId, drive.Name);
                var fileViewList = _fileManager.AddFileFolder(fileView, driveId);

                foreach (var file in fileViewList)
                {
                    string[] fileNameArray = file.Name.Split(".");
                    string   extension     = fileNameArray[1];
                    //create email attachment
                    EmailAttachment emailAttachment = new EmailAttachment()
                    {
                        Name                  = file.Name,
                        FileId                = file.Id,
                        ContentType           = file.ContentType,
                        ContentStorageAddress = Path.Combine(drive.OrganizationId.ToString(), drive.Id.ToString(), $"{file.Id}.{extension}"),
                        SizeInBytes           = file.Size,
                        EmailId               = id,
                        CreatedOn             = DateTime.UtcNow,
                        CreatedBy             = _httpContextAccessor.HttpContext.User.Identity.Name
                    };
                    _emailAttachmentRepository.Add(emailAttachment);
                    attachments.Add(emailAttachment);
                }
            }
            else
            {
                throw new EntityOperationException("No files found to attach");
            }

            return(attachments);
        }
Exemplo n.º 3
0
        public List <EmailAttachment> AddAttachments(IFormFile[] files, Guid id, string hash = null)
        {
            var attachments = new List <EmailAttachment>();

            if (files?.Length != 0 && files != null)
            {
                foreach (var file in files)
                {
                    if (hash == null || hash == string.Empty)
                    {
                        hash = GetHash(hash, file);
                    }
                    var binaryObject = binaryObjectRepository.Find(null, q => q.HashCode == hash && q.CorrelationEntityId == id && q.Name == file.FileName)?.Items?.FirstOrDefault();
                    if (binaryObject == null)
                    {
                        if (file == null)
                        {
                            throw new Exception("No file attached");
                        }

                        long size = file.Length;
                        if (size <= 0)
                        {
                            throw new Exception($"File size of file {file.FileName} cannot be 0");
                        }

                        string organizationId = binaryObjectManager.GetOrganizationId();
                        string apiComponent   = "EmailAPI";

                        //add file to binary objects (create entity and put file in EmailAPI folder in server)
                        binaryObject = new BinaryObject()
                        {
                            Name                = file.FileName,
                            Folder              = apiComponent,
                            CreatedBy           = applicationUser?.UserName,
                            CreatedOn           = DateTime.UtcNow,
                            CorrelationEntityId = id
                        };

                        string filePath = Path.Combine("BinaryObjects", organizationId, apiComponent, binaryObject.Id.ToString());
                        //upload file to server
                        binaryObjectManager.Upload(file, organizationId, apiComponent, binaryObject.Id.ToString());
                        binaryObjectManager.SaveEntity(file, filePath, binaryObject, apiComponent, organizationId);
                        binaryObjectRepository.Add(binaryObject);

                        //create email attachment
                        EmailAttachment emailAttachment = new EmailAttachment()
                        {
                            Name                  = binaryObject.Name,
                            BinaryObjectId        = binaryObject.Id,
                            ContentType           = binaryObject.ContentType,
                            ContentStorageAddress = binaryObject.StoragePath,
                            SizeInBytes           = binaryObject.SizeInBytes,
                            EmailId               = id,
                            CreatedOn             = DateTime.UtcNow,
                            CreatedBy             = httpContextAccessor.HttpContext.User.Identity.Name
                        };
                        emailAttachmentRepository.Add(emailAttachment);
                        attachments.Add(emailAttachment);
                    }
                }
            }
            return(attachments);
        }