From 5136c381a8ee7570262d8fa3f60a980375e51adb Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 21 Jul 2025 23:26:40 +0200 Subject: [PATCH] Mon 21 Jul 22:43:21 CEST 2025 --- .../CordovaHTTP/CordovaHttpDownload.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 js/ui/cordova/plugins/cordova-plugin-http/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java diff --git a/js/ui/cordova/plugins/cordova-plugin-http/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java b/js/ui/cordova/plugins/cordova-plugin-http/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java new file mode 100644 index 0000000..cd0a9d5 --- /dev/null +++ b/js/ui/cordova/plugins/cordova-plugin-http/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java @@ -0,0 +1,70 @@ +/** + * A HTTP plugin for Cordova / Phonegap + */ +package com.synconset; + +import android.util.Log; + +import com.github.kevinsawicki.http.HttpRequest; +import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; + +import java.io.File; +import java.net.UnknownHostException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Map; + +import javax.net.ssl.SSLHandshakeException; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.file.FileUtils; + +import org.json.JSONException; +import org.json.JSONObject; + +public class CordovaHttpDownload extends CordovaHttp implements Runnable { + private String filePath; + + public CordovaHttpDownload(String urlString, Map params, Map headers, CallbackContext callbackContext, String filePath) { + super(urlString, params, headers, callbackContext); + this.filePath = filePath; + } + + @Override + public void run() { + try { + HttpRequest request = HttpRequest.get(this.getUrlString(), this.getParams(), true); + this.setupSecurity(request); + request.acceptCharset(CHARSET); + request.headers(this.getHeaders()); + int code = request.code(); + + JSONObject response = new JSONObject(); + this.addResponseHeaders(request, response); + response.put("status", code); + if (code >= 200 && code < 300) { + URI uri = new URI(filePath); + File file = new File(uri); + request.receive(file); + JSONObject fileEntry = FileUtils.getFilePlugin().getEntryForFile(file); + response.put("file", fileEntry); + this.getCallbackContext().success(response); + } else { + response.put("error", "There was an error downloading the file"); + this.getCallbackContext().error(response); + } + } catch(URISyntaxException e) { + this.respondWithError("There was an error with the given filePath"); + } catch (JSONException e) { + this.respondWithError("There was an error generating the response"); + } catch (HttpRequestException e) { + if (e.getCause() instanceof UnknownHostException) { + this.respondWithError(0, "The host could not be resolved"); + } else if (e.getCause() instanceof SSLHandshakeException) { + this.respondWithError("SSL handshake failed"); + } else { + this.respondWithError("There was an error with the request"); + } + } + } +}