upgrade to .net 8

This commit is contained in:
Tsanie 2024-10-12 14:34:11 +08:00
parent 7a44b7fd9c
commit c914f13828
Signed by: tsanie
GPG Key ID: DA27B68C1D10203C
15 changed files with 384 additions and 393 deletions

View File

@ -9,12 +9,8 @@ namespace Blahblah.FlowerStory.Server.Controller;
[ApiController]
[Produces("application/json")]
[Route("api")]
public partial class ApiController : BaseController
public partial class ApiController(FlowerDatabase database, ILogger<ApiController>? logger = null) : BaseController<ApiController>(database, logger)
{
/// <inheritdoc/>
public ApiController(FlowerDatabase database, ILogger<BaseController>? logger = null) : base(database, logger)
{
}
/// <summary>
/// 获取版本号
@ -46,7 +42,7 @@ public partial class ApiController : BaseController
/// </remarks>
/// <returns>字典集</returns>
/// <response code="200">返回常量字典集</response>
[Route("consts", Name = "getConsts")]
[Route("consts", Name = "getConstants")]
[HttpGet]
public ActionResult<DefinitionResult> GetDefinitions()
{

View File

@ -12,7 +12,12 @@ namespace Blahblah.FlowerStory.Server.Controller;
/// <summary>
/// 基础服务抽象类
/// </summary>
public abstract partial class BaseController : ControllerBase
/// <remarks>
/// 构造基础服务类
/// </remarks>
/// <param name="database">数据库对象</param>
/// <param name="logger">日志对象</param>
public abstract partial class BaseController<T>(FlowerDatabase database, ILogger<T>? logger = null) : ControllerBase
{
private const string Salt = "Blah blah, o! Flower story, intimately help you record every bit of the garden.";
@ -32,17 +37,17 @@ public abstract partial class BaseController : ControllerBase
/// <summary>
/// 支持的图片文件签名
/// </summary>
protected static readonly List<byte[]> PhotoSignatures = new()
{
protected static readonly List<byte[]> PhotoSignatures =
[
// jpeg
new byte[] { 0xFF, 0xD8, 0xFF, 0xDB },
new byte[] { 0xFF, 0xD8, 0xFF, 0xE0 },
new byte[] { 0xFF, 0xD8, 0xFF, 0xE1 },
new byte[] { 0xFF, 0xD8, 0xFF, 0xE2 },
new byte[] { 0xFF, 0xD8, 0xFF, 0xE3 },
[0xFF, 0xD8, 0xFF, 0xDB],
[0xFF, 0xD8, 0xFF, 0xE0],
[0xFF, 0xD8, 0xFF, 0xE1],
[0xFF, 0xD8, 0xFF, 0xE2],
[0xFF, 0xD8, 0xFF, 0xE3],
// png
new byte[] { 0x89, 0x50, 0x4E, 0x47 }
};
[0x89, 0x50, 0x4E, 0x47]
];
/// <summary>
/// 自定义认证头的关键字
@ -64,22 +69,11 @@ public abstract partial class BaseController : ControllerBase
/// <summary>
/// 数据库对象
/// </summary>
protected readonly FlowerDatabase database;
protected readonly FlowerDatabase database = database;
/// <summary>
/// 日志对象
/// </summary>
protected readonly ILogger<BaseController>? logger;
/// <summary>
/// 构造基础服务类
/// </summary>
/// <param name="database">数据库对象</param>
/// <param name="logger">日志对象</param>
protected BaseController(FlowerDatabase database, ILogger<BaseController>? logger = null)
{
this.database = database;
this.logger = logger;
}
protected readonly ILogger? logger = logger;
/// <summary>
/// 计算密码的 hash
@ -88,16 +82,11 @@ public abstract partial class BaseController : ControllerBase
/// <param name="id">用户 id</param>
/// <returns>密码 hash值为 SHA256(password+id+salt)</returns>
/// <exception cref="ArgumentNullException"></exception>
protected string HashPassword(string password, string id)
public string HashPassword(string password, string id)
{
if (string.IsNullOrEmpty(password))
{
throw new ArgumentNullException(nameof(password));
}
if (string.IsNullOrEmpty(id))
{
throw new ArgumentNullException(nameof(id));
}
ArgumentException.ThrowIfNullOrEmpty(password);
ArgumentException.ThrowIfNullOrEmpty(id);
var data = Encoding.UTF8.GetBytes($"{password}{id}{Salt}");
var hash = SHA256.HashData(data);
return Convert.ToHexString(hash);
@ -216,7 +205,7 @@ public abstract partial class BaseController : ControllerBase
using Stream? stream = asm.GetManifestResourceStream($"{@namespace}.{filename}");
if (stream == null)
{
return Array.Empty<byte>();
return [];
}
using var ms = new MemoryStream();
stream.CopyTo(ms);

View File

@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
namespace Blahblah.FlowerStory.Server.Controller;
partial class BaseController
partial class BaseController<T>
{
private static ErrorResponse CreateErrorResponse(int status, string title, string? detail = null, string? instance = null)
{

View File

@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore;
namespace Blahblah.FlowerStory.Server.Controller;
partial class BaseController
partial class BaseController<T>
{
/// <summary>
/// 执行事务
@ -13,7 +13,8 @@ partial class BaseController
/// <exception cref="ArgumentNullException">执行代理为 null</exception>
protected async Task ExecuteTransaction(Func<CancellationToken, Task> executor, CancellationToken token = default)
{
if (executor == null) throw new ArgumentNullException(nameof(executor));
ArgumentNullException.ThrowIfNull(executor);
using var trans = await database.Database.BeginTransactionAsync(token);
try
{

View File

@ -12,14 +12,9 @@ namespace Blahblah.FlowerStory.Server.Controller;
[ApiController]
[Produces("application/json")]
[Route("api/comment")]
public class CommentApiController : BaseController
public class CommentApiController(FlowerDatabase database, ILogger<CommentApiController>? logger = null) : BaseController<CommentApiController>(database, logger)
{
static readonly int?[] specialTypes = { 1, 2, 3 };
/// <inheritdoc/>
public CommentApiController(FlowerDatabase database, ILogger<BaseController>? logger = null) : base(database, logger)
{
}
static readonly int?[] specialTypes = [1, 2, 3];
/// <summary>
/// 获取符合条件的评论

View File

@ -12,12 +12,8 @@ namespace Blahblah.FlowerStory.Server.Controller;
[ApiController]
[Produces("application/json")]
[Route("api/event")]
public class EventApiController : BaseController
public class EventApiController(FlowerDatabase database, ILogger<EventApiController>? logger = null) : BaseController<EventApiController>(database, logger)
{
/// <inheritdoc/>
public EventApiController(FlowerDatabase database, ILogger<BaseController>? logger = null) : base(database, logger)
{
}
/// <summary>
/// 获取符合条件的公共事件
@ -93,8 +89,8 @@ public class EventApiController : BaseController
if (key != null)
{
records = records.Where(r =>
r.ByUserName != null && r.ByUserName.ToLower().Contains(key.ToLower()) ||
r.Memo != null && r.Memo.ToLower().Contains(key.ToLower()));
r.ByUserName != null && r.ByUserName.Contains(key, StringComparison.CurrentCultureIgnoreCase) ||
r.Memo != null && r.Memo.Contains(key, StringComparison.CurrentCultureIgnoreCase));
}
if (from != null)
{
@ -208,7 +204,7 @@ public class EventApiController : BaseController
if (includePhoto == true)
{
item.Photos = database.Photos.Where(p => p.RecordId == item.Id).ToList();
item.Photos = [.. database.Photos.Where(p => p.RecordId == item.Id)];
foreach (var photo in item.Photos)
{
photo.Url = $"photo/flower/{item.FlowerId}/{photo.Path}";
@ -270,7 +266,7 @@ public class EventApiController : BaseController
/// <remarks>
/// 请求示例:
///
/// POST /api/event/remove
/// POST /api/event/removeany
/// Authorization: authorization id
/// [
/// 2, 4, 5, 11

View File

@ -12,12 +12,8 @@ namespace Blahblah.FlowerStory.Server.Controller;
[ApiController]
[Produces("application/json")]
[Route("api/flower")]
public class FlowerApiController : BaseController
public class FlowerApiController(FlowerDatabase database, ILogger<FlowerApiController>? logger = null) : BaseController<FlowerApiController>(database, logger)
{
/// <inheritdoc/>
public FlowerApiController(FlowerDatabase database, ILogger<BaseController>? logger = null) : base(database, logger)
{
}
/// <summary>
/// 获取用户名下所有符合条件的花草
@ -184,9 +180,9 @@ public class FlowerApiController : BaseController
if (key != null)
{
flowers = flowers.Where(f =>
f.Name.ToLower().Contains(key.ToLower()) ||
f.Name.Contains(key, StringComparison.CurrentCultureIgnoreCase) ||
f.Purchase != null &&
f.Purchase.ToLower().Contains(key.ToLower()));
f.Purchase.Contains(key, StringComparison.CurrentCultureIgnoreCase));
}
if (buyFrom != null)
{
@ -325,7 +321,7 @@ public class FlowerApiController : BaseController
if (includePhoto == true)
{
item.Photos = database.Photos.Where(p => p.FlowerId == item.Id && p.RecordId == null).ToList();
item.Photos = [.. database.Photos.Where(p => p.FlowerId == item.Id && p.RecordId == null)];
foreach (var photo in item.Photos)
{
photo.Url = $"photo/flower/{item.Id}/{photo.Path}";
@ -388,7 +384,7 @@ public class FlowerApiController : BaseController
/// <remarks>
/// 请求示例:
///
/// POST /api/flower/remove
/// POST /api/flower/removeany
/// Authorization: authorization id
/// [
/// 2, 4, 5, 11

View File

@ -9,17 +9,12 @@ namespace Blahblah.FlowerStory.Server.Controller;
/// 图片相关服务
/// </summary>
[Route("photo")]
public class ImageController : BaseController
public class ImageController(FlowerDatabase database, ILogger<ImageController>? logger = null) : BaseController<ImageController>(database, logger)
{
static byte[]? emptyAvatar;
static byte[] EmptyAvatar => emptyAvatar ??= GetEmbeddedData("image.avatar.jpg");
/// <inheritdoc/>
public ImageController(FlowerDatabase database, ILogger<BaseController>? logger = null) : base(database, logger)
{
}
/// <summary>
/// 请求自己的头像
/// </summary>

View File

@ -7,16 +7,30 @@ namespace Blahblah.FlowerStory.Server.Controller;
/// <inheritdoc/>
[Route("")]
public class SwaggerController : ControllerBase
public class SwaggerController(SwaggerGenerator generator) : ControllerBase
{
private readonly SwaggerGenerator generator;
/// <inheritdoc/>
public SwaggerController(SwaggerGenerator generator)
[Route("login")]
[Produces("text/html")]
[HttpGet]
public ActionResult Login()
{
this.generator = generator;
var address = Request.Host.Host == "api.tsanie.com" ? "183.63.123.9" : "172.16.0.1";
return Content($@"<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset=""utf-8""/>
<meta name=""viewport"" content=""width=device-width, initial-scale=1"">
</head>
<body>
<button>Login</button>
<p>Will redirect to {address}</p>
</body>
</html>", "text/html");
}
/// <inheritdoc/>
[Route("")]
[Produces("text/html")]
@ -42,8 +56,8 @@ public class SwaggerController : ControllerBase
</style>
</head>
<body>
<redoc spec-url=""/swagger/{Program.Version}/swagger.json""></redoc>
<script src=""/js/redoc.standalone.js""> </script>
<redoc spec-url=""swagger/{Program.Version}/swagger.json""></redoc>
<script src=""js/redoc.standalone.js""> </script>
</body>
</html>", "text/html");
}

View File

@ -13,12 +13,8 @@ namespace Blahblah.FlowerStory.Server.Controller;
[ApiController]
[Produces("application/json")]
[Route("api/user")]
public partial class UserApiController : BaseController
public partial class UserApiController(FlowerDatabase db, ILogger<UserApiController> logger) : BaseController<UserApiController>(db, logger)
{
/// <inheritdoc/>
public UserApiController(FlowerDatabase db, ILogger<UserApiController> logger) : base(db, logger)
{
}
/// <summary>
/// 用户登录
@ -60,7 +56,7 @@ public partial class UserApiController : BaseController
return NotFound();
}
// comput password hash with salt
// compute password hash with salt
string hash = HashPassword(login.Password, login.Id);
if (hash != user.Password)
{
@ -103,7 +99,7 @@ public partial class UserApiController : BaseController
user.ActiveDateUnixTime = token.ActiveDateUnixTime;
SaveDatabase();
Response.Headers.Add(AuthHeader, token.Id);
Response.Headers.Append(AuthHeader, token.Id);
return Ok(user);
}

View File

@ -6,16 +6,9 @@ namespace Blahblah.FlowerStory.Server.Data;
/// <summary>
/// 数据库管理类
/// </summary>
public class FlowerDatabase : DbContext
/// <param name="options">选项参数</param>
public class FlowerDatabase(DbContextOptions<FlowerDatabase> options) : DbContext(options)
{
/// <summary>
/// 构造数据库对象
/// </summary>
/// <param name="options">选项参数</param>
public FlowerDatabase(DbContextOptions<FlowerDatabase> options) : base(options)
{
//Database.Migrate();
}
/// <summary>
/// 用户集

View File

@ -1,6 +1,6 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:7.0
FROM mcr.microsoft.com/dotnet/aspnet:8.0
RUN apt-get update && apt-get install -y libfontconfig1
COPY . /app
WORKDIR /app

View File

@ -11,7 +11,7 @@ public class Program
/// <inheritdoc/>
public const string ProjectName = "Flower Story";
/// <inheritdoc/>
public const string Version = "1.2.809";
public const string Version = "1.23.1226";
/// <inheritdoc/>
public static string DataPath =>
#if DEBUG

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Blahblah.FlowerStory.Server</RootNamespace>
@ -19,15 +19,15 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9">
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SkiaSharp" Version="2.88.3" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.1" />
</ItemGroup>
<ItemGroup>

File diff suppressed because one or more lines are too long