mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 20:33:07 +08:00
Merge pull request #42 from aflx/download
added download() to the filetransfer
This commit is contained in:
commit
29001eeb3a
@ -83,6 +83,17 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
|
||||
PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, debug, chunkedMode]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Downloads a file form a given URL and saves it to the specified directory.
|
||||
* @param source {String} URL of the server to receive the file
|
||||
* @param target {String} Full path of the file on the device
|
||||
* @param successCallback (Function} Callback to be invoked when upload has completed
|
||||
* @param errorCallback {Function} Callback to be invoked upon error
|
||||
*/
|
||||
FileTransfer.prototype.download = function(source, target, successCallback, errorCallback) {
|
||||
PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'download', [source, target]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Options to customize the HTTP request used to upload files.
|
||||
* @constructor
|
||||
|
@ -20,8 +20,10 @@ package com.phonegap;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
@ -69,17 +71,19 @@ public class FileTransfer extends Plugin {
|
||||
*/
|
||||
@Override
|
||||
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
||||
String file = null;
|
||||
String server = null;
|
||||
String source = null;
|
||||
String target = null;
|
||||
try {
|
||||
file = args.getString(0);
|
||||
server = args.getString(1);
|
||||
source = args.getString(0);
|
||||
target = args.getString(1);
|
||||
}
|
||||
catch (JSONException e) {
|
||||
Log.d(LOG_TAG, "Missing filename or server name");
|
||||
return new PluginResult(PluginResult.Status.JSON_EXCEPTION, "Missing filename or server name");
|
||||
Log.d(LOG_TAG, "Missing source or target");
|
||||
return new PluginResult(PluginResult.Status.JSON_EXCEPTION, "Missing source or target");
|
||||
}
|
||||
|
||||
try {
|
||||
if (action.equals("upload")) {
|
||||
// Setup the options
|
||||
String fileKey = null;
|
||||
String fileName = null;
|
||||
@ -88,35 +92,35 @@ public class FileTransfer extends Plugin {
|
||||
fileKey = getArgument(args, 2, "file");
|
||||
fileName = getArgument(args, 3, "image.jpg");
|
||||
mimeType = getArgument(args, 4, "image/jpeg");
|
||||
|
||||
try {
|
||||
JSONObject params = args.optJSONObject(5);
|
||||
boolean trustEveryone = args.optBoolean(6);
|
||||
boolean chunkedMode = args.getBoolean(7);
|
||||
|
||||
if (action.equals("upload")) {
|
||||
FileUploadResult r = upload(file, server, fileKey, fileName, mimeType, params, trustEveryone, chunkedMode);
|
||||
boolean chunkedMode = args.optBoolean(7);
|
||||
FileUploadResult r = upload(source, target, fileKey, fileName, mimeType, params, trustEveryone, chunkedMode);
|
||||
Log.d(LOG_TAG, "****** About to return a result from upload");
|
||||
return new PluginResult(PluginResult.Status.OK, r.toJSONObject());
|
||||
} else if (action.equals("download")) {
|
||||
JSONObject r = download(source, target);
|
||||
Log.d(LOG_TAG, "****** About to return a result from download");
|
||||
return new PluginResult(PluginResult.Status.OK, r, "window.localFileSystem._castEntry");
|
||||
} else {
|
||||
return new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
JSONObject error = createFileUploadError(FILE_NOT_FOUND_ERR);
|
||||
JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, target);
|
||||
return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
JSONObject error = createFileUploadError(INVALID_URL_ERR);
|
||||
JSONObject error = createFileTransferError(INVALID_URL_ERR, source, target);
|
||||
return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||
} catch (SSLException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
Log.d(LOG_TAG, "Got my ssl exception!!!");
|
||||
JSONObject error = createFileUploadError(CONNECTION_ERR);
|
||||
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target);
|
||||
return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||
} catch (IOException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
JSONObject error = createFileUploadError(CONNECTION_ERR);
|
||||
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target);
|
||||
return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
@ -173,11 +177,13 @@ public class FileTransfer extends Plugin {
|
||||
* @param errorCode the error
|
||||
* @return JSONObject containing the error
|
||||
*/
|
||||
private JSONObject createFileUploadError(int errorCode) {
|
||||
private JSONObject createFileTransferError(int errorCode, String source, String target) {
|
||||
JSONObject error = null;
|
||||
try {
|
||||
error = new JSONObject();
|
||||
error.put("code", errorCode);
|
||||
error.put("source", source);
|
||||
error.put("target", target);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
@ -362,6 +368,54 @@ public class FileTransfer extends Plugin {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a file form a given URL and saves it to the specified directory.
|
||||
*
|
||||
* @param source URL of the server to receive the file
|
||||
* @param target Full path of the file on the file system
|
||||
* @return JSONObject the downloaded file
|
||||
*/
|
||||
public JSONObject download(String source, String target) throws IOException {
|
||||
try {
|
||||
File file = new File(target);
|
||||
|
||||
// create needed directories
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
// connect to server
|
||||
URL url = new URL(source);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setDoOutput(true);
|
||||
connection.connect();
|
||||
|
||||
Log.d(LOG_TAG, "Download file:" + url);
|
||||
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead = 0;
|
||||
|
||||
FileOutputStream outputStream = new FileOutputStream(file);
|
||||
|
||||
// write bytes to file
|
||||
while ( (bytesRead = inputStream.read(buffer)) > 0 ) {
|
||||
outputStream.write(buffer,0, bytesRead);
|
||||
}
|
||||
|
||||
outputStream.close();
|
||||
|
||||
Log.d(LOG_TAG, "Saved file: " + target);
|
||||
|
||||
// create FileEntry object
|
||||
FileUtils fileUtil = new FileUtils();
|
||||
|
||||
return fileUtil.getEntry(file);
|
||||
} catch (Exception e) {
|
||||
Log.d(LOG_TAG, e.getMessage(), e);
|
||||
throw new IOException("Error while downloading");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an input stream based on file path or content:// uri
|
||||
*
|
||||
|
@ -838,7 +838,7 @@ public class FileUtils extends Plugin {
|
||||
* @return
|
||||
* @throws JSONException
|
||||
*/
|
||||
private JSONObject getEntry(File file) throws JSONException {
|
||||
public JSONObject getEntry(File file) throws JSONException {
|
||||
JSONObject entry = new JSONObject();
|
||||
|
||||
entry.put("isFile", file.isFile());
|
||||
|
Loading…
Reference in New Issue
Block a user