| 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 | "github.com/tombuildsstuff/giovanni/storage/internal/metadata" |
| 14 | ) |
| 15 | |
| 16 | // SetMetaData updates the specified File to have the specified MetaData. |
| 17 | func (client Client) SetMetaData(ctx context.Context, accountName, shareName, path, fileName string, metaData map[string]string) (result autorest.Response, err error) { |
| 18 | if accountName == "" { |
| 19 | return result, validation.NewError("files.Client", "SetMetaData", "`accountName` cannot be an empty string.") |
| 20 | } |
| 21 | if shareName == "" { |
| 22 | return result, validation.NewError("files.Client", "SetMetaData", "`shareName` cannot be an empty string.") |
| 23 | } |
| 24 | if strings.ToLower(shareName) != shareName { |
| 25 | return result, validation.NewError("files.Client", "SetMetaData", "`shareName` must be a lower-cased string.") |
| 26 | } |
| 27 | if fileName == "" { |
| 28 | return result, validation.NewError("files.Client", "SetMetaData", "`fileName` cannot be an empty string.") |
| 29 | } |
| 30 | if err := metadata.Validate(metaData); err != nil { |
| 31 | return result, validation.NewError("files.Client", "SetMetaData", fmt.Sprintf("`metaData` is not valid: %s.", err)) |
| 32 | } |
| 33 | |
| 34 | req, err := client.SetMetaDataPreparer(ctx, accountName, shareName, path, fileName, metaData) |
| 35 | if err != nil { |
| 36 | err = autorest.NewErrorWithError(err, "files.Client", "SetMetaData", nil, "Failure preparing request") |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | resp, err := client.SetMetaDataSender(req) |
| 41 | if err != nil { |
| 42 | result = autorest.Response{Response: resp} |
| 43 | err = autorest.NewErrorWithError(err, "files.Client", "SetMetaData", resp, "Failure sending request") |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | result, err = client.SetMetaDataResponder(resp) |
| 48 | if err != nil { |
| 49 | err = autorest.NewErrorWithError(err, "files.Client", "SetMetaData", resp, "Failure responding to request") |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | // SetMetaDataPreparer prepares the SetMetaData request. |
| 57 | func (client Client) SetMetaDataPreparer(ctx context.Context, accountName, shareName, path, fileName string, metaData map[string]string) (*http.Request, error) { |
| 58 | if path != "" { |
| 59 | path = fmt.Sprintf("%s/", path) |
| 60 | } |
| 61 | pathParameters := map[string]interface{}{ |
| 62 | "shareName": autorest.Encode("path", shareName), |
| 63 | "directory": autorest.Encode("path", path), |
| 64 | "fileName": autorest.Encode("path", fileName), |
| 65 | } |
| 66 | |
| 67 | queryParameters := map[string]interface{}{ |
| 68 | "comp": autorest.Encode("query", "metadata"), |
| 69 | } |
| 70 | |
| 71 | headers := map[string]interface{}{ |
| 72 | "x-ms-version": APIVersion, |
| 73 | } |
| 74 | |
| 75 | headers = metadata.SetIntoHeaders(headers, metaData) |
| 76 | |
| 77 | preparer := autorest.CreatePreparer( |
| 78 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 79 | autorest.AsPut(), |
| 80 | autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)), |
| 81 | autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters), |
| 82 | autorest.WithQueryParameters(queryParameters), |
| 83 | autorest.WithHeaders(headers)) |
| 84 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 85 | } |
| 86 | |
| 87 | // SetMetaDataSender sends the SetMetaData request. The method will close the |
| 88 | // http.Response Body if it receives an error. |
| 89 | func (client Client) SetMetaDataSender(req *http.Request) (*http.Response, error) { |
| 90 | return autorest.SendWithSender(client, req, |
| 91 | azure.DoRetryWithRegistration(client.Client)) |
| 92 | } |
| 93 | |
| 94 | // SetMetaDataResponder handles the response to the SetMetaData request. The method always |
| 95 | // closes the http.Response Body. |
| 96 | func (client Client) SetMetaDataResponder(resp *http.Response) (result autorest.Response, err error) { |
| 97 | err = autorest.Respond( |
| 98 | resp, |
| 99 | client.ByInspecting(), |
| 100 | azure.WithErrorUnlessStatusCode(http.StatusOK), |
| 101 | autorest.ByClosing()) |
| 102 | result = autorest.Response{Response: resp} |
| 103 | |
| 104 | return |
| 105 | } |