@@ -105,9 +105,9 @@ type FileValidator struct {
|
||||
Categories []string
|
||||
}
|
||||
|
||||
type Opt func(v *FileValidator) *FileValidator
|
||||
type Option func(v *FileValidator) *FileValidator
|
||||
|
||||
func WithCategories(categories ...string) Opt {
|
||||
func WithCategories(categories ...string) Option {
|
||||
return func(v *FileValidator) *FileValidator {
|
||||
v.Categories = categories
|
||||
|
||||
@@ -132,7 +132,7 @@ func WithCategories(categories ...string) Opt {
|
||||
}
|
||||
}
|
||||
|
||||
func WithMaxFileSize(maxFileSize int64) Opt {
|
||||
func WithMaxFileSize(maxFileSize int64) Option {
|
||||
return func(v *FileValidator) *FileValidator {
|
||||
v.MaxFileSize = maxFileSize
|
||||
|
||||
@@ -140,7 +140,7 @@ func WithMaxFileSize(maxFileSize int64) Opt {
|
||||
}
|
||||
}
|
||||
|
||||
func NewValidator(opts ...Opt) *FileValidator {
|
||||
func NewValidator(opts ...Option) *FileValidator {
|
||||
v := &FileValidator{
|
||||
MaxFileSize: DefaultMaxFileSize,
|
||||
AllowedMimeTypes: make(map[string]bool),
|
||||
@@ -151,18 +151,6 @@ func NewValidator(opts ...Opt) *FileValidator {
|
||||
v = opt(v)
|
||||
}
|
||||
|
||||
if len(v.Categories) == 0 {
|
||||
for _, fileType := range FileTypes {
|
||||
v.AllowedMimeTypes[fileType.MimeType] = true
|
||||
for _, ext := range fileType.Extensions {
|
||||
if v.AllowedExtensions[ext] == nil {
|
||||
v.AllowedExtensions[ext] = []string{}
|
||||
}
|
||||
v.AllowedExtensions[ext] = append(v.AllowedExtensions[ext], fileType.MimeType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
|
||||
@@ -31,9 +31,9 @@ func TestNewValidator(t *testing.T) {
|
||||
{
|
||||
name: "No categories (all types)",
|
||||
categories: []string{},
|
||||
expectedMimes: []string{"application/pdf", "image/jpeg", "text/plain", "video/mp4"},
|
||||
expectedExts: []string{".pdf", ".jpg", ".txt", ".mp4"},
|
||||
unexpectedMime: "application/octet-stream",
|
||||
expectedMimes: []string{},
|
||||
expectedExts: []string{},
|
||||
unexpectedMime: "application/pdf",
|
||||
},
|
||||
{
|
||||
name: "Only documents",
|
||||
@@ -131,7 +131,7 @@ func TestValidate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "File too large",
|
||||
validator: NewValidator().WithMaxFileSize(1024 * 1024), // 1MB max
|
||||
validator: NewValidator(WithCategories(CategoryDocument)).WithMaxFileSize(1024 * 1024), // 1MB max
|
||||
filename: "test.pdf",
|
||||
contentType: "application/pdf",
|
||||
fileSize: 2 * 1024 * 1024, // 2MB
|
||||
@@ -149,7 +149,7 @@ func TestValidate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Missing file extension",
|
||||
validator: NewValidator(),
|
||||
validator: NewValidator(WithCategories(CategoryDocument)),
|
||||
filename: "testfile",
|
||||
contentType: "application/pdf",
|
||||
fileSize: 1024,
|
||||
@@ -167,7 +167,7 @@ func TestValidate(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Content type doesn't match extension",
|
||||
validator: NewValidator(),
|
||||
validator: NewValidator(WithCategories(CategoryImage, CategoryDocument)),
|
||||
filename: "test.pdf",
|
||||
contentType: "image/jpeg",
|
||||
fileSize: 1024,
|
||||
@@ -225,7 +225,7 @@ func TestValidateEdgeCases(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "Zero file size",
|
||||
validator: NewValidator(),
|
||||
validator: NewValidator(WithCategories(CategoryText)),
|
||||
filename: "empty.txt",
|
||||
contentType: "text/plain",
|
||||
fileSize: 0,
|
||||
@@ -233,7 +233,7 @@ func TestValidateEdgeCases(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Exact max file size",
|
||||
validator: NewValidator().WithMaxFileSize(1024),
|
||||
validator: NewValidator(WithCategories(CategoryText)).WithMaxFileSize(1024),
|
||||
filename: "exact.txt",
|
||||
contentType: "text/plain",
|
||||
fileSize: 1024,
|
||||
@@ -241,7 +241,7 @@ func TestValidateEdgeCases(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "File with uppercase extension",
|
||||
validator: NewValidator(),
|
||||
validator: NewValidator(WithCategories(CategoryDocument)),
|
||||
filename: "test.PDF",
|
||||
contentType: "application/pdf",
|
||||
fileSize: 1024,
|
||||
@@ -258,7 +258,7 @@ func TestValidateEdgeCases(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Empty filename",
|
||||
validator: NewValidator(),
|
||||
validator: NewValidator(WithCategories(CategoryText)),
|
||||
filename: "",
|
||||
contentType: "text/plain",
|
||||
fileSize: 1024,
|
||||
@@ -267,7 +267,7 @@ func TestValidateEdgeCases(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Empty content type",
|
||||
validator: NewValidator(),
|
||||
validator: NewValidator(WithCategories(CategoryText)),
|
||||
filename: "test.txt",
|
||||
contentType: "",
|
||||
fileSize: 1024,
|
||||
@@ -407,7 +407,17 @@ func TestExtensionsWithMultipleMimeTypes(t *testing.T) {
|
||||
// Find extensions that have multiple MIME types
|
||||
for ext, mimeTypes := range extToMimes {
|
||||
if len(mimeTypes) > 1 {
|
||||
v := NewValidator()
|
||||
v := NewValidator(
|
||||
WithCategories(
|
||||
CategoryData,
|
||||
CategoryDocument,
|
||||
CategoryImage,
|
||||
CategoryPresentation,
|
||||
CategorySpreadsheet,
|
||||
CategoryText,
|
||||
CategoryVideo,
|
||||
),
|
||||
)
|
||||
|
||||
// All MIME types for this extension should be valid
|
||||
for _, mimeType := range mimeTypes {
|
||||
@@ -423,7 +433,7 @@ func TestExtensionsWithMultipleMimeTypes(t *testing.T) {
|
||||
|
||||
// BenchmarkValidate benchmarks the Validate function
|
||||
func BenchmarkValidate(b *testing.B) {
|
||||
v := NewValidator()
|
||||
v := NewValidator(WithCategories(CategoryDocument))
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
||||
Reference in New Issue
Block a user