Import agent from zip file. Add swagger file mock FileUploadOperation.
This commit is contained in:
parent
39f508ae08
commit
94592bb911
|
|
@ -4,6 +4,7 @@ using BotSharp.Core.Engines.BotSharp;
|
||||||
using BotSharp.Core.Models;
|
using BotSharp.Core.Models;
|
||||||
using DotNetToolkit;
|
using DotNetToolkit;
|
||||||
using EntityFrameworkCore.BootKit;
|
using EntityFrameworkCore.BootKit;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
@ -12,6 +13,7 @@ using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BotSharp.RestApi
|
namespace BotSharp.RestApi
|
||||||
{
|
{
|
||||||
|
|
@ -43,23 +45,38 @@ namespace BotSharp.RestApi
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Restore a agent from a uploaded zip file
|
/// Restore a agent from a uploaded zip file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="agentId"></param>
|
/// <param name="file"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("{agentId}")]
|
[HttpPost]
|
||||||
public ActionResult Restore([FromRoute] String agentId)
|
public async Task<ActionResult> 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<AgentImporterInDialogflow>("", new AgentImportHeader { });
|
||||||
|
/*var botsHeaderFilePath = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), $"DbInitializer{Path.DirectorySeparatorChar}Agents{Path.DirectorySeparatorChar}agents.json");
|
||||||
var agents = JsonConvert.DeserializeObject<List<AgentImportHeader>>(System.IO.File.ReadAllText(botsHeaderFilePath));
|
var agents = JsonConvert.DeserializeObject<List<AgentImportHeader>>(System.IO.File.ReadAllText(botsHeaderFilePath));
|
||||||
|
|
||||||
var rasa = new BotSharpAi();
|
var rasa = new BotSharpAi();
|
||||||
var agentHeader = agents.First(x => x.Id == agentId);
|
var agentHeader = agents.First(x => x.Id == agentId);
|
||||||
rasa.RestoreAgent<AgentImporterInDialogflow>(agentHeader);
|
rasa.RestoreAgent<AgentImporterInDialogflow>(agentHeader);*/
|
||||||
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Restore a agent from a uploaded zip file
|
/// Train agent
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="agentId"></param>
|
/// <param name="agentId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,7 @@ namespace BotSharp.WebHost
|
||||||
|
|
||||||
var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "BotSharp.RestApi.xml");
|
var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "BotSharp.RestApi.xml");
|
||||||
c.IncludeXmlComments(filePath);
|
c.IncludeXmlComments(filePath);
|
||||||
|
c.OperationFilter<SwaggerFileUploadOperation>();
|
||||||
});
|
});
|
||||||
|
|
||||||
// register platform dependency
|
// register platform dependency
|
||||||
|
|
|
||||||
28
BotSharp.WebHost/SwaggerFileUploadOperation.cs
Normal file
28
BotSharp.WebHost/SwaggerFileUploadOperation.cs
Normal file
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue