Skip to content

Commit c5b1ba8

Browse files
committed
Starting rclone
1 parent d6a6e13 commit c5b1ba8

2 files changed

Lines changed: 392 additions & 0 deletions

File tree

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
Class constructor($configname : Text)
2+
This:C1470.onData:=New object:C1471("text"; "")
3+
If (Is macOS:C1572)
4+
This:C1470._return:=Char:C90(10)
5+
This:C1470._Path:="rclone"
6+
Else
7+
This:C1470._return:=Char:C90(10) //Char(13)+Char(10)
8+
This:C1470._Path:="rclone.exe"
9+
End if
10+
This:C1470._timeout:=0
11+
This:C1470.config:=$configname
12+
13+
// uses https://rclone.org
14+
15+
//MARK: FileTransfer
16+
Function getDirectoryListing($targetpath : Text)->$success : Object
17+
If ($targetpath="")
18+
$targetpath:="/"
19+
End if
20+
// add +This.config+":/"
21+
$url:="lsjson "+This:C1470.config+":"+$targetpath
22+
$success:=This:C1470._runWorker($url)
23+
If ($success.success)
24+
If ($success.data="[@")
25+
$json:=JSON Parse:C1218($success.data)
26+
$success.list:=$json
27+
Else
28+
$success.success:=False:C215
29+
$success.error:=$success.data
30+
End if
31+
End if
32+
33+
Function upload($sourcepath : Text; $targetpath : Text)->$success : Object
34+
//$sourcepath just file name for local directory, else full path in POSIX syntax
35+
// targetpath is full remote path (starting with /, ending with file name
36+
ASSERT:C1129($sourcepath#""; "source path must not be empty")
37+
ASSERT:C1129($targetpath#""; "target path must not be empty")
38+
$url:="copyto "+$sourcepath+" "+This:C1470.config+":"+$targetpath
39+
$success:=This:C1470._runWorker($url)
40+
If ($success.data#"")
41+
$success.success:=False:C215
42+
$success.error:=$success.data
43+
End if
44+
45+
Function download($sourcepath : Text; $targetpath : Text)->$success : Object
46+
$success:=This:C1470.upload($sourcepath; $targetpath)
47+
ASSERT:C1129($sourcepath#""; "source path must not be empty")
48+
ASSERT:C1129($targetpath#""; "target path must not be empty")
49+
$url:="copyto "+This:C1470.config+":"+$sourcepath+" "+$targetpath
50+
$success:=This:C1470._runWorker($url)
51+
If ($success.data#"")
52+
$success.success:=False:C215
53+
$success.error:=$success.data
54+
End if
55+
56+
Function createDirectory($targetpath : Text)->$success : Object
57+
ASSERT:C1129($targetpath#""; "target path must not be empty")
58+
$url:="mkdir "+$targetpath
59+
$success:=This:C1470._runWorker($url)
60+
61+
Function deleteDirectory($targetpath : Text; $force : Boolean)->$success : Object
62+
ASSERT:C1129($targetpath#""; "target path must not be empty")
63+
If ($force)
64+
$url:="rm -f "+$targetpath
65+
Else
66+
$url:="rm "+$targetpath
67+
End if
68+
$success:=This:C1470._runWorker($url)
69+
70+
Function deleteFile($targetpath : Text)->$success : Object
71+
// same as deleteDirectory
72+
ASSERT:C1129($targetpath#""; "target path must not be empty")
73+
$url:="rm "+$targetpath
74+
$success:=This:C1470._runWorker($url)
75+
76+
Function renameFile($sourcepath : Text; $targetpath : Text)->$success : Object
77+
ASSERT:C1129($sourcepath#""; "source path must not be empty")
78+
ASSERT:C1129($targetpath#""; "target path must not be empty")
79+
$url:="mv "+$sourcepath+" "+$targetpath
80+
$success:=This:C1470._runWorker($url)
81+
82+
Function moveFile($sourcepath : Text; $targetpath : Text)->$success : Object
83+
$success:=This:C1470.renameFile($sourcepath; $targetpath)
84+
85+
Function copyFile($sourcepath : Text; $targetpath : Text)->$success : Object
86+
ASSERT:C1129($sourcepath#""; "source path must not be empty")
87+
ASSERT:C1129($targetpath#""; "target path must not be empty")
88+
$url:="cp "+$sourcepath+" "+$targetpath
89+
$success:=This:C1470._runWorker($url)
90+
91+
Function executeCommand($command : Text)->$success : Object
92+
ASSERT:C1129($command#""; "command must not be empty")
93+
$success:=This:C1470._runWorker($command)
94+
95+
//MARK: Settings
96+
Function validate()->$success : Object
97+
$success:=This:C1470._runWorker("about "+This:C1470.config+":/")
98+
99+
Function version()->$data : Object
100+
$data:=This:C1470._runWorker("version")
101+
102+
Function setPath($path : Text)
103+
This:C1470._Path:=$path
104+
105+
Function useCallback($callback : 4D:C1709.Function; $ID : Text)
106+
ASSERT:C1129(Value type:C1509($callback)=Is object:K8:27; "Callback must be of type function")
107+
ASSERT:C1129(OB Instance of:C1731($callback; 4D:C1709.Function); "Callback must be of type function")
108+
ASSERT:C1129($ID#""; "Callback ID Method must not be empty")
109+
This:C1470._Callback:=$callback
110+
This:C1470._CallbackID:=$ID
111+
This:C1470._noProgress:=False:C215
112+
113+
Function setTimeout($timeout : Integer)
114+
This:C1470._timeout:=$timeout
115+
116+
Function setAsyncMode($async : Boolean)
117+
This:C1470._async:=$async
118+
119+
Function enableStopButton($enable : Object)
120+
This:C1470._enableStopButton:=$enable
121+
122+
Function stop()
123+
If (This:C1470._worker#Null:C1517)
124+
This:C1470._worker.terminate()
125+
End if
126+
127+
Function status()->$status : Object
128+
$status:=New object:C1471
129+
$status.terminated:=This:C1470._worker.terminated
130+
$status.response:=This:C1470._worker.response
131+
$status.responseError:=This:C1470._worker.responseError
132+
$status.exitCode:=This:C1470._worker.exitCode
133+
$status.errors:=This:C1470._worker.errors
134+
135+
Function wait($max : Integer)
136+
This:C1470._worker.wait($max)
137+
138+
// MARK: Internal helper calls
139+
Function _runWorker($para : Text)->$result : Object
140+
If (This:C1470._Callback#Null:C1517)
141+
$workerpara:=cs:C1710.SystemWorkerProperties.new("rclone"; This:C1470.onData; This:C1470._Callback; This:C1470._CallbackID; This:C1470._enableStopButton)
142+
Else
143+
$workerpara:=cs:C1710.SystemWorkerProperties.new("rclone"; This:C1470.onData)
144+
End if
145+
146+
If ((This:C1470._Path) && (This:C1470._Path#""))
147+
$path:=This:C1470._Path
148+
Else
149+
$path:="rclone"
150+
End if
151+
152+
/*
153+
154+
If ((This._noProgress#Null) && (This._noProgress))
155+
$path+=" --no-progress-meter"
156+
End if
157+
If (This._connectTimeout#Null)
158+
$path+=" --connect-timeout "+String(This._connectTimeout)
159+
End if
160+
If (This._maxTime#Null)
161+
$path+=" --max-time "+String(This._maxTime)
162+
End if
163+
If (This._range#Null)
164+
$path+=" --range "+This._range
165+
End if
166+
If ((This._ActiveMode#Null) && (This._ActiveMode)) // default passive
167+
$path+=" --ftp-port "+This.ActiveModeIP
168+
End if
169+
If (This._prefix#Null)
170+
$path+=(" "+This._prefix)
171+
End if
172+
173+
*/
174+
175+
$command:=$path+" "+$para
176+
$old:=Method called on error:C704
177+
ON ERR CALL:C155(Formula:C1597(ErrorHandler).source)
178+
This:C1470._worker:=4D:C1709.SystemWorker.new($command; $workerpara)
179+
$worker:=This:C1470._worker
180+
181+
If ($worker#Null:C1517)
182+
If ((This:C1470._async#Null:C1517) && (This:C1470._async))
183+
$result:=New object:C1471("data"; "async"; "success"; True:C214)
184+
Else
185+
$waittimeout:=(This:C1470._timeout=0) ? 60 : This:C1470._timeout
186+
$worker.wait($waittimeout)
187+
If (($worker.responseError#Null:C1517) && ($worker.responseError#""))
188+
$result:=New object:C1471("responseError"; $worker.responseError; "success"; False:C215)
189+
$pos:=Position:C15("Error:"; $worker.responseError; *)
190+
If ($pos>0)
191+
$result.error:=Replace string:C233(Substring:C12($worker.responseError; $pos+6); Char:C90(10); "")
192+
Else
193+
If (($worker.response#Null:C1517) && ($worker.response#"")) // seems not to be an error, sometime curl set's process bar in error and result in response.
194+
$result:=New object:C1471("data"; $worker.response; "success"; True:C214)
195+
Else
196+
$result:=New object:C1471("data"; $worker.responseError; "success"; True:C214)
197+
End if
198+
End if
199+
Else
200+
$result:=New object:C1471("data"; $worker.response; "success"; True:C214)
201+
End if
202+
End if
203+
204+
If (This:C1470._Callback#Null:C1517)
205+
This:C1470._worker.onTerminate(New object:C1471; New object:C1471)
206+
End if
207+
Else
208+
$result:=New object:C1471("success"; False:C215; "responseError"; "rclone execution error")
209+
End if
210+
ON ERR CALL:C155($old)
211+
212+
Function _trim($text : Text)->$result : Text
213+
$result:=$text
214+
While (Substring:C12($result; 1; 1)=" ")
215+
$result:=Substring:C12($result; 2)
216+
End while
217+
While (Substring:C12($result; Length:C16($result); 1)=" ")
218+
$result:=Substring:C12($result; 1; Length:C16($result)-1)
219+
End while
220+
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
//%attributes = {}
2+
// overwrite or modify this to get your own credentials!
3+
// url like ftp.4D.com or ftp.4d.com:1234
4+
// if you use ftps or sftp, also modify the last parameter (protocol) in .new() below
5+
// don't add the protocol to the hostname, don't use https://xxx or ftp://xxx
6+
$credentialspath:=Get 4D folder:C485(Database folder:K5:14)
7+
$folder:=Folder:C1567($credentialspath; fk platform path:K87:2)
8+
$credentialsfile:=$folder.parent.file("credentials.txt").getText()
9+
$credentials:=JSON Parse:C1218($credentialsfile)
10+
If (True:C214)
11+
$credentials.url:="192.168.10.54:3421"
12+
// $credentials.user:="myself"
13+
// $credentials.password:="notmypass"
14+
End if
15+
16+
var $ftp : cs:C1710.FileTransfer_rclone
17+
//$ftp:=cs.FileTransfer_rclone.new("ftp_nas")
18+
$ftp:=cs:C1710.FileTransfer_rclone.new("Devcon")
19+
20+
$ftp.setPath("/users/thomas/Desktop/rclone-v1.59.1-osx-arm64/rclone")
21+
22+
If (False:C215)
23+
$result:=$ftp.version()
24+
If ($result.success)
25+
$answer:=$result.data
26+
End if
27+
End if
28+
29+
If (False:C215)
30+
$result:=$ftp.validate()
31+
If ($result.success)
32+
33+
Else
34+
$error:=$result.error
35+
End if
36+
End if
37+
38+
If (False:C215)
39+
$result:=$ftp.getDirectoryListing("/")
40+
If ($result.success)
41+
$list:=$result.list
42+
End if
43+
End if
44+
45+
46+
If (False:C215)
47+
$target:=System folder:C487(Desktop:K41:16)+"test.zip"
48+
$target:=Convert path system to POSIX:C1106($target)
49+
$source:="/Master_Class/4DSummit2018-MasterClass-Demos_v17.zip"
50+
// $source:="/media/backup/nas/Archiv/Produkte/4D Vorlage/Mac.zip"
51+
//$result:=$ftp.download( $source;$target)
52+
$result:=$ftp.download($source; $target)
53+
If ($result.success)
54+
55+
End if
56+
End if
57+
58+
59+
If (True:C214)
60+
$target:=System folder:C487(Desktop:K41:16)+"test.zip"
61+
$target:=Convert path system to POSIX:C1106($target)
62+
$source:="/Master_Class/4DSummit2018-MasterClass-Demos_v17.zip"
63+
64+
// $ftp.setCurlPrefix("--limit-rate 25M") // make it slow for testing - limiting bandwidth
65+
66+
$progressid:="Download 4D.dmg"
67+
$ftp.useCallback(Formula:C1597(ProgressCallback); $progressid)
68+
$ftp.setAsyncMode(False:C215) // default is false, no need to set
69+
$checkstop:=New shared object:C1526("stop"; False:C215)
70+
$ftp.enableStopButton($checkstop)
71+
72+
$result:=$ftp.download($source; $target)
73+
74+
If ($checkstop.stop=True:C214) // user clicked stop button
75+
// user canceled!!
76+
Else
77+
// now check for errors in $status.ResponseError
78+
End if
79+
80+
End if
81+
82+
83+
If (True:C214) // download two in parallel
84+
$source:="/large/4D.dmg"
85+
86+
var $ftp2 : cs:C1710.FileTransfer_curl
87+
$ftp2:=cs:C1710.FileTransfer_curl.new($credentials.url; $credentials.user; $credentials.pass; "ftp")
88+
//$ftp.setCurlPrefix("--limit-rate 25M")
89+
$progressid2:="2-Download 4D.dmg"
90+
//$ftp2.setCurlPrefix("--limit-rate 25M")
91+
$ftp.setAutoCreateRemoteDirectory(True:C214)
92+
$ftp.setAutoCreateLocalDirectory(True:C214)
93+
$ftp2.setAutoCreateRemoteDirectory(True:C214)
94+
$ftp2.setAutoCreateLocalDirectory(True:C214)
95+
$ftp2.useCallback(Formula:C1597(ProgressCallback); $progressid2)
96+
$ftp2.setAsyncMode(True:C214)
97+
$checkstop:=New shared object:C1526("stop"; False:C215)
98+
$ftp.enableStopButton($checkstop)
99+
$checkstop2:=New shared object:C1526("stop"; False:C215)
100+
$ftp2.enableStopButton($checkstop2)
101+
$target:=System folder:C487(Desktop:K41:16)+"neu2"+Folder separator:K24:12
102+
$target:=Convert path system to POSIX:C1106($target)
103+
$result2:=$ftp2.download($source; $target)
104+
105+
$ftp.setCurlPrefix("--limit-rate 25M")
106+
$progressid:="Download 4D.dmg"
107+
$ftp.useCallback(Formula:C1597(ProgressCallback); $progressid)
108+
$ftp.setAsyncMode(True:C214)
109+
110+
$target:=System folder:C487(Desktop:K41:16)+"neu"+Folder separator:K24:12
111+
$target:=Convert path system to POSIX:C1106($target)
112+
$result:=$ftp.download($source; $target)
113+
// async, so we need to loop...
114+
// normally we are supposed to do something else and either
115+
// check from time to time or to use the callback method to inform us (percent=100)
116+
Repeat
117+
$ftp.wait(0.1) // needed while our process is running
118+
$ftp2.wait(0.1) // needed while our process is running
119+
120+
// wait is not needed if a form would be open or if a worker would handle the job
121+
$status:=$ftp.status()
122+
$status2:=$ftp2.status()
123+
124+
Until (Bool:C1537($status.terminated) & Bool:C1537($status2.terminated))
125+
126+
End if
127+
128+
129+
130+
131+
132+
If (False:C215)
133+
$result:=$ftp.createDirectory("/folder2/")
134+
If ($result.success)
135+
136+
End if
137+
End if
138+
139+
If (False:C215)
140+
$result:=$ftp.deleteDirectory("/folder2/")
141+
If ($result.success)
142+
$list:=$result.list
143+
End if
144+
End if
145+
146+
147+
148+
If (False:C215)
149+
$result:=$ftp.deleteFile("/test2.txt")
150+
If ($result.success)
151+
$list:=$result.list
152+
End if
153+
End if
154+
155+
If (False:C215)
156+
$result:=$ftp.renameFile("/test3.txt"; "/test2.txt")
157+
If ($result.success)
158+
$list:=$result.list
159+
End if
160+
End if
161+
162+
163+
If (False:C215)
164+
$source:=System folder:C487(Desktop:K41:16)+"neu"+Folder separator:K24:12+"test[1-3].txt"
165+
$source:=Convert path system to POSIX:C1106($source)
166+
$ftp.setAutoCreateRemoteDirectory(True:C214)
167+
$ftp.setAutoCreateLocalDirectory(True:C214)
168+
$result:=$ftp.upload($source; "/meinfolder/"; True:C214)
169+
If ($result.success)
170+
$answer:=$result.data
171+
End if
172+
End if

0 commit comments

Comments
 (0)