| 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 | type CopyInput struct { |
| 17 | // Specifies the URL of the source file or blob, up to 2 KB in length. |
| 18 | // |
| 19 | // To copy a file to another file within the same storage account, you may use Shared Key to authenticate |
| 20 | // the source file. If you are copying a file from another storage account, or if you are copying a blob from |
| 21 | // the same storage account or another storage account, then you must authenticate the source file or blob using a |
| 22 | // shared access signature. If the source is a public blob, no authentication is required to perform the copy |
| 23 | // operation. A file in a share snapshot can also be specified as a copy source. |
| 24 | CopySource string |
| 25 | |
| 26 | MetaData map[string]string |
| 27 | } |
| 28 | |
| 29 | type CopyResult struct { |
| 30 | autorest.Response |
| 31 | |
| 32 | // The CopyID, which can be passed to AbortCopy to abort the copy. |
| 33 | CopyID string |
| 34 | |
| 35 | // Either `success` or `pending` |
| 36 | CopySuccess string |
| 37 | } |
| 38 | |
| 39 | // Copy copies a blob or file to a destination file within the storage account asynchronously. |
| 40 | func (client Client) Copy(ctx context.Context, accountName, shareName, path, fileName string, input CopyInput) (result CopyResult, err error) { |
| 41 | if accountName == "" { |
| 42 | return result, validation.NewError("files.Client", "Copy", "`accountName` cannot be an empty string.") |
| 43 | } |
| 44 | if shareName == "" { |
| 45 | return result, validation.NewError("files.Client", "Copy", "`shareName` cannot be an empty string.") |
| 46 | } |
| 47 | if strings.ToLower(shareName) != shareName { |
| 48 | return result, validation.NewError("files.Client", "Copy", "`shareName` must be a lower-cased string.") |
| 49 | } |
| 50 | if fileName == "" { |
| 51 | return result, validation.NewError("files.Client", "Copy", "`fileName` cannot be an empty string.") |
| 52 | } |
| 53 | if input.CopySource == "" { |
| 54 | return result, validation.NewError("files.Client", "Copy", "`input.CopySource` cannot be an empty string.") |
| 55 | } |
| 56 | if err := metadata.Validate(input.MetaData); err != nil { |
| 57 | return result, validation.NewError("files.Client", "Copy", fmt.Sprintf("`input.MetaData` is not valid: %s.", err)) |
| 58 | } |
| 59 | |
| 60 | req, err := client.CopyPreparer(ctx, accountName, shareName, path, fileName, input) |
| 61 | if err != nil { |
| 62 | err = autorest.NewErrorWithError(err, "files.Client", "Copy", nil, "Failure preparing request") |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | resp, err := client.CopySender(req) |
| 67 | if err != nil { |
| 68 | result.Response = autorest.Response{Response: resp} |
| 69 | err = autorest.NewErrorWithError(err, "files.Client", "Copy", resp, "Failure sending request") |
| 70 | return |
| 71 | } |
| 72 | |
| 73 | result, err = client.CopyResponder(resp) |
| 74 | if err != nil { |
| 75 | err = autorest.NewErrorWithError(err, "files.Client", "Copy", resp, "Failure responding to request") |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | // CopyPreparer prepares the Copy request. |
| 83 | func (client Client) CopyPreparer(ctx context.Context, accountName, shareName, path, fileName string, input CopyInput) (*http.Request, error) { |
| 84 | if path != "" { |
| 85 | path = fmt.Sprintf("%s/", path) |
| 86 | } |
| 87 | pathParameters := map[string]interface{}{ |
| 88 | "shareName": autorest.Encode("path", shareName), |
| 89 | "directory": autorest.Encode("path", path), |
| 90 | "fileName": autorest.Encode("path", fileName), |
| 91 | } |
| 92 | |
| 93 | headers := map[string]interface{}{ |
| 94 | "x-ms-version": APIVersion, |
| 95 | "x-ms-copy-source": input.CopySource, |
| 96 | } |
| 97 | |
| 98 | headers = metadata.SetIntoHeaders(headers, input.MetaData) |
| 99 | |
| 100 | preparer := autorest.CreatePreparer( |
| 101 | autorest.AsContentType("application/xml; charset=utf-8"), |
| 102 | autorest.AsPut(), |
| 103 | autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)), |
| 104 | autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters), |
| 105 | autorest.WithHeaders(headers)) |
| 106 | return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 107 | } |
| 108 | |
| 109 | // CopySender sends the Copy request. The method will close the |
| 110 | // http.Response Body if it receives an error. |
| 111 | func (client Client) CopySender(req *http.Request) (*http.Response, error) { |
| 112 | return autorest.SendWithSender(client, req, |
| 113 | azure.DoRetryWithRegistration(client.Client)) |
| 114 | } |
| 115 | |
| 116 | // CopyResponder handles the response to the Copy request. The method always |
| 117 | // closes the http.Response Body. |
| 118 | func (client Client) CopyResponder(resp *http.Response) (result CopyResult, err error) { |
| 119 | if resp != nil && resp.Header != nil { |
| 120 | result.CopyID = resp.Header.Get("x-ms-copy-id") |
| 121 | result.CopySuccess = resp.Header.Get("x-ms-copy-status") |
| 122 | } |
| 123 | |
| 124 | err = autorest.Respond( |
| 125 | resp, |
| 126 | client.ByInspecting(), |
| 127 | azure.WithErrorUnlessStatusCode(http.StatusAccepted), |
| 128 | autorest.ByClosing()) |
| 129 | result.Response = autorest.Response{Response: resp} |
| 130 | |
| 131 | return |
| 132 | } |