Debian Codegraph @

storage/2018-11-09/file/files/range_get.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 GetByteRangeInput struct {
16 StartBytes int64
17 EndBytes int64
18 }
19
20 type GetByteRangeResult struct {
21 autorest.Response
22
23 Contents []byte
24 }
25
26 // GetByteRange returns the specified Byte Range from the specified File.
27 func (client Client) GetByteRange(ctx context.Context, accountName, shareName, path, fileName string, input GetByteRangeInput) (result GetByteRangeResult, err error) {
28 if accountName == "" {
29 return result, validation.NewError("files.Client", "GetByteRange", "`accountName` cannot be an empty string.")
30 }
31 if shareName == "" {
32 return result, validation.NewError("files.Client", "GetByteRange", "`shareName` cannot be an empty string.")
33 }
34 if strings.ToLower(shareName) != shareName {
35 return result, validation.NewError("files.Client", "GetByteRange", "`shareName` must be a lower-cased string.")
36 }
37 if fileName == "" {
38 return result, validation.NewError("files.Client", "GetByteRange", "`fileName` cannot be an empty string.")
39 }
40 if input.StartBytes < 0 {
41 return result, validation.NewError("files.Client", "GetByteRange", "`input.StartBytes` must be greater or equal to 0.")
42 }
43 if input.EndBytes <= 0 {
44 return result, validation.NewError("files.Client", "GetByteRange", "`input.EndBytes` must be greater than 0.")
45 }
46 expectedBytes := input.EndBytes - input.StartBytes
47 if expectedBytes < (4 * 1024) {
48 return result, validation.NewError("files.Client", "GetByteRange", "Requested Byte Range must be at least 4KB.")
49 }
50 if expectedBytes > (4 * 1024 * 1024) {
51 return result, validation.NewError("files.Client", "GetByteRange", "Requested Byte Range must be at most 4MB.")
52 }
53
54 req, err := client.GetByteRangePreparer(ctx, accountName, shareName, path, fileName, input)
55 if err != nil {
56 err = autorest.NewErrorWithError(err, "files.Client", "GetByteRange", nil, "Failure preparing request")
57 return
58 }
59
60 resp, err := client.GetByteRangeSender(req)
61 if err != nil {
62 result.Response = autorest.Response{Response: resp}
63 err = autorest.NewErrorWithError(err, "files.Client", "GetByteRange", resp, "Failure sending request")
64 return
65 }
66
67 result, err = client.GetByteRangeResponder(resp, expectedBytes)
68 if err != nil {
69 err = autorest.NewErrorWithError(err, "files.Client", "GetByteRange", resp, "Failure responding to request")
70 return
71 }
72
73 return
74 }
75
76 // GetByteRangePreparer prepares the GetByteRange request.
77 func (client Client) GetByteRangePreparer(ctx context.Context, accountName, shareName, path, fileName string, input GetByteRangeInput) (*http.Request, error) {
78 if path != "" {
79 path = fmt.Sprintf("%s/", path)
80 }
81 pathParameters := map[string]interface{}{
82 "shareName": autorest.Encode("path", shareName),
83 "directory": autorest.Encode("path", path),
84 "fileName": autorest.Encode("path", fileName),
85 }
86
87 headers := map[string]interface{}{
88 "x-ms-version": APIVersion,
89 "x-ms-range": fmt.Sprintf("bytes=%d-%d", input.StartBytes, input.EndBytes-1),
90 }
91
92 preparer := autorest.CreatePreparer(
93 autorest.AsGet(),
94 autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)),
95 autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters),
96 autorest.WithHeaders(headers))
97 return preparer.Prepare((&http.Request{}).WithContext(ctx))
98 }
99
100 // GetByteRangeSender sends the GetByteRange request. The method will close the
101 // http.Response Body if it receives an error.
102 func (client Client) GetByteRangeSender(req *http.Request) (*http.Response, error) {
103 return autorest.SendWithSender(client, req,
104 azure.DoRetryWithRegistration(client.Client))
105 }
106
107 // GetByteRangeResponder handles the response to the GetByteRange request. The method always
108 // closes the http.Response Body.
109 func (client Client) GetByteRangeResponder(resp *http.Response, length int64) (result GetByteRangeResult, err error) {
110 result.Contents = make([]byte, length)
111
112 err = autorest.Respond(
113 resp,
114 client.ByInspecting(),
115 azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusPartialContent),
116 autorest.ByUnmarshallingBytes(&result.Contents),
117 autorest.ByClosing())
118 result.Response = autorest.Response{Response: resp}
119
120 return
121 }