This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
copy.go
91 lines (76 loc) · 2.23 KB
/
copy.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
88
89
90
91
package uploader
import (
"errors"
"io"
"os"
"path"
"context"
"github.com/Netflix/titus-executor/filesystems/xattr"
log "github.com/sirupsen/logrus"
)
// CopyBackend is an uploader that just copies files to another
// location on the same host
type CopyBackend struct {
Dir string `json:"directory"`
}
// NewCopyBackend creates a new instance of a copy uploader
func NewCopyBackend(directory string) Backend {
return &CopyBackend{Dir: directory}
}
// Upload copies a single file only!
func (u *CopyBackend) Upload(ctx context.Context, local, remote string, ctypeFunc ContentTypeInferenceFunction) error {
if err := ctx.Err(); err != nil {
return err
}
l, err := os.Open(local) // nolint: gosec
if err != nil {
return err
}
defer func() {
if err = l.Close(); err != nil {
log.Warningf("Failed to close %s: %s", l.Name(), err)
}
}()
contentType := ctypeFunc(local)
// copy uploader doesn't understand content types, ignore it
return u.uploadFile(l, remote, contentType)
}
// UploadFile copies a single file only!
func (u *CopyBackend) uploadFile(local io.Reader, remote, contentType string) error {
fullremote := path.Join(u.Dir, remote)
log.Println("copy : local io.Reader -> " + fullremote)
remoteDir := path.Dir(fullremote)
if err := os.MkdirAll(remoteDir, 0777); err != nil { // nolint: gosec
return err
}
r, err := os.Create(fullremote)
if err != nil {
return err
}
defer func() {
if err = r.Close(); err != nil {
log.Printf("Failed to close %s: %s", r.Name(), err)
}
}()
if contentType != "" {
err = xattr.FSetXattr(r, xattr.MimeTypeAttr, []byte(contentType))
if err != nil {
log.Warning("Unable to set content type: ", err)
}
}
_, err = io.Copy(r, local)
return err
}
// UploadPartOfFile copies a single file only. It doesn't preserve the cursor location in the file.
func (u *CopyBackend) UploadPartOfFile(ctx context.Context, local io.ReadSeeker, start, length int64, remote, contentType string) error {
if err := ctx.Err(); err != nil {
return err
}
if n, err := local.Seek(start, io.SeekStart); err != nil {
return err
} else if n != start {
return errors.New("Could not seek")
}
limitLocal := io.LimitReader(local, length)
return u.uploadFile(limitLocal, remote, contentType)
}