| 1 | package files |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/Azure/go-autorest/autorest" |
| 10 | ) |
| 11 | |
| 12 | type CopyAndWaitResult struct { |
| 13 | autorest.Response |
| 14 | |
| 15 | CopyID string |
| 16 | } |
| 17 | |
| 18 | const DefaultCopyPollDuration = 15 * time.Second |
| 19 | |
| 20 | // CopyAndWait is a convenience method which doesn't exist in the API, which copies the file and then waits for the copy to complete |
| 21 | func (client Client) CopyAndWait(ctx context.Context, accountName, shareName, path, fileName string, input CopyInput, pollDuration time.Duration) (result CopyResult, err error) { |
| 22 | copy, e := client.Copy(ctx, accountName, shareName, path, fileName, input) |
| 23 | if err != nil { |
| 24 | result.Response = copy.Response |
| 25 | err = fmt.Errorf("Error copying: %s", e) |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | result.CopyID = copy.CopyID |
| 30 | |
| 31 | // since the API doesn't return a LRO, this is a hack which also polls every 10s, but should be sufficient |
| 32 | for true { |
| 33 | props, e := client.GetProperties(ctx, accountName, shareName, path, fileName) |
| 34 | if e != nil { |
| 35 | result.Response = copy.Response |
| 36 | err = fmt.Errorf("Error waiting for copy: %s", e) |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | switch strings.ToLower(props.CopyStatus) { |
| 41 | case "pending": |
| 42 | time.Sleep(pollDuration) |
| 43 | continue |
| 44 | |
| 45 | case "success": |
| 46 | return |
| 47 | |
| 48 | default: |
| 49 | err = fmt.Errorf("Unexpected CopyState %q", e) |
| 50 | return |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return |
| 55 | } |