Debian Codegraph @

storage/2018-11-09/file/files/range_put.go

raw
1 package files
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "strings"
8
9 "github.com/Azure/go-autorest/autorest"
10 "github.com/Azure/go-autorest/autorest/azure"
11 "github.com/Azure/go-autorest/autorest/validation"
12 "github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
13 )
14
15 type PutByteRangeInput struct {
16 StartBytes int64
17 EndBytes int64
18
19 // Content is the File Contents for the specified range
20 // which can be at most 4MB
21 Content []byte
22 }
23
24 // PutByteRange puts the specified Byte Range in the specified File.
25 func (client Client) PutByteRange(ctx context.Context, accountName, shareName, path, fileName string, input PutByteRangeInput) (result autorest.Response, err error) {
26 if accountName == "" {
27 return result, validation.NewError("files.Client", "PutByteRange", "`accountName` cannot be an empty string.")
28 }
29 if shareName == "" {
30 return result, validation.NewError("files.Client", "PutByteRange", "`shareName` cannot be an empty string.")
31 }
32 if strings.ToLower(shareName) != shareName {
33 return result, validation.NewError("files.Client", "PutByteRange", "`shareName` must be a lower-cased string.")
34 }
35 if fileName == "" {
36 return result, validation.NewError("files.Client", "PutByteRange", "`fileName` cannot be an empty string.")
37 }
38 if input.StartBytes < 0 {
39 return result, validation.NewError("files.Client", "PutByteRange", "`input.StartBytes` must be greater or equal to 0.")
40 }
41 if input.EndBytes <= 0 {
42 return result, validation.NewError("files.Client", "PutByteRange", "`input.EndBytes` must be greater than 0.")
43 }
44
45 expectedBytes := input.EndBytes - input.StartBytes
46 actualBytes := len(input.Content)
47 if expectedBytes != int64(actualBytes) {
48 return result, validation.NewError("files.Client", "PutByteRange", fmt.Sprintf("The specified byte-range (%d) didn't match the content size (%d).", expectedBytes, actualBytes))
49 }
50 if expectedBytes < (4 * 1024) {
51 return result, validation.NewError("files.Client", "PutByteRange", "Specified Byte Range must be at least 4KB.")
52 }
53
54 if expectedBytes > (4 * 1024 * 1024) {
55 return result, validation.NewError("files.Client", "PutByteRange", "Specified Byte Range must be at most 4MB.")
56 }
57
58 req, err := client.PutByteRangePreparer(ctx, accountName, shareName, path, fileName, input)
59 if err != nil {
60 err = autorest.NewErrorWithError(err, "files.Client", "PutByteRange", nil, "Failure preparing request")
61 return
62 }
63
64 resp, err := client.PutByteRangeSender(req)
65 if err != nil {
66 result = autorest.Response{Response: resp}
67 err = autorest.NewErrorWithError(err, "files.Client", "PutByteRange", resp, "Failure sending request")
68 return
69 }
70
71 result, err = client.PutByteRangeResponder(resp)
72 if err != nil {
73 err = autorest.NewErrorWithError(err, "files.Client", "PutByteRange", resp, "Failure responding to request")
74 return
75 }
76
77 return
78 }
79
80 // PutByteRangePreparer prepares the PutByteRange request.
81 func (client Client) PutByteRangePreparer(ctx context.Context, accountName, shareName, path, fileName string, input PutByteRangeInput) (*http.Request, error) {
82 if path != "" {
83 path = fmt.Sprintf("%s/", path)
84 }
85 pathParameters := map[string]interface{}{
86 "shareName": autorest.Encode("path", shareName),
87 "directory": autorest.Encode("path", path),
88 "fileName": autorest.Encode("path", fileName),
89 }
90
91 queryParameters := map[string]interface{}{
92 "comp": autorest.Encode("query", "range"),
93 }
94
95 headers := map[string]interface{}{
96 "x-ms-version": APIVersion,
97 "x-ms-write": "update",
98 "x-ms-range": fmt.Sprintf("bytes=%d-%d", input.StartBytes, input.EndBytes-1),
99 "Content-Length": int(len(input.Content)),
100 }
101
102 preparer := autorest.CreatePreparer(
103 autorest.AsPut(),
104 autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)),
105 autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters),
106 autorest.WithHeaders(headers),
107 autorest.WithQueryParameters(queryParameters),
108 autorest.WithBytes(&input.Content))
109 return preparer.Prepare((&http.Request{}).WithContext(ctx))
110 }
111
112 // PutByteRangeSender sends the PutByteRange request. The method will close the
113 // http.Response Body if it receives an error.
114 func (client Client) PutByteRangeSender(req *http.Request) (*http.Response, error) {
115 return autorest.SendWithSender(client, req,
116 azure.DoRetryWithRegistration(client.Client))
117 }
118
119 // PutByteRangeResponder handles the response to the PutByteRange request. The method always
120 // closes the http.Response Body.
121 func (client Client) PutByteRangeResponder(resp *http.Response) (result autorest.Response, err error) {
122 err = autorest.Respond(
123 resp,
124 client.ByInspecting(),
125 azure.WithErrorUnlessStatusCode(http.StatusCreated),
126 autorest.ByClosing())
127 result = autorest.Response{Response: resp}
128
129 return
130 }