/
upload.go
53 lines (49 loc) · 1.29 KB
/
upload.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 file
import (
"context"
"github.com/pkg/errors"
"github.com/viant/afs/object"
"github.com/viant/afs/option"
"github.com/viant/afs/storage"
"io"
"os"
"path"
)
//EnsureParentPathExists create parent path if needed
func EnsureParentPathExists(filename string, fileMode os.FileMode) error {
stat, err := os.Stat(filename)
if err == nil {
if stat.Mode() != fileMode {
_ = os.Chmod(filename, fileMode)
}
return err
}
parent, _ := path.Split(filename)
return os.MkdirAll(parent, fileMode)
}
//Upload writes reader content to supplied URL path.
func Upload(ctx context.Context, URL string, mode os.FileMode, reader io.Reader, options ...storage.Option) error {
filePath := Path(URL)
err := EnsureParentPathExists(filePath, DefaultDirOsMode)
if err != nil {
return errors.Wrap(err, "unable to create parent for "+filePath)
}
link := &object.Link{}
option.Assign(options, &link)
if link.Linkname != "" {
return os.Symlink(filePath, link.Linkname)
}
stat, _ := os.Stat(filePath)
if stat != nil {
_ = os.Remove(filePath)
}
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, mode)
if err != nil {
return errors.Wrapf(err, "unable to open file: %v ", filePath)
}
_, err = io.Copy(file, reader)
if closeErr := file.Close(); err == nil {
err = closeErr
}
return err
}