Debian Codegraph @

storage/2018-11-09/file/files/create.go

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