-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
132 lines (93 loc) · 2.46 KB
/
writer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package writer
import (
"context"
"fmt"
"io"
"log"
"sync"
wof_writer "github.com/whosonfirst/go-writer/v3"
"gocloud.dev/blob"
)
type BlobWriterOptionsKey string
type BlobWriter struct {
wof_writer.Writer
bucket *blob.Bucket
logger *log.Logger
}
// In principle this could also be done with a sync.OnceFunc call but that will
// require that everyone uses Go 1.21 (whose package import changes broke everything)
// which is literally days old as I write this. So maybe a few releases after 1.21.
//
// Also, _not_ using a sync.OnceFunc means we can call RegisterSchemes multiple times
// if and when multiple gomail-sender instances register themselves.
var register_mu = new(sync.RWMutex)
var register_map = map[string]bool{}
func init() {
ctx := context.Background()
err := RegisterSchemes(ctx)
if err != nil {
panic(err)
}
}
// RegisterSchemes will explicitly register all the schemes associated with the `AccessTokensDeliveryAgent` interface.
func RegisterSchemes(ctx context.Context) error {
register_mu.Lock()
defer register_mu.Unlock()
for _, scheme := range blob.DefaultURLMux().BucketSchemes() {
_, exists := register_map[scheme]
if exists {
continue
}
err := wof_writer.RegisterWriter(ctx, scheme, NewBlobWriter)
if err != nil {
return fmt.Errorf("Failed to register blob writer for '%s', %w", scheme, err)
}
register_map[scheme] = true
}
return nil
}
func NewBlobWriter(ctx context.Context, uri string) (wof_writer.Writer, error) {
bucket, err := blob.OpenBucket(ctx, uri)
if err != nil {
return nil, err
}
logger := log.New(io.Discard, "", 0)
wr := &BlobWriter{
bucket: bucket,
logger: logger,
}
return wr, nil
}
func (wr *BlobWriter) Write(ctx context.Context, uri string, fh io.ReadSeeker) (int64, error) {
var wr_opts *blob.WriterOptions
v := ctx.Value(BlobWriterOptionsKey("options"))
if v != nil {
wr_opts = v.(*blob.WriterOptions)
}
wr_fh, err := wr.bucket.NewWriter(ctx, uri, wr_opts)
if err != nil {
return 0, err
}
b, err := io.Copy(wr_fh, fh)
if err != nil {
return b, err
}
err = wr_fh.Close()
if err != nil {
return b, err
}
return b, nil
}
func (wr *BlobWriter) Flush(ctx context.Context) error {
return nil
}
func (wr *BlobWriter) Close(ctx context.Context) error {
return nil
}
func (wr *BlobWriter) SetLogger(ctx context.Context, logger *log.Logger) error {
wr.logger = logger
return nil
}
func (wr *BlobWriter) WriterURI(ctx context.Context, uri string) string {
return uri
}