10
10
using System . Linq ;
11
11
using System . Net ;
12
12
using System . Net . Http . Headers ;
13
+ using System . Text ;
13
14
using System . Threading . Tasks ;
14
15
15
16
namespace Box . V2 . Managers
@@ -81,6 +82,47 @@ public async Task<Uri> GetDownloadUriAsync(string id, string versionId = null)
81
82
return locationUri ;
82
83
}
83
84
85
+ /// <summary>
86
+ /// Verify that a file will be accepted by Box before you send all the bytes over the wire.
87
+ /// </summary>
88
+ /// <param name="preflightCheckRequest"></param>
89
+ /// <returns></returns>
90
+ public async Task PreflightCheck ( BoxPreflightCheckRequest preflightCheckRequest )
91
+ {
92
+ preflightCheckRequest . ThrowIfNull ( "preflightCheckRequest" )
93
+ . Name . ThrowIfNullOrWhiteSpace ( "preflightCheckRequest.Name" ) ;
94
+ preflightCheckRequest . Parent . ThrowIfNull ( "preflightCheckRequest.Parent" )
95
+ . Id . ThrowIfNullOrWhiteSpace ( "preflightCheckRequest.Parent.Id" ) ;
96
+
97
+ BoxRequest request = new BoxRequest ( _config . FilesPreflightCheckUri )
98
+ . Method ( RequestMethod . Options ) ;
99
+
100
+ request . Payload = _converter . Serialize ( preflightCheckRequest ) ;
101
+ request . ContentType = Constants . RequestParameters . ContentTypeJson ;
102
+
103
+ IBoxResponse < BoxEntity > response = await ToResponseAsync < BoxEntity > ( request ) . ConfigureAwait ( false ) ;
104
+ }
105
+
106
+ /// <summary>
107
+ /// Verify that a new version of a file will be accepted by Box before you send all the bytes over the wire.
108
+ /// </summary>
109
+ /// <param name="fileId"></param>
110
+ /// <param name="preflightCheckRequest"></param>
111
+ /// <returns></returns>
112
+ public async Task PreflightCheckNewVersion ( string fileId , BoxPreflightCheckRequest preflightCheckRequest )
113
+ {
114
+ if ( preflightCheckRequest . Size <= 0 )
115
+ throw new ArgumentException ( "Size in bytes must be greater than zero (otherwise preflight check for new version would always succeed)" , "sizeinBytes" ) ;
116
+
117
+ BoxRequest request = new BoxRequest ( new Uri ( string . Format ( Constants . FilesPreflightCheckNewVersionString , fileId ) ) )
118
+ . Method ( RequestMethod . Options ) ;
119
+
120
+ request . Payload = _converter . Serialize ( preflightCheckRequest ) ;
121
+ request . ContentType = Constants . RequestParameters . ContentTypeJson ;
122
+
123
+ IBoxResponse < BoxEntity > response = await ToResponseAsync < BoxEntity > ( request ) . ConfigureAwait ( false ) ;
124
+ }
125
+
84
126
/// <summary>
85
127
/// Uploads a provided file to the target parent folder
86
128
/// If the file already exists, an error will be thrown.
@@ -90,15 +132,20 @@ public async Task<Uri> GetDownloadUriAsync(string id, string versionId = null)
90
132
/// <param name="stream"></param>
91
133
/// <param name="fields"></param>
92
134
/// <param name="timeout"></param>
135
+ /// <param name="contentMD5"></param>
136
+ /// <param name="setStreamPositionToZero"></param>
93
137
/// <returns></returns>
94
- public async Task < BoxFile > UploadAsync ( BoxFileRequest fileRequest , Stream stream , List < string > fields = null , TimeSpan ? timeout = null )
138
+ public async Task < BoxFile > UploadAsync ( BoxFileRequest fileRequest , Stream stream , List < string > fields = null , TimeSpan ? timeout = null , byte [ ] contentMD5 = null , bool setStreamPositionToZero = true )
95
139
{
96
140
stream . ThrowIfNull ( "stream" ) ;
97
141
fileRequest . ThrowIfNull ( "fileRequest" )
98
142
. Name . ThrowIfNullOrWhiteSpace ( "filedRequest.Name" ) ;
99
143
fileRequest . Parent . ThrowIfNull ( "fileRequest.Parent" )
100
144
. Id . ThrowIfNullOrWhiteSpace ( "fileRequest.Parent.Id" ) ;
101
145
146
+ if ( setStreamPositionToZero )
147
+ stream . Position = 0 ;
148
+
102
149
BoxMultiPartRequest request = new BoxMultiPartRequest ( _config . FilesUploadEndpointUri ) { Timeout = timeout }
103
150
. Param ( ParamFields , fields )
104
151
. FormPart ( new BoxStringFormPart ( )
@@ -113,6 +160,9 @@ public async Task<BoxFile> UploadAsync(BoxFileRequest fileRequest, Stream stream
113
160
FileName = fileRequest . Name
114
161
} ) ;
115
162
163
+ if ( contentMD5 != null )
164
+ request . Header ( Constants . RequestParameters . ContentMD5 , HexStringFromBytes ( contentMD5 ) ) ;
165
+
116
166
IBoxResponse < BoxCollection < BoxFile > > response = await ToResponseAsync < BoxCollection < BoxFile > > ( request ) . ConfigureAwait ( false ) ;
117
167
118
168
// We can only upload one file at a time, so return the first entry
@@ -126,15 +176,22 @@ public async Task<BoxFile> UploadAsync(BoxFileRequest fileRequest, Stream stream
126
176
/// A proper timeout should be provided for large uploads
127
177
/// </summary>
128
178
/// <param name="fileName"></param>
179
+ /// <param name="fileId"
129
180
/// <param name="stream"></param>
130
181
/// <param name="etag"></param>
182
+ /// <param name="fields"></param>
131
183
/// <param name="timeout"></param>
184
+ /// <param name="contentMD5"></param>
185
+ /// <param name="setStreamPositionToZero"></param>
132
186
/// <returns></returns>
133
- public async Task < BoxFile > UploadNewVersionAsync ( string fileName , string fileId , Stream stream , string etag = null , List < string > fields = null , TimeSpan ? timeout = null )
187
+ public async Task < BoxFile > UploadNewVersionAsync ( string fileName , string fileId , Stream stream , string etag = null , List < string > fields = null , TimeSpan ? timeout = null , byte [ ] contentMD5 = null , bool setStreamPositionToZero = true )
134
188
{
135
189
stream . ThrowIfNull ( "stream" ) ;
136
190
fileName . ThrowIfNullOrWhiteSpace ( "fileName" ) ;
137
191
192
+ if ( setStreamPositionToZero )
193
+ stream . Position = 0 ;
194
+
138
195
BoxMultiPartRequest request = new BoxMultiPartRequest ( new Uri ( string . Format ( Constants . FilesNewVersionEndpointString , fileId ) ) ) { Timeout = timeout }
139
196
. Header ( "If-Match" , etag )
140
197
. Param ( ParamFields , fields )
@@ -145,12 +202,26 @@ public async Task<BoxFile> UploadNewVersionAsync(string fileName, string fileId,
145
202
FileName = fileName
146
203
} ) ;
147
204
205
+ if ( contentMD5 != null )
206
+ request . Header ( Constants . RequestParameters . ContentMD5 , HexStringFromBytes ( contentMD5 ) ) ;
207
+
148
208
IBoxResponse < BoxCollection < BoxFile > > response = await ToResponseAsync < BoxCollection < BoxFile > > ( request ) . ConfigureAwait ( false ) ;
149
209
150
210
// We can only upload one file at a time, so return the first entry
151
211
return response . ResponseObject . Entries . FirstOrDefault ( ) ;
152
212
}
153
213
214
+ private string HexStringFromBytes ( byte [ ] bytes )
215
+ {
216
+ var sb = new StringBuilder ( ) ;
217
+ foreach ( byte b in bytes )
218
+ {
219
+ var hex = b . ToString ( "x2" ) ;
220
+ sb . Append ( hex ) ;
221
+ }
222
+ return sb . ToString ( ) ;
223
+ }
224
+
154
225
/// <summary>
155
226
/// If there are previous versions of this file, this method can be used to retrieve metadata about the older versions.
156
227
/// <remarks>Versions are only tracked for Box users with premium accounts.</remarks>
0 commit comments