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 e2322b6..0acfd0e 100644 Binary files a/Data/Database/imagebed.sqlite and b/Data/Database/imagebed.sqlite differ 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