Skip to content

Commit

Permalink
🔧 修复多图上传时复制链接错误的 Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Redns committed Apr 9, 2022
1 parent d88314c commit 737ffdc
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 71 deletions.
3 changes: 3 additions & 0 deletions Controllers/ImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ private async Task<ImageEntity> SaveImage(Stream fileReader, string filename, st
using (FileStream fileWriter = System.IO.File.Create(unitFilePath))
{
await fileReader.CopyToAsync(fileWriter);
await fileWriter.FlushAsync();
}
await fileReader.FlushAsync();
fileReader.Dispose();

// 录入数据库
Expand Down Expand Up @@ -145,6 +147,7 @@ private async Task SaveFile(Stream fileReader, string dstDirPath)
{
await fileReader.CopyToAsync(fileWriter);
}
await fileReader.FlushAsync();
fileReader.Dispose();
}

Expand Down
30 changes: 30 additions & 0 deletions Data/Access/OurDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,36 @@ public async Task<bool> Remove(string id)
return false;
}


/// <summary>
/// 移除多个图片信息
/// </summary>
/// <param name="images"></param>
/// <returns></returns>
public async Task<bool> RemoveRangeAsync(List<ImageEntity> images)
{
if ((_context != null) && (_context.Images != null))
{
try
{
images.ForEach(image =>
{
string imagePath = $"{GlobalValues.appSetting?.Data?.Resources?.Images?.Path}/{image.Name}";
if (File.Exists(imagePath))
{
File.Delete(imagePath);
}
});
_context.Images.RemoveRange(images);
_ = _context.SaveChangesAsync();
return true;
}
catch {}
}
return false;
}


public void Dispose()
{
try
Expand Down
Binary file modified Data/Database/imagebed.sqlite
Binary file not shown.
96 changes: 51 additions & 45 deletions Pages/Dashboard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -96,63 +96,69 @@

async void RefreshDashboard(Object? source, ElapsedEventArgs? e)
{
using var context = new OurDbContext();
using var sqlImageData = new SQLImageData(context);

List<ImageEntity> images = await sqlImageData.Get();
using (var context = new OurDbContext())
{
using (var sqlImageData = new SQLImageData(context))
{
List<ImageEntity> images = await sqlImageData.Get();

// 获取托管图片总数
HostImageNums = images.Count;
// 获取托管图片总数
HostImageNums = images.Count;

// 获取托管图片所占磁盘存储总量
double diskOccupancyKB = 0;
foreach(var image in images)
{
diskOccupancyKB += UnitNameGenerator.ParseFileSize(image.Size ?? "0B");
}
DiskOccupancy = $"{(diskOccupancyKB / 1024.0).ToString("f3")} MB";
// 获取托管图片所占磁盘存储总量
double diskOccupancyKB = 0;
images.ForEach(image =>
{
diskOccupancyKB += UnitNameGenerator.ParseFileSize(image.Size ?? "0B");
});
DiskOccupancy = $"{(diskOccupancyKB / 1024.0).ToString("f3")} MB";

// 获取该进程占用的运行内存
Process mainProcess = Process.GetCurrentProcess();
mainProcess.Refresh();
RunningMemUsage = $"{mainProcess.WorkingSet64 / (1024*1024)} MB";
// 获取该进程占用的运行内存
Process mainProcess = Process.GetCurrentProcess();
mainProcess.Refresh();
RunningMemUsage = $"{mainProcess.WorkingSet64 / (1024*1024)} MB";

// 获取应用运行时长
TimeSpan SysRunningTimeSpan = DateTime.Now - mainProcess.StartTime;
SysRunningTime = $"{(int)SysRunningTimeSpan.TotalHours}:{((int)SysRunningTimeSpan.TotalMinutes) % 60}:{((int)SysRunningTimeSpan.TotalSeconds) % 60}";
// 获取应用运行时长
TimeSpan SysRunningTimeSpan = DateTime.Now - mainProcess.StartTime;
SysRunningTime = $"{(int)SysRunningTimeSpan.TotalHours}:{((int)SysRunningTimeSpan.TotalMinutes) % 60}:{((int)SysRunningTimeSpan.TotalSeconds) % 60}";

await InvokeAsync(new Action(() => { StateHasChanged(); }));
await InvokeAsync(new Action(() => { StateHasChanged(); }));
}
}
}


async void RefreshChart()
{
using var context2 = new OurDbContext();
using var sqlRecordData = new SQLRecordData(context2);

SysRecords.Clear();
foreach(var record in await sqlRecordData.Get())
using (var context = new OurDbContext())
{
SysRecords.Add(new
{
date = record.Date,
type = "图片上传数量",
value = record.UploadImageNum
});

SysRecords.Add(new
using (var sqlRecordData = new SQLRecordData(context))
{
date = record.Date,
type = "图片磁盘占用(MB)",
value = record.UploadImageSize
});

SysRecords.Add(new
{
date = record.Date,
type = "图片请求次数",
value = record.RequestNum
});
SysRecords.Clear();
foreach(var record in await sqlRecordData.Get())
{
SysRecords.Add(new
{
date = record.Date,
type = "图片上传数量",
value = record.UploadImageNum
});

SysRecords.Add(new
{
date = record.Date,
type = "图片磁盘占用(MB)",
value = record.UploadImageSize
});

SysRecords.Add(new
{
date = record.Date,
type = "图片请求次数",
value = record.RequestNum
});
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
}
}
});
fileList.Clear();
await JS.InvokeVoidAsync("CopyToClip", urls);
_ = _message.Success("图片上传完成!");
}
Expand Down
56 changes: 31 additions & 25 deletions Pages/Pics.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
searchRunning = true;

