This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
thumbnail.go
72 lines (62 loc) · 1.57 KB
/
thumbnail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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]
}