diff --git a/src/GZCTF/Controllers/AssetsController.cs b/src/GZCTF/Controllers/AssetsController.cs index 501b1115c..1045f892e 100644 --- a/src/GZCTF/Controllers/AssetsController.cs +++ b/src/GZCTF/Controllers/AssetsController.cs @@ -75,16 +75,13 @@ public async Task Upload(List files, [FromQuery] strin try { List results = new(); - foreach (IFormFile file in files) + foreach (var file in files.Where(file => file.Length > 0)) { - if (file.Length > 0) - { - LocalFile res = await fileService.CreateOrUpdateFile(file, filename, token); - logger.SystemLog( - Program.StaticLocalizer[nameof(Resources.Program.Assets_UpdateFile), res.Hash[..8], filename ?? file.FileName, file.Length], - TaskStatus.Success, LogLevel.Debug); - results.Add(res); - } + LocalFile res = await fileService.CreateOrUpdateFile(file, filename, token); + logger.SystemLog( + Program.StaticLocalizer[nameof(Resources.Program.Assets_UpdateFile), res.Hash[..8], filename ?? file.FileName, file.Length], + TaskStatus.Success, LogLevel.Debug); + results.Add(res); } return Ok(results); diff --git a/src/GZCTF/Controllers/GameController.cs b/src/GZCTF/Controllers/GameController.cs index 48db9f7c4..65a2a10e1 100644 --- a/src/GZCTF/Controllers/GameController.cs +++ b/src/GZCTF/Controllers/GameController.cs @@ -635,7 +635,7 @@ public async Task SubmissionSheet([FromRoute] int id, [FromServic Submission[] submissions = await submissionRepository.GetSubmissions(game, count: 0, token: token); - MemoryStream stream = excelHelper.GetSubmissionExcel(submissions, localizer); + MemoryStream stream = excelHelper.GetSubmissionExcel(submissions); stream.Seek(0, SeekOrigin.Begin); return File(stream, diff --git a/src/GZCTF/Controllers/ProxyController.cs b/src/GZCTF/Controllers/ProxyController.cs index 50f281443..2f95e7667 100644 --- a/src/GZCTF/Controllers/ProxyController.cs +++ b/src/GZCTF/Controllers/ProxyController.cs @@ -211,7 +211,7 @@ async Task DoContainerProxy(Guid id, IPEndPoint client, IPEndPoin } } catch (TaskCanceledException) { } - finally { cts.Cancel(); } + finally { await cts.CancelAsync(); } }, ct); Task receiver = Task.Run(async () => @@ -233,7 +233,7 @@ async Task DoContainerProxy(Guid id, IPEndPoint client, IPEndPoin } } catch (TaskCanceledException) { } - finally { cts.Cancel(); } + finally { await cts.CancelAsync(); } }, ct); await Task.WhenAny(sender, receiver); diff --git a/src/GZCTF/Repositories/FileRepository.cs b/src/GZCTF/Repositories/FileRepository.cs index f96904574..7feedcb9a 100644 --- a/src/GZCTF/Repositories/FileRepository.cs +++ b/src/GZCTF/Repositories/FileRepository.cs @@ -13,7 +13,7 @@ public class FileRepository(AppDbContext context, ILogger logger public async Task CreateOrUpdateFile(IFormFile file, string? fileName = null, CancellationToken token = default) { - await using Stream tmp = GetStream(file.Length); + await using Stream tmp = GetTempStream(file.Length); logger.SystemLog(Program.StaticLocalizer[nameof(Resources.Program.FileRepository_CacheLocation), tmp.GetType()], TaskStatus.Pending, LogLevel.Trace); @@ -33,7 +33,7 @@ public async Task CreateOrUpdateFile(IFormFile file, string? fileName { await using Stream webpStream = new MemoryStream(); - await using (Stream tmp = GetStream(file.Length)) + await using (Stream tmp = GetTempStream(file.Length)) { await file.CopyToAsync(tmp, token); tmp.Position = 0; @@ -117,16 +117,19 @@ public Task GetFiles(int count, int skip, CancellationToken token = public async Task DeleteAttachment(Attachment? attachment, CancellationToken token = default) { - if (attachment is null) - return; - - if (attachment is { Type: FileType.Local, LocalFile: not null }) - await DeleteFile(attachment.LocalFile, token); + switch (attachment) + { + case null: + return; + case { Type: FileType.Local, LocalFile: not null }: + await DeleteFile(attachment.LocalFile, token); + break; + } Context.Remove(attachment); } - static Stream GetStream(long bufferSize) + static Stream GetTempStream(long bufferSize) { if (bufferSize <= 16 * 1024 * 1024) return new MemoryStream(); diff --git a/src/GZCTF/Utils/DigitalSignature.cs b/src/GZCTF/Utils/DigitalSignature.cs index be55aff3b..3a8a523c0 100644 --- a/src/GZCTF/Utils/DigitalSignature.cs +++ b/src/GZCTF/Utils/DigitalSignature.cs @@ -20,9 +20,7 @@ public static class DigitalSignature { public static string GenerateSignature(string data, AsymmetricKeyParameter privateKey, SignAlgorithm signAlgorithm) { - if (string.IsNullOrEmpty(data)) - throw new ArgumentNullException(nameof(data)); - + ArgumentException.ThrowIfNullOrEmpty(data); ArgumentNullException.ThrowIfNull(privateKey); var byteData = Encoding.UTF8.GetBytes(data); @@ -36,14 +34,9 @@ public static string GenerateSignature(string data, AsymmetricKeyParameter priva public static bool VerifySignature(string data, string sign, AsymmetricKeyParameter publicKey, SignAlgorithm signAlgorithm) { - if (string.IsNullOrEmpty(data)) - throw new ArgumentNullException(nameof(data)); - - if (string.IsNullOrEmpty(sign)) - throw new ArgumentNullException(nameof(sign)); - - if (publicKey == null) - throw new ArgumentNullException(nameof(publicKey)); + ArgumentException.ThrowIfNullOrEmpty(data); + ArgumentException.ThrowIfNullOrEmpty(sign); + ArgumentNullException.ThrowIfNull(publicKey); var signBytes = Base64.Decode(sign); var plainBytes = Encoding.UTF8.GetBytes(data); diff --git a/src/GZCTF/Utils/ExcelHelper.cs b/src/GZCTF/Utils/ExcelHelper.cs index 83bc68ed8..8316ca7c7 100644 --- a/src/GZCTF/Utils/ExcelHelper.cs +++ b/src/GZCTF/Utils/ExcelHelper.cs @@ -40,13 +40,13 @@ public MemoryStream GetScoreboardExcel(ScoreboardModel scoreboard, Game game) return stream; } - public MemoryStream GetSubmissionExcel(IEnumerable submissions, IStringLocalizer localizer) + public MemoryStream GetSubmissionExcel(IEnumerable submissions) { var workbook = new XSSFWorkbook(); ISheet? subSheet = workbook.CreateSheet(localizer[nameof(Resources.Program.Scoreboard_AllSubmissions)]); ICellStyle headerStyle = GetHeaderStyle(workbook); WriteSubmissionHeader(subSheet, headerStyle); - WriteSubmissionContent(subSheet, submissions, localizer); + WriteSubmissionContent(subSheet, submissions); var stream = new MemoryStream(); workbook.Write(stream, true); @@ -80,7 +80,7 @@ void WriteSubmissionHeader(ISheet sheet, ICellStyle style) } } - void WriteSubmissionContent(ISheet sheet, IEnumerable submissions, IStringLocalizer localizer) + void WriteSubmissionContent(ISheet sheet, IEnumerable submissions) { var rowIndex = 1; diff --git a/src/GZCTF/Utils/LogHelper.cs b/src/GZCTF/Utils/LogHelper.cs index bbaf0516e..63f08f21f 100644 --- a/src/GZCTF/Utils/LogHelper.cs +++ b/src/GZCTF/Utils/LogHelper.cs @@ -100,7 +100,7 @@ public static void Log(this ILogger logger, string msg, string uname, stri logger.Log(level ?? LogLevel.Information, msg); } } - + public static void UseRequestLogging(this WebApplication app) => app.UseSerilogRequestLogging(options => { @@ -136,20 +136,17 @@ public static ILogger GetLogger(IConfiguration configuration, IServiceProvider s .Filter.ByExcluding( Matching.WithProperty("RequestPath", v => "/healthz".Equals(v, StringComparison.OrdinalIgnoreCase) || - v.StartsWith("/assets", StringComparison.OrdinalIgnoreCase) - ) - ) - .MinimumLevel.Debug() + v.StartsWith("/assets", StringComparison.OrdinalIgnoreCase))) .Filter.ByExcluding(logEvent => logEvent.Exception != null && logEvent.Exception.GetType() == typeof(OperationCanceledException)) + .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("AspNetCoreRateLimit", LogEventLevel.Warning) .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning) .WriteTo.Async(t => t.Console( new ExpressionTemplate(LogTemplate, theme: TemplateTheme.Literate), - LogEventLevel.Debug - )) + LogEventLevel.Debug)) .WriteTo.Async(t => t.File( path: $"{FilePath.Logs}/log_.log", formatter: new ExpressionTemplate(LogTemplate),