mirror of
https://github.com/apache/cordova-plugin-file-transfer.git
synced 2026-04-28 00:02:49 +08:00
Upleveled from android port with following commits:
3c1ff16Andrew Grieve - CB-5762 android: Fix lengthComputable set wrong for gzip downloads8374b3dColin Mahoney - CB-5631 Removed SimpleTrackingInputStream.read(byte[] buffer)6f91ac3Bas Bosman - CB-4907 Close stream when we're finished with it651460fChristoph Neumann - CB-6000 Nginx rejects Content-Type without a space before "boundary".35f80e4Ian Clelland - CB-6050: Use instance method on actual file plugin object to get FileEntry to return on download
This commit is contained in:
@@ -158,11 +158,8 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
return updateBytesRead(super.read());
|
return updateBytesRead(super.read());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// Note: FilterInputStream delegates read(byte[] bytes) to the below method,
|
||||||
public int read(byte[] buffer) throws IOException {
|
// so we don't override it or else double count (CB-5631).
|
||||||
return updateBytesRead(super.read(buffer));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(byte[] bytes, int offset, int count) throws IOException {
|
public int read(byte[] bytes, int offset, int count) throws IOException {
|
||||||
return updateBytesRead(super.read(bytes, offset, count));
|
return updateBytesRead(super.read(bytes, offset, count));
|
||||||
@@ -318,7 +315,7 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
|
|
||||||
// Use a post method.
|
// Use a post method.
|
||||||
conn.setRequestMethod(httpMethod);
|
conn.setRequestMethod(httpMethod);
|
||||||
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
|
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
|
||||||
|
|
||||||
// Set the cookies on the response
|
// Set the cookies on the response
|
||||||
String cookie = AmazonCookieManager.getInstance().getCookie(target);
|
String cookie = AmazonCookieManager.getInstance().getCookie(target);
|
||||||
@@ -585,15 +582,19 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
if(err != null)
|
if(err != null)
|
||||||
{
|
{
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(err, "UTF-8"));
|
BufferedReader reader = new BufferedReader(new InputStreamReader(err, "UTF-8"));
|
||||||
String line = reader.readLine();
|
try {
|
||||||
while(line != null)
|
String line = reader.readLine();
|
||||||
{
|
while(line != null) {
|
||||||
bodyBuilder.append(line);
|
bodyBuilder.append(line);
|
||||||
line = reader.readLine();
|
line = reader.readLine();
|
||||||
if(line != null)
|
if(line != null) {
|
||||||
bodyBuilder.append('\n');
|
bodyBuilder.append('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
body = bodyBuilder.toString();
|
||||||
|
} finally {
|
||||||
|
reader.close();
|
||||||
}
|
}
|
||||||
body = bodyBuilder.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// IOException can leave connection object in a bad state, so catch all exceptions.
|
// IOException can leave connection object in a bad state, so catch all exceptions.
|
||||||
@@ -760,8 +761,10 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip")) {
|
if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip")) {
|
||||||
// Only trust content-length header if we understand
|
// Only trust content-length header if we understand
|
||||||
// the encoding -- identity or gzip
|
// the encoding -- identity or gzip
|
||||||
progress.setLengthComputable(true);
|
if (connection.getContentLength() != -1) {
|
||||||
progress.setTotal(connection.getContentLength());
|
progress.setLengthComputable(true);
|
||||||
|
progress.setTotal(connection.getContentLength());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
inputStream = getInputStream(connection);
|
inputStream = getInputStream(connection);
|
||||||
}
|
}
|
||||||
@@ -794,9 +797,21 @@ public class FileTransfer extends CordovaPlugin {
|
|||||||
Log.d(LOG_TAG, "Saved file: " + target);
|
Log.d(LOG_TAG, "Saved file: " + target);
|
||||||
|
|
||||||
// create FileEntry object
|
// create FileEntry object
|
||||||
JSONObject fileEntry = FileUtils.getEntry(file);
|
FileUtils filePlugin = (FileUtils)webView.pluginManager.getPlugin("File");
|
||||||
|
if (filePlugin != null) {
|
||||||
result = new PluginResult(PluginResult.Status.OK, fileEntry);
|
JSONObject fileEntry = filePlugin.getEntryForFile(file);
|
||||||
|
if (fileEntry != null) {
|
||||||
|
result = new PluginResult(PluginResult.Status.OK, fileEntry);
|
||||||
|
} else {
|
||||||
|
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection);
|
||||||
|
Log.e(LOG_TAG, "File plugin cannot represent download path");
|
||||||
|
result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.e(LOG_TAG, "File plugin not found; cannot save downloaded file");
|
||||||
|
result = new PluginResult(PluginResult.Status.ERROR, "File plugin not found; cannot save downloaded file");
|
||||||
|
}
|
||||||
|
|
||||||
} 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);
|
||||||
|
|||||||
Reference in New Issue
Block a user