فهرست منبع

错误页优化

懒得勤快 4 سال پیش
والد
کامیت
4f463ddffe

+ 6 - 18
src/Masuit.MyBlogs.Core/Common/ImagebedClient.cs

@@ -5,7 +5,6 @@ using Masuit.Tools.Html;
 using Masuit.Tools.Logging;
 using Masuit.Tools.Systems;
 using Microsoft.Extensions.Configuration;
-using Polly;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -38,33 +37,22 @@ namespace Masuit.MyBlogs.Core.Common
             _httpClient = httpClient;
         }
 
+        private readonly List<string> _failedList = new();
+
         /// <summary>
         /// 上传图片
         /// </summary>
         /// <param name="stream"></param>
         /// <param name="file"></param>
         /// <returns></returns>
-        public async Task<(string url, bool success)> UploadImage(Stream stream, string file, CancellationToken cancellationToken)
+        public Task<(string url, bool success)> UploadImage(Stream stream, string file, CancellationToken cancellationToken)
         {
             if (stream.Length < 51200)
             {
-                return (null, false);
+                return Task.FromResult<(string, bool)>((null, false));
             }
 
             file = Path.GetFileName(file);
-            return await Policy<(string url, bool success)>.Handle<Exception>().RetryAsync(3).ExecuteAsync(() => UploadGit(stream, file, cancellationToken));
-        }
-
-        private readonly List<string> _failedList = new();
-
-        /// <summary>
-        /// gitlab图床
-        /// </summary>
-        /// <param name="stream"></param>
-        /// <param name="file"></param>
-        /// <returns></returns>
-        private Task<(string url, bool success)> UploadGit(Stream stream, string file, CancellationToken cancellationToken)
-        {
             var gitlabs = AppConfig.GitlabConfigs.Where(c => c.FileLimitSize >= stream.Length && !_failedList.Contains(c.ApiUrl)).OrderBy(c => Guid.NewGuid()).ToList();
             if (gitlabs.Count > 0)
             {
@@ -207,14 +195,14 @@ namespace Masuit.MyBlogs.Core.Common
             }
 
             var srcs = content.MatchImgSrcs();
-            foreach (string src in srcs)
+            foreach (var src in srcs)
             {
                 if (src.StartsWith("http"))
                 {
                     continue;
                 }
 
-                var path = Path.Combine(AppContext.BaseDirectory + "wwwroot", src.Replace("/", @"\").Substring(1));
+                var path = Path.Combine(AppContext.BaseDirectory + "wwwroot", src.Replace("/", @"\")[1..]);
                 if (!File.Exists(path))
                 {
                     continue;

+ 2 - 8
src/Masuit.MyBlogs.Core/Controllers/ErrorController.cs

@@ -87,14 +87,8 @@ namespace Masuit.MyBlogs.Core.Controllers
                             LogManager.Debug("↑↑↑请求参数:\n" + body);
                         }
                         break;
-                    case NotFoundException ex:
-                        Response.StatusCode = 404;
-                        return Request.Method.Equals(HttpMethods.Get) ? View("Index") : Json(new
-                        {
-                            StatusCode = 404,
-                            Success = false,
-                            ex.Message
-                        });
+                    case NotFoundException:
+                        return RedirectToAction("Index");
                     case AccessDenyException:
                         var (location, network) = ip.GetIPLocation();
                         var tips = Template.Create(CommonHelper.SystemSettings.GetOrAdd("AccessDenyTips", @"<h4>遇到了什么问题?</h4>

+ 1 - 1
src/Masuit.MyBlogs.Core/Controllers/LinksController.cs

@@ -142,7 +142,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             {
                 if (t.IsFaulted || t.IsCanceled)
                 {
-                    return ResultData(null, false, link + " 似乎挂了!");
+                    return ResultData(null, false, link + " 似乎挂了!错误信息:" + t.Exception?.Message);
                 }
 
                 using var res = t.Result;

+ 1 - 1
src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj

@@ -56,7 +56,7 @@
         <PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.10" />
         <PackageReference Include="TimeZoneConverter" Version="3.5.0" />
         <PackageReference Include="WilderMinds.RssSyndication" Version="1.7.0" />
-        <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="5.1.41" />
+        <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="5.2.1" />
     </ItemGroup>
     <ItemGroup>
         <Content Update="appsettings.json">

+ 41 - 39
src/Masuit.MyBlogs.Core/Views/Error/Index.cshtml

@@ -1,10 +1,12 @@
 @inject IWebHostEnvironment HostEnvironment
 @using System.IO
 @using Masuit.Tools
+@using Masuit.Tools.Win32
 @using Microsoft.AspNetCore.Hosting
 @using Microsoft.Net.Http.Headers
 
 @{
+    string[] colors = { "success", "info", "primary", "warning", "danger" };
     Layout = null;
 }
 <!DOCTYPE html>
@@ -12,14 +14,6 @@
 <head>
     <title>404-页面找不到啦!</title>
     <style type="text/css">
-        #main-scrollbar {
-            position: fixed;
-            top: 0;
-            right: 0;
-            bottom: 0;
-            left: 0;
-        }
-
         .link, .texts {
             margin: 0 auto 15px;
             color: #505050;
@@ -84,40 +78,48 @@
     <script src="~/Assets/catchcat/catch-the-cat.js"></script>
 </head>
 <body>
-    <main>
-        <div class="container">
-            <header id="header" class="page-header text-center">
-                <h1><a href="/">Oops!</a></h1>
-            </header>
-            <div id="container">
-                @{
-                    Random r = new Random();
-                    string imgPath = $"/Assets/images/404/404{r.StrictNext(Directory.GetFiles(HostEnvironment.WebRootPath + "/Assets/images/404").Length) + 1}.jpg";
-                }
-                <img class="img img-responsive img-thumbnail" width="100%" src="@imgPath" alt="@ViewBag.Keywords" />
-                <h3 class="margintop20">
-                    <em>
+    <div class="container">
+        <header id="header" class="page-header text-center">
+            <h1><a href="/">Oops!</a></h1>
+        </header>
+        <div id="container">
+            @{
+                Random r = new Random();
+                string imgPath = $"/Assets/images/404/404{r.StrictNext(Directory.GetFiles(HostEnvironment.WebRootPath + "/Assets/images/404").Length) + 1}.jpg";
+            }
+            <img class="img img-responsive img-thumbnail" width="100%" src="@imgPath" alt="@ViewBag.Keywords" />
+            <h3 class="margintop20">
+                <em>
                         <span class="STYLE1">404 Error&nbsp;</span>
                     </em>:所查找的页面不存在,可能已被删除或您输错了网址!
-                </h3>
-                <a class="btn btn-info btn-lg" href="/"><i class="icon-reply">返回首页</i></a>
-                <dl class="texts">
-                    <dt class="h2">正在联系火星总部查找您所需要的页面.请返回等待信息......</dt>
-                    <dd>
-                        <ul>
-                            <li class="h2">不要返回吗?</li>
-                            <li class="h3">确定不要返回吗?</li>
-                            <li class="h4">真的真的确定不要返回吗?</li>
-                            <li class="h5">好吧.还是随便你要不要真的确定返回吧</li>
-                            <li class="h6">要是真的不愿意返回就玩个游戏吧,机会很难得哦——</li>
-                            <li>将猫困在一个深色圆点围成的圈子里面就算成功了!</li>
-                        </ul>
-                    </dd>
-                    <div id="games"></div>
-                </dl>
-            </div>
+            </h3>
+            <a class="btn btn-info btn-lg" href="/"><i class="icon-reply">返回首页</i></a>
+            <dl class="texts">
+                <dt class="h2">正在联系火星总部查找您所需要的页面.请返回等待信息......</dt>
+                <dd>
+                    <ul>
+                        <li class="h2">不要返回吗?</li>
+                        <li class="h3">确定不要返回吗?</li>
+                        <li class="h4">真的真的确定不要返回吗?</li>
+                        <li class="h5">好吧.还是随便你要不要真的确定返回吧</li>
+                        <li class="h6">要是真的不愿意返回就玩个游戏吧,机会很难得哦——</li>
+                        <li>将猫困在一个深色圆点围成的圈子里面就算成功了!</li>
+                    </ul>
+                </dd>
+                <div id="games"></div>
+            </dl>
+        </div>
+    </div>
+    <div class="layer animated fadeIn">
+        <div class="loadEffect">
+            @for (int i = 0; i < 7; i++)
+            {
+                <div>
+                    <span class="bg-@colors[new Random().StrictNext() % 5]"></span>
+                </div>
+            }
         </div>
-    </main>
+    </div>
 </body>
 </html>
 <script>