forked from mongodb/mongo-go-driver
/
gridfs_bucket_operation_execution.go
271 lines (233 loc) · 6.44 KB
/
gridfs_bucket_operation_execution.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package unified
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"io"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
func createBucketFindCursor(ctx context.Context, operation *operation) (*cursorResult, error) {
bucket, err := entities(ctx).gridFSBucket(operation.Object)
if err != nil {
return nil, err
}
var filter bson.Raw
opts := options.GridFSFind()
elems, err := operation.Arguments.Elements()
if err != nil {
return nil, err
}
for _, elem := range elems {
key := elem.Key()
val := elem.Value()
switch key {
case "maxTimeMS":
opts.SetMaxTime(time.Duration(val.Int32()) * time.Millisecond)
case "filter":
filter = val.Document()
default:
return nil, fmt.Errorf("unrecognized bucket find option %q", key)
}
}
if filter == nil {
return nil, newMissingArgumentError("filter")
}
cursor, err := bucket.FindContext(ctx, filter, opts)
res := &cursorResult{
cursor: cursor,
err: err,
}
return res, nil
}
func executeBucketDelete(ctx context.Context, operation *operation) (*operationResult, error) {
bucket, err := entities(ctx).gridFSBucket(operation.Object)
if err != nil {
return nil, err
}
var id *bson.RawValue
elems, err := operation.Arguments.Elements()
if err != nil {
return nil, err
}
for _, elem := range elems {
key := elem.Key()
val := elem.Value()
switch key {
case "id":
id = &val
default:
return nil, fmt.Errorf("unrecognized bucket delete option %q", key)
}
}
if id == nil {
return nil, newMissingArgumentError("id")
}
return newErrorResult(bucket.DeleteContext(ctx, *id)), nil
}
func executeBucketDownload(ctx context.Context, operation *operation) (*operationResult, error) {
bucket, err := entities(ctx).gridFSBucket(operation.Object)
if err != nil {
return nil, err
}
var id *bson.RawValue
elems, err := operation.Arguments.Elements()
if err != nil {
return nil, err
}
for _, elem := range elems {
key := elem.Key()
val := elem.Value()
switch key {
case "id":
id = &val
default:
return nil, fmt.Errorf("unrecognized bucket download option %q", key)
}
}
if id == nil {
return nil, newMissingArgumentError("id")
}
stream, err := bucket.OpenDownloadStream(*id)
if err != nil {
return newErrorResult(err), nil
}
var buffer bytes.Buffer
if _, err := io.Copy(&buffer, stream); err != nil {
return newErrorResult(err), nil
}
return newValueResult(bsontype.Binary, bsoncore.AppendBinary(nil, 0, buffer.Bytes()), nil), nil
}
func executeBucketDownloadByName(ctx context.Context, operation *operation) (*operationResult, error) {
bucket, err := entities(ctx).gridFSBucket(operation.Object)
if err != nil {
return nil, err
}
elems, err := operation.Arguments.Elements()
if err != nil {
return nil, err
}
var filename string
opts := options.GridFSName()
for _, elem := range elems {
key := elem.Key()
val := elem.Value()
switch key {
case "filename":
filename = val.StringValue()
case "revision":
opts.SetRevision(val.AsInt32())
default:
return nil, fmt.Errorf("unrecognized bucket download option %q", key)
}
}
if filename == "" {
return nil, newMissingArgumentError("filename")
}
var buf bytes.Buffer
_, err = bucket.DownloadToStreamByName(filename, &buf, opts)
if err != nil {
return newErrorResult(err), nil
}
return newValueResult(bsontype.Binary, bsoncore.AppendBinary(nil, 0, buf.Bytes()), nil), nil
}
func executeBucketDrop(ctx context.Context, operation *operation) (*operationResult, error) {
bucket, err := entities(ctx).gridFSBucket(operation.Object)
if err != nil {
return nil, err
}
return newErrorResult(bucket.DropContext(ctx)), nil
}
func executeBucketRename(ctx context.Context, operation *operation) (*operationResult, error) {
bucket, err := entities(ctx).gridFSBucket(operation.Object)
if err != nil {
return nil, err
}
var id *bson.RawValue
var newFilename string
elems, err := operation.Arguments.Elements()
if err != nil {
return nil, err
}
for _, elem := range elems {
key := elem.Key()
val := elem.Value()
switch key {
case "id":
id = &val
case "newFilename":
newFilename = val.StringValue()
default:
return nil, fmt.Errorf("unrecognized bucket rename option %q", key)
}
}
if id == nil {
return nil, newMissingArgumentError("id")
}
return newErrorResult(bucket.RenameContext(ctx, id, newFilename)), nil
}
func executeBucketUpload(ctx context.Context, operation *operation) (*operationResult, error) {
bucket, err := entities(ctx).gridFSBucket(operation.Object)
if err != nil {
return nil, err
}
var filename string
var fileBytes []byte
opts := options.GridFSUpload()
elems, err := operation.Arguments.Elements()
if err != nil {
return nil, err
}
for _, elem := range elems {
key := elem.Key()
val := elem.Value()
switch key {
case "chunkSizeBytes":
opts.SetChunkSizeBytes(val.Int32())
case "filename":
filename = val.StringValue()
case "metadata":
opts.SetMetadata(val.Document())
case "source":
fileBytes, err = hex.DecodeString(val.Document().Lookup("$$hexBytes").StringValue())
if err != nil {
return nil, fmt.Errorf("error converting source string to bytes: %w", err)
}
case "contentType":
return nil, newSkipTestError("the deprecated contentType file option is not supported")
case "disableMD5":
return nil, newSkipTestError("the deprecated disableMD5 file option is not supported")
default:
return nil, fmt.Errorf("unrecognized bucket upload option %q", key)
}
}
if filename == "" {
return nil, newMissingArgumentError("filename")
}
if fileBytes == nil {
return nil, newMissingArgumentError("source")
}
fileID, err := bucket.UploadFromStream(filename, bytes.NewReader(fileBytes), opts)
if err != nil {
return newErrorResult(err), nil
}
if operation.ResultEntityID != nil {
fileIDValue := bson.RawValue{
Type: bsontype.ObjectID,
Value: fileID[:],
}
if err := entities(ctx).addBSONEntity(*operation.ResultEntityID, fileIDValue); err != nil {
return nil, fmt.Errorf("error storing result as BSON entity: %w", err)
}
}
return newValueResult(bsontype.ObjectID, fileID[:], nil), nil
}