Files
opencloud/webdav/pkg/dav/thumbnails/thumbnail.go
A.Unger 72a987f8e3 Add 'webdav/' from commit '0d5eded8d0068b8df984b3d4c44a66f10c97cf77'
git-subtree-dir: webdav
git-subtree-mainline: 7ffaa859b3
git-subtree-split: 0d5eded8d0
2020-09-18 12:56:12 +02:00

73 lines
1.6 KiB
Go

package thumbnail
import (
"fmt"
"net/http"
"path/filepath"
"strconv"
"strings"
"github.com/go-chi/chi"
)
const (
// DefaultWidth defines the default width of a thumbnail
DefaultWidth = 32
// DefaultHeight defines the default height of a thumbnail
DefaultHeight = 32
)
// Request combines all parameters provided when requesting a thumbnail
type Request struct {
Filepath string
Filetype string
Etag string
Width int
Height int
Authorization string
}
// NewRequest extracts all required parameters from a http request.
func NewRequest(r *http.Request) (Request, error) {
path := extractFilePath(r)
query := r.URL.Query()
width, err := strconv.Atoi(query.Get("x"))
if err != nil {
width = DefaultWidth
}
height, err := strconv.Atoi(query.Get("y"))
if err != nil {
height = DefaultHeight
}
etag := query.Get("c")
if strings.TrimSpace(etag) == "" {
return Request{}, fmt.Errorf("c (etag) is missing in query")
}
authorization := r.Header.Get("Authorization")
tr := Request{
Filepath: path,
Filetype: strings.Replace(filepath.Ext(path), ".", "", 1),
Etag: etag,
Width: width,
Height: height,
Authorization: authorization,
}
return tr, nil
}
// the url looks as followed
//
// /remote.php/dav/files/<user>/<filepath>
//
// User and filepath are dynamic and filepath can contain slashes
// So using the URLParam function is not possible.
func extractFilePath(r *http.Request) string {
user := chi.URLParam(r, "user")
parts := strings.SplitN(r.URL.Path, user, 2)
return parts[1]
}