| 1 |
package files |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net/http" |
| 7 |
"strings" |
| 8 |
"time" |
| 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 CreateInput struct { |
| 18 |
// This header specifies the maximum size for the file, up to 1 TiB. |
| 19 |
ContentLength int64 |
| 20 |
|
| 21 |
// The MIME content type of the file |
| 22 |
// If not specified, the default type is application/octet-stream. |
| 23 |
ContentType *string |
| 24 |
|
| 25 |
// Specifies which content encodings have been applied to the file. |
| 26 |
// This value is returned to the client when the Get File operation is performed |
| 27 |
// on the file resource and can be used to decode file content. |
| 28 |
ContentEncoding *string |
| 29 |
|
| 30 |
// Specifies the natural languages used by this resource. |
| 31 |
ContentLanguage *string |
| 32 |
|
| 33 |
// The File service stores this value but does not use or modify it. |
| 34 |
CacheControl *string |
| 35 |
|
| 36 |
// Sets the file's MD5 hash. |
| 37 |
ContentMD5 *string |
| 38 |
|
| 39 |
// Sets the file’s Content-Disposition header. |
| 40 |
ContentDisposition *string |
| 41 |
|
| 42 |
// The time at which this file was created at - if omitted, this'll be set to "now" |
| 43 |
// This maps to the `x-ms-file-creation-time` field. |
| 44 |
CreatedAt *time.Time |
| 45 |
|
| 46 |
// The time at which this file was last modified - if omitted, this'll be set to "now" |
| 47 |
// This maps to the `x-ms-file-last-write-time` field. |
| 48 |
LastModified *time.Time |
| 49 |
|
| 50 |
// MetaData is a mapping of key value pairs which should be assigned to this file |
| 51 |
MetaData map[string]string |
| 52 |
} |
| 53 |
|
| 54 |
// Create creates a new file or replaces a file. |
| 55 |
func (client Client) Create(ctx context.Context, accountName, shareName, path, fileName string, input CreateInput) (result autorest.Response, err error) { |
| 56 |
if accountName == "" { |
| 57 |
return result, validation.NewError("files.Client", "Create", "`accountName` cannot be an empty string.") |
| 58 |
} |
| 59 |
if shareName == "" { |
| 60 |
return result, validation.NewError("files.Client", "Create", "`shareName` cannot be an empty string.") |
| 61 |
} |
| 62 |
if strings.ToLower(shareName) != shareName { |
| 63 |
return result, validation.NewError("files.Client", "Create", "`shareName` must be a lower-cased string.") |
| 64 |
} |
| 65 |
if fileName == "" { |
| 66 |
return result, validation.NewError("files.Client", "Create", "`fileName` cannot be an empty string.") |
| 67 |
} |
| 68 |
if err := metadata.Validate(input.MetaData); err != nil { |
| 69 |
return result, validation.NewError("files.Client", "Create", "`input.MetaData` cannot be an empty string.") |
| 70 |
} |
| 71 |
|
| 72 |
req, err := client.CreatePreparer(ctx, accountName, shareName, path, fileName, input) |
| 73 |
if err != nil { |
| 74 |
err = autorest.NewErrorWithError(err, "files.Client", "Create", nil, "Failure preparing request") |
| 75 |
return |
| 76 |
} |
| 77 |
|
| 78 |
resp, err := client.CreateSender(req) |
| 79 |
if err != nil { |
| 80 |
result = autorest.Response{Response: resp} |
| 81 |
err = autorest.NewErrorWithError(err, "files.Client", "Create", resp, "Failure sending request") |
| 82 |
return |
| 83 |
} |
| 84 |
|
| 85 |
result, err = client.CreateResponder(resp) |
| 86 |
if err != nil { |
| 87 |
err = autorest.NewErrorWithError(err, "files.Client", "Create", resp, "Failure responding to request") |
| 88 |
return |
| 89 |
} |
| 90 |
|
| 91 |
return |
| 92 |
} |
| 93 |
|
| 94 |
// CreatePreparer prepares the Create request. |
| 95 |
func (client Client) CreatePreparer(ctx context.Context, accountName, shareName, path, fileName string, input CreateInput) (*http.Request, error) { |
| 96 |
if path != "" { |
| 97 |
path = fmt.Sprintf("%s/", path) |
| 98 |
} |
| 99 |
pathParameters := map[string]interface{}{ |
| 100 |
"shareName": autorest.Encode("path", shareName), |
| 101 |
"directory": autorest.Encode("path", path), |
| 102 |
"fileName": autorest.Encode("path", fileName), |
| 103 |
} |
| 104 |
|
| 105 |
var coalesceDate = func(input *time.Time, defaultVal string) string { |
| 106 |
if input == nil { |
| 107 |
return defaultVal |
| 108 |
} |
| 109 |
|
| 110 |
return input.Format(time.RFC1123) |
| 111 |
} |
| 112 |
|
| 113 |
headers := map[string]interface{}{ |
| 114 |
"x-ms-version": APIVersion, |
| 115 |
"x-ms-content-length": input.ContentLength, |
| 116 |
"x-ms-type": "file", |
| 117 |
|
| 118 |
"x-ms-file-permission": "inherit", // TODO: expose this in future |
| 119 |
"x-ms-file-attributes": "None", // TODO: expose this in future |
| 120 |
"x-ms-file-creation-time": coalesceDate(input.CreatedAt, "now"), |
| 121 |
"x-ms-file-last-write-time": coalesceDate(input.LastModified, "now"), |
| 122 |
} |
| 123 |
|
| 124 |
if input.ContentDisposition != nil { |
| 125 |
headers["x-ms-content-disposition"] = *input.ContentDisposition |
| 126 |
} |
| 127 |
|
| 128 |
if input.ContentEncoding != nil { |
| 129 |
headers["x-ms-content-encoding"] = *input.ContentEncoding |
| 130 |
} |
| 131 |
|
| 132 |
if input.ContentMD5 != nil { |
| 133 |
headers["x-ms-content-md5"] = *input.ContentMD5 |
| 134 |
} |
| 135 |
|
| 136 |
if input.ContentType != nil { |
| 137 |
headers["x-ms-content-type"] = *input.ContentType |
| 138 |
} |
| 139 |
|
| 140 |
headers = metadata.SetIntoHeaders(headers, input.MetaData) |
| 141 |
|
| 142 |
preparer := autorest.CreatePreparer( |
| 143 |
autorest.AsContentType("application/xml; charset=utf-8"), |
| 144 |
autorest.AsPut(), |
| 145 |
autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)), |
| 146 |
autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters), |
| 147 |
autorest.WithHeaders(headers)) |
| 148 |
return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 149 |
} |
| 150 |
|
| 151 |
// CreateSender sends the Create request. The method will close the |
| 152 |
// http.Response Body if it receives an error. |
| 153 |
func (client Client) CreateSender(req *http.Request) (*http.Response, error) { |
| 154 |
return autorest.SendWithSender(client, req, |
| 155 |
azure.DoRetryWithRegistration(client.Client)) |
| 156 |
} |
| 157 |
|
| 158 |
// CreateResponder handles the response to the Create request. The method always |
| 159 |
// closes the http.Response Body. |
| 160 |
func (client Client) CreateResponder(resp *http.Response) (result autorest.Response, err error) { |
| 161 |
err = autorest.Respond( |
| 162 |
resp, |
| 163 |
client.ByInspecting(), |
| 164 |
azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 165 |
autorest.ByClosing()) |
| 166 |
result = autorest.Response{Response: resp} |
| 167 |
|
| 168 |
return |
| 169 |
} |