Esempio n. 1
0
func SortFieldAndDirection(c *gin.Context, fields []string) (string, string) {
	sortDir := c.DefaultQuery("sortDir", "DESC")
	sortField := c.DefaultQuery("sortField", "createdAt")

	if !slice.StringInSlice(sortField, fields) {
		sortField = "createdAt"
	}

	if !slice.StringInSlice(sortDir, []string{"desc", "asc", "DESC", "ASC"}) {
		sortDir = "DESC"
	}

	return sortDir, sortField
}
Esempio n. 2
0
func Whitelist(from interface{}, fields []string) map[string]interface{} {
	out := make(map[string]interface{})

	var v reflect.Value
	var t reflect.Type
	v = reflect.ValueOf(from)
	t = reflect.TypeOf(from)

	if reflect.Ptr == t.Kind() {
		v = v.Elem()
		t = t.Elem()
	}

	for i := 0; i < v.NumField(); i++ {
		valueOfField := v.Field(i)
		typeOfField := t.Field(i)

		val := valueOfField.Interface()
		name := strings.Split(typeOfField.Tag.Get("json"), ",")[0]

		if len(fields) > 0 && fields[0] != "" {
			if slice.StringInSlice(name, fields) {
				out[name] = val
			}
		} else {
			out[name] = val
		}
	}
	return out
}
Esempio n. 3
0
func Locale(c *gin.Context, locales []string, defaultLocale string) string {
	locale := c.DefaultQuery("locale", defaultLocale)

	if !slice.StringInSlice(locale, locales) {
		locale = defaultLocale
	}

	return locale
}
Esempio n. 4
0
func Upload(tx *sql.Tx, fileType string, tmpPath string, uploadData map[string]map[string]interface{}, w http.ResponseWriter, r *http.Request) (*model.File, error) {
	f := &model.File{}

	if len(fileType) == 0 {
		return nil, &web.ValidationError{
			Fields: map[string]string{"error": "no_type"},
		}
	}

	if _, ok := uploadData[fileType]; !ok {
		return nil, &web.ValidationError{
			Fields: map[string]string{"error": "wrong_type"},
		}
	}

	t := uploadData[fileType]
	mime := t["mime"].([]string)

	MaxFileSize := t["size"].(int)

	if r.ContentLength > int64(MaxFileSize) {
		return nil, &web.ValidationError{
			Fields: map[string]string{"error": "file_too_large_cl"},
		}
	}

	r.Body = http.MaxBytesReader(w, r.Body, int64(MaxFileSize))
	err := r.ParseMultipartForm(10000000)
	if err != nil {
		return nil, &web.ValidationError{
			Fields: map[string]string{"error": "file_too_large"},
		}
	}

	file, header, err := r.FormFile("file")
	if file != nil {
		defer file.Close()
	}

	if err != nil {
		return nil, &web.ValidationError{
			Fields: map[string]string{"error": "file_not_provided"},
		}
	}

	f.Tmp = true
	f.Type = fileType
	f.Name = header.Filename
	f.UniqueID = utils.NewUUID()
	f.Mime = header.Header.Get("Content-Type")

	if !slice.StringInSlice(f.Mime, mime) {
		return nil, &web.ValidationError{
			Fields: map[string]string{"error": "mime_type_not_allowed"},
		}
	}

	// create directories
	err = os.MkdirAll(tmpPath, 0777)
	if err != nil {
		return nil, err
	}

	err = web.CreateFile(file, tmpPath, f.UniqueID+"-"+f.Name)
	if err != nil {
		return nil, err
	}

	err = Create(tx, f)
	if err != nil {
		return nil, err
	}

	err = DeleteOlderThan(tx, 5)
	if err != nil {
		Log.Debug("%+v", err)
	}

	return f, nil
}