Ejemplo n.º 1
0
        public async Task <IActionResult> Post(string queueItemId, [FromBody] string[] requests)
        {
            try
            {
                var  entityId  = new Guid(queueItemId);
                var  queueItem = queueItemRepository.Find(0, 1).Items?.Where(q => q.Id.ToString() == queueItemId).FirstOrDefault();
                long?payload   = 0;

                if (requests.Length == 0 || requests == null)
                {
                    ModelState.AddModelError("Attach", "No files uploaded to attach");
                    return(BadRequest(ModelState));
                }

                foreach (var request in requests)
                {
                    var binaryObject = binaryObjectRepository.Find(null, q => q.Id == Guid.Parse(request))?.Items?.FirstOrDefault();
                    if (binaryObject == null)
                    {
                        ModelState.AddModelError("Save", "No file attached");
                        return(BadRequest(ModelState));
                    }

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

                    //create queue item attachment
                    QueueItemAttachment queueItemAttachment = new QueueItemAttachment()
                    {
                        BinaryObjectId = (Guid)binaryObject.Id,
                        QueueItemId    = Guid.Parse(queueItemId),
                        CreatedBy      = applicationUser?.UserName,
                        CreatedOn      = DateTime.UtcNow,
                        SizeInBytes    = (long)binaryObject.SizeInBytes
                    };
                    repository.Add(queueItemAttachment);
                    payload += queueItemAttachment.SizeInBytes;
                }

                //update queue item payload
                queueItem.PayloadSizeInBytes += (long)payload;
                queueItemRepository.Update(queueItem);
                await webhookPublisher.PublishAsync("QueueItems.QueueItemUpdated", queueItem.Id.ToString(), queueItem.Name).ConfigureAwait(false);

                var queueItemAttachments = repository.Find(null).Items?.Where(q => q.QueueItemId == entityId);
                return(Ok(queueItemAttachments));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Attach", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 2
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.º 3
0
        public async Task <IActionResult> Post(string emailId, [FromBody] string[] requests)
        {
            try
            {
                if (requests.Length == 0 || requests == null)
                {
                    ModelState.AddModelError("Attach", "No files uploaded to attach");
                    return(BadRequest(ModelState));
                }

                var emailAttachments = new List <EmailAttachment>();

                foreach (var request in requests)
                {
                    var binaryObject = binaryObjectRepository.Find(null, q => q.Id == Guid.Parse(request))?.Items?.FirstOrDefault();
                    if (binaryObject == null)
                    {
                        ModelState.AddModelError("Save", "No file attached");
                        return(BadRequest(ModelState));
                    }

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

                    //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),
                        CreatedBy             = applicationUser?.UserName,
                        CreatedOn             = DateTime.UtcNow
                    };
                    repository.Add(emailAttachment);
                    emailAttachments.Add(emailAttachment);
                }
                return(Ok(emailAttachments));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Attach", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 4
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.º 5
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));
            }
        }