| 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 | |
| 51 | if expectedBytes > (4 * 1024 * 1024) { |
| 52 | return result, validation.NewError("files.Client", "PutByteRange", "Specified Byte Range must be at most 4MB.") |
| 53 | } |
| 54 | |
| 55 | req, err := client.PutByteRangePreparer(ctx, accountName, shareName, path, fileName, input) |
| 56 | if err != nil { |
| 57 | err = autorest.NewErrorWithError(err, "files.Client", "PutByteRange", nil, "Failure preparing request") |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | resp, err := client.PutByteRangeSender(req) |
| 62 | if err != nil { |
| 63 | result = autorest.Response{Response: resp} |
| 64 | err = autorest.NewErrorWithError(err, "files.Client", "PutByteRange", resp, "Failure sending request") |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | result, err = client.PutByteRangeResponder(resp) |
| 69 | if err != nil { |
| 70 | err = autorest.NewErrorWithError(err, "files.Client", "PutByteRange", resp, "Failure responding to request") |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | // PutByteRangePreparer prepares the PutByteRange request. |
| 78 | func (client Client) PutByteRangePreparer(ctx context.Context, accountName, shareName, path, fileName string, input PutByteRangeInput) (*http.Request, error) { |
| 79 | if path != "" { |
| 80 | path = fmt.Sprintf("%s/", path) |
| 81 | } |
| 82 | pathParameters := map[string]interface{}{ |
| 83 | "shareName": autorest.Encode("path", shareName), |
| 84 | "directory": autorest.Encode("path", path), |
| 85 | "fileName": autorest.Encode("path", fileName), |
| 86 | } |
| 87 | |
| 88 | queryParameters := map[string]interface{}{ |
| 89 | "comp": autorest.Encode("query", "range"), |
| 90 | } |
| 91 | |
| 92 | headers := map[string]interface{}{ |
| 93 | "x-ms-version": APIVersion, |
| 94 | "x-ms-write": "update", |
| 95 | "x-ms-range": fmt.Sprintf("bytes=%d-%d", input.StartBytes, input.EndBytes-1), |
| 96 | "Content-Length": int(len(input.Content)), |
| 97 | } |
| 98 | |
| 99 | preparer := autorest.CreatePreparer( |
| 100 | autorest.AsPut(), |
| 101 | autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)), |
| 102 | autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters), |
| 103 | autorest.WithHeaders(headers), |
| 104 | autorest.WithQueryParameters(queryParameters), |
| 105 | autorest.WithBytes(&input.Content)) |
| 106 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 107 | } |
| 108 | |
| 109 | // PutByteRangeSender sends the PutByteRange request. The method will close the |
| 110 | // http.Response Body if it receives an error. |
| 111 | func (client Client) PutByteRangeSender(req *http.Request) (*http.Response, error) { |
| 112 | return autorest.SendWithSender(client, req, |
| 113 | azure.DoRetryWithRegistration(client.Client)) |
| 114 | } |
| 115 | |
| 116 | // PutByteRangeResponder handles the response to the PutByteRange request. The method always |
| 117 | // closes the http.Response Body. |
| 118 | func (client Client) PutByteRangeResponder(resp *http.Response) (result autorest.Response, err error) { |
| 119 | err = autorest.Respond( |
| 120 | resp, |
| 121 | client.ByInspecting(), |
| 122 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 123 | autorest.ByClosing()) |
| 124 | result = autorest.Response{Response: resp} |
| 125 | |
| 126 | return |
| 127 | } |