修复 生产者端的 请求日志无法读取request问题
This commit is contained in:
parent
bbad3da13a
commit
d49550807b
|
|
@ -54,10 +54,25 @@ namespace Learn.VideoAnalysis.API.Expand
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="context"></param>
|
/// <param name="context"></param>
|
||||||
/// <exception cref="CustomException"></exception>
|
/// <exception cref="CustomException"></exception>
|
||||||
public void ExecutingFileCached(ActionExecutingContext context)
|
public void ExecutingCached(ActionExecutingContext context)
|
||||||
{
|
{
|
||||||
//特殊处理:ResultIgnore,不进行返回结果包装,原样输出
|
//特殊处理:ResultIgnore,不进行返回结果包装,原样输出
|
||||||
var endpoint = context.HttpContext.GetEndpoint();
|
var endpoint = context.HttpContext.GetEndpoint();
|
||||||
|
var request = context.HttpContext.Request;
|
||||||
|
// 1. 只有非 GET 请求且不是文件上传时才读取 Body
|
||||||
|
if (!request.Method.Equals("GET", StringComparison.OrdinalIgnoreCase)
|
||||||
|
&& !request.HasFormContentType)
|
||||||
|
{
|
||||||
|
// 确保从头开始读
|
||||||
|
request.Body.Position = 0;
|
||||||
|
// 使用 leaveOpen: true 防止 StreamReader 关闭底层的 Request.Body
|
||||||
|
using (var reader = new System.IO.StreamReader(request.Body, System.Text.Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 1024, leaveOpen: true))
|
||||||
|
{
|
||||||
|
var body = reader.ReadToEnd();
|
||||||
|
context.HttpContext.Items["RequestBodyRaw"] = body;
|
||||||
|
request.Body.Position = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
// 直接返回原始结果,不封装
|
// 直接返回原始结果,不封装
|
||||||
if (endpoint?.Metadata.GetMetadata<HttpLogEnable>() == null) return;
|
if (endpoint?.Metadata.GetMetadata<HttpLogEnable>() == null) return;
|
||||||
if (context.HttpContext.Request.HasFormContentType &&
|
if (context.HttpContext.Request.HasFormContentType &&
|
||||||
|
|
@ -74,6 +89,8 @@ namespace Learn.VideoAnalysis.API.Expand
|
||||||
}).ToArray();
|
}).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行接口前400 处理
|
/// 执行接口前400 处理
|
||||||
|
|
@ -149,34 +166,19 @@ namespace Learn.VideoAnalysis.API.Expand
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task AddHttpLogAsync(HttpContext context, BaseReturn<object>? result = null, Exception? e = null)
|
public async Task AddHttpLogAsync(HttpContext context, BaseReturn<object>? result = null, Exception? e = null)
|
||||||
{
|
{
|
||||||
//特殊处理:ResultIgnore,不进行返回结果包装,原样输出
|
|
||||||
var endpoint = context.GetEndpoint();
|
|
||||||
// 所有请求都记录
|
|
||||||
//if (endpoint?.Metadata.GetMetadata<HttpLogEnable>() == null&& e is null) return;
|
|
||||||
|
|
||||||
string request = null;
|
string request = null;
|
||||||
var logId = Yitter.IdGenerator.YitIdHelper.NextId();
|
var logId = Yitter.IdGenerator.YitIdHelper.NextId();
|
||||||
if (!context.Request.Method.Equals("GET", StringComparison.InvariantCultureIgnoreCase))
|
if (!context.Request.Method.Equals("GET", StringComparison.InvariantCultureIgnoreCase))
|
||||||
{
|
|
||||||
context.Request.EnableBuffering();
|
|
||||||
//记录请求参数
|
|
||||||
if (context.Request.Body.CanSeek)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
var fileArr = context.Items.ContainsKey("FileCached") ? context.Items["FileCached"] as (IFormFile file, MemoryStream stream)[] : null;
|
var fileArr = context.Items.ContainsKey("FileCached") ? context.Items["FileCached"] as (IFormFile file, MemoryStream stream)[] : null;
|
||||||
if (context.Request.HasFormContentType && fileArr != null)
|
if (context.Request.HasFormContentType && fileArr != null)
|
||||||
{
|
{
|
||||||
// 设置保存目录(例如:项目根目录下的Uploads文件夹)
|
|
||||||
string uploadsFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UploadLogs", logId.ToString());
|
string uploadsFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UploadLogs", logId.ToString());
|
||||||
// 创建目录(如果不存在)
|
|
||||||
if (!Directory.Exists(uploadsFolder))
|
if (!Directory.Exists(uploadsFolder))
|
||||||
Directory.CreateDirectory(uploadsFolder);
|
Directory.CreateDirectory(uploadsFolder);
|
||||||
foreach (var fileInfo in fileArr)
|
foreach (var fileInfo in fileArr)
|
||||||
{
|
{
|
||||||
// 生成安全文件名(防止路径遍历攻击)
|
|
||||||
string uniqueFileName = Guid.NewGuid().ToString().Substring(0, 5) + "_" + Path.GetFileName(fileInfo.file.FileName);
|
string uniqueFileName = Guid.NewGuid().ToString().Substring(0, 5) + "_" + Path.GetFileName(fileInfo.file.FileName);
|
||||||
// 保存文件
|
|
||||||
using var stream = new FileStream(Path.Combine(uploadsFolder, uniqueFileName), FileMode.Create, FileAccess.Write);
|
using var stream = new FileStream(Path.Combine(uploadsFolder, uniqueFileName), FileMode.Create, FileAccess.Write);
|
||||||
fileInfo.stream.Position = 0;
|
fileInfo.stream.Position = 0;
|
||||||
await fileInfo.stream.CopyToAsync(stream);
|
await fileInfo.stream.CopyToAsync(stream);
|
||||||
|
|
@ -184,18 +186,11 @@ namespace Learn.VideoAnalysis.API.Expand
|
||||||
}
|
}
|
||||||
request = $"请求体包含{context.Request.Form.Files.Count()}个文件 目录 {uploadsFolder}";
|
request = $"请求体包含{context.Request.Form.Files.Count()}个文件 目录 {uploadsFolder}";
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
}
|
||||||
|
if (context.Items.ContainsKey("RequestBodyRaw"))
|
||||||
{
|
{
|
||||||
context.Request.Body.Position = 0;
|
request = context.Items["RequestBodyRaw"] as string;
|
||||||
using var sr = new System.IO.StreamReader(context.Request.Body);
|
|
||||||
request = await sr.ReadToEndAsync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
request = "处理请求入参时发生了错误 \r\n" + ex.ToString() + "\r\n 原有请求数据 " + request;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//写入队列
|
//写入队列
|
||||||
await DbScoped.Sugar.CopyNew()
|
await DbScoped.Sugar.CopyNew()
|
||||||
|
|
@ -205,7 +200,7 @@ namespace Learn.VideoAnalysis.API.Expand
|
||||||
Url = context.Request.Path + context.Request.QueryString,
|
Url = context.Request.Path + context.Request.QueryString,
|
||||||
Method = context.Request.Method,
|
Method = context.Request.Method,
|
||||||
Request = request,
|
Request = request,
|
||||||
IP = $"{context.Connection?.RemoteIpAddress?.ToString()}",
|
IP = GetClientIp(context),
|
||||||
ResponseCode = result?.Code ?? -1,
|
ResponseCode = result?.Code ?? -1,
|
||||||
Response = (result != null ? JsonSerializer.Serialize(result) : null) ,
|
Response = (result != null ? JsonSerializer.Serialize(result) : null) ,
|
||||||
Authorization = context.Request.Headers.ContainsKey("Authorization")
|
Authorization = context.Request.Headers.ContainsKey("Authorization")
|
||||||
|
|
@ -218,21 +213,31 @@ namespace Learn.VideoAnalysis.API.Expand
|
||||||
}).ExecuteCommandAsync();
|
}).ExecuteCommandAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string GetClientIp(HttpContext context)
|
||||||
|
{
|
||||||
|
var headers = context.Request.Headers;
|
||||||
|
var ip = headers["X-Forwarded-For"].FirstOrDefault();
|
||||||
|
if (!string.IsNullOrWhiteSpace(ip))
|
||||||
|
{
|
||||||
|
ip = ip.Split(',', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault()?.Trim();
|
||||||
|
}
|
||||||
|
if (string.IsNullOrWhiteSpace(ip))
|
||||||
|
{
|
||||||
|
ip = headers["X-Real-IP"].FirstOrDefault();
|
||||||
|
}
|
||||||
|
if (string.IsNullOrWhiteSpace(ip) && context.Connection.RemoteIpAddress != null)
|
||||||
|
{
|
||||||
|
ip = context.Connection.RemoteIpAddress.ToString();
|
||||||
|
}
|
||||||
|
return ip ?? string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
public override async void OnActionExecuting(ActionExecutingContext context)
|
public override async void OnActionExecuting(ActionExecutingContext context)
|
||||||
{
|
{
|
||||||
// 过期的
|
|
||||||
//if (context.HttpContext.GetEndpoint()?
|
|
||||||
// .Metadata.GetMetadata<IAllowAnonymous>() is null)
|
|
||||||
//{
|
|
||||||
// context.Result = new UnauthorizedResult();
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
Executing400(context);
|
Executing400(context);
|
||||||
ExecutingFileCached(context);
|
ExecutingCached(context);
|
||||||
base.OnActionExecuting(context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -248,6 +253,9 @@ namespace Learn.VideoAnalysis.API.Expand
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine($"{DateTime.Now}=>接口规范出现异常");
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
Console.WriteLine(ex.StackTrace);
|
||||||
}
|
}
|
||||||
|
|
||||||
base.OnActionExecuted(context);
|
base.OnActionExecuted(context);
|
||||||
|
|
|
||||||
|
|
@ -106,8 +106,6 @@ namespace Learn.VideoAnalysis
|
||||||
AppCommon.Services = app.Services;
|
AppCommon.Services = app.Services;
|
||||||
app.UseMiddleware<BasicAuthMiddleware>("Swagger");
|
app.UseMiddleware<BasicAuthMiddleware>("Swagger");
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
//开启redis队列服务
|
|
||||||
_ = app.Services.GetRequiredService<RedisInit>();
|
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI();
|
app.UseSwaggerUI();
|
||||||
app.UseExceptionHandler("/Error");
|
app.UseExceptionHandler("/Error");
|
||||||
|
|
@ -133,7 +131,12 @@ namespace Learn.VideoAnalysis
|
||||||
app.UseCorsExpand();
|
app.UseCorsExpand();
|
||||||
app.UseSqlSugarExpand();
|
app.UseSqlSugarExpand();
|
||||||
app.UseCoravelExpand();
|
app.UseCoravelExpand();
|
||||||
app.UseServiceSystem();
|
app.UseServiceSystem(() =>
|
||||||
|
{
|
||||||
|
//开启redis队列服务
|
||||||
|
_ = AppCommon.Services.GetRequiredService<RedisInit>();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
using VideoAnalysisCore.Common;
|
using VideoAnalysisCore.Common;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using VideoAnalysisCore.Model;
|
using VideoAnalysisCore.Model;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
|
||||||
|
|
@ -109,7 +109,7 @@ namespace VideoAnalysisCore.Common
|
||||||
public static string FrameName = "frame_";
|
public static string FrameName = "frame_";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 删除 AI分析任务的缓存文件
|
/// 删除 AI分析任务视频/PPT的缓存文件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="taskId"></param>
|
/// <param name="taskId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
@ -144,6 +144,32 @@ namespace VideoAnalysisCore.Common
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除 AI分析任务的缓存文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="taskId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task<bool> DeleteTaskAllFileAsync(long? taskId, RedisManager redisManager)
|
||||||
|
{
|
||||||
|
if (taskId is null || taskId == 0) return false;
|
||||||
|
var path = taskId.ToString().LocalPath();
|
||||||
|
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Directory.Delete(path, true);
|
||||||
|
await redisManager.AddTaskLog(taskId, $"已清理所有缓存文件: {taskId}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await redisManager.AddTaskLog(taskId, $"删除缓存文件 {taskId} 时出错: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
await redisManager.AddTaskLog(taskId, $"未识别到任务缓存: {path}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 对象转化为JSON字符串
|
/// 对象转化为JSON字符串
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,9 @@ namespace VideoAnalysisCore.Common
|
||||||
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!context.Request.Method.Equals("GET", StringComparison.OrdinalIgnoreCase)
|
||||||
|
&& !context.Request.HasFormContentType)
|
||||||
|
context.Request.EnableBuffering();
|
||||||
await _next(context);
|
await _next(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ namespace VideoAnalysisCore.Common.Expand
|
||||||
/// 系统服务
|
/// 系统服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="app1"></param>
|
/// <param name="app1"></param>
|
||||||
public static void UseServiceSystem(this IHost app1)
|
public static void UseServiceSystem(this IHost app1,Action? action=null)
|
||||||
{
|
{
|
||||||
var app = app1.Services;
|
var app = app1.Services;
|
||||||
// 注册启动后的回调
|
// 注册启动后的回调
|
||||||
|
|
@ -46,7 +46,10 @@ namespace VideoAnalysisCore.Common.Expand
|
||||||
.Replace("+", "127.0.0.1");
|
.Replace("+", "127.0.0.1");
|
||||||
var uri = new Uri(normalizedAddress);
|
var uri = new Uri(normalizedAddress);
|
||||||
int port = uri.Port; // 这里的 port 就是你要的数字 (int)
|
int port = uri.Port; // 这里的 port 就是你要的数字 (int)
|
||||||
|
if (OperatingSystem.IsWindows())
|
||||||
OpenBrowser($"http://localhost:{uri.Port}/ui/index.html");
|
OpenBrowser($"http://localhost:{uri.Port}/ui/index.html");
|
||||||
|
if(action != null)
|
||||||
|
action();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -414,7 +414,9 @@ namespace VideoAnalysisCore.Common
|
||||||
}).ExecuteCommandAsync();
|
}).ExecuteCommandAsync();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await ExpandFunction.DeleteTaskFileAsync(tId, this);
|
//await ExpandFunction.DeleteTaskFileAsync(tId, this);
|
||||||
|
|
||||||
|
await ExpandFunction.DeleteTaskAllFileAsync(tId, this);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
|
@ -429,6 +431,7 @@ namespace VideoAnalysisCore.Common
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task InitChannel()
|
public async Task InitChannel()
|
||||||
{
|
{
|
||||||
|
Thread.Sleep(1000);
|
||||||
if (Redis is null) throw new Exception("redis未初始化");
|
if (Redis is null) throw new Exception("redis未初始化");
|
||||||
//处理之前程序结束前未能执行完的情况
|
//处理之前程序结束前未能执行完的情况
|
||||||
var oldTaskCount = Redis.LLen(RedisExpandKey.IDTask);
|
var oldTaskCount = Redis.LLen(RedisExpandKey.IDTask);
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ namespace VideoAnalysisCore.Controllers
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通用接口
|
/// 通用接口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Authorize(AuthenticationSchemes = Authentication.vdAdmin)]
|
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class PublicController : ControllerBase
|
public class PublicController : ControllerBase
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -31,12 +31,12 @@ namespace VideoAnalysisCore.Job
|
||||||
this.videotaskDB = videotaskDB;
|
this.videotaskDB = videotaskDB;
|
||||||
this.redisManager = redisManager;
|
this.redisManager = redisManager;
|
||||||
}
|
}
|
||||||
public void DeleteTaskAllCaches()
|
public async Task DeleteTaskAllCachesAsync()
|
||||||
{
|
{
|
||||||
|
|
||||||
var startTime = -5;
|
var startTime = -2;
|
||||||
var timeSpan = startTime - 999;
|
var timeSpan = startTime - 999;
|
||||||
// 计算 6 天前已完成任务缓存
|
// 计算 {startTime} 天前已完成任务缓存
|
||||||
DateTime twoDaysAgo = DateTime.Now.AddDays(startTime);
|
DateTime twoDaysAgo = DateTime.Now.AddDays(startTime);
|
||||||
DateTime endDaysAgo = DateTime.Now.AddDays(timeSpan);
|
DateTime endDaysAgo = DateTime.Now.AddDays(timeSpan);
|
||||||
|
|
||||||
|
|
@ -54,21 +54,7 @@ namespace VideoAnalysisCore.Job
|
||||||
|
|
||||||
// 遍历查询结果,删除缓存文件
|
// 遍历查询结果,删除缓存文件
|
||||||
foreach (var taskId in completedTasks)
|
foreach (var taskId in completedTasks)
|
||||||
{
|
await ExpandFunction.DeleteTaskAllFileAsync(taskId, redisManager);
|
||||||
var path = taskId.ToString().LocalPath();
|
|
||||||
if (!string.IsNullOrEmpty(path) && Directory.Exists(path))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Directory.Delete(path, true);
|
|
||||||
Console.WriteLine($"已删除缓存文件: {taskId}");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"删除缓存文件 {taskId} 时出错: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public async void DeleteTaskVideoCaches()
|
public async void DeleteTaskVideoCaches()
|
||||||
{
|
{
|
||||||
|
|
@ -105,7 +91,7 @@ namespace VideoAnalysisCore.Job
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{DateTime.Now} 执行=>{this.GetType().FullName}");
|
Console.WriteLine($"{DateTime.Now} 执行=>{this.GetType().FullName}");
|
||||||
DeleteTaskVideoCaches();
|
DeleteTaskVideoCaches();
|
||||||
DeleteTaskAllCaches();
|
DeleteTaskAllCachesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue