From 8e0cc812832128f3c016139fcf477dabdb70b27e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=82=A5=E7=BE=8A?= <1048382248@qq.com> Date: Mon, 25 Nov 2024 17:45:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=20=20=20=20[Authorize]?= =?UTF-8?q?=E6=97=B6=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20=E6=8E=A5=E5=8F=A3=E5=BC=82=E5=B8=B8=E6=8D=95?= =?UTF-8?q?=E8=8E=B7=20=E6=96=B0=E5=A2=9E=20dockerFile=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VideoAnalysis/AntDeploy.json | 6 ++- VideoAnalysis/Components/Error.razor | 16 +++--- .../Components/Layouts/BasicLayout.razor.cs | 9 +++- .../Pages/EvaluationProject.razor.cs | 2 - VideoAnalysis/Components/Pages/Login.razor | 4 +- VideoAnalysis/Dockerfile | 7 ++- VideoAnalysis/Program.cs | 9 +++- VideoAnalysis/appsettings.Production.json | 2 +- .../AICore/FFMPGE/FFMPGEHandle.cs | 5 +- VideoAnalysisCore/Common/ExceptionFilter.cs | 49 +++++++++++++++++++ VideoAnalysisCore/Common/RedisExpand.cs | 6 ++- VideoAnalysisCore/VideoAnalysisCore.csproj | 3 +- 12 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 VideoAnalysisCore/Common/ExceptionFilter.cs diff --git a/VideoAnalysis/AntDeploy.json b/VideoAnalysis/AntDeploy.json index 738458a..e40e57a 100644 --- a/VideoAnalysis/AntDeploy.json +++ b/VideoAnalysis/AntDeploy.json @@ -10,12 +10,14 @@ "Host": "192.168.2.21:10107", "NickName": "", "IIsFireUrl": null, - "DockerFireUrl": "", + "DockerFireUrl": "https://videoanalysis.w.23544.com:8843/", "WindowsServiceFireUrl": null, "LinuxServiceFireUrl": null } ], - "IgnoreList": [], + "IgnoreList": [ + "ffmpeg.exe" + ], "WindowsBackUpIgnoreList": [] } ], diff --git a/VideoAnalysis/Components/Error.razor b/VideoAnalysis/Components/Error.razor index 576cc2d..3e72685 100644 --- a/VideoAnalysis/Components/Error.razor +++ b/VideoAnalysis/Components/Error.razor @@ -3,8 +3,8 @@ Error -

Error.

-

An error occurred while processing your request.

+

错误页面.

+

处理您的请求时出错。

@if (ShowRequestId) { @@ -13,15 +13,15 @@

} -

Development Mode

+

开发模式

- Swapping to Development environment will display more detailed information about the error that occurred. + 切换到Development环境将显示有关发生的错误的更多详细信息。

- The Development environment shouldn't be enabled for deployed applications. - It can result in displaying sensitive information from exceptions to end users. - For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development - and restarting the app. + 不应为已部署的应用程序启用开发环境。 + 它可能导致向最终用户显示来自异常的敏感信息。 + 对于本地调试,通过将 ASPNETCORE_ENVIRONMENT 环境变量设置为 Development 来启用 开发 环境 + 并重新启动应用程序。

