@@ -33,6 +33,8 @@ public abstract class FileAsyncHttpResponseHandler extends AsyncHttpResponseHand
33
33
34
34
protected final File file ;
35
35
protected final boolean append ;
36
+ protected final boolean renameIfExists ;
37
+ protected File frontendFile ;
36
38
private static final String LOG_TAG = "FileAsyncHttpRH" ;
37
39
38
40
/**
@@ -51,14 +53,30 @@ public FileAsyncHttpResponseHandler(File file) {
51
53
* @param append whether data should be appended to existing file
52
54
*/
53
55
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 ) {
54
67
super ();
55
68
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 ()) {
58
70
Utils .asserts (file .getParentFile ().mkdirs (), "Cannot create parent directories for requested File location" );
59
71
}
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
+ }
60
77
this .file = file ;
61
78
this .append = append ;
79
+ this .renameIfExists = renameTargetFileIfExists ;
62
80
}
63
81
64
82
/**
@@ -70,6 +88,7 @@ public FileAsyncHttpResponseHandler(Context context) {
70
88
super ();
71
89
this .file = getTemporaryFile (context );
72
90
this .append = false ;
91
+ this .renameIfExists = false ;
73
92
}
74
93
75
94
/**
@@ -90,8 +109,6 @@ public boolean deleteTargetFile() {
90
109
protected File getTemporaryFile (Context context ) {
91
110
Utils .asserts (context != null , "Tried creating temporary file without having Context" );
92
111
try {
93
- // not effective in release mode
94
- assert context != null ;
95
112
return File .createTempFile ("temp_" , "_handled" , context .getCacheDir ());
96
113
} catch (IOException e ) {
97
114
Log .e (LOG_TAG , "Cannot create temporary file" , e );
@@ -102,13 +119,55 @@ protected File getTemporaryFile(Context context) {
102
119
/**
103
120
* Retrieves File object in which the response is stored
104
121
*
105
- * @return File file in which the response is stored
122
+ * @return File file in which the response was to be stored
106
123
*/
107
- protected File getTargetFile () {
108
- assert (file != null );
124
+ protected File getOriginalFile () {
125
+ Utils . asserts (file != null , "Target file is null, fatal!" );
109
126
return file ;
110
127
}
111
128
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
+
112
171
@ Override
113
172
public final void onFailure (int statusCode , Header [] headers , byte [] responseBytes , Throwable throwable ) {
114
173
onFailure (statusCode , headers , throwable , getTargetFile ());
0 commit comments