1- Class constructor ()
2- If (Is macOS:C1572)
3- This:C1470 ._return := Char:C90 (10 )
4- $path := Get 4D folder:C485 (Current resources folder:K5:16 )+ "Dropbox" + Folder separator:K24:12 + "dbxcli"
5- This:C1470 ._Path := Convert path system to POSIX:C1106 ($path )
6- Else
7- This:C1470 ._return := Char:C90 (10 ) // Char(13)+Char(10)
8- $path := Get 4D folder:C485 (Current resources folder:K5:16 )+ "Dropbox" + Folder separator:K24:12 + "dbxcli.exe"
9- This:C1470 ._Path := Convert path system to POSIX:C1106 ($path )
10- End if
11-
12-
13- // MARK: FileTransfer
14- Function getDirectoryListing ($targetpath : Text)- > $success : Object
15- If ($targetpath= "")
16- $targetpath := "/"
17- End if
18- $url := "ls -l " + $targetpath
19- $success := This:C1470 ._runWorker ($url )
20- If ($success .success )
21- // data contains a text based dir listing
22- This:C1470 ._parseDirListing ($success )
23- End if
24-
25- Function upload ($sourcepath : Text; $targetpath : Text)- > $success : Object
26- // $sourcepath just file name for local directory, else full path in POSIX syntax
27- // targetpath is full remote path (starting with /, ending with file name
28- ASSERT:C1129 ($sourcepath # "" ; "source path must not be empty" )
29- ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
30-
31- $url := "put " + $sourcepath + " " + $targetpath
32- $success := This:C1470 ._runWorker ($url )
33-
34- Function download ($sourcepath : Text; $targetpath : Text)- > $success : Object
35- // $sourcepath just file name for local directory, else full path in POSIX syntax
36- // targetpath is full remote path (starting with /, ending with file name
37- ASSERT:C1129 ($sourcepath # "" ; "source path must not be empty" )
38- ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
39-
40- $url := "get " + $sourcepath + " " + $targetpath
41- $success := This:C1470 ._runWorker ($url )
42-
43- Function createDirectory ($targetpath : Text)- > $success : Object
44- ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
45- $url := "mkdir " + $targetpath
46- $success := This:C1470 ._runWorker ($url )
47-
48- Function deleteDirectory ($targetpath : Text; $force : Boolean)- > $success : Object
49- ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
50- If ($force)
51- $url := "rm -f " + $targetpath
52- Else
53- $url := "rm " + $targetpath
54- End if
55- $success := This:C1470 ._runWorker ($url )
56-
57- Function deleteFile ($targetpath : Text)- > $success : Object
58- // same as deleteDirectory
59- ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
60- $url := "rm " + $targetpath
61- $success := This:C1470 ._runWorker ($url )
62-
63- Function renameFile ($sourcepath : Text; $targetpath : Text)- > $success : Object
64- ASSERT:C1129 ($sourcepath # "" ; "source path must not be empty" )
65- ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
66- $url := "mv " + $sourcepath + " " + $targetpath
67- $success := This:C1470 ._runWorker ($url )
68-
69- Function moveFile ($sourcepath : Text; $targetpath : Text)- > $success : Object
70- $success := This:C1470 .renameFile ($sourcepath ; $targetpath )
71-
72- Function copyFile ($sourcepath : Text; $targetpath : Text)- > $success : Object
73- ASSERT:C1129 ($sourcepath # "" ; "source path must not be empty" )
74- ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
75- $url := "cp " + $sourcepath + " " + $targetpath
76- $success := This:C1470 ._runWorker ($url )
77-
78- // MARK: Settings
79- Function version ()- > $data : Object
80- $data := This:C1470 ._runWorker ("version" )
81-
82- Function setPath ($path : Text)
83- This:C1470 ._Path := $path
84-
85- Function useCallback ($callback : 4D:C1709 .Function ; $ID : Text)
86- ASSERT:C1129 (Value type:C1509 ($callback )= Is object:K8:27 ; "Callback must be of type function" )
87- ASSERT:C1129 (OB Instance of:C1731 ($callback ; 4D:C1709 .Function ); "Callback must be of type function" )
88- ASSERT:C1129 ($ID # "" ; "Callback ID Method must not be empty" )
89- This:C1470 ._Callback := $callback
90- This:C1470 ._CallbackID := $ID
91- This:C1470 ._noProgress := False:C215
92-
93- // MARK: Internal helper calls
94- Function _parseDirListing ($success : Object)
95- $col := Split string:C1554 (String:C10 ($success .data ); This:C1470 ._return ; sk ignore empty strings:K86:1 )
96- If (($col .length > 0) && ($col[0 ]= "Revision@"))
97- $success .list := New collection:C1472
98- $posSize := Position:C15 ("Size" ; $col [0 ])
99- $posLast := Position:C15 ("Last" ; $col [0 ])
100- $posPath := Position:C15 ("Path" ; $col [0 ])
101- If (($posSize# 0) & ($posLast# 0) & ($posPath# 0))
102- For each ($line; $col; 1)
103- $diritem := New object:C1471
104- $diritem .revision := This:C1470 ._trim (Substring:C12 ($line ; 1 ; $posSize - 1 ))
105- $diritem .size := This:C1470 ._trim (Substring:C12 ($line ; $posSize ; $posLast - $posSize - 1 ))
106- $diritem .date := This:C1470 ._trim (Substring:C12 ($line ; $posLast ; $posPath - $posLast - 1 ))
107- $diritem .path := This:C1470 ._trim (Substring:C12 ($line ; $posPath ))
108- $success .list .push ($diritem )
109- End for each
110- Else // error?
111- $success .success := False:C215
112- $success .responseError := "Directory listing unexpected format"
113- End if
114- Else
115- $success .success := False:C215
116- $success .responseError := "Directory listing unexpected format"
117- End if
118-
119- Function _runWorker ($para : Text)- > $result : Object
120- If (This:C1470 ._Callback # Null:C1517)
121- $workerpara := cs:C1710 .SystemWorkerProperties .new (This:C1470 .onData; This:C1470 ._Callback; This:C1470 ._CallbackID)
122- Else
123- $workerpara := cs:C1710 .SystemWorkerProperties .new (This:C1470 .onData)
124- End if
125-
126- If ((This:C1470 ._Path ) && (This:C1470 ._Path # ""))
127- $path := This:C1470 ._Path
128- Else
129- // TODO: needs to be set to use Resource/Mac or /win
130- $path := "dbxcli"
131- End if
132-
133- $command := $path + " " + $para
134- $old := Method called on error:C704
135- ON ERR CALL:C155 (Formula:C1597 (ErrorHandler).source )
136- This:C1470 ._worker := 4D:C1709 .SystemWorker .new ($command ; $workerpara )
137- $worker := This:C1470 ._worker
138-
139- If ($worker# Null:C1517)
140- If ((This:C1470 ._async # Null:C1517) && (This:C1470 ._async ))
141- $result := New object:C1471 ("data" ; "async" ; "success" ; True:C214 )
142- Else
143- $worker .wait ()
144- If (($worker .responseError # Null:C1517) && ($worker .responseError # ""))
145- $result := New object:C1471 ("responseError" ; $worker .responseError ; "success" ; False:C215 )
146- $pos := Position:C15 ("Error:" ; $worker .responseError ; * )
147- If ($pos> 0)
148- $result .error := Replace string:C233 (Substring:C12 ($worker .responseError ; $pos + 6 ); Char:C90 (10 ); "" )
149- Else
150- 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.
151- $result := New object:C1471 ("data" ; $worker .response ; "success" ; True:C214 )
152- Else
153- $result := New object:C1471 ("data" ; $worker .responseError ; "success" ; True:C214 )
154- End if
155- End if
156- Else
157- $result := New object:C1471 ("data" ; $worker .response ; "success" ; True:C214 )
158- End if
159- End if
160- Else
161- $result := New object:C1471 ("success" ; False:C215 ; "responseError" ; "Curl execution error" )
162- End if
163- ON ERR CALL:C155 ($old )
164-
165- Function _trim ($text : Text)- > $result : Text
166- $result := $text
167- While (Substring:C12 ($result ; 1 ; 1 )= " ")
168- $result := Substring:C12 ($result ; 2 )
169- End while
170- While (Substring:C12 ($result ; Length:C16 ($result ); 1 )= " ")
171- $result := Substring:C12 ($result ; 1 ; Length:C16 ($result )- 1 )
172- End while
1+ Class constructor ()
2+ This:C1470 .onData := New object:C1471 ("text" ; "" )
3+ If (Is macOS:C1572)
4+ This:C1470 ._return := Char:C90 (10 )
5+ $path := Get 4D folder:C485 (Current resources folder:K5:16 )+ "Dropbox" + Folder separator:K24:12 + "dbxcli"
6+ This:C1470 ._Path := Convert path system to POSIX:C1106 ($path )
7+ Else
8+ This:C1470 ._return := Char:C90 (10 ) // Char(13)+Char(10)
9+ $path := Get 4D folder:C485 (Current resources folder:K5:16 )+ "Dropbox" + Folder separator:K24:12 + "dbxcli.exe"
10+ This:C1470 ._Path := Convert path system to POSIX:C1106 ($path )
11+ End if
12+
13+
14+ // MARK: FileTransfer
15+ Function getDirectoryListing ($targetpath : Text)- > $success : Object
16+ If ($targetpath= "")
17+ $targetpath := "/"
18+ End if
19+ $url := "ls -l " + $targetpath
20+ $success := This:C1470 ._runWorker ($url )
21+ If ($success .success )
22+ // data contains a text based dir listing
23+ This:C1470 ._parseDirListing ($success )
24+ End if
25+
26+ Function upload ($sourcepath : Text; $targetpath : Text)- > $success : Object
27+ // $sourcepath just file name for local directory, else full path in POSIX syntax
28+ // targetpath is full remote path (starting with /, ending with file name
29+ ASSERT:C1129 ($sourcepath # "" ; "source path must not be empty" )
30+ ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
31+
32+ $url := "put " + $sourcepath + " " + $targetpath
33+ $success := This:C1470 ._runWorker ($url )
34+
35+ Function download ($sourcepath : Text; $targetpath : Text)- > $success : Object
36+ // $sourcepath just file name for local directory, else full path in POSIX syntax
37+ // targetpath is full remote path (starting with /, ending with file name
38+ ASSERT:C1129 ($sourcepath # "" ; "source path must not be empty" )
39+ ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
40+
41+ $url := "get " + $sourcepath + " " + $targetpath
42+ $success := This:C1470 ._runWorker ($url )
43+
44+ Function createDirectory ($targetpath : Text)- > $success : Object
45+ ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
46+ $url := "mkdir " + $targetpath
47+ $success := This:C1470 ._runWorker ($url )
48+
49+ Function deleteDirectory ($targetpath : Text; $force : Boolean)- > $success : Object
50+ ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
51+ If ($force)
52+ $url := "rm -f " + $targetpath
53+ Else
54+ $url := "rm " + $targetpath
55+ End if
56+ $success := This:C1470 ._runWorker ($url )
57+
58+ Function deleteFile ($targetpath : Text)- > $success : Object
59+ // same as deleteDirectory
60+ ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
61+ $url := "rm " + $targetpath
62+ $success := This:C1470 ._runWorker ($url )
63+
64+ Function renameFile ($sourcepath : Text; $targetpath : Text)- > $success : Object
65+ ASSERT:C1129 ($sourcepath # "" ; "source path must not be empty" )
66+ ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
67+ $url := "mv " + $sourcepath + " " + $targetpath
68+ $success := This:C1470 ._runWorker ($url )
69+
70+ Function moveFile ($sourcepath : Text; $targetpath : Text)- > $success : Object
71+ $success := This:C1470 .renameFile ($sourcepath ; $targetpath )
72+
73+ Function copyFile ($sourcepath : Text; $targetpath : Text)- > $success : Object
74+ ASSERT:C1129 ($sourcepath # "" ; "source path must not be empty" )
75+ ASSERT:C1129 ($targetpath # "" ; "target path must not be empty" )
76+ $url := "cp " + $sourcepath + " " + $targetpath
77+ $success := This:C1470 ._runWorker ($url )
78+
79+ // MARK: Settings
80+ Function version ()- > $data : Object
81+ $data := This:C1470 ._runWorker ("version" )
82+
83+ Function setPath ($path : Text)
84+ This:C1470 ._Path := $path
85+
86+ Function useCallback ($callback : 4D:C1709 .Function ; $ID : Text)
87+ ASSERT:C1129 (Value type:C1509 ($callback )= Is object:K8:27 ; "Callback must be of type function" )
88+ ASSERT:C1129 (OB Instance of:C1731 ($callback ; 4D:C1709 .Function ); "Callback must be of type function" )
89+ ASSERT:C1129 ($ID # "" ; "Callback ID Method must not be empty" )
90+ This:C1470 ._Callback := $callback
91+ This:C1470 ._CallbackID := $ID
92+ This:C1470 ._noProgress := False:C215
93+
94+ Function setAsyncMode ($async : Boolean)
95+ This:C1470 ._async := $async
96+
97+ Function stop ()
98+ If (This:C1470 ._worker # Null:C1517)
99+ This:C1470 ._worker .terminate ()
100+ End if
101+
102+ Function status ()- > $status : Object
103+ $status := New object:C1471
104+ $status .terminated := This:C1470 ._worker .terminated
105+ $status .response := This:C1470 ._worker .response
106+ $status .responseError := This:C1470 ._worker .responseError
107+ $status .exitCode := This:C1470 ._worker .exitCode
108+ $status .errors := This:C1470 ._worker .errors
109+
110+ Function wait ($max : Integer)
111+ This:C1470 ._worker .wait ($max )
112+
113+ // MARK: Internal helper calls
114+ Function _parseDirListing ($success : Object)
115+ $col := Split string:C1554 (String:C10 ($success .data ); This:C1470 ._return ; sk ignore empty strings:K86:1 )
116+ If (($col .length > 0) && ($col[0 ]= "Revision@"))
117+ $success .list := New collection:C1472
118+ $posSize := Position:C15 ("Size" ; $col [0 ])
119+ $posLast := Position:C15 ("Last" ; $col [0 ])
120+ $posPath := Position:C15 ("Path" ; $col [0 ])
121+ If (($posSize# 0) & ($posLast# 0) & ($posPath# 0))
122+ For each ($line; $col; 1)
123+ $diritem := New object:C1471
124+ $diritem .revision := This:C1470 ._trim (Substring:C12 ($line ; 1 ; $posSize - 1 ))
125+ $diritem .size := This:C1470 ._trim (Substring:C12 ($line ; $posSize ; $posLast - $posSize - 1 ))
126+ $diritem .date := This:C1470 ._trim (Substring:C12 ($line ; $posLast ; $posPath - $posLast - 1 ))
127+ $diritem .path := This:C1470 ._trim (Substring:C12 ($line ; $posPath ))
128+ $success .list .push ($diritem )
129+ End for each
130+ Else // error?
131+ $success .success := False:C215
132+ $success .responseError := "Directory listing unexpected format"
133+ End if
134+ Else
135+ $success .success := False:C215
136+ $success .responseError := "Directory listing unexpected format"
137+ End if
138+
139+ Function _runWorker ($para : Text)- > $result : Object
140+ If (This:C1470 ._Callback # Null:C1517)
141+ $workerpara := cs:C1710 .SystemWorkerProperties .new (This:C1470 .onData; This:C1470 ._Callback; This:C1470 ._CallbackID)
142+ Else
143+ $workerpara := cs:C1710 .SystemWorkerProperties .new (This:C1470 .onData)
144+ End if
145+
146+ If ((This:C1470 ._Path ) && (This:C1470 ._Path # ""))
147+ $path := This:C1470 ._Path
148+ Else
149+ // TODO: needs to be set to use Resource/Mac or /win
150+ $path := "dbxcli"
151+ End if
152+
153+ $command := $path + " " + $para
154+ $old := Method called on error:C704
155+ ON ERR CALL:C155 (Formula:C1597 (ErrorHandler).source )
156+ This:C1470 ._worker := 4D:C1709 .SystemWorker .new ($command ; $workerpara )
157+ $worker := This:C1470 ._worker
158+
159+ If ($worker# Null:C1517)
160+ If ((This:C1470 ._async # Null:C1517) && (This:C1470 ._async ))
161+ $result := New object:C1471 ("data" ; "async" ; "success" ; True:C214 )
162+ Else
163+ $worker .wait ()
164+ If (($worker .responseError # Null:C1517) && ($worker .responseError # ""))
165+ $result := New object:C1471 ("responseError" ; $worker .responseError ; "success" ; False:C215 )
166+ $pos := Position:C15 ("Error:" ; $worker .responseError ; * )
167+ If ($pos> 0)
168+ $result .error := Replace string:C233 (Substring:C12 ($worker .responseError ; $pos + 6 ); Char:C90 (10 ); "" )
169+ Else
170+ 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.
171+ $result := New object:C1471 ("data" ; $worker .response ; "success" ; True:C214 )
172+ Else
173+ $result := New object:C1471 ("data" ; $worker .responseError ; "success" ; True:C214 )
174+ End if
175+ End if
176+ Else
177+ $result := New object:C1471 ("data" ; $worker .response ; "success" ; True:C214 )
178+ End if
179+ End if
180+ // dbxcli seems not to terminate directly,progress bar could be open too long
181+ If (This:C1470 ._Callback # Null:C1517)
182+ This:C1470 ._worker .onTerminate (New object:C1471; New object:C1471)
183+ End if
184+ Else
185+ $result := New object:C1471 ("success" ; False:C215 ; "responseError" ; "dbxcli execution error" )
186+ End if
187+ ON ERR CALL:C155 ($old )
188+
189+ Function _trim ($text : Text)- > $result : Text
190+ $result := $text
191+ While (Substring:C12 ($result ; 1 ; 1 )= " ")
192+ $result := Substring:C12 ($result ; 2 )
193+ End while
194+ While (Substring:C12 ($result ; Length:C16 ($result ); 1 )= " ")
195+ $result := Substring:C12 ($result ; 1 ; Length:C16 ($result )- 1 )
196+ End while
173197
0 commit comments