mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
[CB-2095] Delete file on FileTransfer.download fail
This commit is contained in:
parent
1246a81d39
commit
dd86d7a5ed
@ -77,6 +77,7 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
private static final class RequestContext {
|
private static final class RequestContext {
|
||||||
String source;
|
String source;
|
||||||
String target;
|
String target;
|
||||||
|
File targetFile;
|
||||||
CallbackContext callbackContext;
|
CallbackContext callbackContext;
|
||||||
InputStream currentInputStream;
|
InputStream currentInputStream;
|
||||||
OutputStream currentOutputStream;
|
OutputStream currentOutputStream;
|
||||||
@ -626,11 +627,13 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
URLConnection connection = null;
|
URLConnection connection = null;
|
||||||
HostnameVerifier oldHostnameVerifier = null;
|
HostnameVerifier oldHostnameVerifier = null;
|
||||||
SSLSocketFactory oldSocketFactory = null;
|
SSLSocketFactory oldSocketFactory = null;
|
||||||
|
File file = null;
|
||||||
|
PluginResult result = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
file = getFileFromPath(target);
|
||||||
|
context.targetFile = file;
|
||||||
// create needed directories
|
// create needed directories
|
||||||
File file = getFileFromPath(target);
|
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
|
|
||||||
// connect to server
|
// connect to server
|
||||||
@ -718,22 +721,22 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
FileUtils fileUtil = new FileUtils();
|
FileUtils fileUtil = new FileUtils();
|
||||||
JSONObject fileEntry = fileUtil.getEntry(file);
|
JSONObject fileEntry = fileUtil.getEntry(file);
|
||||||
|
|
||||||
context.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileEntry));
|
result = new PluginResult(PluginResult.Status.OK, fileEntry);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, target, connection);
|
JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, target, connection);
|
||||||
Log.e(LOG_TAG, error.toString(), e);
|
Log.e(LOG_TAG, error.toString(), e);
|
||||||
context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
|
result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection);
|
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection);
|
||||||
Log.e(LOG_TAG, error.toString(), e);
|
Log.e(LOG_TAG, error.toString(), e);
|
||||||
context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
|
result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
Log.e(LOG_TAG, e.getMessage(), e);
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
context.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
|
result = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection);
|
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection);
|
||||||
Log.e(LOG_TAG, error.toString(), e);
|
Log.e(LOG_TAG, error.toString(), e);
|
||||||
context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
|
result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||||
} finally {
|
} finally {
|
||||||
synchronized (activeRequests) {
|
synchronized (activeRequests) {
|
||||||
activeRequests.remove(objectId);
|
activeRequests.remove(objectId);
|
||||||
@ -747,6 +750,15 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
https.setSSLSocketFactory(oldSocketFactory);
|
https.setSSLSocketFactory(oldSocketFactory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (result == null) {
|
||||||
|
result = new PluginResult(PluginResult.Status.ERROR, createFileTransferError(CONNECTION_ERR, source, target, connection));
|
||||||
|
}
|
||||||
|
// Remove incomplete download.
|
||||||
|
if (result.getStatus() != PluginResult.Status.OK.ordinal() && file != null) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
context.sendPluginResult(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -809,6 +821,10 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
context = activeRequests.remove(objectId);
|
context = activeRequests.remove(objectId);
|
||||||
}
|
}
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
|
File file = context.targetFile;
|
||||||
|
if (file != null) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
// Trigger the abort callback immediately to minimize latency between it and abort() being called.
|
// Trigger the abort callback immediately to minimize latency between it and abort() being called.
|
||||||
JSONObject error = createFileTransferError(ABORTED_ERR, context.source, context.target, -1);
|
JSONObject error = createFileTransferError(ABORTED_ERR, context.source, context.target, -1);
|
||||||
synchronized (context) {
|
synchronized (context) {
|
||||||
|
Loading…
Reference in New Issue
Block a user