-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcreate.go
87 lines (77 loc) · 2.4 KB
/
create.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package storage
import (
"errors"
"github.com/viant/afs/option"
"github.com/viant/endly/model/location"
"github.com/viant/afs/file"
"github.com/viant/afs/storage"
"github.com/viant/endly"
"io"
"os"
"strings"
)
// CreateRequest represents a resources Upload request, it takes context state key to Upload to target destination.
type CreateRequest struct {
SourceKey string `required:"true" description:"state key with asset content"`
Region string `description:"cloud storage region"`
Mode int `description:"os.FileMode"`
IsDir bool `description:"is directory flag"`
Dest *location.Resource `required:"true" description:"destination asset or directory"` //target URL with credentials
}
// CreateResponse represents a Upload response
type CreateResponse struct {
Size int
URL string
}
// Create creates a resource
func (s *service) Create(context *endly.Context, request *CreateRequest) (*CreateResponse, error) {
var response = &CreateResponse{}
err := s.create(context, request, response)
return response, err
}
func (s *service) create(context *endly.Context, request *CreateRequest, response *CreateResponse) error {
options := gerReaderOption(request, context, response)
if request.Region != "" {
options = append(options, option.NewRegion(request.Region))
}
dest, storageOpts, err := GetResourceWithOptions(context, request.Dest, options...)
if err != nil {
return err
}
fs, err := StorageService(context, dest)
if err != nil {
return err
}
response.URL = dest.URL
return fs.Create(context.Background(), dest.URL, os.FileMode(request.Mode), request.IsDir, storageOpts...)
}
func gerReaderOption(request *CreateRequest, context *endly.Context, response *CreateResponse) []storage.Option {
var options = make([]storage.Option, 0)
if !request.IsDir {
var state = context.State()
if state.Has(request.SourceKey) {
data := state.GetString(request.SourceKey)
options = append(options, io.Reader(strings.NewReader(data)))
response.Size = len(data)
}
}
return options
}
// Init initialises Upload request
func (r *CreateRequest) Init() error {
if r.Mode == 0 {
if r.IsDir {
r.Mode = int(file.DefaultDirOsMode)
} else {
r.Mode = int(file.DefaultFileOsMode)
}
}
return nil
}
// Validate checks if request is valid
func (r *CreateRequest) Validate() error {
if r.Dest == nil {
return errors.New("dest was empty")
}
return nil
}