using Microsoft.AspNetCore.Http; using System.Collections; using System.ComponentModel.DataAnnotations; namespace SharedKernel.Attributes { /* * 파일 크기 제한 */ [AttributeUsage(AttributeTargets.Property)] public class MaxFileSizeAttribute : ValidationAttribute { private readonly long _maxKBytes; public MaxFileSizeAttribute(long maxKBytes) { _maxKBytes = maxKBytes; } public override bool IsValid(object? value) { if (value is IEnumerable files) { foreach (var obj in files) { if (obj is IFormFile file && file.Length > _maxKBytes) { return false; } } } return true; } public override string FormatErrorMessage(string name) { var template = string.IsNullOrWhiteSpace(ErrorMessage) ? "{0}의 크기는 최대 {1:N0}KB까지 허용됩니다." : ErrorMessage; return string.Format(template, name, _maxKBytes); } } }