1package azblob
2
3import (
4	"context"
5	"io"
6	"net/http"
7	"time"
8)
9
10// BlobHTTPHeaders contains read/writeable blob properties.
11type BlobHTTPHeaders struct {
12	ContentType        string
13	ContentMD5         []byte
14	ContentEncoding    string
15	ContentLanguage    string
16	ContentDisposition string
17	CacheControl       string
18}
19
20// NewHTTPHeaders returns the user-modifiable properties for this blob.
21func (bgpr BlobGetPropertiesResponse) NewHTTPHeaders() BlobHTTPHeaders {
22	return BlobHTTPHeaders{
23		ContentType:        bgpr.ContentType(),
24		ContentEncoding:    bgpr.ContentEncoding(),
25		ContentLanguage:    bgpr.ContentLanguage(),
26		ContentDisposition: bgpr.ContentDisposition(),
27		CacheControl:       bgpr.CacheControl(),
28		ContentMD5:         bgpr.ContentMD5(),
29	}
30}
31
32///////////////////////////////////////////////////////////////////////////////
33
34// NewHTTPHeaders returns the user-modifiable properties for this blob.
35func (dr downloadResponse) NewHTTPHeaders() BlobHTTPHeaders {
36	return BlobHTTPHeaders{
37		ContentType:        dr.ContentType(),
38		ContentEncoding:    dr.ContentEncoding(),
39		ContentLanguage:    dr.ContentLanguage(),
40		ContentDisposition: dr.ContentDisposition(),
41		CacheControl:       dr.CacheControl(),
42		ContentMD5:         dr.ContentMD5(),
43	}
44}
45
46///////////////////////////////////////////////////////////////////////////////
47
48// DownloadResponse wraps AutoRest generated downloadResponse and helps to provide info for retry.
49type DownloadResponse struct {
50	r       *downloadResponse
51	ctx     context.Context
52	b       BlobURL
53	getInfo HTTPGetterInfo
54}
55
56// Body constructs new RetryReader stream for reading data. If a connection failes
57// while reading, it will make additional requests to reestablish a connection and
58// continue reading. Specifying a RetryReaderOption's with MaxRetryRequests set to 0
59// (the default), returns the original response body and no retries will be performed.
60func (r *DownloadResponse) Body(o RetryReaderOptions) io.ReadCloser {
61	if o.MaxRetryRequests == 0 { // No additional retries
62		return r.Response().Body
63	}
64	return NewRetryReader(r.ctx, r.Response(), r.getInfo, o,
65		func(ctx context.Context, getInfo HTTPGetterInfo) (*http.Response, error) {
66			resp, err := r.b.Download(ctx, getInfo.Offset, getInfo.Count,
67				BlobAccessConditions{
68					ModifiedAccessConditions: ModifiedAccessConditions{IfMatch: getInfo.ETag},
69				},
70				false)
71			if err != nil {
72				return nil, err
73			}
74			return resp.Response(), err
75		},
76	)
77}
78
79// Response returns the raw HTTP response object.
80func (r DownloadResponse) Response() *http.Response {
81	return r.r.Response()
82}
83
84// NewHTTPHeaders returns the user-modifiable properties for this blob.
85func (r DownloadResponse) NewHTTPHeaders() BlobHTTPHeaders {
86	return r.r.NewHTTPHeaders()
87}
88
89// BlobContentMD5 returns the value for header x-ms-blob-content-md5.
90func (r DownloadResponse) BlobContentMD5() []byte {
91	return r.r.BlobContentMD5()
92}
93
94// ContentMD5 returns the value for header Content-MD5.
95func (r DownloadResponse) ContentMD5() []byte {
96	return r.r.ContentMD5()
97}
98
99// StatusCode returns the HTTP status code of the response, e.g. 200.
100func (r DownloadResponse) StatusCode() int {
101	return r.r.StatusCode()
102}
103
104// Status returns the HTTP status message of the response, e.g. "200 OK".
105func (r DownloadResponse) Status() string {
106	return r.r.Status()
107}
108
109// AcceptRanges returns the value for header Accept-Ranges.
110func (r DownloadResponse) AcceptRanges() string {
111	return r.r.AcceptRanges()
112}
113
114// BlobCommittedBlockCount returns the value for header x-ms-blob-committed-block-count.
115func (r DownloadResponse) BlobCommittedBlockCount() int32 {
116	return r.r.BlobCommittedBlockCount()
117}
118
119// BlobSequenceNumber returns the value for header x-ms-blob-sequence-number.
120func (r DownloadResponse) BlobSequenceNumber() int64 {
121	return r.r.BlobSequenceNumber()
122}
123
124// BlobType returns the value for header x-ms-blob-type.
125func (r DownloadResponse) BlobType() BlobType {
126	return r.r.BlobType()
127}
128
129// CacheControl returns the value for header Cache-Control.
130func (r DownloadResponse) CacheControl() string {
131	return r.r.CacheControl()
132}
133
134// ContentDisposition returns the value for header Content-Disposition.
135func (r DownloadResponse) ContentDisposition() string {
136	return r.r.ContentDisposition()
137}
138
139// ContentEncoding returns the value for header Content-Encoding.
140func (r DownloadResponse) ContentEncoding() string {
141	return r.r.ContentEncoding()
142}
143
144// ContentLanguage returns the value for header Content-Language.
145func (r DownloadResponse) ContentLanguage() string {
146	return r.r.ContentLanguage()
147}
148
149// ContentLength returns the value for header Content-Length.
150func (r DownloadResponse) ContentLength() int64 {
151	return r.r.ContentLength()
152}
153
154// ContentRange returns the value for header Content-Range.
155func (r DownloadResponse) ContentRange() string {
156	return r.r.ContentRange()
157}
158
159// ContentType returns the value for header Content-Type.
160func (r DownloadResponse) ContentType() string {
161	return r.r.ContentType()
162}
163
164// CopyCompletionTime returns the value for header x-ms-copy-completion-time.
165func (r DownloadResponse) CopyCompletionTime() time.Time {
166	return r.r.CopyCompletionTime()
167}
168
169// CopyID returns the value for header x-ms-copy-id.
170func (r DownloadResponse) CopyID() string {
171	return r.r.CopyID()
172}
173
174// CopyProgress returns the value for header x-ms-copy-progress.
175func (r DownloadResponse) CopyProgress() string {
176	return r.r.CopyProgress()
177}
178
179// CopySource returns the value for header x-ms-copy-source.
180func (r DownloadResponse) CopySource() string {
181	return r.r.CopySource()
182}
183
184// CopyStatus returns the value for header x-ms-copy-status.
185func (r DownloadResponse) CopyStatus() CopyStatusType {
186	return r.r.CopyStatus()
187}
188
189// CopyStatusDescription returns the value for header x-ms-copy-status-description.
190func (r DownloadResponse) CopyStatusDescription() string {
191	return r.r.CopyStatusDescription()
192}
193
194// Date returns the value for header Date.
195func (r DownloadResponse) Date() time.Time {
196	return r.r.Date()
197}
198
199// ETag returns the value for header ETag.
200func (r DownloadResponse) ETag() ETag {
201	return r.r.ETag()
202}
203
204// IsServerEncrypted returns the value for header x-ms-server-encrypted.
205func (r DownloadResponse) IsServerEncrypted() string {
206	return r.r.IsServerEncrypted()
207}
208
209// LastModified returns the value for header Last-Modified.
210func (r DownloadResponse) LastModified() time.Time {
211	return r.r.LastModified()
212}
213
214// LeaseDuration returns the value for header x-ms-lease-duration.
215func (r DownloadResponse) LeaseDuration() LeaseDurationType {
216	return r.r.LeaseDuration()
217}
218
219// LeaseState returns the value for header x-ms-lease-state.
220func (r DownloadResponse) LeaseState() LeaseStateType {
221	return r.r.LeaseState()
222}
223
224// LeaseStatus returns the value for header x-ms-lease-status.
225func (r DownloadResponse) LeaseStatus() LeaseStatusType {
226	return r.r.LeaseStatus()
227}
228
229// RequestID returns the value for header x-ms-request-id.
230func (r DownloadResponse) RequestID() string {
231	return r.r.RequestID()
232}
233
234// Version returns the value for header x-ms-version.
235func (r DownloadResponse) Version() string {
236	return r.r.Version()
237}
238
239// NewMetadata returns user-defined key/value pairs.
240func (r DownloadResponse) NewMetadata() Metadata {
241	return r.r.NewMetadata()
242}
243