@@ -136,11 +136,73 @@ public String url() {
136
136
* successfully synced with the server.
137
137
*/
138
138
/* package for tests */ byte [] data ;
139
+ /* package for tests */ File file ;
139
140
140
141
/* package for tests */ final TaskQueue taskQueue = new TaskQueue ();
141
142
private Set <Task <?>.TaskCompletionSource > currentTasks = Collections .synchronizedSet (
142
143
new HashSet <Task <?>.TaskCompletionSource >());
143
144
145
+ /**
146
+ * Creates a new file from a file pointer, file name, and content type. Content type will be used
147
+ * instead of auto-detection by file extension.
148
+ *
149
+ * @param name
150
+ * The file's name, ideally with extension. The file name must begin with an alphanumeric
151
+ * character, and consist of alphanumeric characters, periods, spaces, underscores, or
152
+ * dashes.
153
+ * @param file
154
+ * The file.
155
+ * @param contentType
156
+ * The file's content type.
157
+ */
158
+ public ParseFile (String name , File file , String contentType ) {
159
+ this (new State .Builder ().name (name ).mimeType (contentType ).build ());
160
+ if (file .length () > MAX_FILE_SIZE ) {
161
+ throw new IllegalArgumentException (String .format ("ParseFile must be less than %d bytes" ,
162
+ MAX_FILE_SIZE ));
163
+ }
164
+ this .file = file ;
165
+ }
166
+
167
+ /**
168
+ * Creates a new file from a file pointer.
169
+ *
170
+ * @param file
171
+ * The file.
172
+ */
173
+ public ParseFile (File file ) {
174
+ this (null , file , null );
175
+ }
176
+
177
+ /**
178
+ * Creates a new file from a file pointer and a name. Giving a name with a proper file extension
179
+ * (e.g. ".png") is ideal because it allows Parse to deduce the content type of the file and set
180
+ * appropriate HTTP headers when it is fetched.
181
+ *
182
+ * @param name
183
+ * The file's name, ideally with extension. The file name must begin with an alphanumeric
184
+ * character, and consist of alphanumeric characters, periods, spaces, underscores, or
185
+ * dashes.
186
+ * @param file
187
+ * The file.
188
+ */
189
+ public ParseFile (String name , File file ) {
190
+ this (name , file , null );
191
+ }
192
+
193
+ /**
194
+ * Creates a new file from a file pointer, and content type. Content type will be used instead of
195
+ * auto-detection by file extension.
196
+ *
197
+ * @param file
198
+ * The file.
199
+ * @param contentType
200
+ * The file's content type.
201
+ */
202
+ public ParseFile (File file , String contentType ) {
203
+ this (null , file , contentType );
204
+ }
205
+
144
206
/**
145
207
* Creates a new file from a byte array, file name, and content type. Content type will be used
146
208
* instead of auto-detection by file extension.
@@ -275,12 +337,21 @@ public Task<Void> then(Task<Void> task) throws Exception {
275
337
return Task .cancelled ();
276
338
}
277
339
278
- return getFileController ().saveAsync (
279
- state ,
280
- data ,
281
- sessionToken ,
282
- progressCallbackOnMainThread (uploadProgressCallback ),
283
- cancellationToken ).onSuccessTask (new Continuation <State , Task <Void >>() {
340
+ Task <ParseFile .State > saveTask = data != null ?
341
+ getFileController ().saveAsync (
342
+ state ,
343
+ data ,
344
+ sessionToken ,
345
+ progressCallbackOnMainThread (uploadProgressCallback ),
346
+ cancellationToken ) :
347
+ getFileController ().saveAsync (
348
+ state ,
349
+ file ,
350
+ sessionToken ,
351
+ progressCallbackOnMainThread (uploadProgressCallback ),
352
+ cancellationToken );
353
+
354
+ return saveTask .onSuccessTask (new Continuation <State , Task <Void >>() {
284
355
@ Override
285
356
public Task <Void > then (Task <State > task ) throws Exception {
286
357
state = task .getResult ();
@@ -300,6 +371,10 @@ public Task<Void> then(Task<State> task) throws Exception {
300
371
* @return A Task that will be resolved when the save completes.
301
372
*/
302
373
public Task <Void > saveInBackground (final ProgressCallback uploadProgressCallback ) {
374
+ if (data != null && file != null ) {
375
+ throw new IllegalArgumentException ("File and data can not be set at the same time" );
376
+ }
377
+
303
378
final Task <Void >.TaskCompletionSource cts = Task .create ();
304
379
currentTasks .add (cts );
305
380
0 commit comments