Merge pull request #42 from aflx/download

added download() to the filetransfer
This commit is contained in:
macdonst 2011-12-01 08:20:04 -08:00
commit 29001eeb3a
3 changed files with 1163 additions and 1098 deletions

View File

@ -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]); 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. * Options to customize the HTTP request used to upload files.
* @constructor * @constructor

View File

@ -20,8 +20,10 @@ package com.phonegap;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -69,17 +71,19 @@ public class FileTransfer extends Plugin {
*/ */
@Override @Override
public PluginResult execute(String action, JSONArray args, String callbackId) { public PluginResult execute(String action, JSONArray args, String callbackId) {
String file = null; String source = null;
String server = null; String target = null;
try { try {
file = args.getString(0); source = args.getString(0);
server = args.getString(1); target = args.getString(1);
} }
catch (JSONException e) { catch (JSONException e) {
Log.d(LOG_TAG, "Missing filename or server name"); Log.d(LOG_TAG, "Missing source or target");
return new PluginResult(PluginResult.Status.JSON_EXCEPTION, "Missing filename or server name"); return new PluginResult(PluginResult.Status.JSON_EXCEPTION, "Missing source or target");
} }
try {
if (action.equals("upload")) {
// Setup the options // Setup the options
String fileKey = null; String fileKey = null;
String fileName = null; String fileName = null;
@ -88,35 +92,35 @@ public class FileTransfer extends Plugin {
fileKey = getArgument(args, 2, "file"); fileKey = getArgument(args, 2, "file");
fileName = getArgument(args, 3, "image.jpg"); fileName = getArgument(args, 3, "image.jpg");
mimeType = getArgument(args, 4, "image/jpeg"); mimeType = getArgument(args, 4, "image/jpeg");
try {
JSONObject params = args.optJSONObject(5); JSONObject params = args.optJSONObject(5);
boolean trustEveryone = args.optBoolean(6); boolean trustEveryone = args.optBoolean(6);
boolean chunkedMode = args.getBoolean(7); boolean chunkedMode = args.optBoolean(7);
FileUploadResult r = upload(source, target, fileKey, fileName, mimeType, params, trustEveryone, chunkedMode);
if (action.equals("upload")) {
FileUploadResult r = upload(file, server, fileKey, fileName, mimeType, params, trustEveryone, chunkedMode);
Log.d(LOG_TAG, "****** About to return a result from upload"); Log.d(LOG_TAG, "****** About to return a result from upload");
return new PluginResult(PluginResult.Status.OK, r.toJSONObject()); 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 { } else {
return new PluginResult(PluginResult.Status.INVALID_ACTION); return new PluginResult(PluginResult.Status.INVALID_ACTION);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.e(LOG_TAG, e.getMessage(), 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); return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
Log.e(LOG_TAG, e.getMessage(), 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); return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
} catch (SSLException e) { } catch (SSLException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
Log.d(LOG_TAG, "Got my ssl exception!!!"); 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); return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
} catch (IOException e) { } catch (IOException e) {
Log.e(LOG_TAG, e.getMessage(), 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); return 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);
@ -173,11 +177,13 @@ public class FileTransfer extends Plugin {
* @param errorCode the error * @param errorCode the error
* @return JSONObject containing the error * @return JSONObject containing the error
*/ */
private JSONObject createFileUploadError(int errorCode) { private JSONObject createFileTransferError(int errorCode, String source, String target) {
JSONObject error = null; JSONObject error = null;
try { try {
error = new JSONObject(); error = new JSONObject();
error.put("code", errorCode); error.put("code", errorCode);
error.put("source", source);
error.put("target", target);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
@ -362,6 +368,54 @@ public class FileTransfer extends Plugin {
return result; 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 * Get an input stream based on file path or content:// uri
* *

View File

@ -838,7 +838,7 @@ public class FileUtils extends Plugin {
* @return * @return
* @throws JSONException * @throws JSONException
*/ */
private JSONObject getEntry(File file) throws JSONException { public JSONObject getEntry(File file) throws JSONException {
JSONObject entry = new JSONObject(); JSONObject entry = new JSONObject();
entry.put("isFile", file.isFile()); entry.put("isFile", file.isFile());