@@ -599,51 +599,57 @@ public void run() {
599599 String source = args .optString (0 );
600600 String destination = args .optString (1 );
601601
602- if (ssh == null || sftp == null ) {
603- callback .error ("Not connected" );
604- return ;
605- }
606-
607- try {
608- copyRecursively (source , destination );
609- callback .success ();
610- } catch (Exception e ) {
611- callback .error ("Copy failed: " + errMessage (e ));
612- }
613- } catch (Exception e ) {
614- callback .error (errMessage (e ));
615- }
616- }
617-
618- private void copyRecursively (String source , String destination )
619- throws SftpStatusException , SshException , IOException {
620- SftpFileAttributes attrs = sftp .stat (source );
602+ if (ssh != null && sftp != null ) {
603+ try {
604+ SftpFileAttributes sourceAttrs = sftp .stat (source );
621605
622- if (attrs .isDirectory ()) {
623- try {
624- sftp .mkdir (destination );
625- } catch (SftpStatusException e ) {
626- // Directory may already exist - ignore
627- }
606+ if (sourceAttrs .isFile ()) {
607+ // file copy
608+ try (java .io .InputStream in = sftp .getInputStream (source )) {
609+ sftp .put (in , destination );
610+ }
611+ } else if (sourceAttrs .isDirectory ()) {
612+ // directory copy
613+ sftp .mkdir (destination );
614+
615+ for (SftpFile file : sftp .ls (source )) {
616+ String filename = file .getFilename ();
617+ if (!filename .equals ("." ) && !filename .equals (".." )) {
618+ String sourcePath = source + "/" + filename ;
619+ String destPath = destination + "/" + filename ;
620+
621+ if (file .getAttributes ().isDirectory ()) {
622+ // Recursively copy subdirectories
623+ JSONArray subDirArgs = new JSONArray ();
624+ subDirArgs .put (sourcePath );
625+ subDirArgs .put (destPath );
626+ copy (subDirArgs , callback );
627+ } else {
628+ // Copy files within directory
629+ try (
630+ java .io .InputStream in = sftp .getInputStream (
631+ sourcePath
632+ )
633+ ) {
634+ sftp .put (in , destPath );
635+ }
636+ }
637+ }
638+ }
639+ }
628640
629- // Copy directory contents
630- for (SftpFile file : sftp .ls (source )) {
631- String filename = file .getFilename ();
632- if (!filename .equals ("." ) && !filename .equals (".." )) {
633- String srcPath = source + "/" + filename ;
634- String destPath = destination + "/" + filename ;
635- copyRecursively (srcPath , destPath );
641+ callback .success ();
642+ return ;
643+ } catch (SftpStatusException e ) {
644+ callback .error ("Source does not exist: " + errMessage (e ));
645+ return ;
636646 }
637647 }
638- } else {
639- // Copy file using buffered streams
640- try (
641- java .io .InputStream in = sftp .getInputStream (source );
642- java .io .BufferedInputStream bis =
643- new java .io .BufferedInputStream (in )
644- ) {
645- sftp .put (bis , destination );
646- } catch (TransferCancelledException e ) {}
648+ callback .error ("Not connected" );
649+ } catch (
650+ SshException | IOException | TransferCancelledException e
651+ ) {
652+ callback .error (errMessage (e ));
647653 }
648654 }
649655 }
0 commit comments