-
Notifications
You must be signed in to change notification settings - Fork 329
/
util.go
53 lines (44 loc) · 1023 Bytes
/
util.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
package objstore
import (
"fmt"
"github.com/pkg/errors"
"io"
"mime/multipart"
"net/url"
"github.com/cortezaproject/corteza/server/pkg/http"
)
func FromURL(fileURL string) (io.ReadCloser, error) {
if u, err := url.ParseRequestURI(fileURL); err != nil {
return nil, errors.WithStack(err)
} else if u.Scheme != "https" {
return nil, fmt.Errorf("Only HTTPS is supported for file uploads")
}
client, err := http.New(&http.Config{
Timeout: 10,
})
if err != nil {
return nil, errors.WithStack(err)
}
req, err := client.Get(fileURL)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp.Body, nil
}
func FromMultipartFile(file *multipart.FileHeader) (io.ReadCloser, error) {
reader, err := file.Open()
if err != nil {
return nil, errors.WithStack(err)
}
return reader, nil
}
func FromAny(file *multipart.FileHeader, url string) (io.ReadCloser, error) {
if file != nil {
return FromMultipartFile(file)
}
return FromURL(url)
}