forked from mark-kubacki/http.upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
70 lines (57 loc) · 1.84 KB
/
setup.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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package upload
import (
"context"
"net/http"
"path/filepath"
"unicode"
"gocloud.dev/blob"
_ "gocloud.dev/blob/fileblob" // Registers scheme "file://"
"golang.org/x/text/unicode/norm"
)
// Handler will deal with anything that manipulates files,
// but won't deliver a listing or serve them.
type Handler struct {
MaxFilesize int64
MaxTransactionSize int64
// The upload destination.
Bucket *blob.Bucket
// Uploaded files can be gotten back from here.
// If ≠ "" this will trigger sending headers such as "Location".
ApparentLocation string
// Enables MOVE, DELETE, and similar. Without this only POST and PUT will be recognized.
EnableWebdav bool
// Set this to reject any non-conforming filenames.
UnicodeForm *struct{ Use norm.Form }
// Limit the acceptable alphabet(s) for filenames by setting this value.
RestrictFilenamesTo []*unicode.RangeTable
// Append '_' and a randomized suffix of that length.
RandomizedSuffixLength uint32
// For methods that are not recognized.
Next http.Handler
// The path, to be stripped from the full URL and the target path swapped in.
Scope string
}
// NewHandler creates a new instance of this plugin's upload handler,
// meant to be used in Go's own http server.
//
// 'scope' is the prefix of the upload destination's URL.Path, like `/dir/to/upload/destination`.
//
// 'next' is optional and can be nil.
func NewHandler(scope string, targetDirectory string, next http.Handler) (*Handler, error) {
targetDirectory = filepath.Clean(targetDirectory) // Gets rid of any trailing slash.
bucket, err := blob.OpenBucket(
context.Background(),
"file://"+targetDirectory,
)
if err != nil {
return nil, err
}
h := Handler{
Bucket: bucket,
Next: next,
Scope: scope,
}
return &h, nil
}