| 472 | } |
| 473 | |
| 474 | public void rename(JSONArray args, CallbackContext callback) { |
| 475 | cordova |
| 476 | .getThreadPool() |
| 477 | .execute( |
| 478 | new Runnable() { |
| 479 | public void run() { |
| 480 | try { |
| 481 | String ftpId = args.optString(0); |
| 482 | String oldPath = args.optString(1); |
| 483 | String newPath = args.optString(2); |
| 484 | |
| 485 | if (ftpId == null || ftpId.isEmpty()) { |
| 486 | callback.error("FTP ID is required."); |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (oldPath == null || oldPath.isEmpty()) { |
| 491 | callback.error("Old path is required."); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | if (newPath == null || newPath.isEmpty()) { |
| 496 | callback.error("New path is required."); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | FTPClient ftp = ftpProfiles.get(ftpId); |
| 501 | |
| 502 | if (ftp == null) { |
| 503 | callback.error("FTP client not found."); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | // get list of files in the parent directory |
| 508 | String parentPath = getParentPath(oldPath); |
| 509 | FTPFile[] ftpFiles = ftp.listFiles(parentPath); |
| 510 | |
| 511 | Log.d("FTP", "Renaming " + oldPath + " to " + newPath); |
| 512 | ftp.rename(oldPath, newPath); |
| 513 | |
| 514 | // check if file is renamed successfully |
| 515 | FTPFile[] newFile = ftp.listFiles(newPath); |
| 516 | if (newFile.length > 0) { |
| 517 | callback.success(newPath); |
| 518 | } else { |
| 519 | // get latest list of files in the parent directory |
| 520 | FTPFile[] latestFtpFiles = ftp.listFiles(parentPath); |
| 521 | // some time src file is renamed and not moved to destination |
| 522 | // check if for changed file and rename it original name |
| 523 | FTPFile changedFile = null; |
| 524 | for (FTPFile file : latestFtpFiles) { |
| 525 | boolean found = false; |
| 526 | for (FTPFile oldFile : ftpFiles) { |
| 527 | if (oldFile.getName().equals(file.getName())) { |
| 528 | found = true; |
| 529 | break; |
| 530 | } |
| 531 | } |