diff --git a/BotSharp.RestApi/AgentController.cs b/BotSharp.RestApi/AgentController.cs index c32122c1..dc4a59e3 100644 --- a/BotSharp.RestApi/AgentController.cs +++ b/BotSharp.RestApi/AgentController.cs @@ -4,6 +4,7 @@ using BotSharp.Core.Engines.BotSharp; using BotSharp.Core.Models; using DotNetToolkit; using EntityFrameworkCore.BootKit; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; @@ -12,6 +13,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Threading.Tasks; namespace BotSharp.RestApi { @@ -43,23 +45,38 @@ namespace BotSharp.RestApi /// /// Restore a agent from a uploaded zip file /// - /// + /// /// - [HttpGet("{agentId}")] - public ActionResult Restore([FromRoute] String agentId) + [HttpPost] + public async Task Restore(IFormFile file) { - var botsHeaderFilePath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), $"DbInitializer{Path.DirectorySeparatorChar}Agents{Path.DirectorySeparatorChar}agents.json"); + System.IO.Compression.ZipFile.ExtractToDirectory("", ""); + + if (file == null || file.Length == 0) + { + return BadRequest(); + } + + var filePath = Path.GetTempFileName(); + + using (var stream = new FileStream(filePath, FileMode.Create)) + { + await file.CopyToAsync(stream); + } + + _platform.LoadAgentFromFile("", new AgentImportHeader { }); + /*var botsHeaderFilePath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), $"DbInitializer{Path.DirectorySeparatorChar}Agents{Path.DirectorySeparatorChar}agents.json"); var agents = JsonConvert.DeserializeObject>(System.IO.File.ReadAllText(botsHeaderFilePath)); var rasa = new BotSharpAi(); var agentHeader = agents.First(x => x.Id == agentId); - rasa.RestoreAgent(agentHeader); + rasa.RestoreAgent(agentHeader);*/ return Ok(); } /// - /// Restore a agent from a uploaded zip file + /// Train agent /// /// /// diff --git a/BotSharp.WebHost/Startup.cs b/BotSharp.WebHost/Startup.cs index e5359f55..5b9241fd 100644 --- a/BotSharp.WebHost/Startup.cs +++ b/BotSharp.WebHost/Startup.cs @@ -59,6 +59,7 @@ namespace BotSharp.WebHost var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "BotSharp.RestApi.xml"); c.IncludeXmlComments(filePath); + c.OperationFilter(); }); // register platform dependency diff --git a/BotSharp.WebHost/SwaggerFileUploadOperation.cs b/BotSharp.WebHost/SwaggerFileUploadOperation.cs new file mode 100644 index 00000000..331a2db5 --- /dev/null +++ b/BotSharp.WebHost/SwaggerFileUploadOperation.cs @@ -0,0 +1,28 @@ +using Swashbuckle.AspNetCore.Swagger; +using Swashbuckle.AspNetCore.SwaggerGen; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.WebHost +{ + public class SwaggerFileUploadOperation : IOperationFilter + { + public void Apply(Operation operation, OperationFilterContext context) + { + if (operation.OperationId == "V1AgentRestorePost") + { + operation.Parameters.Clear(); + operation.Parameters.Add(new NonBodyParameter + { + Name = "uploadedFile", + In = "formData", + Description = "Upload File", + Required = true, + Type = "file" + }); + operation.Consumes.Add("multipart/form-data"); + } + } + } +}