2019-12-05 19:21:35 +01:00
|
|
|
package form
|
2018-08-15 09:59:51 +02:00
|
|
|
|
|
|
|
import (
|
2019-05-15 21:51:00 +02:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-08-15 09:59:51 +02:00
|
|
|
"time"
|
2019-05-15 21:51:00 +02:00
|
|
|
"unicode"
|
2019-05-15 23:07:25 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2019-05-16 02:22:38 +02:00
|
|
|
|
|
|
|
"github.com/araddon/dateparse"
|
2018-08-15 09:59:51 +02:00
|
|
|
)
|
|
|
|
|
2018-11-06 19:02:03 +01:00
|
|
|
// Query parameters for GET /api/v1/photos
|
2019-12-05 19:21:35 +01:00
|
|
|
type PhotoSearch struct {
|
2019-05-16 02:22:38 +02:00
|
|
|
Query string `form:"q"`
|
|
|
|
|
2019-12-20 23:05:44 +01:00
|
|
|
Title string `form:"title"`
|
|
|
|
Description string `form:"description"`
|
|
|
|
Notes string `form:"notes"`
|
|
|
|
Artist string `form:"artist"`
|
|
|
|
Hash string `form:"hash"`
|
|
|
|
Duplicate bool `form:"duplicate"`
|
2020-01-09 02:09:54 +01:00
|
|
|
Archived bool `form:"archived"`
|
2019-12-20 23:05:44 +01:00
|
|
|
Lat float64 `form:"lat"`
|
|
|
|
Lng float64 `form:"lng"`
|
|
|
|
Dist uint `form:"dist"`
|
|
|
|
Fmin float64 `form:"fmin"`
|
|
|
|
Fmax float64 `form:"fmax"`
|
|
|
|
Chroma uint `form:"chroma"`
|
|
|
|
Mono bool `form:"mono"`
|
|
|
|
Portrait bool `form:"portrait"`
|
|
|
|
Location bool `form:"location"`
|
|
|
|
Album string `form:"album"`
|
|
|
|
Label string `form:"label"`
|
|
|
|
Country string `form:"country"`
|
2019-12-28 23:06:44 +01:00
|
|
|
Year uint `form:"year"`
|
|
|
|
Month uint `form:"month"`
|
2019-05-16 02:22:38 +02:00
|
|
|
Color string `form:"color"`
|
|
|
|
Camera int `form:"camera"`
|
2019-12-28 23:06:44 +01:00
|
|
|
Lens int `form:"lens"`
|
2019-05-16 02:22:38 +02:00
|
|
|
Before time.Time `form:"before" time_format:"2006-01-02"`
|
|
|
|
After time.Time `form:"after" time_format:"2006-01-02"`
|
|
|
|
Favorites bool `form:"favorites"`
|
2019-12-11 14:10:20 +01:00
|
|
|
Public bool `form:"public"`
|
|
|
|
Story bool `form:"story"`
|
|
|
|
Safe bool `form:"safe"`
|
2020-01-06 05:45:03 +01:00
|
|
|
Nsfw bool `form:"nsfw"`
|
2019-05-16 02:22:38 +02:00
|
|
|
|
|
|
|
Count int `form:"count" binding:"required"`
|
|
|
|
Offset int `form:"offset"`
|
|
|
|
Order string `form:"order"`
|
2019-05-15 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 19:21:35 +01:00
|
|
|
func (f *PhotoSearch) ParseQueryString() (result error) {
|
2019-12-08 22:45:45 +01:00
|
|
|
var key, value []rune
|
2019-05-15 21:51:00 +02:00
|
|
|
var escaped, isKeyValue bool
|
|
|
|
|
2020-01-05 14:09:17 +01:00
|
|
|
q := f.Query
|
2019-05-15 21:51:00 +02:00
|
|
|
|
|
|
|
f.Query = ""
|
|
|
|
|
|
|
|
formValues := reflect.ValueOf(f).Elem()
|
|
|
|
|
2020-01-05 14:09:17 +01:00
|
|
|
q = strings.TrimSpace(q) + "\n"
|
2019-05-15 21:51:00 +02:00
|
|
|
|
2020-01-05 14:09:17 +01:00
|
|
|
for _, char := range q {
|
2019-05-15 21:51:00 +02:00
|
|
|
if unicode.IsSpace(char) && !escaped {
|
|
|
|
if isKeyValue {
|
2019-12-08 22:45:45 +01:00
|
|
|
fieldName := strings.Title(string(key))
|
2019-05-15 21:51:00 +02:00
|
|
|
field := formValues.FieldByName(fieldName)
|
2019-12-08 22:45:45 +01:00
|
|
|
stringValue := string(value)
|
2019-05-15 21:51:00 +02:00
|
|
|
|
|
|
|
if field.CanSet() {
|
|
|
|
switch field.Interface().(type) {
|
2019-05-15 23:07:25 +02:00
|
|
|
case time.Time:
|
2019-05-16 02:22:38 +02:00
|
|
|
if timeValue, err := dateparse.ParseAny(stringValue); err != nil {
|
2019-05-15 23:07:25 +02:00
|
|
|
result = err
|
2019-05-15 21:51:00 +02:00
|
|
|
} else {
|
2019-05-15 23:07:25 +02:00
|
|
|
field.Set(reflect.ValueOf(timeValue))
|
|
|
|
}
|
2019-05-16 02:22:38 +02:00
|
|
|
case float64:
|
|
|
|
if floatValue, err := strconv.ParseFloat(stringValue, 64); err != nil {
|
|
|
|
result = err
|
|
|
|
} else {
|
|
|
|
field.SetFloat(floatValue)
|
|
|
|
}
|
2019-05-15 23:07:25 +02:00
|
|
|
case int, int64:
|
2019-05-16 02:22:38 +02:00
|
|
|
if intValue, err := strconv.Atoi(stringValue); err != nil {
|
2019-05-15 21:51:00 +02:00
|
|
|
result = err
|
2019-05-15 23:07:25 +02:00
|
|
|
} else {
|
2019-05-16 02:22:38 +02:00
|
|
|
field.SetInt(int64(intValue))
|
2019-05-15 21:51:00 +02:00
|
|
|
}
|
|
|
|
case uint, uint64:
|
2019-05-16 02:22:38 +02:00
|
|
|
if intValue, err := strconv.Atoi(stringValue); err != nil {
|
2019-05-15 21:51:00 +02:00
|
|
|
result = err
|
2019-05-15 23:07:25 +02:00
|
|
|
} else {
|
2019-05-16 02:22:38 +02:00
|
|
|
field.SetUint(uint64(intValue))
|
2019-05-15 21:51:00 +02:00
|
|
|
}
|
|
|
|
case string:
|
2019-05-15 23:07:25 +02:00
|
|
|
field.SetString(stringValue)
|
2019-05-15 21:51:00 +02:00
|
|
|
case bool:
|
2019-05-15 23:07:25 +02:00
|
|
|
if stringValue == "1" || stringValue == "true" || stringValue == "yes" {
|
2019-05-15 21:51:00 +02:00
|
|
|
field.SetBool(true)
|
2019-05-15 23:07:25 +02:00
|
|
|
} else if stringValue == "0" || stringValue == "false" || stringValue == "no" {
|
2019-05-15 21:51:00 +02:00
|
|
|
field.SetBool(false)
|
|
|
|
} else {
|
|
|
|
result = fmt.Errorf("not a bool value: %s", fieldName)
|
|
|
|
}
|
|
|
|
default:
|
2019-05-16 08:41:16 +02:00
|
|
|
result = fmt.Errorf("unsupported type: %s", fieldName)
|
2019-05-15 21:51:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-16 08:41:16 +02:00
|
|
|
result = fmt.Errorf("unknown filter: %s", fieldName)
|
2019-05-15 21:51:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-08 22:45:45 +01:00
|
|
|
f.Query = string(key)
|
2019-05-15 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
escaped = false
|
|
|
|
isKeyValue = false
|
|
|
|
key = key[:0]
|
|
|
|
value = value[:0]
|
|
|
|
} else if char == ':' {
|
|
|
|
isKeyValue = true
|
|
|
|
} else if char == '"' {
|
|
|
|
escaped = !escaped
|
|
|
|
} else if isKeyValue {
|
2019-12-08 22:45:45 +01:00
|
|
|
value = append(value, unicode.ToLower(char))
|
2019-05-15 21:51:00 +02:00
|
|
|
} else {
|
2019-12-08 22:45:45 +01:00
|
|
|
key = append(key, unicode.ToLower(char))
|
2019-05-15 21:51:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 23:07:25 +02:00
|
|
|
if result != nil {
|
|
|
|
log.Errorf("error while parsing search form: %s", result)
|
|
|
|
}
|
|
|
|
|
2019-05-15 21:51:00 +02:00
|
|
|
return result
|
2018-08-15 09:59:51 +02:00
|
|
|
}
|