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