Example #1
0
        public async Task <string> Update(Guid binaryObjectId, IFormFile file, string organizationId = "", string apiComponent = "", string name = "")
        {
            //Update file in OpenBots.Server.Web using relative directory
            binaryObjectManager.Update(file, organizationId, apiComponent, binaryObjectId);

            //find relative directory where binary object is being saved
            string filePath = Path.Combine("BinaryObjects", organizationId, apiComponent, binaryObjectId.ToString());

            await binaryObjectManager.UpdateEntity(file, filePath, binaryObjectId.ToString(), apiComponent, apiComponent, name);

            return("Success");
        }
        public IFormFile[] CheckFiles(IFormFile[] files, Guid id, string hash, List <EmailAttachment> attachments)
        {
            if (files != null)
            {
                var filesList = files.ToList();
                foreach (var attachment in attachments)
                {
                    var binaryObject = binaryObjectRepository.GetOne((Guid)attachment.BinaryObjectId);
                    //check if file with same hash and email id already exists
                    foreach (var file in files)
                    {
                        hash = GetHash(hash, file);

                        //if email attachment already exists and hash is the same: remove from files list
                        if (binaryObject.ContentType == file.ContentType && binaryObject.CorrelationEntityId == id && binaryObject.Name == file.FileName && binaryObject.HashCode == hash)
                        {
                            filesList.Remove(file);
                        }
                        //if email attachment exists but the hash is not the same: update the attachment and file, remove from files list
                        else if (binaryObject.ContentType == file.ContentType && binaryObject.CorrelationEntityId == id && binaryObject.Name == file.Name)
                        {
                            emailAttachmentRepository.Update(attachment);
                            var organizationId = binaryObjectManager.GetOrganizationId();
                            binaryObjectManager.Update(file, organizationId, "EmailAPI", (Guid)binaryObject.Id);
                            filesList.Remove(file);
                        }
                    }
                }
                //if file doesn't exist, keep it in files list and return files to be attached
                var filesArray = filesList.ToArray();
                return(filesArray);
            }
            else
            {
                return(Array.Empty <IFormFile>());
            }
        }
        public async Task <IActionResult> Update(string id, [FromForm] BinaryObjectViewModel request)
        {
            try
            {
                Guid entityId = new Guid(id);

                if (string.IsNullOrEmpty(request.Folder))
                {
                    request.Folder = request.CorrelationEntity;
                }

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

                var existingBinaryObject = repository.GetOne(entityId);
                if (existingBinaryObject == null)
                {
                    return(NotFound());
                }

                string organizationId = existingBinaryObject.OrganizationId.ToString();
                if (!string.IsNullOrEmpty(organizationId))
                {
                    organizationId = binaryObjectManager.GetOrganizationId().ToString();
                }

                string apiComponent = existingBinaryObject.CorrelationEntity;

                if (request == null)
                {
                    ModelState.AddModelError("Save", "No data passed");
                    return(BadRequest(ModelState));
                }

                long size = request.File.Length;

                if (existingBinaryObject.Id != Guid.Empty && size > 0)
                {
                    existingBinaryObject.Name        = request.Name;
                    existingBinaryObject.ContentType = request.File.ContentType;
                    existingBinaryObject.SizeInBytes = request.File.Length;
                    existingBinaryObject.Folder      = request.Folder;

                    //Update file in OpenBots.Server.Web using relative directory
                    binaryObjectManager.Update(request.File, organizationId, apiComponent, Guid.Parse(id));
                    await binaryObjectManager.UpdateEntity(request.File, existingBinaryObject.StoragePath, existingBinaryObject.Id.ToString(), apiComponent, existingBinaryObject.Folder, existingBinaryObject.Name);
                }
                else
                {
                    existingBinaryObject.Name   = request.Name;
                    existingBinaryObject.Folder = request.Folder;
                    await base.PutEntity(id, existingBinaryObject);
                }

                return(Ok(existingBinaryObject));
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
Example #4
0
        public async Task <IActionResult> Put(string queueItemId, string id, [FromForm] IFormFile file)
        {
            try
            {
                Guid entityId           = new Guid(id);
                Guid queueItemEntityId  = new Guid(queueItemId);
                var  queueItem          = queueItemRepository.GetOne(queueItemEntityId);
                var  existingAttachment = repository.GetOne(entityId);
                if (existingAttachment == null)
                {
                    return(NotFound());
                }

                queueItem.PayloadSizeInBytes -= existingAttachment.SizeInBytes;

                string binaryObjectId = existingAttachment.BinaryObjectId.ToString();
                var    binaryObject   = binaryObjectRepository.GetOne(Guid.Parse(binaryObjectId));

                string organizationId = binaryObject.OrganizationId.ToString();
                if (!string.IsNullOrEmpty(organizationId))
                {
                    organizationId = binaryObjectManager.GetOrganizationId().ToString();
                }

                if (file == null)
                {
                    ModelState.AddModelError("Save", "No attachment uploaded");
                    return(BadRequest(ModelState));
                }

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

                try
                {
                    existingAttachment.SizeInBytes = file.Length;

                    if (existingAttachment.BinaryObjectId != Guid.Empty && size > 0)
                    {
                        //update attachment file in OpenBots.Server.Web using relative directory
                        string apiComponent = "QueueItemAPI";
                        binaryObjectManager.Update(file, organizationId, apiComponent, Guid.Parse(binaryObjectId));
                        await webhookPublisher.PublishAsync("Files.FileUpdated", binaryObject.Id.ToString(), binaryObject.Name).ConfigureAwait(false);
                    }

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

                    //update attachment entity
                    await base.PutEntity(id, existingAttachment);

                    return(Ok(existingAttachment));
                }
                catch (Exception ex)
                {
                    return(ex.GetActionResult());
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Queue Item Attachment", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Example #5
0
        public async Task <IActionResult> Put(string id, [FromForm] UpdateEmailAttachmentViewModel request)
        {
            try
            {
                Guid entityId           = new Guid(id);
                var  existingAttachment = repository.GetOne(entityId);
                if (existingAttachment == null)
                {
                    return(NotFound());
                }

                string binaryObjectId = existingAttachment.BinaryObjectId.ToString();
                var    binaryObject   = binaryObjectRepository.GetOne(Guid.Parse(binaryObjectId));

                string organizationId = binaryObject.OrganizationId.ToString();
                if (!string.IsNullOrEmpty(organizationId))
                {
                    organizationId = binaryObjectManager.GetOrganizationId().ToString();
                }

                if (request.file == null)
                {
                    ModelState.AddModelError("Save", "No attachment uploaded");
                    return(BadRequest(ModelState));
                }

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

                try
                {
                    if (!string.IsNullOrEmpty(request.file.FileName))
                    {
                        existingAttachment.Name = request.file.FileName;
                    }

                    existingAttachment.ContentType = request.file.ContentType;
                    existingAttachment.SizeInBytes = request.file.Length;

                    if (existingAttachment.BinaryObjectId != Guid.Empty && size > 0)
                    {
                        //update attachment file in OpenBots.Server.Web using relative directory
                        string apiComponent = "EmailAPI";
                        binaryObjectManager.Update(request.file, organizationId, apiComponent, Guid.Parse(binaryObjectId));
                    }

                    //update attachment entity
                    await base.PutEntity(id, existingAttachment);

                    return(Ok(existingAttachment));
                }
                catch (Exception ex)
                {
                    return(ex.GetActionResult());
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Email Attachment", ex.Message);
                return(BadRequest(ModelState));
            }
        }