Ejemplo n.º 1
0
        public async Task <IActionResult> Post(string id, [FromForm] IFormFile file)
        {
            try
            {
                if (file == null)
                {
                    ModelState.AddModelError("Save", "No automation uploaded");
                    return(BadRequest(ModelState));
                }

                long size = file.Length;
                if (size <= 0)
                {
                    ModelState.AddModelError("Automation Upload", $"File size of automation {file.FileName} cannot be 0");
                    return(BadRequest(ModelState));
                }

                var    automation     = repository.GetOne(Guid.Parse(id));
                string organizationId = binaryObjectManager.GetOrganizationId();
                string apiComponent   = "AutomationAPI";

                BinaryObject binaryObject = new BinaryObject();
                binaryObject.Name                = file.FileName;
                binaryObject.Folder              = apiComponent;
                binaryObject.CreatedOn           = DateTime.UtcNow;
                binaryObject.CreatedBy           = applicationUser?.UserName;
                binaryObject.CorrelationEntityId = automation.Id;

                string filePath = Path.Combine("BinaryObjects", organizationId, apiComponent, binaryObject.Id.ToString());

                binaryObjectManager.Upload(file, organizationId, apiComponent, binaryObject.Id.ToString());
                binaryObjectManager.SaveEntity(file, filePath, binaryObject, apiComponent, organizationId);
                binaryObjectRepo.Add(binaryObject);

                automation.BinaryObjectId      = (Guid)binaryObject.Id;
                automation.OriginalPackageName = file.FileName;
                repository.Update(automation);

                await webhookPublisher.PublishAsync("Files.NewFileCreated", binaryObject.Id.ToString(), binaryObject.Name).ConfigureAwait(false);

                await webhookPublisher.PublishAsync("Automations.AutomationUpdated", automation.Id.ToString(), automation.Name).ConfigureAwait(false);

                return(Ok(automation));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Asset", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 2
0
        public List <BinaryObject> AttachFiles(List <IFormFile> files, Guid queueItemId, QueueItem queueItem)
        {
            var  binaryObjects = new List <BinaryObject>();
            long payload       = 0;

            if (files.Count != 0 || files != null)
            {
                foreach (var file in files)
                {
                    if (file == null)
                    {
                        throw new FileNotFoundException("No file attached");
                    }

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

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

                    //create binary object
                    BinaryObject binaryObject = new BinaryObject()
                    {
                        Name                = file.FileName,
                        Folder              = apiComponent,
                        CreatedOn           = DateTime.UtcNow,
                        CreatedBy           = httpContextAccessor.HttpContext.User.Identity.Name,
                        CorrelationEntityId = queueItemId
                    };

                    string filePath = Path.Combine("BinaryObjects", organizationId, apiComponent, binaryObject.Id.ToString());

                    //upload file to the Server
                    binaryObjectManager.Upload(file, organizationId, apiComponent, binaryObject.Id.ToString());
                    binaryObjectManager.SaveEntity(file, filePath, binaryObject, apiComponent, organizationId);
                    binaryObjectRepository.Add(binaryObject);

                    //create queue item attachment
                    QueueItemAttachment attachment = new QueueItemAttachment()
                    {
                        BinaryObjectId = (Guid)binaryObject.Id,
                        QueueItemId    = queueItemId,
                        CreatedBy      = httpContextAccessor.HttpContext.User.Identity.Name,
                        CreatedOn      = DateTime.UtcNow,
                        SizeInBytes    = file.Length
                    };
                    queueItemAttachmentRepository.Add(attachment);
                    binaryObjects.Add(binaryObject);
                    payload += attachment.SizeInBytes;
                }
            }
            //update queue item payload
            queueItem.PayloadSizeInBytes += payload;
            repo.Update(queueItem);

            return(binaryObjects);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Post(string id, [FromForm] IFormFile file)
        {
            try
            {
                if (file == null)
                {
                    ModelState.AddModelError("Save", "No data passed");
                    return(BadRequest(ModelState));
                }

                long size = file.Length;
                if (size <= 0)
                {
                    ModelState.AddModelError("Process Upload", "No process uploaded");
                    return(BadRequest(ModelState));
                }

                var    process        = repository.GetOne(Guid.Parse(id));
                string organizationId = binaryObjectManager.GetOrganizationId();
                string apiComponent   = "ProcessAPI";

                BinaryObject binaryObject = new BinaryObject();
                binaryObject.Name                = file.FileName;
                binaryObject.Folder              = apiComponent;
                binaryObject.CreatedOn           = DateTime.UtcNow;
                binaryObject.CreatedBy           = applicationUser?.UserName;
                binaryObject.CorrelationEntityId = process.Id;
                binaryObjectRepo.Add(binaryObject);

                string filePath = Path.Combine("BinaryObjects", organizationId, apiComponent, binaryObject.Id.ToString());

                binaryObjectManager.Upload(file, organizationId, apiComponent, binaryObject.Id.ToString());
                binaryObjectManager.SaveEntity(file, filePath, binaryObject, apiComponent, organizationId);

                process.BinaryObjectId = (Guid)binaryObject.Id;
                repository.Update(process);

                return(Ok(process));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Asset", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Post(string id, [FromForm] IFormFile file)
        {
            try
            {
                if (file == null)
                {
                    ModelState.AddModelError("Save", "No data passed");
                    return(BadRequest(ModelState));
                }

                long size = file.Length;
                if (size <= 0)
                {
                    ModelState.AddModelError("Asset Upload", "No asset uploaded");
                    return(BadRequest(ModelState));
                }

                var    asset          = repository.GetOne(Guid.Parse(id));
                string organizationId = binaryObjectManager.GetOrganizationId();
                string apiComponent   = "AssetAPI";

                BinaryObject binaryObject = new BinaryObject();
                binaryObject.Name                = file.FileName;
                binaryObject.Folder              = apiComponent;
                binaryObject.CreatedOn           = DateTime.UtcNow;
                binaryObject.CreatedBy           = applicationUser?.UserName;
                binaryObject.CorrelationEntityId = asset.Id;
                binaryObjectRepo.Add(binaryObject);

                string filePath = Path.Combine("BinaryObjects", organizationId, apiComponent, binaryObject.Id.ToString());

                var existingbinary = binaryObjectRepo.Find(null, x => x.Folder?.ToLower(null) == binaryObject.Folder.ToLower(null) && x.Name.ToLower(null) == file?.FileName?.ToLower(null) && x.Id != binaryObject.Id)?.Items?.FirstOrDefault();
                if (existingbinary != null)
                {
                    ModelState.AddModelError("BinaryObject", "Same file name already exists in the given folder");
                    return(BadRequest(ModelState));
                }

                binaryObjectManager.Upload(file, organizationId, apiComponent, binaryObject.Id.ToString());
                binaryObjectManager.SaveEntity(file, filePath, binaryObject, apiComponent, organizationId);

                asset.BinaryObjectID = binaryObject.Id;
                repository.Update(asset);

                return(Ok(asset));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Asset", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Attach(string emailId, [FromForm] IFormFile[] files)
        {
            try
            {
                if (files.Length == 0 || files == null)
                {
                    ModelState.AddModelError("Attach", "No files uploaded to attach");
                    return(BadRequest(ModelState));
                }

                var emailAttachments = new List <EmailAttachment>();

                foreach (var file in files)
                {
                    if (file == null)
                    {
                        ModelState.AddModelError("Save", "No file attached");
                        return(BadRequest(ModelState));
                    }

                    long size = file.Length;
                    if (size <= 0)
                    {
                        ModelState.AddModelError("File attachment", $"File size of file {file.FileName} cannot be 0");
                        return(BadRequest(ModelState));
                    }

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

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

                    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               = Guid.Parse(emailId),
                        CreatedOn             = DateTime.UtcNow,
                        CreatedBy             = applicationUser?.UserName
                    };
                    repository.Add(emailAttachment);
                    emailAttachments.Add(emailAttachment);
                }
                return(Ok(emailAttachments));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Attach", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Post(string id, [FromForm] IFormFile file)
        {
            try
            {
                if (file == null)
                {
                    ModelState.AddModelError("Save", "No asset uploaded");
                    return(BadRequest(ModelState));
                }

                long size = file.Length;
                if (size <= 0)
                {
                    ModelState.AddModelError("Asset Upload", $"File size of file {file.FileName} cannot be 0");
                    return(BadRequest(ModelState));
                }

                var existingAsset = repository.GetOne(Guid.Parse(id));
                if (existingAsset == null)
                {
                    ModelState.AddModelError("Asset", "Asset cannot be found or does not exist.");
                    return(NotFound(ModelState));
                }
                string organizationId = binaryObjectManager.GetOrganizationId();
                string apiComponent   = "AssetAPI";

                BinaryObject binaryObject = new BinaryObject();
                binaryObject.Name                = file.FileName;
                binaryObject.Folder              = apiComponent;
                binaryObject.CreatedOn           = DateTime.UtcNow;
                binaryObject.CreatedBy           = applicationUser?.UserName;
                binaryObject.CorrelationEntityId = existingAsset.Id;

                string filePath = Path.Combine("BinaryObjects", organizationId, apiComponent, binaryObject.Id.ToString());

                var existingbinary = binaryObjectRepo.Find(null, x => x.Folder?.ToLower(null) == binaryObject.Folder.ToLower(null) && x.Name.ToLower(null) == file?.FileName?.ToLower(null) && x.Id != binaryObject.Id)?.Items?.FirstOrDefault();
                if (existingbinary != null)
                {
                    ModelState.AddModelError("BinaryObject", "Same file name already exists in the given folder");
                    return(BadRequest(ModelState));
                }
                binaryObjectManager.Upload(file, organizationId, apiComponent, binaryObject.Id.ToString());
                binaryObjectManager.SaveEntity(file, filePath, binaryObject, apiComponent, organizationId);
                binaryObjectRepo.Add(binaryObject);

                existingAsset.BinaryObjectID = binaryObject.Id;
                existingAsset.SizeInBytes    = file.Length;
                repository.Update(existingAsset);

                await webhookPublisher.PublishAsync("Files.NewFileCreated", binaryObject.Id.ToString(), binaryObject.Name).ConfigureAwait(false);

                await webhookPublisher.PublishAsync("Assets.AssetUpdated", existingAsset.Id.ToString(), existingAsset.Name).ConfigureAwait(false);

                return(Ok(existingAsset));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Asset", ex.Message);
                return(BadRequest(ModelState));
            }
        }