Skip to content

Commit f39b23f

Browse files
committed
Allowing for FileAsyncHttpResponseHandler to be constructed with directory/folder path and file will be created automatically from URL
1 parent 54a888c commit f39b23f

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed

library/src/main/java/com/loopj/android/http/FileAsyncHttpResponseHandler.java

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public abstract class FileAsyncHttpResponseHandler extends AsyncHttpResponseHand
3333

3434
protected final File file;
3535
protected final boolean append;
36+
protected final boolean renameIfExists;
37+
protected File frontendFile;
3638
private static final String LOG_TAG = "FileAsyncHttpRH";
3739

3840
/**
@@ -51,14 +53,30 @@ public FileAsyncHttpResponseHandler(File file) {
5153
* @param append whether data should be appended to existing file
5254
*/
5355
public FileAsyncHttpResponseHandler(File file, boolean append) {
56+
this(file, append, false);
57+
}
58+
59+
/**
60+
* Obtains new FileAsyncHttpResponseHandler and stores response in passed file
61+
*
62+
* @param file File to store response within, must not be null
63+
* @param append whether data should be appended to existing file
64+
* @param renameTargetFileIfExists whether target file should be renamed if it already exists
65+
*/
66+
public FileAsyncHttpResponseHandler(File file, boolean append, boolean renameTargetFileIfExists) {
5467
super();
5568
Utils.asserts(file != null, "File passed into FileAsyncHttpResponseHandler constructor must not be null");
56-
Utils.asserts(!file.isDirectory(), "File passed into FileAsyncHttpResponseHandler constructor must not point to directory");
57-
if (!file.getParentFile().isDirectory()) {
69+
if (!file.isDirectory() && !file.getParentFile().isDirectory()) {
5870
Utils.asserts(file.getParentFile().mkdirs(), "Cannot create parent directories for requested File location");
5971
}
72+
if (file.isDirectory()) {
73+
if (!file.mkdirs()) {
74+
Log.d(LOG_TAG, "Cannot create directories for requested Directory location, might not be a problem");
75+
}
76+
}
6077
this.file = file;
6178
this.append = append;
79+
this.renameIfExists = renameTargetFileIfExists;
6280
}
6381

6482
/**
@@ -70,6 +88,7 @@ public FileAsyncHttpResponseHandler(Context context) {
7088
super();
7189
this.file = getTemporaryFile(context);
7290
this.append = false;
91+
this.renameIfExists = false;
7392
}
7493

7594
/**
@@ -90,8 +109,6 @@ public boolean deleteTargetFile() {
90109
protected File getTemporaryFile(Context context) {
91110
Utils.asserts(context != null, "Tried creating temporary file without having Context");
92111
try {
93-
// not effective in release mode
94-
assert context != null;
95112
return File.createTempFile("temp_", "_handled", context.getCacheDir());
96113
} catch (IOException e) {
97114
Log.e(LOG_TAG, "Cannot create temporary file", e);
@@ -102,13 +119,55 @@ protected File getTemporaryFile(Context context) {
102119
/**
103120
* Retrieves File object in which the response is stored
104121
*
105-
* @return File file in which the response is stored
122+
* @return File file in which the response was to be stored
106123
*/
107-
protected File getTargetFile() {
108-
assert (file != null);
124+
protected File getOriginalFile() {
125+
Utils.asserts(file != null, "Target file is null, fatal!");
109126
return file;
110127
}
111128

129+
/**
130+
* Retrieves File which represents response final location after possible renaming
131+
*
132+
* @return File final target file
133+
*/
134+
public File getTargetFile() {
135+
if (frontendFile == null) {
136+
frontendFile = getOriginalFile().isDirectory() ? getTargetFileByParsingURL() : getOriginalFile();
137+
}
138+
return frontendFile;
139+
}
140+
141+
/**
142+
* Will return File instance for file representing last URL segment in given folder.
143+
* If file already exists and renameTargetFileIfExists was set as true, will try to find file
144+
* which doesn't exist, naming template for such cases is "filename.ext" => "filename (%d).ext",
145+
* or without extension "filename" => "filename (%d)"
146+
*/
147+
protected File getTargetFileByParsingURL() {
148+
Utils.asserts(getOriginalFile().isDirectory(), "Target file is not a directory, cannot proceed");
149+
Utils.asserts(getRequestURI() != null, "RequestURI is null, cannot proceed");
150+
String requestURL = getRequestURI().toString();
151+
String filename = requestURL.substring(requestURL.lastIndexOf('/') + 1, requestURL.length());
152+
File targetFileRtn = new File(getOriginalFile(), filename);
153+
if (targetFileRtn.exists() && renameIfExists) {
154+
String format;
155+
if (!filename.contains(".")) {
156+
format = filename + " (%d)";
157+
} else {
158+
format = filename.substring(0, filename.lastIndexOf('.')) + " (%d)" + filename.substring(filename.lastIndexOf('.'), filename.length());
159+
}
160+
int index = 0;
161+
while (true) {
162+
targetFileRtn = new File(getOriginalFile(), String.format(format, index));
163+
if (!targetFileRtn.exists())
164+
return targetFileRtn;
165+
index++;
166+
}
167+
}
168+
return targetFileRtn;
169+
}
170+
112171
@Override
113172
public final void onFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
114173
onFailure(statusCode, headers, throwable, getTargetFile());

0 commit comments

Comments
 (0)