FileTransfer returns JSONObject with code, source and target for upload and download

This commit is contained in:
Alexander Keller 2011-11-30 09:15:01 +01:00
parent 8dbb8f58b1
commit f387720e54
2 changed files with 26 additions and 24 deletions

View File

@ -85,13 +85,13 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
/** /**
* Downloads a file form a given URL and saves it to the specified directory. * Downloads a file form a given URL and saves it to the specified directory.
* @param sourceUrl {String} URL of the server to receive the file * @param source {String} URL of the server to receive the file
* @param targetFile {String} Full path of the file on the device * @param target {String} Full path of the file on the device
* @param successCallback (Function} Callback to be invoked when upload has completed * @param successCallback (Function} Callback to be invoked when upload has completed
* @param errorCallback {Function} Callback to be invoked upon error * @param errorCallback {Function} Callback to be invoked upon error
*/ */
FileTransfer.prototype.download = function(sourceUrl, targetFile, successCallback, errorCallback) { FileTransfer.prototype.download = function(source, target, successCallback, errorCallback) {
PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'download', [targetFile, sourceUrl]); PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'download', [source, target]);
}; };
/** /**

View File

@ -71,15 +71,15 @@ 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 { try {
@ -94,12 +94,12 @@ public class FileTransfer extends Plugin {
mimeType = getArgument(args, 4, "image/jpeg"); mimeType = getArgument(args, 4, "image/jpeg");
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(file, server, fileKey, fileName, mimeType, params, trustEveryone, chunkedMode); FileUploadResult r = upload(source, target, 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")) { } else if (action.equals("download")) {
JSONObject r = download(file, server); JSONObject r = download(source, target);
Log.d(LOG_TAG, "****** About to return a result from download"); Log.d(LOG_TAG, "****** About to return a result from download");
return new PluginResult(PluginResult.Status.OK, r, "window.localFileSystem._castEntry"); return new PluginResult(PluginResult.Status.OK, r, "window.localFileSystem._castEntry");
} else { } else {
@ -107,20 +107,20 @@ public class FileTransfer extends Plugin {
} }
} 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);
@ -177,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);
} }
@ -369,19 +371,19 @@ public class FileTransfer extends Plugin {
/** /**
* Downloads a file form a given URL and saves it to the specified directory. * Downloads a file form a given URL and saves it to the specified directory.
* *
* @param server URL of the server to receive the file * @param source URL of the server to receive the file
* @param file Full path of the file on the file system * @param target Full path of the file on the file system
* @return JSONObject the downloaded file * @return JSONObject the downloaded file
*/ */
public JSONObject download(String filePath, String sourceUrl) throws IOException { public JSONObject download(String source, String target) throws IOException {
try { try {
File file = new File(filePath); File file = new File(target);
// create needed directories // create needed directories
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
// connect to server // connect to server
URL url = new URL(sourceUrl); URL url = new URL(source);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setDoOutput(true); connection.setDoOutput(true);
@ -402,7 +404,7 @@ public class FileTransfer extends Plugin {
outputStream.close(); outputStream.close();
Log.d(LOG_TAG, "Saved file: " + filePath); Log.d(LOG_TAG, "Saved file: " + target);
// create FileEntry object // create FileEntry object
FileUtils fileUtil = new FileUtils(); FileUtils fileUtil = new FileUtils();