Files
opencloud/webdav/pkg/dav/thumbnails/thumbnail.go
T
David Christofas ff618c076a fix jpeg thumbnails
2021-03-10 10:06:19 +01:00

75 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
Extension string
Etag string
Width int
Height int
Authorization string
Username 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,
Extension: filepath.Ext(path),
Etag: etag,
Width: width,
Height: height,
Authorization: authorization,
Username: chi.URLParam(r, "user"),
}
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]
}