Example #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);
        }
        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);
        }
        public Asset CreateAsset(Asset asset, IFormFile file, string driveName)
        {
            AssetNameAvailability(asset);

            if (asset.Type == "Text")
            {
                asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.TextValue);
            }
            else if (asset.Type == "Number")
            {
                asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.NumberValue.ToString());
            }
            else if (asset.Type == "Json")
            {
                asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.JsonValue);
            }
            else if (asset.Type == "File")
            {
                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";
                }

                if (file != null)
                {
                    IFormFile[] fileArray = { file };
                    asset.Id = Guid.NewGuid();

                    var fileView = new FileFolderViewModel()
                    {
                        ContentType = file.ContentType,
                        Files       = fileArray,
                        StoragePath = Path.Combine(driveName, "Assets", asset.Id.ToString()),
                        IsFile      = true
                    };

                    var request = new AgentAssetViewModel();
                    request = request.Map(asset, file, driveName);
                    CheckStoragePathExists(fileView, request);

                    fileView          = _fileManager.AddFileFolder(fileView, driveName)[0];
                    asset.FileId      = fileView.Id;
                    asset.SizeInBytes = file.Length;
                }
                else
                {
                    throw new EntityDoesNotExistException("File does not exist");
                }
            }

            return(asset);
        }
        public Automation AddAutomation(AutomationViewModel request)
        {
            var drive = new StorageDrive();

            if (string.IsNullOrEmpty(request.DriveId))
            {
                drive = _storageDriveRepository.Find(null, q => q.IsDefault == true).Items?.FirstOrDefault();
                if (drive == null)
                {
                    throw new EntityDoesNotExistException("Default drive does not exist or could not be found");
                }
                else
                {
                    request.DriveId = drive.Id.ToString();
                }
            }
            else
            {
                drive = _storageDriveRepository.GetOne(Guid.Parse(request.DriveId));
            }

            IFormFile[] fileArray = { request.File };
            string      shortPath = Path.Combine(drive.Name, "Automations");
            string      path      = Path.Combine(shortPath, request.Id.ToString());

            var fileView = new FileFolderViewModel()
            {
                Files           = fileArray,
                StoragePath     = shortPath,
                FullStoragePath = path,
                ContentType     = fileArray[0].ContentType,
                IsFile          = true
            };

            CheckStoragePathExists(fileView, request, drive.Name);
            fileView.StoragePath = fileView.FullStoragePath;
            fileView             = _fileManager.AddFileFolder(fileView, request.DriveId)[0];

            var automationEngine = GetAutomationEngine(request.AutomationEngine);

            var automation = new Automation()
            {
                Name             = request.Name,
                AutomationEngine = automationEngine,
                Id     = request.Id,
                FileId = fileView.Id,
                OriginalPackageName = request.File.FileName
            };

            AddAutomationVersion(request);

            return(automation);
        }
