web: formulář nového hlášení závady s přílohami
Název, kategorie, budova, upřesnění místa, popis a až 5 příloh po 10 MB (JPG, PNG, HEIC, PDF, DOCX). Přílohy jdou do API v jednom multipart požadavku spolu s hlášením. Typ, velikost a počet souborů kontroluje už web, obsah souboru ověřuje API. Tlačítko Nahlásit závadu v menu a v seznamu, delší časový limit volání API kvůli nahrávání. Unit testy příloh, místa v seznamu a odeslání formuláře. Refs #23
This commit is contained in:
1 parent
2fe939ba5c
commit
946ff5ce12
9 files changed
+348
-12
No files matched your search
@@ -0,0 +1,76 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Helpdesk.Web.Api;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
|
||||
namespace Helpdesk.Web.Models;
|
||||
|
||||
/// <summary>Limity příloh – stejné jako v API (FR-11).</summary>
|
||||
public static class AttachmentRules
|
||||
{
|
||||
public const int MaxFiles = 5;
|
||||
public const long MaxFileBytes = 10 * 1024 * 1024;
|
||||
/// <summary>Celý požadavek: všechny soubory plus pole formuláře.</summary>
|
||||
public const long MaxRequestBytes = MaxFiles * MaxFileBytes + 1024 * 1024;
|
||||
|
||||
public static readonly IReadOnlyList<string> Extensions = [".jpg", ".jpeg", ".png", ".heic", ".pdf", ".docx"];
|
||||
|
||||
/// <summary>Atribut <c>accept</c> pro výběr souborů.</summary>
|
||||
public static string Accept => string.Join(",", Extensions);
|
||||
|
||||
/// <summary>
|
||||
/// Rychlá kontrola na webu, aby uživatel nečekal na nahrání 50 MB jen kvůli špatnému typu.
|
||||
/// Skutečný obsah souboru (signaturu) ověřuje API.
|
||||
/// </summary>
|
||||
public static IEnumerable<string> Validate(IReadOnlyCollection<IFormFile> files)
|
||||
{
|
||||
if (files.Count > MaxFiles)
|
||||
yield return $"Najednou můžete přiložit nejvýše {MaxFiles} souborů.";
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
if (!Extensions.Contains(Path.GetExtension(file.FileName).ToLowerInvariant()))
|
||||
yield return $"Soubor {file.FileName} nemá povolený typ. Povolené jsou JPG, PNG, HEIC, PDF a DOCX.";
|
||||
else if (file.Length > MaxFileBytes)
|
||||
yield return $"Soubor {file.FileName} je větší než 10 MB.";
|
||||
else if (file.Length == 0)
|
||||
yield return $"Soubor {file.FileName} je prázdný.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NewIssueViewModel
|
||||
{
|
||||
[Required(ErrorMessage = "Zadejte název.")]
|
||||
[StringLength(200, ErrorMessage = "Název může mít nejvýše 200 znaků.")]
|
||||
[Display(Name = "Co se stalo")]
|
||||
public string Title { get; set; } = "";
|
||||
|
||||
[Required(ErrorMessage = "Popište závadu.")]
|
||||
[StringLength(4000, ErrorMessage = "Popis může mít nejvýše 4000 znaků.")]
|
||||
[Display(Name = "Popis")]
|
||||
public string Description { get; set; } = "";
|
||||
|
||||
[Required(ErrorMessage = "Vyberte kategorii.")]
|
||||
[Display(Name = "Kategorie")]
|
||||
public int? CategoryId { get; set; }
|
||||
|
||||
[Display(Name = "Budova")]
|
||||
public int? BuildingId { get; set; }
|
||||
|
||||
[StringLength(255, ErrorMessage = "Upřesnění místa může mít nejvýše 255 znaků.")]
|
||||
[Display(Name = "Upřesnění místa")]
|
||||
public string? LocationDetail { get; set; }
|
||||
|
||||
[Display(Name = "Fotografie nebo soubory")]
|
||||
public List<IFormFile> Files { get; set; } = [];
|
||||
|
||||
// Nabídky pro výběr – plní je kontroler z číselníků API.
|
||||
public IReadOnlyList<SelectListItem> Categories { get; set; } = [];
|
||||
public IReadOnlyList<SelectListItem> Buildings { get; set; } = [];
|
||||
|
||||
public void SetCatalogs(IEnumerable<CategoryDto> categories, IEnumerable<BuildingDto> buildings)
|
||||
{
|
||||
Categories = categories.Select(c => new SelectListItem(c.Name, c.Id.ToString())).ToList();
|
||||
Buildings = buildings.Select(b => new SelectListItem(b.Name, b.Id.ToString())).ToList();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user