Skip to content

Commit 632df8d

Browse files
committed
Revise constructor and unhold file pointer
1 parent d537280 commit 632df8d

File tree

2 files changed

+31
-68
lines changed

2 files changed

+31
-68
lines changed

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

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -140,52 +140,14 @@ public String url() {
140140
private Set<Task<?>.TaskCompletionSource> currentTasks = Collections.synchronizedSet(
141141
new HashSet<Task<?>.TaskCompletionSource>());
142142

143-
/**
144-
* Creates a new file from a file pointer, file name, and content type. Content type will be used
145-
* instead of auto-detection by file extension.
146-
*
147-
* @param name
148-
* The file's name, ideally with extension. The file name must begin with an alphanumeric
149-
* character, and consist of alphanumeric characters, periods, spaces, underscores, or
150-
* dashes.
151-
* @param file
152-
* The file.
153-
* @param contentType
154-
* The file's content type.
155-
*/
156-
public ParseFile(String name, File file, String contentType) {
157-
this(new State.Builder().name(name).mimeType(contentType).build());
158-
if (file.length() > MAX_FILE_SIZE) {
159-
throw new IllegalArgumentException(String.format("ParseFile must be less than %d bytes",
160-
MAX_FILE_SIZE));
161-
}
162-
this.file = file;
163-
}
164-
165143
/**
166144
* Creates a new file from a file pointer.
167145
*
168146
* @param file
169147
* The file.
170148
*/
171149
public ParseFile(File file) {
172-
this(null, file, null);
173-
}
174-
175-
/**
176-
* Creates a new file from a file pointer and a name. Giving a name with a proper file extension
177-
* (e.g. ".png") is ideal because it allows Parse to deduce the content type of the file and set
178-
* appropriate HTTP headers when it is fetched.
179-
*
180-
* @param name
181-
* The file's name, ideally with extension. The file name must begin with an alphanumeric
182-
* character, and consist of alphanumeric characters, periods, spaces, underscores, or
183-
* dashes.
184-
* @param file
185-
* The file.
186-
*/
187-
public ParseFile(String name, File file) {
188-
this(name, file, null);
150+
this(file, null);
189151
}
190152

191153
/**
@@ -198,7 +160,12 @@ public ParseFile(String name, File file) {
198160
* The file's content type.
199161
*/
200162
public ParseFile(File file, String contentType) {
201-
this(null, file, contentType);
163+
this(new State.Builder().name(file.getName()).mimeType(contentType).build());
164+
if (file.length() > MAX_FILE_SIZE) {
165+
throw new IllegalArgumentException(String.format("ParseFile must be less than %d bytes",
166+
MAX_FILE_SIZE));
167+
}
168+
this.file = file;
202169
}
203170

204171
/**
@@ -335,24 +302,30 @@ public Task<Void> then(Task<Void> task) throws Exception {
335302
return Task.cancelled();
336303
}
337304

338-
Task<ParseFile.State> saveTask = data != null ?
339-
getFileController().saveAsync(
340-
state,
341-
data,
342-
sessionToken,
343-
progressCallbackOnMainThread(uploadProgressCallback),
344-
cancellationToken) :
345-
getFileController().saveAsync(
346-
state,
347-
file,
348-
sessionToken,
349-
progressCallbackOnMainThread(uploadProgressCallback),
350-
cancellationToken);
305+
Task<ParseFile.State> saveTask;
306+
if (data != null) {
307+
saveTask = getFileController().saveAsync(
308+
state,
309+
data,
310+
sessionToken,
311+
progressCallbackOnMainThread(uploadProgressCallback),
312+
cancellationToken);
313+
} else {
314+
saveTask = getFileController().saveAsync(
315+
state,
316+
file,
317+
sessionToken,
318+
progressCallbackOnMainThread(uploadProgressCallback),
319+
cancellationToken);
320+
}
351321

352322
return saveTask.onSuccessTask(new Continuation<State, Task<Void>>() {
353323
@Override
354324
public Task<Void> then(Task<State> task) throws Exception {
355325
state = task.getResult();
326+
// Since we have successfully uploaded the file, we do not need to hold the file pointer
327+
// anymore.
328+
file = null;
356329
return task.makeVoid();
357330
}
358331
});

Parse/src/test/java/com/parse/ParseFileTest.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testConstructor() throws Exception {
5454
String name = "name";
5555
byte[] data = "hello".getBytes();
5656
String contentType = "content_type";
57-
File file = temporaryFolder.newFile("test");
57+
File file = temporaryFolder.newFile(name);
5858

5959
ParseFile parseFile = new ParseFile(name, data, contentType);
6060
assertEquals("name", parseFile.getName());
@@ -81,23 +81,13 @@ public void testConstructor() throws Exception {
8181
assertTrue(parseFile.isDirty());
8282

8383
// TODO(mengyan): Test file pointer in ParseFile when we have proper stage strategy
84-
parseFile = new ParseFile(name, file, contentType);
85-
assertEquals("name", parseFile.getName());
86-
assertEquals("content_type", parseFile.getState().mimeType());
87-
assertTrue(parseFile.isDirty());
88-
8984
parseFile = new ParseFile(file);
90-
assertEquals("file", parseFile.getName()); // Default
91-
assertEquals(null, parseFile.getState().mimeType());
92-
assertTrue(parseFile.isDirty());
93-
94-
parseFile = new ParseFile(name, file);
95-
assertEquals("name", parseFile.getName());
85+
assertEquals(name, parseFile.getName()); // Default
9686
assertEquals(null, parseFile.getState().mimeType());
9787
assertTrue(parseFile.isDirty());
9888

9989
parseFile = new ParseFile(file, contentType);
100-
assertEquals("file", parseFile.getName()); // Default
90+
assertEquals(name, parseFile.getName()); // Default
10191
assertEquals("content_type", parseFile.getState().mimeType());
10292
}
10393

@@ -223,7 +213,7 @@ public void testSaveAsyncSuccessWithData() throws Exception {
223213
@Test
224214
public void testSaveAsyncSuccessWithFile() throws Exception {
225215
String name = "name";
226-
File file = temporaryFolder.newFile("test");
216+
File file = temporaryFolder.newFile(name);
227217
String contentType = "content_type";
228218
String url = "url";
229219
ParseFile.State state = new ParseFile.State.Builder()
@@ -238,7 +228,7 @@ public void testSaveAsyncSuccessWithFile() throws Exception {
238228
Matchers.<Task<Void>>any())).thenReturn(Task.forResult(state));
239229
ParseCorePlugins.getInstance().registerFileController(controller);
240230

241-
ParseFile parseFile = new ParseFile(name, file, contentType);
231+
ParseFile parseFile = new ParseFile(file, contentType);
242232
ParseTaskUtils.wait(parseFile.saveAsync(null, null, null));
243233

244234
// Verify controller get the correct data

0 commit comments

Comments
 (0)