From 88c6c96b0f3f81775d904c239f8c76b5145eb77c Mon Sep 17 00:00:00 2001 From: Redns <69022576+Redns@users.noreply.github.com> Date: Sat, 9 Apr 2022 21:11:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20=E4=BC=98=E5=8C=96=E5=86=85?= =?UTF-8?q?=E5=AD=98=E5=8D=A0=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 3 +++ Controllers/ImageController.cs | 10 ++++++---- Data/Access/OurDbContext.cs | 9 +++++---- Data/Database/imagebed.sqlite | Bin 20480 -> 20480 bytes Pages/Pics.razor | 33 ++++++++++++++++++--------------- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/.editorconfig b/.editorconfig index 99b0e38..58b988c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,3 +2,6 @@ # IDE0063: 使用简单的 "using" 语句 dotnet_diagnostic.IDE0063.severity = none + +# CS8602: 解引用可能出现空引用。 +dotnet_diagnostic.CS8602.severity = none diff --git a/Controllers/ImageController.cs b/Controllers/ImageController.cs index b246ae7..83fb05c 100644 --- a/Controllers/ImageController.cs +++ b/Controllers/ImageController.cs @@ -2,6 +2,7 @@ using ImageBed.Data.Access; using ImageBed.Data.Entity; using Microsoft.AspNetCore.Mvc; +using static ImageBed.Common.UnitNameGenerator; namespace ImageBed.Controllers { @@ -90,14 +91,15 @@ public async Task> Post([FromForm] IFormCollection formCollect /// 图片名称 /// 图片存储文件夹 /// - private async Task SaveImage(Stream fileReader, string filename, string imageDirPath) + private static async Task SaveImage(Stream fileReader, string filename, string imageDirPath) { // 格式化文件名 - string unitFileName = UnitNameGenerator.RenameFile(imageDirPath, filename, GlobalValues.appSetting.Data.Resources.Images.RenameFormat); + RenameFormat renameFormat = GlobalValues.appSetting?.Data?.Resources?.Images?.RenameFormat ?? UnitNameGenerator.RenameFormat.MD5; + string unitFileName = RenameFile(imageDirPath, filename, renameFormat); string unitFilePath = $"{imageDirPath}/{unitFileName}"; // 检查是否命名冲突 - if ((GlobalValues.appSetting.Data.Resources.Images.RenameFormat == UnitNameGenerator.RenameFormat.NONE) && System.IO.File.Exists(unitFilePath)) + if ((renameFormat == RenameFormat.NONE) && System.IO.File.Exists(unitFilePath)) { System.IO.File.Delete(unitFilePath); } @@ -137,7 +139,7 @@ private async Task SaveImage(Stream fileReader, string filename, st /// 文件输入流 /// 文件存储路径 /// - private async Task SaveFile(Stream fileReader, string dstDirPath) + private static async Task SaveFile(Stream fileReader, string dstDirPath) { if (System.IO.File.Exists(dstDirPath)) { diff --git a/Data/Access/OurDbContext.cs b/Data/Access/OurDbContext.cs index a95e1e7..8254a37 100644 --- a/Data/Access/OurDbContext.cs +++ b/Data/Access/OurDbContext.cs @@ -9,8 +9,8 @@ public class OurDbContext : DbContext public OurDbContext() { } public OurDbContext(DbContextOptions options) : base(options) { } - public DbSet Images { get; set; } - public DbSet Records { get; set; } + public DbSet? Images { get; set; } + public DbSet? Records { get; set; } /// @@ -93,7 +93,7 @@ public async Task Update(ImageEntity image) try { _context.Images.Update(image); - _ = _context.SaveChangesAsync(); + await _context.SaveChangesAsync(); return true; } catch (Exception) { } @@ -115,6 +115,7 @@ public async Task> Get() return new List(); } + /// /// 获取数据库中指定ID的图片信息 /// @@ -202,7 +203,7 @@ public async Task RemoveRangeAsync(List images) } }); _context.Images.RemoveRange(images); - _ = _context.SaveChangesAsync(); + await _context.SaveChangesAsync(); return true; } catch {} diff --git a/Data/Database/imagebed.sqlite b/Data/Database/imagebed.sqlite index e2322b622ce49432add4caba24351c865dcd5a65..0acfd0e08450257ef49fb2ac4714bbd8254bd4bf 100644 GIT binary patch delta 101 zcmZozz}T>Wae_4C^ocUgjMFzJtmJ2o;g6jxARskaL4akmmVgmI4+8_k65r5FNqu>7 z#>o%t6()DeCy5za85&p_TN;^~8E6@q8W}jIk delta 86 zcmZozz}T>Wae_4C#ECM_j1xB|tmJ2o<&T{#ARskaL4akmmVgmIYaRmw!{W*N#e0P; pt&EM0OwA0mj7*IT98+>L^EN4P3NTONpEOy(0I0(NsKelzJOC<87w`Z8 diff --git a/Pages/Pics.razor b/Pages/Pics.razor index 2fd9e06..3860700 100644 --- a/Pages/Pics.razor +++ b/Pages/Pics.razor @@ -92,7 +92,7 @@ } @code{ - async void RemoveSelectedImages() + async Task RemoveSelectedImages() { using (var context = new OurDbContext()) { @@ -170,13 +170,13 @@ @code { - ITable table; + ITable? table; int _pageSize = 8; int _total; List imagesShow = new(); List imagesAll = new(); - IEnumerable imagesSelected; + IEnumerable? imagesSelected; protected override async Task OnInitializedAsync() { @@ -193,11 +193,11 @@ if (!string.IsNullOrEmpty(content)) { await JS.InvokeVoidAsync("CopyToClip", content); - _message.Success("已拷贝链接到剪贴板!"); + _ = _message.Success("已拷贝链接到剪贴板!"); } else { - await _message.Error("拷贝链接失败!"); + _ = _message.Error("拷贝链接失败!"); } } @@ -212,19 +212,22 @@ { if (!string.IsNullOrEmpty(id)) { - using var context = new OurDbContext(); - using var sqlImageData = new SQLImageData(context); + using (var context = new OurDbContext()) + { + using (var sqlImageData = new SQLImageData(context)) + { + _ = sqlImageData.Remove(id); - await sqlImageData.Remove(id); + ImageEntity? image = imagesAll.FirstOrDefault(image => image.Id == id); + if(image != null) + { + imagesAll.Remove(image); + OnSearch(); + } - ImageEntity? image = imagesAll.FirstOrDefault(image => image.Id == id); - if(image != null) - { - imagesAll.Remove(image); - OnSearch(); + _ = _message.Success("图片已删除!"); + } } - - _message.Success("图片已删除!"); } } } \ No newline at end of file