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