Skip to content

Commit

Permalink
🔧 优化内存占用
Browse files Browse the repository at this point in the history
  • Loading branch information
Redns committed Apr 9, 2022
1 parent 737ffdc commit 88c6c96
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

# IDE0063: 使用简单的 "using" 语句
dotnet_diagnostic.IDE0063.severity = none

# CS8602: 解引用可能出现空引用。
dotnet_diagnostic.CS8602.severity = none
10 changes: 6 additions & 4 deletions Controllers/ImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ImageBed.Data.Access;
using ImageBed.Data.Entity;
using Microsoft.AspNetCore.Mvc;
using static ImageBed.Common.UnitNameGenerator;

namespace ImageBed.Controllers
{
Expand Down Expand Up @@ -90,14 +91,15 @@ public async Task<ApiResult<object>> Post([FromForm] IFormCollection formCollect
/// <param name="filename">图片名称</param>
/// <param name="imageDirPath">图片存储文件夹</param>
/// <returns></returns>
private async Task<ImageEntity> SaveImage(Stream fileReader, string filename, string imageDirPath)
private static async Task<ImageEntity> 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);
}
Expand Down Expand Up @@ -137,7 +139,7 @@ private async Task<ImageEntity> SaveImage(Stream fileReader, string filename, st
/// <param name="fileReader">文件输入流</param>
/// <param name="dstDirPath">文件存储路径</param>
/// <returns></returns>
private async Task SaveFile(Stream fileReader, string dstDirPath)
private static async Task SaveFile(Stream fileReader, string dstDirPath)
{
if (System.IO.File.Exists(dstDirPath))
{
Expand Down
9 changes: 5 additions & 4 deletions Data/Access/OurDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class OurDbContext : DbContext
public OurDbContext() { }
public OurDbContext(DbContextOptions<OurDbContext> options) : base(options) { }

public DbSet<ImageEntity> Images { get; set; }
public DbSet<RecordEntity> Records { get; set; }
public DbSet<ImageEntity>? Images { get; set; }
public DbSet<RecordEntity>? Records { get; set; }


/// <summary>
Expand Down Expand Up @@ -93,7 +93,7 @@ public async Task<bool> Update(ImageEntity image)
try
{
_context.Images.Update(image);
_ = _context.SaveChangesAsync();
await _context.SaveChangesAsync();
return true;
}
catch (Exception) { }
Expand All @@ -115,6 +115,7 @@ public async Task<List<ImageEntity>> Get()
return new List<ImageEntity>();
}


/// <summary>
/// 获取数据库中指定ID的图片信息
/// </summary>
Expand Down Expand Up @@ -202,7 +203,7 @@ public async Task<bool> RemoveRangeAsync(List<ImageEntity> images)
}
});
_context.Images.RemoveRange(images);
_ = _context.SaveChangesAsync();
await _context.SaveChangesAsync();
return true;
}
catch {}
Expand Down
Binary file modified Data/Database/imagebed.sqlite
Binary file not shown.
33 changes: 18 additions & 15 deletions Pages/Pics.razor
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
}
</Space>
@code{
async void RemoveSelectedImages()
async Task RemoveSelectedImages()
{
using (var context = new OurDbContext())
{
Expand Down Expand Up @@ -170,13 +170,13 @@
</Table>

@code {
ITable table;
ITable? table;
int _pageSize = 8;
int _total;

List<ImageEntity> imagesShow = new();
List<ImageEntity> imagesAll = new();
IEnumerable<ImageEntity> imagesSelected;
IEnumerable<ImageEntity>? imagesSelected;

protected override async Task OnInitializedAsync()
{
Expand All @@ -193,11 +193,11 @@
if (!string.IsNullOrEmpty(content))
{
await JS.InvokeVoidAsync("CopyToClip", content);
_message.Success("已拷贝链接到剪贴板!");
_ = _message.Success("已拷贝链接到剪贴板!");
}
else
{
await _message.Error("拷贝链接失败!");
_ = _message.Error("拷贝链接失败!");
}
}

Expand All @@ -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("图片已删除!");
}
}
}

0 comments on commit 88c6c96

Please sign in to comment.