Example #5
0
        public async Task <IActionResult> Post([FromForm] FileFolderViewModel request, string driveId, string type)
        {
            try
            {
                if (request.StoragePath.Contains("Assets") || request.StoragePath.Contains("Automations") ||
                    request.StoragePath.Contains("Email Attachments") || request.StoragePath.Contains("Queue Item Attachments"))
                {
                    throw new EntityOperationException("Component files and folders cannot be added or changed in File Manager");
                }

                if (request.IsFile == null && type == "Files")
                {
                    request.IsFile = true;
                }
                else if (request.IsFile == null && type == "Folders")
                {
                    request.IsFile = false;
                }
                var response = _manager.AddFileFolder(request, driveId);
                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
Example #6
0
        public Automation AddAutomation(AutomationViewModel request)
        {
            if (request.DriveName != "Files" && !string.IsNullOrEmpty(request.DriveName))
            {
                throw new EntityOperationException("Component files can only be saved in the Files drive");
            }
            else if (string.IsNullOrEmpty(request.DriveName))
            {
                request.DriveName = "Files";
            }

            IFormFile[] fileArray = { request.File };
            string      path      = Path.Combine(request.DriveName, "Automations", request.Id.ToString());

            var fileView = new FileFolderViewModel()
            {
                Files           = fileArray,
                StoragePath     = path,
                FullStoragePath = path,
                ContentType     = fileArray[0].ContentType,
                IsFile          = true
            };

            CheckStoragePathExists(fileView, request);
            fileView = _fileManager.AddFileFolder(fileView, request.DriveName)[0];

            var automationEngine = GetAutomationEngine(request.AutomationEngine);

            var automation = new Automation()
            {
                Name             = request.Name,
                AutomationEngine = automationEngine,
                Id     = request.Id,
                FileId = fileView.Id,
                OriginalPackageName = request.File.FileName
            };

            AddAutomationVersion(request);

            return(automation);
        }
        public List <QueueItemAttachment> AddFileAttachments(QueueItem queueItem, string[] requests, string driveId)
        {
            if (requests.Length == 0 || requests == null)
            {
                throw new EntityOperationException("No files found to attach");
            }

            var drive = GetDrive(driveId);

            driveId = drive.Id.ToString();

            long?payload = 0;
            var  queueItemAttachments = new List <QueueItemAttachment>();
            var  files = new List <FileFolderViewModel>();

            foreach (var request in requests)
            {
                var file      = _fileManager.GetFileFolder(request, driveId, "Files");
                var fileToAdd = _storageFileRepository.GetOne(file.Id.Value);
                if (file == null)
                {
                    throw new EntityDoesNotExistException($"File could not be found");
                }

                long?size = file.Size;
                if (size <= 0)
                {
                    throw new EntityOperationException($"File size of file {file.Name} cannot be 0");
                }

                //create queue item attachment file under queue item id folder
                var path = Path.Combine(drive.Name, "Queue Item Attachments", queueItem.Id.ToString());
                using (var stream = IOFile.OpenRead(fileToAdd.StorageLocation))
                {
                    file.Files           = new IFormFile[] { new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) };
                    file.StoragePath     = path;
                    file.FullStoragePath = path;

                    CheckStoragePathExists(file, 0, queueItem.Id, driveId, drive.Name);
                    file = _fileManager.AddFileFolder(file, driveId)[0];
                    files.Add(file);
                }

                //create queue item attachment
                QueueItemAttachment queueItemAttachment = new QueueItemAttachment()
                {
                    FileId      = file.Id.Value,
                    QueueItemId = queueItem.Id.Value,
                    CreatedBy   = _httpContextAccessor.HttpContext.User.Identity.Name,
                    CreatedOn   = DateTime.UtcNow,
                    SizeInBytes = file.Size.Value
                };
                _queueItemAttachmentRepository.Add(queueItemAttachment);
                payload += queueItemAttachment.SizeInBytes;
                queueItemAttachments.Add(queueItemAttachment);
            }

            _fileManager.AddBytesToFoldersAndDrive(files);

            //update queue item payload
            queueItem.PayloadSizeInBytes += payload.Value;
            _repo.Update(queueItem);

            return(queueItemAttachments);
        }
        public Asset CreateAsset(Asset asset, IFormFile file, string driveId = null)
        {
            AssetNameAvailability(asset);

            if (asset.Type == "Text")
            {
                asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.TextValue);
            }
            else if (asset.Type == "Number")
            {
                asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.NumberValue.ToString());
            }
            else if (asset.Type == "Json")
            {
                asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.JsonValue);
            }
            else if (asset.Type == "File")
            {
                var drive = new StorageDrive();
                if (string.IsNullOrEmpty(driveId))
                {
                    drive = _storageDriveRepository.Find(null).Items.Where(q => q.IsDefault == true).FirstOrDefault();
                }
                else
                {
                    drive = _fileManager.GetDriveById(driveId);
                }

                if (drive == null)
                {
                    throw new EntityDoesNotExistException("Default drive could not be found or does not exist");
                }
                else
                {
                    driveId = drive.Id.ToString();
                }

                if (file != null)
                {
                    IFormFile[] fileArray = { file };
                    asset.Id = Guid.NewGuid();
                    string shortPath = Path.Combine(drive.Name, "Assets");
                    var    fileView  = new FileFolderViewModel()
                    {
                        ContentType = file.ContentType,
                        Files       = fileArray,
                        StoragePath = shortPath,
                        IsFile      = true
                    };

                    var request = new AgentAssetViewModel();
                    request = request.Map(asset, file, drive.Id);
                    CheckStoragePathExists(fileView, request, true, drive);
                    fileView.StoragePath = Path.Combine(shortPath, asset.Id.ToString());
                    CheckStoragePathExists(fileView, request, true, drive);

                    fileView          = _fileManager.AddFileFolder(fileView, driveId)[0];
                    asset.FileId      = fileView.Id;
                    asset.SizeInBytes = file.Length;
                }
                else
                {
                    throw new EntityDoesNotExistException("File does not exist");
                }
            }

            return(asset);
        }