public Asset UpdateAssetFile(string id, UpdateAssetViewModel request)
        {
            var existingAsset = GetAsset(id);

            string fileId = existingAsset.FileId.ToString();

            if (string.IsNullOrEmpty(request.DriveId))
            {
                var fileToUpdate = _storageFileRepository.GetOne(existingAsset.FileId.Value);
                request.DriveId = fileToUpdate.StorageDriveId.ToString();
            }

            var file = _fileManager.GetFileFolder(fileId, request.DriveId, "Files");

            if (file == null)
            {
                throw new EntityDoesNotExistException($"Asset file with id {fileId} could not be found or doesn't exist");
            }

            file.StoragePath = Path.Combine(file.StoragePath, request.File.FileName);
            file.ContentType = request.File.ContentType;
            file.Name        = request.File.FileName;
            file.Size        = request.File.Length;
            file.Files       = new IFormFile[] { request.File };

            _fileManager.UpdateFile(file);

            existingAsset.SizeInBytes = file.Size;

            AssetNameAvailability(existingAsset);

            return(existingAsset);
        }
        public Automation UpdateAutomationFile(string id, AutomationViewModel request)
        {
            Guid entityId           = new Guid(id);
            var  existingAutomation = _repo.GetOne(entityId);

            if (existingAutomation == null)
            {
                throw new EntityDoesNotExistException($"Automation with id {id} could not be found");
            }

            string fileId = existingAutomation.FileId.ToString();

            if (string.IsNullOrEmpty(request.DriveId))
            {
                var fileToUpdate = _storageFileRepository.GetOne(existingAutomation.FileId.Value);
                request.DriveId = fileToUpdate.StorageDriveId.ToString();
            }

            var file = _fileManager.GetFileFolder(fileId, request.DriveId, "Files");

            if (file == null)
            {
                throw new EntityDoesNotExistException($"Automation file with id {fileId} could not be found or does not exist");
            }

            var response = AddAutomation(request);

            return(response);
        }
        public QueueItemViewModel UpdateAttachedFiles(QueueItem queueItem, UpdateQueueItemViewModel request)
        {
            if (queueItem == null)
            {
                throw new EntityDoesNotExistException("Queue item could not be found or does not exist");
            }
            UpdateItemsStates(queueItem.QueueId.ToString());

            queueItem.DataJson         = request.DataJson;
            queueItem.Event            = request.Event;
            queueItem.ExpireOnUTC      = request.ExpireOnUTC;
            queueItem.PostponeUntilUTC = request.PostponeUntilUTC;
            queueItem.Name             = request.Name;
            queueItem.QueueId          = request.QueueId.Value;
            queueItem.Source           = request.Source;
            queueItem.Type             = request.Type;
            queueItem.State            = request.State;

            if (queueItem.State == "New")
            {
                queueItem.StateMessage = null;
                queueItem.RetryCount   = 0;
            }

            //if files don't exist in file manager: add file entity, upload file, and add email attachment attachment entity
            var    attachments = _queueItemAttachmentRepository.Find(null, q => q.QueueItemId == queueItem.Id)?.Items;
            string hash        = string.Empty;

            if (string.IsNullOrEmpty(request.DriveId))
            {
                var drive = new StorageDrive();
                if (attachments.Count() > 0)
                {
                    var fileToCheck = _storageFileRepository.GetOne(attachments[0].Id.Value);
                    drive = _storageDriveRepository.GetOne(fileToCheck.StorageDriveId.Value);
                }
                else
                {
                    drive = _storageDriveRepository.Find(null, q => q.IsDefault == true).Items?.FirstOrDefault();
                }

                request.DriveId = drive.Id.ToString();
            }

            IFormFile[] filesArray           = CheckFiles(request.Files, hash, attachments, request.DriveId);
            var         queueItemAttachments = new List <QueueItemAttachment>();

            if (filesArray.Length > 0)
            {
                queueItemAttachments = AddNewAttachments(queueItem, filesArray, request.DriveId);
            }

            _repo.Update(queueItem);

            //attach new files
            QueueItemViewModel response = new QueueItemViewModel();

            response = response.Map(queueItem);
            foreach (var attachment in attachments)
            {
                queueItemAttachments.Add(attachment);
            }
            response.Attachments = queueItemAttachments;

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

            var drive = GetDrive(driveId);

            driveId = drive.Id.ToString();

            var emailAttachments = new List <EmailAttachment>();
            var files            = new List <FileFolderViewModel>();

            foreach (var request in requests)
            {
                var file = _fileManager.ExportFileFolder(request, driveId).Result;
                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 email attachment file under email id folder
                var fileToCheck = _storageFileRepository.GetOne(file.Id.Value);
                var orgId       = _organizationManager.GetDefaultOrganization().Id.ToString();
                using (var stream = IOFile.OpenRead(fileToCheck.StorageLocation))
                {
                    file.Files = new IFormFile[] { new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) };
                };
                var path = Path.Combine(driveId, "Email Attachments", emailId);
                file.StoragePath     = path;
                file.FullStoragePath = path;

                CheckStoragePathExists(file, 0, Guid.Parse(emailId), driveId, drive.Name);
                file = _fileManager.AddFileFolder(file, driveId)[0];
                files.Add(file);

                //create email attachment
                EmailAttachment emailAttachment = new EmailAttachment()
                {
                    Name                  = file.Name,
                    FileId                = file.Id,
                    ContentType           = file.ContentType,
                    ContentStorageAddress = file.FullStoragePath,
                    SizeInBytes           = file.Size,
                    EmailId               = Guid.Parse(emailId),
                    CreatedBy             = _applicationUser?.UserName,
                    CreatedOn             = DateTime.UtcNow
                };
                _emailAttachmentRepository.Add(emailAttachment);
                emailAttachments.Add(emailAttachment);
            }

            _fileManager.AddBytesToFoldersAndDrive(files);

            return(emailAttachments);
        }