| 300 | } |
| 301 | |
| 302 | public void getFile(JSONArray args, CallbackContext callback) { |
| 303 | cordova |
| 304 | .getThreadPool() |
| 305 | .execute( |
| 306 | new Runnable() { |
| 307 | public void run() { |
| 308 | try { |
| 309 | String filename = args.optString(0); |
| 310 | String localFilename = args.optString(1); |
| 311 | if (ssh != null && sftp != null) { |
| 312 | URI uri = new URI(localFilename); |
| 313 | DocumentFile file = DocumentFile.fromSingleUri( |
| 314 | context, |
| 315 | Uri.parse(localFilename) |
| 316 | ); |
| 317 | Uri fileUri = file.getUri(); |
| 318 | ContentResolver contentResolver = context.getContentResolver(); |
| 319 | |
| 320 | try ( |
| 321 | InputStream inputStream = sftp.getInputStream(filename); |
| 322 | java.io.OutputStream outputStream = |
| 323 | contentResolver.openOutputStream(fileUri, "wt") |
| 324 | ) { |
| 325 | byte[] buffer = new byte[32768]; |
| 326 | int bytesRead; |
| 327 | |
| 328 | while ((bytesRead = inputStream.read(buffer)) != -1) { |
| 329 | outputStream.write(buffer, 0, bytesRead); |
| 330 | } |
| 331 | |
| 332 | outputStream.flush(); |
| 333 | callback.success(); |
| 334 | return; |
| 335 | } catch (SftpStatusException e) { |
| 336 | callback.error("SFTP transfer error: " + errMessage(e)); |
| 337 | return; |
| 338 | } |
| 339 | } |
| 340 | Log.d("getFile", "ssh or sftp is null"); |
| 341 | callback.error("Not connected"); |
| 342 | } catch ( |
| 343 | IOException |
| 344 | | URISyntaxException |
| 345 | | SecurityException |
| 346 | | SshException e |
| 347 | ) { |
| 348 | Log.e("getFile", "Error downloading file", e); |
| 349 | callback.error("File transfer error: " + errMessage(e)); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | ); |
| 354 | } |
| 355 | |
| 356 | public void putFile(JSONArray args, CallbackContext callback) { |
| 357 | cordova |