imagesShow.Clear();
foreach (var image in imagesAll.Where(image => (searchText == null) || (image.Name ?? "").Contains(searchText) || (image.Owner ?? "").Contains(searchText))) {
imagesShow.Add(image);
}

imagesShow.AddRange(imagesAll.Where(image => (searchText == null) ||
(image.Name ?? "").Contains(searchText) ||
(image.Owner ?? "").Contains(searchText)));
searchRunning = false;
}
}
Expand Down Expand Up @@ -69,18 +68,22 @@

async void UploadFinished()
{
using var context = new OurDbContext();
using var sqlImageData = new SQLImageData(context);
imagesAll = await sqlImageData.Get();
OnSearch();

await _notice.Open(new NotificationConfig()
using (var context = new OurDbContext())
{
Message = "图片导入完成",
Description = "图片已成功导入至服务器!"
});
}
using (var sqlImageData = new SQLImageData(context))
{
imagesAll = await sqlImageData.Get();
OnSearch();

_ = _notice.Open(new NotificationConfig()
{
Message = "图片导入完成",
Description = "图片已成功导入至服务器!"
});
}
}
}
}
</SpaceItem>
@if ((imagesSelected != null ) && (imagesSelected.Any()))
{
Expand All @@ -91,17 +94,20 @@
@code{
async void RemoveSelectedImages()
{
using var context = new OurDbContext();
using var sqlImageData = new SQLImageData(context);

foreach(var image in imagesSelected)
using (var context = new OurDbContext())
{
await sqlImageData.Remove(image.Id);
imagesAll.Remove(image);
}
_message.Success("图片已删除!");
using (var sqlImageData = new SQLImageData(context))
{
_ = sqlImageData.RemoveRangeAsync(imagesSelected.ToList());
foreach(var image in imagesSelected)
{
imagesAll.Remove(image);
}
_ = _message.Success("图片已删除!");

OnSearch();
OnSearch();
}
}
}


Expand All @@ -126,8 +132,8 @@
}
FileOperator.CompressMulti(imageFullpaths.ToArray(), $"{GlobalValues.appSetting?.Data?.Resources?.Images?.Path}/Images.export");

await JS.InvokeVoidAsync("downloadFileFromStream", "Images.export", "api/image/Images.export");
await _message.Success("导出成功!");
_ = JS.InvokeVoidAsync("downloadFileFromStream", "Images.export", "api/image/Images.export");
_ = _message.Success("导出成功!");
}


Expand Down
2 changes: 1 addition & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Resources": {
"Images": {
"Path": "Data/Resources/Images",
"RenameFormat": 4
"RenameFormat": 1
},
"Database": {
"Path": "Data Source=Data/Database/imagebed.sqlite;",
Expand Down

0 comments on commit 737ffdc

Please sign in to comment.