@@ -26,6 +26,7 @@ import kotlinx.coroutines.withContext
2626import org.json.JSONArray
2727import org.json.JSONObject
2828import special.anonymous
29+ import java.io.ByteArrayInputStream
2930import java.io.File
3031import java.io.IOException
3132import java.net.URI
@@ -85,7 +86,7 @@ class Sftp : Plugin() {
8586 it.printStackTrace()
8687 ssh?.close()
8788 reject(" Failed to initialize SFTP subsystem: ${it.message.toString()} " )
88- return @asyncContext
89+ return @withContext
8990 }
9091
9192 runCatching {
@@ -374,6 +375,142 @@ class Sftp : Plugin() {
374375
375376 }
376377
378+ @PluginMethod
379+ fun mkdir (call : PluginCall ) = with (call){
380+ async{
381+ existsNot(" path" ){
382+ return @async
383+ }
384+ if (ssh == null || sftp == null ){
385+ reject(" Not Connected" )
386+ return @async
387+ }
388+ val path = getString(" path" )!!
389+ runCatching {
390+ sftp!! .mkdir(path);
391+ resolve()
392+ }.onFailure {
393+ it.printStackTrace()
394+ reject(it.message)
395+ }
396+
397+ }
398+ }
399+
400+ @PluginMethod
401+ fun rm (call : PluginCall ) = with (call){
402+ async{
403+ existsNot(" path" ){
404+ return @async
405+ }
406+
407+ if (ssh == null || sftp == null ){
408+ reject(" Not Connected" )
409+ return @async
410+ }
411+ val path = getString(" path" )!!
412+ val force = getBoolean(" force" ) == true
413+ val recursive = getBoolean(" recursive" ) == true
414+
415+ runCatching {
416+ sftp!! .rm(path,force,recursive)
417+ resolve()
418+ }.onFailure {
419+ it.printStackTrace()
420+ reject(it.message)
421+ }
422+
423+ }
424+ }
425+
426+ @PluginMethod
427+ fun pwd (call : PluginCall ) = with (call){
428+ async{
429+ if (ssh == null || sftp == null ){
430+ reject(" Not Connected" )
431+ return @async
432+ }
433+
434+ runCatching {
435+ val pwd = sftp!! .pwd()
436+ val result = JSObject ()
437+ result.put(" result" ,pwd)
438+ resolve(result)
439+ }.onFailure {
440+ it.printStackTrace()
441+ reject(it.message)
442+ }
443+
444+
445+ }
446+ }
447+
448+ @PluginMethod
449+ fun rename (call : PluginCall ) = with (call){
450+ async{
451+ runCatching {
452+ if (ssh == null || sftp == null ){
453+ reject(" Not Connected" )
454+ return @async
455+ }
456+
457+ existsNot(" oldPath" ){
458+ return @async
459+ }
460+ existsNot(" newPath" ){
461+ return @async
462+ }
463+
464+ val oldPath = getString(" oldPath" )!!
465+ val newPath = getString(" newPath" )!!
466+ sftp!! .rename(oldPath,newPath)
467+ resolve()
468+ }.onFailure {
469+ it.printStackTrace()
470+ reject(it.message)
471+ }
472+ }
473+ }
474+
475+ @PluginMethod
476+ fun createFile (call : PluginCall ) = with (call){
477+ async{
478+ runCatching {
479+ if (ssh == null || sftp == null ){
480+ reject(" Not Connected" )
481+ return @async
482+ }
483+
484+ existsNot(" path" ){
485+ return @async
486+ }
487+
488+ val path = getString(" path" )!!
489+ val content = getString(" content" ).toString()
490+
491+
492+ if (sftp!! .exists(path)){
493+ reject(" File already exists" )
494+ return @async
495+ }
496+
497+ ByteArrayInputStream (content.toByteArray(Charsets .UTF_8 )).use {
498+ runCatching {
499+ sftp!! .put(it,path)
500+ }.onFailure {
501+ it.printStackTrace()
502+ reject(it.message)
503+ }
504+ }
505+
506+
507+ }.onFailure {
508+ it.printStackTrace()
509+ reject(it.message)
510+ }
511+ }
512+ }
513+
377514 @PluginMethod
378515 fun getStat (call : PluginCall ) = with (call){
379516 async{
0 commit comments