@code{ diff --git a/VideoAnalysis/Components/Layouts/BasicLayout.razor.cs b/VideoAnalysis/Components/Layouts/BasicLayout.razor.cs index 629624a..47de42b 100644 --- a/VideoAnalysis/Components/Layouts/BasicLayout.razor.cs +++ b/VideoAnalysis/Components/Layouts/BasicLayout.razor.cs @@ -62,7 +62,14 @@ namespace VideoAnalysisRazor.Layouts public async Task CheckLogin() { - return (await session.GetAsync("Login")).Value; + try + { + return (await session.GetAsync("Login")).Value; + } + catch + { + return false; + } } void Reload() { diff --git a/VideoAnalysis/Components/Pages/EvaluationProject.razor.cs b/VideoAnalysis/Components/Pages/EvaluationProject.razor.cs index 81c841c..eb292fd 100644 --- a/VideoAnalysis/Components/Pages/EvaluationProject.razor.cs +++ b/VideoAnalysis/Components/Pages/EvaluationProject.razor.cs @@ -1,5 +1,4 @@ using AntDesign.TableModels; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components; using SqlSugar; using System.Linq.Expressions; @@ -8,7 +7,6 @@ using VideoAnalysisCore.Model; namespace Learn.VideoAnalysis.Components.Pages { - [Authorize] public partial class EvaluationProject : ComponentBase { diff --git a/VideoAnalysis/Components/Pages/Login.razor b/VideoAnalysis/Components/Pages/Login.razor index 5292de6..cc4f441 100644 --- a/VideoAnalysis/Components/Pages/Login.razor +++ b/VideoAnalysis/Components/Pages/Login.razor @@ -23,7 +23,7 @@

登录 视频分析平台

-
+
@@ -47,7 +47,7 @@ 注册

*@ - +
diff --git a/VideoAnalysis/Dockerfile b/VideoAnalysis/Dockerfile index 07dacb1..651cea3 100644 --- a/VideoAnalysis/Dockerfile +++ b/VideoAnalysis/Dockerfile @@ -1,8 +1,12 @@ #See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. FROM dotnet/aspnet:8.0 + +RUN ["apt-get", "--assume-yes", "update"] +RUN ["apt-get", "--assume-yes", "install", "ffmpeg"] + WORKDIR /app -EXPOSE 80 +EXPOSE 9040 COPY . . #设置时间为中国上海 环境为开发环境 @@ -12,5 +16,6 @@ ENV TZ=Asia/Shanghai #RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ sid main contrib non-free" > /etc/apt/sources.list #RUN apt-get update && apt-get install -y apt-transport-https ca-certificates apt-utils libgdiplus libc6-dev && apt-get install -y libfreetype6 && apt-get install -y libfontconfig1 && apt-get install -y fontconfig # +ENV ASPNETCORE_URLS=http://+:9040 ENTRYPOINT ["dotnet", "Learn.VideoAnalysis.dll"] diff --git a/VideoAnalysis/Program.cs b/VideoAnalysis/Program.cs index 13476c8..55514e4 100644 --- a/VideoAnalysis/Program.cs +++ b/VideoAnalysis/Program.cs @@ -55,6 +55,10 @@ namespace Learn.VideoAnalysis RedisExpand.Init(); Speaker.Init(); //SenseVoice.Init(); + builder.Services.AddControllersWithViews(options => + { + options.Filters.Add(typeof(ExceptionFilter)); + }); builder.Services.AddScoped(sp => @@ -94,8 +98,11 @@ namespace Learn.VideoAnalysis app.UseSwaggerUI(); app.UseExceptionHandler("/Error"); } + //else + //{ + // app.UseExceptionHandler("/Login"); + //} - app.UseAuthorization(); app.UseStaticFiles(); app.UseAntiforgery(); diff --git a/VideoAnalysis/appsettings.Production.json b/VideoAnalysis/appsettings.Production.json index 496ecf4..216e7d8 100644 --- a/VideoAnalysis/appsettings.Production.json +++ b/VideoAnalysis/appsettings.Production.json @@ -7,7 +7,7 @@ }, "AppConfig": { "Redis": { - "ConnectionString": "10.255.255.3,password=qwe123!@#,defaultDatabase=10" + "ConnectionString": "10.255.255.3,password=qwe123!@#,defaultDatabase=3" }, "FFmpeg": { " TimeSlice": 600 diff --git a/VideoAnalysisCore/AICore/FFMPGE/FFMPGEHandle.cs b/VideoAnalysisCore/AICore/FFMPGE/FFMPGEHandle.cs index 37bf74f..8e36eb3 100644 --- a/VideoAnalysisCore/AICore/FFMPGE/FFMPGEHandle.cs +++ b/VideoAnalysisCore/AICore/FFMPGE/FFMPGEHandle.cs @@ -5,6 +5,7 @@ using VideoAnalysisCore.Common; using System.Threading.Tasks; using static System.Runtime.InteropServices.JavaScript.JSType; using System.Xml.Linq; +using System.Runtime.InteropServices; namespace VideoAnalysisCore.AICore.FFMPGE { @@ -16,7 +17,9 @@ namespace VideoAnalysisCore.AICore.FFMPGE /// /// /// - public static string FFmpegPath = Path.Combine(AppCommon.AIModelFile, "ffmpeg.exe"); + public static string FFmpegPath = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) + ? $"/usr/bin/ffmpeg" + : Path.Combine(AppCommon.AIModelFile, "ffmpeg.exe"); /// /// 音频转码为 wav_16k /// diff --git a/VideoAnalysisCore/Common/ExceptionFilter.cs b/VideoAnalysisCore/Common/ExceptionFilter.cs new file mode 100644 index 0000000..7a439ce --- /dev/null +++ b/VideoAnalysisCore/Common/ExceptionFilter.cs @@ -0,0 +1,49 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Filters; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace VideoAnalysisCore.Common +{ + public class ExceptionFilter : IAsyncExceptionFilter + { + readonly NavigationManager nm; + public ExceptionFilter(NavigationManager nm) + { + this.nm = nm; + } + + public async Task OnExceptionAsync(ExceptionContext context) + { + if (context.Exception is InvalidOperationException) + { + nm.NavigateTo("/Login"); + } + else + { + // 创建一个包含错误信息的对象 + var errorObject = new + { + ErrorMessage = context.Exception.Message, + StackTrace = context.Exception.StackTrace, + }; + // 将错误对象序列化为JSON格式 + var json = JsonSerializer.Serialize(errorObject); + + // 设置响应内容类型为JSON + context.HttpContext.Response.ContentType = "application/json"; + // 设置状态码 + context.HttpContext.Response.StatusCode = 500; + // 将JSON数据写入响应体 + await context.HttpContext.Response.WriteAsync(json); + } + } + } +} diff --git a/VideoAnalysisCore/Common/RedisExpand.cs b/VideoAnalysisCore/Common/RedisExpand.cs index b9d4f65..92485d9 100644 --- a/VideoAnalysisCore/Common/RedisExpand.cs +++ b/VideoAnalysisCore/Common/RedisExpand.cs @@ -92,7 +92,11 @@ namespace VideoAnalysisCore.Common Console.WriteLine("初始化 redis"); Redis.Serialize = obj => System.Text.Json.JsonSerializer.Serialize(obj); Redis.Deserialize = (json, type) => System.Text.Json.JsonSerializer.Deserialize(json, type); - InitChannel(); + Task.Run(() => + { + Task.Delay(1000 * 5); + InitChannel(); + }); } /// diff --git a/VideoAnalysisCore/VideoAnalysisCore.csproj b/VideoAnalysisCore/VideoAnalysisCore.csproj index d4d557f..9e02d07 100644 --- a/VideoAnalysisCore/VideoAnalysisCore.csproj +++ b/VideoAnalysisCore/VideoAnalysisCore.csproj @@ -15,7 +15,7 @@ - PreserveNewest + Never Never @@ -32,6 +32,7 @@ +