2015-04-16 17:26:33 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-07-22 15:07:18 -04:00
|
|
|
"fmt"
|
2015-04-16 17:26:33 -04:00
|
|
|
"net/http"
|
2015-07-28 22:55:14 -04:00
|
|
|
"path/filepath"
|
2015-04-16 17:26:33 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func boolValue(r *http.Request, k string) bool {
|
|
|
|
s := strings.ToLower(strings.TrimSpace(r.FormValue(k)))
|
|
|
|
return !(s == "" || s == "0" || s == "no" || s == "false" || s == "none")
|
|
|
|
}
|
|
|
|
|
2015-05-23 10:09:39 -04:00
|
|
|
// boolValueOrDefault returns the default bool passed if the query param is
|
|
|
|
// missing, otherwise it's just a proxy to boolValue above
|
|
|
|
func boolValueOrDefault(r *http.Request, k string, d bool) bool {
|
|
|
|
if _, ok := r.Form[k]; !ok {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
return boolValue(r, k)
|
|
|
|
}
|
|
|
|
|
2015-04-23 03:40:23 -04:00
|
|
|
func int64ValueOrZero(r *http.Request, k string) int64 {
|
2015-04-16 17:26:33 -04:00
|
|
|
val, err := strconv.ParseInt(r.FormValue(k), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
2015-07-22 15:07:18 -04:00
|
|
|
|
|
|
|
type archiveOptions struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func archiveFormValues(r *http.Request, vars map[string]string) (archiveOptions, error) {
|
|
|
|
if vars == nil {
|
|
|
|
return archiveOptions{}, fmt.Errorf("Missing parameter")
|
|
|
|
}
|
|
|
|
if err := parseForm(r); err != nil {
|
|
|
|
return archiveOptions{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := vars["name"]
|
2015-07-28 22:55:14 -04:00
|
|
|
path := filepath.FromSlash(r.Form.Get("path"))
|
2015-07-22 15:07:18 -04:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case name == "":
|
|
|
|
return archiveOptions{}, fmt.Errorf("bad parameter: 'name' cannot be empty")
|
|
|
|
case path == "":
|
|
|
|
return archiveOptions{}, fmt.Errorf("bad parameter: 'path' cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return archiveOptions{name, path}, nil
|
|
|
|
}
|