Skip to content

Commit 0f1cb7b

Browse files
feat: add checksums for single chunk json uploads (#3448)
* feat: add checksums for single chunk json uploads * minor fixes * nit changes
1 parent e92945d commit 0f1cb7b

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

google-api-go-generator/gen.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,17 @@ func (meth *Method) generateCode() {
22802280
} else {
22812281
pn(`reqHeaders := gensupport.SetHeaders(c.s.userAgent(), %s, c.header_, "x-goog-api-version", %q)`, contentType, apiVersion)
22822282
}
2283+
if meth.supportsMediaUpload() && meth.api.Name == "storage" {
2284+
comment = "Set auto checksum in case of a single chunk upload if enabled."
2285+
p("%s", asComment("", comment))
2286+
pn("if c.object != nil &&")
2287+
pn(` c.object.Crc32c == "" &&`)
2288+
pn(" c.mediaInfo_ != nil &&")
2289+
pn(` c.mediaInfo_.UploadType() == "multipart" &&`)
2290+
pn(" c.mediaInfo_.ChecksumEnabled() {")
2291+
pn(" c.object.Crc32c = c.mediaInfo_.GetAutoChecksum()")
2292+
pn("}")
2293+
}
22832294
if httpMethod == "GET" {
22842295
pn(`if c.ifNoneMatch_ != "" {`)
22852296
pn(` reqHeaders.Set("If-None-Match", c.ifNoneMatch_)`)

internal/gensupport/media.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ func (mi *MediaInfo) UploadType() string {
198198
return "resumable"
199199
}
200200

201+
// ChecksumEnabled returns if auto checksum is enabled in buffer.
202+
func (mi *MediaInfo) ChecksumEnabled() bool {
203+
return mi.buffer != nil && mi.buffer.enableAutoChecksum
204+
}
205+
206+
// GetAutoChecksum returns the computed auto checksum from buffer (if enabled).
207+
// Make sure whole data is written and read from the buffer before calling this
208+
// function to get correct checksum of the data.
209+
func (mi *MediaInfo) GetAutoChecksum() string {
210+
if mi.buffer != nil &&
211+
mi.buffer.enableAutoChecksum {
212+
return encodeUint32(mi.buffer.fullObjectChecksum)
213+
}
214+
return ""
215+
}
216+
201217
// UploadRequest sets up an HTTP request for media upload. It adds headers
202218
// as necessary, and returns a replacement for the body and a function for http.Request.GetBody.
203219
func (mi *MediaInfo) UploadRequest(reqHeaders http.Header, body io.Reader) (newBody io.Reader, getBody func() (io.ReadCloser, error), cleanup func()) {

storage/v1/storage-gen.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)