backend + web: Improve validation and email retry handling
This commit is contained in:
1 parent
e9782a1413
commit
722d318898
13 files changed
+195
-44
No files matched your search
@@ -13,11 +13,11 @@ internal sealed class FileAttachmentStore(ILogger<FileAttachmentStore> logger) :
|
||||
public async Task<StoredAttachment> SaveAsync(AttachmentUpload upload, CancellationToken ct)
|
||||
{
|
||||
if (upload.SizeBytes is <= 0 or > AttachmentLimits.MaxFileBytes)
|
||||
throw new BadRequestException("Příloha musí mít velikost od 1 B do 10 MB.");
|
||||
throw new FieldValidationException("files", "Příloha musí mít velikost od 1 B do 10 MB.");
|
||||
|
||||
var name = Path.GetFileName(upload.FileName.Replace('\\', '/')).Trim();
|
||||
if (name.Length is 0 or > 255 || name.Any(char.IsControl))
|
||||
throw new BadRequestException("Neplatný název přílohy.");
|
||||
throw new FieldValidationException("files", "Neplatný název přílohy.");
|
||||
|
||||
var extension = Path.GetExtension(name).ToLowerInvariant();
|
||||
var contentType = extension switch
|
||||
@@ -27,7 +27,7 @@ internal sealed class FileAttachmentStore(ILogger<FileAttachmentStore> logger) :
|
||||
".heic" => "image/heic",
|
||||
".pdf" => "application/pdf",
|
||||
".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
_ => throw new BadRequestException("Tento typ přílohy není povolen."),
|
||||
_ => throw new FieldValidationException("files", "Tento typ přílohy není povolen."),
|
||||
};
|
||||
|
||||
Directory.CreateDirectory(UploadDirectory);
|
||||
@@ -49,16 +49,16 @@ internal sealed class FileAttachmentStore(ILogger<FileAttachmentStore> logger) :
|
||||
{
|
||||
written += read;
|
||||
if (written > upload.SizeBytes)
|
||||
throw new BadRequestException("Velikost přílohy neodpovídá požadavku.");
|
||||
throw new FieldValidationException("files", "Velikost přílohy neodpovídá požadavku.");
|
||||
await output.WriteAsync(buffer.AsMemory(0, read), ct);
|
||||
}
|
||||
|
||||
if (written != upload.SizeBytes)
|
||||
throw new BadRequestException("Velikost přílohy neodpovídá požadavku.");
|
||||
throw new FieldValidationException("files", "Velikost přílohy neodpovídá požadavku.");
|
||||
|
||||
output.Position = 0;
|
||||
if (!await HasExpectedFormatAsync(output, extension, ct))
|
||||
throw new BadRequestException("Obsah přílohy neodpovídá její příponě.");
|
||||
throw new FieldValidationException("files", "Obsah přílohy neodpovídá její příponě.");
|
||||
|
||||
return new StoredAttachment(name, key, contentType, written);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user