| 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 ClearByteRangeInput struct { |
| 16 | StartBytes int64 |
| 17 | EndBytes int64 |
| 18 | } |
| 19 | |
| 20 | // ClearByteRange clears the specified Byte Range from within the specified File |
| 21 | func (client Client) ClearByteRange(ctx context.Context, accountName, shareName, path, fileName string, input ClearByteRangeInput) (result autorest.Response, err error) { |
| 22 | if accountName == "" { |
| 23 | return result, validation.NewError("files.Client", "ClearByteRange", "`accountName` cannot be an empty string.") |
| 24 | } |
| 25 | if shareName == "" { |
| 26 | return result, validation.NewError("files.Client", "ClearByteRange", "`shareName` cannot be an empty string.") |
| 27 | } |
| 28 | if strings.ToLower(shareName) != shareName { |
| 29 | return result, validation.NewError("files.Client", "ClearByteRange", "`shareName` must be a lower-cased string.") |
| 30 | } |
| 31 | if fileName == "" { |
| 32 | return result, validation.NewError("files.Client", "ClearByteRange", "`fileName` cannot be an empty string.") |
| 33 | } |
| 34 | if input.StartBytes < 0 { |
| 35 | return result, validation.NewError("files.Client", "ClearByteRange", "`input.StartBytes` must be greater or equal to 0.") |
| 36 | } |
| 37 | if input.EndBytes <= 0 { |
| 38 | return result, validation.NewError("files.Client", "ClearByteRange", "`input.EndBytes` must be greater than 0.") |
| 39 | } |
| 40 | |
| 41 | req, err := client.ClearByteRangePreparer(ctx, accountName, shareName, path, fileName, input) |
| 42 | if err != nil { |
| 43 | err = autorest.NewErrorWithError(err, "files.Client", "ClearByteRange", nil, "Failure preparing request") |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | resp, err := client.ClearByteRangeSender(req) |
| 48 | if err != nil { |
| 49 | result = autorest.Response{Response: resp} |
| 50 | err = autorest.NewErrorWithError(err, "files.Client", "ClearByteRange", resp, "Failure sending request") |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | result, err = client.ClearByteRangeResponder(resp) |
| 55 | if err != nil { |
| 56 | err = autorest.NewErrorWithError(err, "files.Client", "ClearByteRange", resp, "Failure responding to request") |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | // ClearByteRangePreparer prepares the ClearByteRange request. |
| 64 | func (client Client) ClearByteRangePreparer(ctx context.Context, accountName, shareName, path, fileName string, input ClearByteRangeInput) (*http.Request, error) { |
| 65 | if path != "" { |
| 66 | path = fmt.Sprintf("%s/", path) |
| 67 | } |
| 68 | pathParameters := map[string]interface{}{ |
| 69 | "shareName": autorest.Encode("path", shareName), |
| 70 | "directory": autorest.Encode("path", path), |
| 71 | "fileName": autorest.Encode("path", fileName), |
| 72 | } |
| 73 | |
| 74 | queryParameters := map[string]interface{}{ |
| 75 | "comp": autorest.Encode("query", "range"), |
| 76 | } |
| 77 | |
| 78 | headers := map[string]interface{}{ |
| 79 | "x-ms-version": APIVersion, |
| 80 | "x-ms-write": "clear", |
| 81 | "x-ms-range": fmt.Sprintf("bytes=%d-%d", input.StartBytes, input.EndBytes), |
| 82 | } |
| 83 | |
| 84 | preparer := autorest.CreatePreparer( |
| 85 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 86 | autorest.AsPut(), |
| 87 | autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)), |
| 88 | autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters), |
| 89 | autorest.WithHeaders(headers), |
| 90 | autorest.WithQueryParameters(queryParameters)) |
| 91 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 92 | } |
| 93 | |
| 94 | // ClearByteRangeSender sends the ClearByteRange request. The method will close the |
| 95 | // http.Response Body if it receives an error. |
| 96 | func (client Client) ClearByteRangeSender(req *http.Request) (*http.Response, error) { |
| 97 | return autorest.SendWithSender(client, req, |
| 98 | azure.DoRetryWithRegistration(client.Client)) |
| 99 | } |
| 100 | |
| 101 | // ClearByteRangeResponder handles the response to the ClearByteRange request. The method always |
| 102 | // closes the http.Response Body. |
| 103 | func (client Client) ClearByteRangeResponder(resp *http.Response) (result autorest.Response, err error) { |
| 104 | err = autorest.Respond( |
| 105 | resp, |
| 106 | client.ByInspecting(), |
| 107 | azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 108 | autorest.ByClosing()) |
| 109 | result = autorest.Response{Response: resp} |
| 110 | |
| 111 | return |
| 112 | } |