| 1 | package blobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/Azure/go-autorest/autorest" |
| 11 | "github.com/Azure/go-autorest/autorest/azure" |
| 12 | "github.com/Azure/go-autorest/autorest/validation" |
| 13 | "github.com/tombuildsstuff/giovanni/storage/internal/endpoints" |
| 14 | ) |
| 15 | |
| 16 | type GetPageRangesInput struct { |
| 17 | LeaseID *string |
| 18 | |
| 19 | StartByte *int64 |
| 20 | EndByte *int64 |
| 21 | } |
| 22 | |
| 23 | type GetPageRangesResult struct { |
| 24 | autorest.Response |
| 25 | |
| 26 | // The size of the blob in bytes |
| 27 | ContentLength *int64 |
| 28 | |
| 29 | // The Content Type of the blob |
| 30 | ContentType string |
| 31 | |
| 32 | // The ETag associated with this blob |
| 33 | ETag string |
| 34 | |
| 35 | PageRanges []PageRange `xml:"PageRange"` |
| 36 | } |
| 37 | |
| 38 | type PageRange struct { |
| 39 | // The start byte offset for this range, inclusive |
| 40 | Start int64 `xml:"Start"` |
| 41 | |
| 42 | // The end byte offset for this range, inclusive |
| 43 | End int64 `xml:"End"` |
| 44 | } |
| 45 | |
| 46 | // GetPageRanges returns the list of valid page ranges for a page blob or snapshot of a page blob. |
| 47 | func (client Client) GetPageRanges(ctx context.Context, accountName, containerName, blobName string, input GetPageRangesInput) (result GetPageRangesResult, err error) { |
| 48 | if accountName == "" { |
| 49 | return result, validation.NewError("blobs.Client", "GetPageRanges", "`accountName` cannot be an empty string.") |
| 50 | } |
| 51 | if containerName == "" { |
| 52 | return result, validation.NewError("blobs.Client", "GetPageRanges", "`containerName` cannot be an empty string.") |
| 53 | } |
| 54 | if strings.ToLower(containerName) != containerName { |
| 55 | return result, validation.NewError("blobs.Client", "GetPageRanges", "`containerName` must be a lower-cased string.") |
| 56 | } |
| 57 | if blobName == "" { |
| 58 | return result, validation.NewError("blobs.Client", "GetPageRanges", "`blobName` cannot be an empty string.") |
| 59 | } |
| 60 | if (input.StartByte != nil && input.EndByte == nil) || input.StartByte == nil && input.EndByte != nil { |
| 61 | return result, validation.NewError("blobs.Client", "GetPageRanges", "`input.StartByte` and `input.EndByte` must both be specified, or both be nil.") |
| 62 | } |
| 63 | |
| 64 | req, err := client.GetPageRangesPreparer(ctx, accountName, containerName, blobName, input) |
| 65 | if err != nil { |
| 66 | err = autorest.NewErrorWithError(err, "blobs.Client", "GetPageRanges", nil, "Failure preparing request") |
| 67 | return |
| 68 | } |
| 69 | |
| 70 | resp, err := client.GetPageRangesSender(req) |
| 71 | if err != nil { |
| 72 | result.Response = autorest.Response{Response: resp} |
| 73 | err = autorest.NewErrorWithError(err, "blobs.Client", "GetPageRanges", resp, "Failure sending request") |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | result, err = client.GetPageRangesResponder(resp) |
| 78 | if err != nil { |
| 79 | err = autorest.NewErrorWithError(err, "blobs.Client", "GetPageRanges", resp, "Failure responding to request") |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | // GetPageRangesPreparer prepares the GetPageRanges request. |
| 87 | func (client Client) GetPageRangesPreparer(ctx context.Context, accountName, containerName, blobName string, input GetPageRangesInput) (*http.Request, error) { |
| 88 | pathParameters := map[string]interface{}{ |
| 89 | "containerName": autorest.Encode("path", containerName), |
| 90 | "blobName": autorest.Encode("path", blobName), |
| 91 | } |
| 92 | |
| 93 | queryParameters := map[string]interface{}{ |
| 94 | "comp": autorest.Encode("query", "pagelist"), |
| 95 | } |
| 96 | |
| 97 | headers := map[string]interface{}{ |
| 98 | "x-ms-version": APIVersion, |
| 99 | } |
| 100 | |
| 101 | if input.LeaseID != nil { |
| 102 | headers["x-ms-lease-id"] = *input.LeaseID |
| 103 | } |
| 104 | |
| 105 | if input.StartByte != nil && input.EndByte != nil { |
| 106 | headers["x-ms-range"] = fmt.Sprintf("bytes=%d-%d", *input.StartByte, *input.EndByte) |
| 107 | } |
| 108 | |
| 109 | preparer := autorest.CreatePreparer( |
| 110 | autorest.AsGet(), |
| 111 | autorest.WithBaseURL(endpoints.GetBlobEndpoint(client.BaseURI, accountName)), |
| 112 | autorest.WithPathParameters("/{containerName}/{blobName}", pathParameters), |
| 113 | autorest.WithHeaders(headers), |
| 114 | autorest.WithQueryParameters(queryParameters)) |
| 115 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 116 | } |
| 117 | |
| 118 | // GetPageRangesSender sends the GetPageRanges request. The method will close the |
| 119 | // http.Response Body if it receives an error. |
| 120 | func (client Client) GetPageRangesSender(req *http.Request) (*http.Response, error) { |
| 121 | return autorest.SendWithSender(client, req, |
| 122 | azure.DoRetryWithRegistration(client.Client)) |
| 123 | } |
| 124 | |
| 125 | // GetPageRangesResponder handles the response to the GetPageRanges request. The method always |
| 126 | // closes the http.Response Body. |
| 127 | func (client Client) GetPageRangesResponder(resp *http.Response) (result GetPageRangesResult, err error) { |
| 128 | if resp != nil && resp.Header != nil { |
| 129 | result.ContentType = resp.Header.Get("Content-Type") |
| 130 | result.ETag = resp.Header.Get("ETag") |
| 131 | |
| 132 | if v := resp.Header.Get("x-ms-blob-content-length"); v != "" { |
| 133 | i, innerErr := strconv.Atoi(v) |
| 134 | if innerErr != nil { |
| 135 | err = fmt.Errorf("Error parsing %q as an integer: %s", v, innerErr) |
| 136 | return |
| 137 | } |
| 138 | |
| 139 | i64 := int64(i) |
| 140 | result.ContentLength = &i64 |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | err = autorest.Respond( |
| 145 | resp, |
| 146 | client.ByInspecting(), |
| 147 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 148 | autorest.ByUnmarshallingXML(&result), |
| 149 | autorest.ByClosing()) |
| 150 | result.Response = autorest.Response{Response: resp} |
| 151 | return |
| 152 | } |