github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/validation/files.go (about)

     1  package validation
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	"github.com/System-Glitch/goyave/v2/helper"
     8  	"github.com/System-Glitch/goyave/v2/helper/filesystem"
     9  )
    10  
    11  func validateFile(field string, value interface{}, parameters []string, form map[string]interface{}) bool {
    12  	_, ok := value.([]filesystem.File)
    13  	return ok
    14  }
    15  
    16  func validateMIME(field string, value interface{}, parameters []string, form map[string]interface{}) bool {
    17  	files, ok := value.([]filesystem.File)
    18  	if ok {
    19  		for _, file := range files {
    20  			mime := file.MIMEType
    21  			if i := strings.Index(mime, ";"); i != -1 { // Ignore MIME settings (example: "text/plain; charset=utf-8")
    22  				mime = mime[:i]
    23  			}
    24  			if !helper.ContainsStr(parameters, mime) {
    25  				return false
    26  			}
    27  		}
    28  		return true
    29  	}
    30  	return false
    31  }
    32  
    33  func validateImage(field string, value interface{}, parameters []string, form map[string]interface{}) bool {
    34  	params := []string{"image/jpeg", "image/png", "image/gif", "image/bmp", "image/svg+xml", "image/webp"}
    35  	return validateMIME(field, value, params, form)
    36  }
    37  
    38  func validateExtension(field string, value interface{}, parameters []string, form map[string]interface{}) bool {
    39  	files, ok := value.([]filesystem.File)
    40  	if ok {
    41  		for _, file := range files {
    42  			if i := strings.LastIndex(file.Header.Filename, "."); i != -1 {
    43  				if !helper.ContainsStr(parameters, file.Header.Filename[i+1:]) {
    44  					return false
    45  				}
    46  			} else {
    47  				return false
    48  			}
    49  		}
    50  		return true
    51  	}
    52  	return false
    53  }
    54  
    55  func validateCount(field string, value interface{}, parameters []string, form map[string]interface{}) bool {
    56  	files, ok := value.([]filesystem.File)
    57  	size, err := strconv.Atoi(parameters[0])
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  
    62  	if ok {
    63  		return len(files) == size
    64  	}
    65  
    66  	return false
    67  }
    68  
    69  func validateCountMin(field string, value interface{}, parameters []string, form map[string]interface{}) bool {
    70  	files, ok := value.([]filesystem.File)
    71  	size, err := strconv.Atoi(parameters[0])
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  
    76  	if ok {
    77  		return len(files) >= size
    78  	}
    79  
    80  	return false
    81  }
    82  
    83  func validateCountMax(field string, value interface{}, parameters []string, form map[string]interface{}) bool {
    84  	files, ok := value.([]filesystem.File)
    85  	size, err := strconv.Atoi(parameters[0])
    86  	if err != nil {
    87  		panic(err)
    88  	}
    89  
    90  	if ok {
    91  		return len(files) <= size
    92  	}
    93  
    94  	return false
    95  }
    96  
    97  func validateCountBetween(field string, value interface{}, parameters []string, form map[string]interface{}) bool {
    98  	files, ok := value.([]filesystem.File)
    99  	min, errMin := strconv.Atoi(parameters[0])
   100  	max, errMax := strconv.Atoi(parameters[1])
   101  	if errMin != nil {
   102  		panic(errMin)
   103  	}
   104  	if errMax != nil {
   105  		panic(errMax)
   106  	}
   107  
   108  	if ok {
   109  		length := len(files)
   110  		return length >= min && length <= max
   111  	}
   112  
   113  	return false
   114  }