| 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 SetPropertiesInput struct { |
| 18 |
// Resizes a file to the specified size. |
| 19 |
// If the specified byte value is less than the current size of the file, |
| 20 |
// then all ranges above the specified byte value are cleared. |
| 21 |
ContentLength int64 |
| 22 |
|
| 23 |
// Modifies the cache control string for the file. |
| 24 |
// If this property is not specified on the request, then the property will be cleared for the file. |
| 25 |
// Subsequent calls to Get File Properties will not return this property, |
| 26 |
// unless it is explicitly set on the file again. |
| 27 |
ContentControl *string |
| 28 |
|
| 29 |
// Sets the file’s Content-Disposition header. |
| 30 |
// If this property is not specified on the request, then the property will be cleared for the file. |
| 31 |
// Subsequent calls to Get File Properties will not return this property, |
| 32 |
// unless it is explicitly set on the file again. |
| 33 |
ContentDisposition *string |
| 34 |
|
| 35 |
// Sets the file's content encoding. |
| 36 |
// If this property is not specified on the request, then the property will be cleared for the file. |
| 37 |
// Subsequent calls to Get File Properties will not return this property, |
| 38 |
// unless it is explicitly set on the file again. |
| 39 |
ContentEncoding *string |
| 40 |
|
| 41 |
// Sets the file's content language. |
| 42 |
// If this property is not specified on the request, then the property will be cleared for the file. |
| 43 |
// Subsequent calls to Get File Properties will not return this property, |
| 44 |
// unless it is explicitly set on the file again. |
| 45 |
ContentLanguage *string |
| 46 |
|
| 47 |
// Sets the file's MD5 hash. |
| 48 |
// If this property is not specified on the request, then the property will be cleared for the file. |
| 49 |
// Subsequent calls to Get File Properties will not return this property, |
| 50 |
// unless it is explicitly set on the file again. |
| 51 |
ContentMD5 *string |
| 52 |
|
| 53 |
// Sets the file's content type. |
| 54 |
// If this property is not specified on the request, then the property will be cleared for the file. |
| 55 |
// Subsequent calls to Get File Properties will not return this property, |
| 56 |
// unless it is explicitly set on the file again. |
| 57 |
ContentType *string |
| 58 |
|
| 59 |
// The time at which this file was created at - if omitted, this'll be set to "now" |
| 60 |
// This maps to the `x-ms-file-creation-time` field. |
| 61 |
CreatedAt *time.Time |
| 62 |
|
| 63 |
// The time at which this file was last modified - if omitted, this'll be set to "now" |
| 64 |
// This maps to the `x-ms-file-last-write-time` field. |
| 65 |
LastModified *time.Time |
| 66 |
|
| 67 |
// MetaData is a mapping of key value pairs which should be assigned to this file |
| 68 |
MetaData map[string]string |
| 69 |
} |
| 70 |
|
| 71 |
// SetProperties sets the specified properties on the specified File |
| 72 |
func (client Client) SetProperties(ctx context.Context, accountName, shareName, path, fileName string, input SetPropertiesInput) (result autorest.Response, err error) { |
| 73 |
if accountName == "" { |
| 74 |
return result, validation.NewError("files.Client", "SetProperties", "`accountName` cannot be an empty string.") |
| 75 |
} |
| 76 |
if shareName == "" { |
| 77 |
return result, validation.NewError("files.Client", "SetProperties", "`shareName` cannot be an empty string.") |
| 78 |
} |
| 79 |
if strings.ToLower(shareName) != shareName { |
| 80 |
return result, validation.NewError("files.Client", "SetProperties", "`shareName` must be a lower-cased string.") |
| 81 |
} |
| 82 |
if fileName == "" { |
| 83 |
return result, validation.NewError("files.Client", "SetProperties", "`fileName` cannot be an empty string.") |
| 84 |
} |
| 85 |
|
| 86 |
req, err := client.SetPropertiesPreparer(ctx, accountName, shareName, path, fileName, input) |
| 87 |
if err != nil { |
| 88 |
err = autorest.NewErrorWithError(err, "files.Client", "SetProperties", nil, "Failure preparing request") |
| 89 |
return |
| 90 |
} |
| 91 |
|
| 92 |
resp, err := client.SetPropertiesSender(req) |
| 93 |
if err != nil { |
| 94 |
result = autorest.Response{Response: resp} |
| 95 |
err = autorest.NewErrorWithError(err, "files.Client", "SetProperties", resp, "Failure sending request") |
| 96 |
return |
| 97 |
} |
| 98 |
|
| 99 |
result, err = client.SetPropertiesResponder(resp) |
| 100 |
if err != nil { |
| 101 |
err = autorest.NewErrorWithError(err, "files.Client", "SetProperties", resp, "Failure responding to request") |
| 102 |
return |
| 103 |
} |
| 104 |
|
| 105 |
return |
| 106 |
} |
| 107 |
|
| 108 |
// SetPropertiesPreparer prepares the SetProperties request. |
| 109 |
func (client Client) SetPropertiesPreparer(ctx context.Context, accountName, shareName, path, fileName string, input SetPropertiesInput) (*http.Request, error) { |
| 110 |
if path != "" { |
| 111 |
path = fmt.Sprintf("%s/", path) |
| 112 |
} |
| 113 |
pathParameters := map[string]interface{}{ |
| 114 |
"shareName": autorest.Encode("path", shareName), |
| 115 |
"directory": autorest.Encode("path", path), |
| 116 |
"fileName": autorest.Encode("path", fileName), |
| 117 |
} |
| 118 |
|
| 119 |
var coalesceDate = func(input *time.Time, defaultVal string) string { |
| 120 |
if input == nil { |
| 121 |
return defaultVal |
| 122 |
} |
| 123 |
|
| 124 |
return input.Format(time.RFC1123) |
| 125 |
} |
| 126 |
|
| 127 |
headers := map[string]interface{}{ |
| 128 |
"x-ms-version": APIVersion, |
| 129 |
"x-ms-type": "file", |
| 130 |
|
| 131 |
"x-ms-content-length": input.ContentLength, |
| 132 |
"x-ms-file-permission": "inherit", // TODO: expose this in future |
| 133 |
"x-ms-file-attributes": "None", // TODO: expose this in future |
| 134 |
"x-ms-file-creation-time": coalesceDate(input.CreatedAt, "now"), |
| 135 |
"x-ms-file-last-write-time": coalesceDate(input.LastModified, "now"), |
| 136 |
} |
| 137 |
|
| 138 |
if input.ContentControl != nil { |
| 139 |
headers["x-ms-cache-control"] = *input.ContentControl |
| 140 |
} |
| 141 |
if input.ContentDisposition != nil { |
| 142 |
headers["x-ms-content-disposition"] = *input.ContentDisposition |
| 143 |
} |
| 144 |
if input.ContentEncoding != nil { |
| 145 |
headers["x-ms-content-encoding"] = *input.ContentEncoding |
| 146 |
} |
| 147 |
if input.ContentLanguage != nil { |
| 148 |
headers["x-ms-content-language"] = *input.ContentLanguage |
| 149 |
} |
| 150 |
if input.ContentMD5 != nil { |
| 151 |
headers["x-ms-content-md5"] = *input.ContentMD5 |
| 152 |
} |
| 153 |
if input.ContentType != nil { |
| 154 |
headers["x-ms-content-type"] = *input.ContentType |
| 155 |
} |
| 156 |
|
| 157 |
headers = metadata.SetIntoHeaders(headers, input.MetaData) |
| 158 |
|
| 159 |
preparer := autorest.CreatePreparer( |
| 160 |
autorest.AsContentType("application/xml; charset=utf-8"), |
| 161 |
autorest.AsPut(), |
| 162 |
autorest.WithBaseURL(endpoints.GetFileEndpoint(client.BaseURI, accountName)), |
| 163 |
autorest.WithPathParameters("/{shareName}/{directory}{fileName}", pathParameters), |
| 164 |
autorest.WithHeaders(headers)) |
| 165 |
return preparer.Prepare((&http.Request{}).WithContext(ctx)) |
| 166 |
} |
| 167 |
|
| 168 |
// SetPropertiesSender sends the SetProperties request. The method will close the |
| 169 |
// http.Response Body if it receives an error. |
| 170 |
func (client Client) SetPropertiesSender(req *http.Request) (*http.Response, error) { |
| 171 |
return autorest.SendWithSender(client, req, |
| 172 |
azure.DoRetryWithRegistration(client.Client)) |
| 173 |
} |
| 174 |
|
| 175 |
// SetPropertiesResponder handles the response to the SetProperties request. The method always |
| 176 |
// closes the http.Response Body. |
| 177 |
func (client Client) SetPropertiesResponder(resp *http.Response) (result autorest.Response, err error) { |
| 178 |
err = autorest.Respond( |
| 179 |
resp, |
| 180 |
client.ByInspecting(), |
| 181 |
azure.WithErrorUnlessStatusCode(http.StatusCreated), |
| 182 |
autorest.ByClosing()) |
| 183 |
result = autorest.Response{Response: resp} |
| 184 |
|
| 185 |
return |
| 186 |
} |