using VideoAnalysisCore.Common;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
using MapsterMapper;
using Mapster;
using VideoAnalysisCore.AICore.SherpaOnnx;
using UserCenter.Model.Enum;
using VideoAnalysisCore.AICore.GPT.ChatGPT;
using VideoAnalysisCore.AICore.GPT;
using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using VideoAnalysisCore.Model.Enum;
using FFmpeg.NET.Services;
using static FFmpeg.NET.MetaData;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Yitter.IdGenerator;
using VideoAnalysisCore.AICore.GPT.Dto;
using VideoAnalysisCore.Model;
using VideoAnalysisCore.Controllers.Dto;
namespace VideoAnalysisCore.Controllers
{
///
/// 蓝鲸字库接口
///
[ApiController]
[Route("LJZK/[action]")]
public class LJZK_Controller : ControllerBase
{
private readonly IMapper mp;
private readonly Repository nodesubscriptionDB;
private readonly Repository videoTaskDB;
private readonly Repository videoKonwPointDB;
private readonly Repository nodePackageInfoDB;
public LJZK_Controller( IMapper mp, Repository nodesubscriptionDB,
Repository videoTaskDB = null, Repository videoKonwPointDB = null
, Repository nodePackageInfoDB = null)
{
this.mp = mp;
this.nodesubscriptionDB = nodesubscriptionDB;
this.videoTaskDB = videoTaskDB;
this.videoKonwPointDB = videoKonwPointDB;
this.nodePackageInfoDB = nodePackageInfoDB;
}
///
/// 蓝鲸智库_文件包订阅
///
/// 请求体
///
[HttpPost(Name = "NodePackage")]
public async Task NodePackage(NodePackageReq[] reqArr)
{
Console.WriteLine($"{DateTime.Now} 文件包订阅请求 req=" + reqArr.ToJson());
if (reqArr is null || reqArr.Count() == 0)
return BadRequest("无效视频列表数据");
var videos = new List(reqArr.Count());
var nodePackages = new List(reqArr.Count());
var videoIdArr = videoTaskDB.AsQueryable().Select(v => v.TagId).Distinct().ToArray();
foreach (var sGroup in reqArr.GroupBy(s=>s.ContentId))
{
var s= sGroup.First(s=>s.VideoType==VideoType.摄像头);
var sPPT= sGroup.FirstOrDefault(s=>s.VideoType==VideoType.PPT课件);
var np = new NodePackageInfo()
{
VideoCode = s.VideoCode,
MaterialId = s.MaterialId,
AttachmentId = s.AttachmentId,
TaskType = s.TaskType,
SubjectType = s.SubjectId,
VideoUrl =s.VideoUrl,
CourseType = s.CourseType,
CallBackUrl=s.CallBackUrl,
Area = s.Area,
HostIP = s.HostIP,
};
nodePackages.Add(np);
if (videoIdArr.Contains(s.VideoCode))
continue;
var pptCode = sPPT!=null ? sPPT.VideoCode : string.Empty;
videos.Add(new VideoTask()
{
Id = YitIdHelper.NextId(),
ComeFrom = GetClientIpAddress(),
ApiToken = "",
Type = s.TaskType,
Subject = s.SubjectId,
TagId = s.VideoCode,
MediaUrl =s.VideoUrl,
PPTVideoCode = pptCode,
VideoType =s.CourseType
});
}
await nodePackageInfoDB.InsertRangeAsync(nodePackages);
await videoTaskDB.InsertRangeAsync(videos);
if (videos is null || videos.Count == 0)
return Ok();
var ids = videos.Select(s => s.Id).ToArray();
RedisExpand.JoinQueue(ids);
return Ok();
}
private string GetClientIpAddress()
{
// 检查 X-Forwarded-For 请求头
if (HttpContext.Request.Headers.ContainsKey("X-Forwarded-For")
&& !string.IsNullOrEmpty(HttpContext.Request.Headers["X-Forwarded-For"]))
return HttpContext.Request.Headers["X-Forwarded-For"].ToString();
if (HttpContext.Connection.RemoteIpAddress != null)
return HttpContext.Connection.RemoteIpAddress.ToString();
throw new Exception("未能获取到客户端ip地址");
}
///
/// 获取任务类型
///
///
[HttpGet(Name = "TaskTypList")]
public IActionResult TaskType()
{
Type type = typeof(TaskTypeEnum);
return Ok(Enum.GetValues(type).Cast