Skip to content

Commit c5e462d

Browse files
committed
Add upload stream api
1 parent 9d6163b commit c5e462d

File tree

1 file changed

+81
-6
lines changed

1 file changed

+81
-6
lines changed

Parse/src/main/java/com/parse/ParseFile.java

+81-6
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,73 @@ public String url() {
136136
* successfully synced with the server.
137137
*/
138138
/* package for tests */ byte[] data;
139+
/* package for tests */ File file;
139140

140141
/* package for tests */ final TaskQueue taskQueue = new TaskQueue();
141142
private Set<Task<?>.TaskCompletionSource> currentTasks = Collections.synchronizedSet(
142143
new HashSet<Task<?>.TaskCompletionSource>());
143144

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+
144206
/**
145207
* Creates a new file from a byte array, file name, and content type. Content type will be used
146208
* instead of auto-detection by file extension.
@@ -275,12 +337,21 @@ public Task<Void> then(Task<Void> task) throws Exception {
275337
return Task.cancelled();
276338
}
277339

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>>() {
284355
@Override
285356
public Task<Void> then(Task<State> task) throws Exception {
286357
state = task.getResult();
@@ -300,6 +371,10 @@ public Task<Void> then(Task<State> task) throws Exception {
300371
* @return A Task that will be resolved when the save completes.
301372
*/
302373
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+
303378
final Task<Void>.TaskCompletionSource cts = Task.create();
304379
currentTasks.add(cts);
305380

0 commit comments

Comments
 (0)