public EmailAttachment UpdateAttachment(string id, UpdateEmailAttachmentViewModel request)
        {
            Guid entityId           = new Guid(id);
            var  existingAttachment = _emailAttachmentRepository.GetOne(entityId);

            if (existingAttachment == null)
            {
                throw new EntityOperationException("No file found to update");
            }

            request.DriveId = CheckDriveId(request.DriveId);
            var drive = _storageDriveRepository.GetOne(Guid.Parse(request.DriveId));

            var file = _fileManager.GetFileFolder(existingAttachment.FileId.ToString(), request.DriveId, "Files");

            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");
            }

            //update email attachment entity
            var    formFile    = request.File;
            string storagePath = Path.Combine(drive.Name, "Email Attachments", formFile.FileName);

            if (!string.IsNullOrEmpty(formFile.FileName))
            {
                existingAttachment.Name = formFile.FileName;
            }

            existingAttachment.ContentType           = formFile.ContentType;
            existingAttachment.SizeInBytes           = formFile.Length;
            existingAttachment.ContentStorageAddress = storagePath;
            _emailAttachmentRepository.Update(existingAttachment);

            //update file entity and file
            file.Files       = new IFormFile[] { formFile };
            file.StoragePath = storagePath;
            var fileView = _fileManager.GetFileFolder(existingAttachment.FileId.ToString(), request.DriveId, "Files");

            file.FullStoragePath = fileView.FullStoragePath;
            _fileManager.UpdateFile(file);

            return(existingAttachment);
        }
        public async Task <IActionResult> Put(string id, [FromForm] UpdateEmailAttachmentViewModel request)
        {
            try
            {
                var existingAttachment = _manager.UpdateAttachment(id, request);
                await base.PutEntity(id, existingAttachment);

                return(Ok(existingAttachment));
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
Beispiel #3
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));
            }
        }