public async void SaveEntity(IFormFile file, string filePath, BinaryObject binaryObject, string apiComponent, string organizationId, string storageProvider, string hash) { binaryObject.ContentType = file.ContentType; binaryObject.CorrelationEntity = apiComponent; binaryObject.HashCode = hash; binaryObject.OrganizationId = Guid.Parse(organizationId); binaryObject.SizeInBytes = file.Length; binaryObject.StoragePath = filePath; binaryObject.StorageProvider = storageProvider; repo.Update(binaryObject); }
public async Task <IActionResult> Update(string id, [FromForm] AutomationViewModel request) { Guid entityId = new Guid(id); var existingAutomation = repository.GetOne(entityId); if (existingAutomation == null) { return(NotFound()); } if (request.File == null) { ModelState.AddModelError("Save", "No data passed"); return(BadRequest(ModelState)); } long size = request.File.Length; if (size <= 0) { ModelState.AddModelError("Automation Upload", $"File size of automation {request.File.FileName} cannot be 0"); return(BadRequest(ModelState)); } string binaryObjectId = existingAutomation.BinaryObjectId.ToString(); var binaryObject = binaryObjectRepo.GetOne(Guid.Parse(binaryObjectId)); string organizationId = binaryObject.OrganizationId.ToString(); if (!string.IsNullOrEmpty(organizationId)) { organizationId = manager.GetOrganizationId().ToString(); } try { BinaryObject newBinaryObject = new BinaryObject(); if (existingAutomation.BinaryObjectId != Guid.Empty && size > 0) { string apiComponent = "AutomationAPI"; //update file in OpenBots.Server.Web using relative directory newBinaryObject.Id = Guid.NewGuid(); newBinaryObject.Name = request.File.FileName; newBinaryObject.Folder = apiComponent; newBinaryObject.StoragePath = Path.Combine("BinaryObjects", organizationId, apiComponent, newBinaryObject.Id.ToString()); newBinaryObject.CreatedBy = applicationUser?.UserName; newBinaryObject.CreatedOn = DateTime.UtcNow; newBinaryObject.CorrelationEntityId = request.Id; binaryObjectRepo.Add(newBinaryObject); binaryObjectManager.Upload(request.File, organizationId, apiComponent, newBinaryObject.Id.ToString()); binaryObjectManager.SaveEntity(request.File, newBinaryObject.StoragePath, newBinaryObject, apiComponent, organizationId); binaryObjectRepo.Update(binaryObject); } //update automation (create new automation and automation version entities) Automation response = existingAutomation; AutomationVersion automationVersion = automationVersionRepo.Find(null, q => q.AutomationId == response.Id).Items?.FirstOrDefault(); if (existingAutomation.Name.Trim().ToLower() != request.Name.Trim().ToLower() || automationVersion.Status.Trim().ToLower() != request.Status?.Trim().ToLower()) { existingAutomation.OriginalPackageName = request.File.FileName; existingAutomation.AutomationEngine = request.AutomationEngine; automationVersion.Status = request.Status; } else { request.BinaryObjectId = newBinaryObject.Id; response = manager.UpdateAutomation(existingAutomation, request); } await webhookPublisher.PublishAsync("Files.NewFileCreated", newBinaryObject.Id.ToString(), newBinaryObject.Name).ConfigureAwait(false); await webhookPublisher.PublishAsync("Automations.AutomationUpdated", existingAutomation.Id.ToString(), existingAutomation.Name).ConfigureAwait(false); return(Ok(response)); } catch (Exception ex) { return(ex.GetActionResult()); } }