| 1 | package files |
| 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 | "github.com/tombuildsstuff/giovanni/storage/internal/metadata" |
| 15 | ) |
| 16 | |
| 17 | type GetResult struct { |
| 18 | autorest.Response |
| 19 | |
| 20 | CacheControl string |
| 21 | ContentDisposition string |
| 22 | ContentEncoding string |
| 23 | ContentLanguage string |
| 24 | ContentLength *int64 |
| 25 | ContentMD5 string |
| 26 | ContentType string |
| 27 | CopyID string |
| 28 | CopyStatus string |
| 29 | CopySource string |
| 30 | CopyProgress string |
| 31 | CopyStatusDescription string |
| 32 | CopyCompletionTime string |
| 33 | Encrypted bool |
| 34 | |
| 35 | MetaData map[string]string |
| 36 | } |
| 37 | |
| 38 | // GetProperties returns the Properties for the specified file |
| 39 | func (client Client) GetProperties(ctx context.Context, accountName, shareName, path, fileName string) (result GetResult, err error) { |
| 40 | if accountName == "" { |
| 41 | return result, validation.NewError("files.Client", "GetProperties", "`accountName` cannot be an empty string.") |
| 42 | } |
| 43 | if shareName == "" { |
| 44 | return result, validation.NewError("files.Client", "GetProperties", "`shareName` cannot be an empty string.") |
| 45 | } |
| 46 | if strings.ToLower(shareName) != shareName { |
| 47 | return result, validation.NewError("files.Client", "GetProperties", "`shareName` must be a lower-cased string.") |
| 48 | } |
| 49 | if fileName == "" { |
| 50 | return result, validation.NewError("files.Client", "GetProperties", "`fileName` cannot be an empty string.") |
| 51 | } |
| 52 | |
| 53 | req, err := client.GetPropertiesPreparer(ctx, accountName, shareName, path, fileName) |
| 54 | if err != nil { |
| 55 | err = autorest.NewErrorWithError(err, "files.Client", "GetProperties", nil, "Failure preparing request") |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | resp, err := client.GetPropertiesSender(req) |
| 60 | if err != nil { |
| 61 | result.Response = autorest.Response{Response: resp} |
| 62 | err = autorest.NewErrorWithError(err, "files.Client", "GetProperties", resp, "Failure sending request") |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | result, err = client.GetPropertiesResponder(resp) |
| 67 | if err != nil { |
| 68 | err = autorest.NewErrorWithError(err, "files.Client", "GetProperties", resp, "Failure responding to request") |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | // GetPropertiesPreparer prepares the GetProperties request. |
| 76 | func (client Client) GetPropertiesPreparer(ctx context.Context, accountName, shareName, path, fileName string) (*http.Request, error) { |
| 77 | if path != "" { |
| 78 | path = fmt.Sprintf("%s/", path) |
| 79 | } |
| 80 | pathParameters := map[string]interface{}{ |
| 81 | "shareName": autorest.Encode("path", shareName), |
| 82 | "directory": autorest.Encode("path", path), |
| 83 | "fileName": autorest.Encode("path", fileName), |
| 84 | } |
| 85 | |
| 86 | headers := map[string]interface{}{ |
| 87 | "x-ms-version": APIVersion, |
| 88 | } |
| 89 | |
| 90 | preparer := autorest.CreatePreparer( |
| 91 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 92 | autorest.AsHead(), |
| 93 | autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)), |
| 94 | autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters), |
| 95 | autorest.WithHeaders(headers)) |
| 96 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 97 | } |
| 98 | |
| 99 | // GetPropertiesSender sends the GetProperties request. The method will close the |
| 100 | // http.Response Body if it receives an error. |
| 101 | func (client Client) GetPropertiesSender(req *http.Request) (*http.Response, error) { |
| 102 | return autorest.SendWithSender(client, req, |
| 103 | azure.DoRetryWithRegistration(client.Client)) |
| 104 | } |
| 105 | |
| 106 | // GetPropertiesResponder handles the response to the GetProperties request. The method always |
| 107 | // closes the http.Response Body. |
| 108 | func (client Client) GetPropertiesResponder(resp *http.Response) (result GetResult, err error) { |
| 109 | if resp != nil && resp.Header != nil { |
| 110 | result.CacheControl = resp.Header.Get("Cache-Control") |
| 111 | result.ContentDisposition = resp.Header.Get("Content-Disposition") |
| 112 | result.ContentEncoding = resp.Header.Get("Content-Encoding") |
| 113 | result.ContentLanguage = resp.Header.Get("Content-Language") |
| 114 | result.ContentMD5 = resp.Header.Get("x-ms-content-md5") |
| 115 | result.ContentType = resp.Header.Get("Content-Type") |
| 116 | result.CopyID = resp.Header.Get("x-ms-copy-id") |
| 117 | result.CopyProgress = resp.Header.Get("x-ms-copy-progress") |
| 118 | result.CopySource = resp.Header.Get("x-ms-copy-source") |
| 119 | result.CopyStatus = resp.Header.Get("x-ms-copy-status") |
| 120 | result.CopyStatusDescription = resp.Header.Get("x-ms-copy-status-description") |
| 121 | result.CopyCompletionTime = resp.Header.Get("x-ms-copy-completion-time") |
| 122 | result.Encrypted = strings.EqualFold(resp.Header.Get("x-ms-server-encrypted"), "true") |
| 123 | result.MetaData = metadata.ParseFromHeaders(resp.Header) |
| 124 | |
| 125 | contentLengthRaw := resp.Header.Get("Content-Length") |
| 126 | if contentLengthRaw != "" { |
| 127 | contentLength, err := strconv.Atoi(contentLengthRaw) |
| 128 | if err != nil { |
| 129 | return result, fmt.Errorf("Error parsing %q for Content-Length as an integer: %s", contentLengthRaw, err) |
| 130 | } |
| 131 | contentLengthI64 := int64(contentLength) |
| 132 | result.ContentLength = &contentLengthI64 |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | err = autorest.Respond( |
| 137 | resp, |
| 138 | client.ByInspecting(), |
| 139 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 140 | autorest.ByClosing()) |
| 141 | result.Response = autorest.Response{Response: resp} |
| 142 | |
| 143 | return |
| 144 | } |