Skip to content

Commit

Permalink
🔧 完善设置界面
Browse files Browse the repository at this point in the history
  • Loading branch information
Redns committed Apr 11, 2022
1 parent cf4e60e commit c343f6c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 11 deletions.
25 changes: 21 additions & 4 deletions Controllers/ImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ public async Task<ApiResult<object>> Post([FromForm] IFormCollection formCollect
}

var sqlImageData = new SQLImageData(context);
foreach (IFormFile fileReader in (FormFileCollection)formCollection.Files)
var uploadImages = (FormFileCollection)formCollection.Files;
var imageMaxNumLimit = GlobalValues.appSetting?.Data?.Resources?.Images?.MaxNum ?? 0;
if((imageMaxNumLimit > 0) && (uploadImages.Count > imageMaxNumLimit))
{
int indexStart = imageMaxNumLimit;
int indexCount = uploadImages.Count - imageMaxNumLimit;
uploadImages.RemoveRange(indexStart, indexCount);
}

foreach (IFormFile fileReader in uploadImages)
{
try
{
Expand All @@ -54,9 +63,17 @@ public async Task<ApiResult<object>> Post([FromForm] IFormCollection formCollect
}
else
{
var image = await FileOperator.SaveImage(fileReader.OpenReadStream(), fileReader.FileName, imageDir);
images.Add(image);
imageUrls.Add($"{image.Url}");
int imageMaxSizeLimit = GlobalValues.appSetting.Data.Resources.Images.MaxSize;
if((imageMaxSizeLimit <= 0) || fileReader.Length <= imageMaxSizeLimit*1024*1024)
{
var image = await FileOperator.SaveImage(fileReader.OpenReadStream(), fileReader.FileName, imageDir);
images.Add(image);
imageUrls.Add($"{image.Url}");
}
else
{
imageUrls.Add(string.Empty);
}
}
}
catch (Exception)
Expand Down
Binary file modified Data/Database/imagebed.sqlite
Binary file not shown.
2 changes: 2 additions & 0 deletions Data/Entity/AppSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public class Image
{
public string? Path { get; set; }
public RenameFormat RenameFormat { get; set; }
public int MaxSize { get; set; }
public int MaxNum { get; set; }
}


Expand Down
26 changes: 26 additions & 0 deletions Pages/Cog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@
OnFinish="OnFinish"
OnFinishFailed="OnFinishFailed"
Style="margin-top:80px;">
<FormItem Label="图片尺寸限制">
<Tooltip Title="@("设置为 0 则不限制图片尺寸")">
<Space>
<SpaceItem>
<AntDesign.InputNumber Step="1" Min="0" Max="99999" @bind-Value="ImageMaxSize" />
</SpaceItem>
<SpaceItem>
MB
</SpaceItem>
</Space>

</Tooltip>
</FormItem>
<FormItem Label="单次上传数量">
<Tooltip Title="@("设置为 0 则不限制单次上传图片数")">
<Space>
<SpaceItem>
<AntDesign.InputNumber Step="1" Min="0" Max="99999" @bind-Value="ImageMaxNum" />
</SpaceItem>
<SpaceItem>
</SpaceItem>
</Space>

</Tooltip>
</FormItem>
<FormItem Label="重命名方式">
<Tooltip Title="@("若不开启重命名, 图片名冲突时将覆盖原图片")">
<Select @bind-Value="@renameFormat"
Expand Down
18 changes: 17 additions & 1 deletion Pages/Cog.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ partial class Cog
// 系统资源统计开始时间
private double RefreshStartTime = GlobalValues.appSetting?.Record?.RefreshStartTime ?? 0;

// 图片尺寸限制
private double ImageMaxSize = GlobalValues.appSetting?.Data.Resources.Images.MaxSize ?? 0;

// 单次上传数量限制
private double ImageMaxNum = GlobalValues.appSetting?.Data.Resources.Images.MaxNum ?? 0;


/// <summary>
/// 修改设置文件
Expand All @@ -28,7 +34,9 @@ private void OnFinish(EditContext editContext)
{
if ((renameFormat != GlobalValues.appSetting?.Data?.Resources?.Images?.RenameFormat) ||
(RefreshDashboardRealTime != GlobalValues.appSetting?.Record?.RefreshRealTime) ||
(RefreshStartTime != GlobalValues.appSetting?.Record?.RefreshStartTime))
(RefreshStartTime != GlobalValues.appSetting?.Record?.RefreshStartTime) ||
(ImageMaxSize != GlobalValues.appSetting?.Data?.Resources?.Images?.MaxSize) ||
(ImageMaxNum != GlobalValues.appSetting?.Data?.Resources?.Images?.MaxNum))
{
GlobalValues.appSetting.Data.Resources.Images.RenameFormat = renameFormat;
GlobalValues.appSetting.Record.RefreshRealTime = RefreshDashboardRealTime;
Expand All @@ -37,6 +45,14 @@ private void OnFinish(EditContext editContext)
else if(RefreshStartTime > 18) { RefreshStartTime = 18; }
GlobalValues.appSetting.Record.RefreshStartTime = (int)RefreshStartTime;

if(ImageMaxSize < 0) { ImageMaxSize = 0; }
else if(ImageMaxSize > 99999) { ImageMaxSize = 99999; }
GlobalValues.appSetting.Data.Resources.Images.MaxSize = (int)ImageMaxSize;

if(ImageMaxNum < 0) { ImageMaxNum = 0; }
else if(ImageMaxNum > 99999) { ImageMaxNum = 99999; }
GlobalValues.appSetting.Data.Resources.Images.MaxNum = (int)ImageMaxNum;

AppSetting.Save(GlobalValues.appSetting, "appsettings.json");
}
_message.Success("设置完成!");
Expand Down
14 changes: 8 additions & 6 deletions appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
"Resources": {
"Images": {
"Path": "Data/Resources/Images",
"RenameFormat": 2
"RenameFormat": 1,
"MaxSize": 1,
"MaxNum": 2
},
"Database": {
"Path": "Data Source=Data/Database/imagebed.sqlite;",
"TemplatePath": "Data/Database/imagebed-blank.sqlite"
}
}
},
"Record": {
"RefreshRealTime": false,
"RefreshStartTime": 18,
"RefreshInterval": 6
}
"Record": {
"RefreshRealTime": false,
"RefreshStartTime": 18,
"RefreshInterval": 6
}
}

0 comments on commit c343f6c

Please sign in to comment.