This class is a wrapper for GDrive command line interface, a tool allowing upload, download or file manipulation on GDrive. https://github.com/prasmussen/gdrive
See https://github.com/ThomasMaul/FileTransfer_Class for details
var $ftp : cs.FileTransfer_GDrive
$ftp:=cs.FileTransfer_GDrive.new()
$source:="/product/4D.dmg"
$target:=Convert path system to POSIX(System folder(Desktop))
$result:=$ftp.download($source; $target)
If ($result.success)
...
End if| cs.FileTransfer_FileTransfer_GDrive_.new creates and returns a FileTransfer object allow to access Gdrive. |
| result parameter(#new) All transfer function returns a result object |
| .upload Upload a file to the server. |
| .download Download a file from the server. |
| .import Import (upload) a local file to a Google document. |
| .export Export (download) a Google document from the server as local file. |
| .getdirectorylisting Returns directory listing from remote server. |
| .createDirectory Creates a new directory on remote server. |
| .deleteDirectory Deletes a directory on remote server. |
| .deleteFile Deletes a file on remote server. |
| .renameFile Renames a file on remote server. |
| .moveFile Moves a file on remote server. |
| .executeCommand Allows to pass any valid GDrive command and directly execute it. |
| .version returns in result.data version information from Gdrive Command Line Interface Tool |
| .setPath Allows to specify the installation path. |
| .enableStopButton Display stop button in progress dialog. |
| .setAsyncMode By default all commands are executed synchronously, meaning the command do not return till execution is completed or a timeout occurred. This allows all command to return the result or execution information.. |
| .setTimeout sets a maximum worker execution time, stopping everything. |
| .stop Terminates the execution of a running operation, such as upload or download. |
| .status Returns informations about the execution of a running operation. |
| .wait Only useful in combination with setAsyncMode. |
| .useCallback Allows to show a progress bar during long running operations. |
creates and returns a FileTransfer object allow to access Google Drive account.
| Parameter | Type | Description | |
|---|---|---|---|
| result | cs.FileTransfer | <- | New FileTransfer object |
The cs.FileTransfer_GDrive.new() function creates and returns an object allow to access a GDrive account for file transfer operations, such as upload or download a document, get directory, as well as create and delete a directory or rename, delete or remove a document.
Internally the class uses gdrive to access the file server.
var $ftp : cs.FileTransfer
$ftp:=cs.FileTransfer_Gdrive.new()All function returns a result object
| Name | Type | Description |
|---|---|---|
| success | boolean | false if command failed |
| responseError | Text | error message if command failed |
| data | Text | optional: execution result |
| list | collection | optional: execution result |
| Parameter | Type | Description | |
|---|---|---|---|
| source | Text | -> | POSIX path to local file |
| targetpath | Text | -> | path to remote file or folder |
| result | Object | <- | result object |
Upload one file to server.
If file already exists another instance is created. It could make sense to delete existing files (or use deleteFile even if the file is not existing)
If source or target contains spaces, encapsulate with quotes (char(34)).
After uploading a file, the result object returns false or true.
| Parameter | Type | Description | |
|---|---|---|---|
| source | Text | -> | path to remote file |
| target | Text | -> | POSIX path to local file |
| fileID | Text | -> | optional: Google file ID |
| fileQuery | Text | -> | optional: Google Query to find file |
| result | Object | <- | result object |
Download one file from server.
If file already exists it is overwritten.
If source or target contains spaces, encapsulate with quotes (char(34)).
After downloading a file, the result object returns false or true.
Specify file with name (source) for comfort or compatibility with Dropbox/cURL class. Better/Faster to specify file by Google ID or Google Drive Query statement. Pass either ID, Query or source, only one.
For Query:
valid query command, such as
"name = 'test' and modifiedTime > '2012-06-04T12:00:00' and (mimeType contains 'image/' or mimeType contains 'video/')"
See: https://developers.google.com/drive/search-parameters
If the query returns 2 or more documents with the same name (but different locations), only the last one will be used.
| Parameter | Type | Description | |
|---|---|---|---|
| source | Text | -> | POSIX path to local file |
| targetpath | Text | -> | path to remote file or folder |
| mime | Text | -> | Mime for local file |
| result | Object | <- | result object |
Upload one file to server and import it as Google document.
If file already exists another instance is created. It could make sense to delete existing files (or use deleteFile even if the file is not existing)
If source or target contains spaces, encapsulate with quotes (char(34)).
After uploading a file, the result object returns false or true.
If mime is not passed, Google will do type recognition automatically. By passing the type, this can be overwritten. The import/converting follow this rules (status March 2022)
| From | To |
|---|---|
| application/vnd.ms-excel | application/vnd.google-apps.spreadsheet |
| application/x-vnd.oasis.opendocument.text | application/vnd.google-apps.document |
| image/x-bmp | application/vnd.google-apps.document |
| application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | application/vnd.google-apps.spreadsheet |
| image/gif | application/vnd.google-apps.document |
| image/jpeg | application/vnd.google-apps.document |
| application/vnd.openxmlformats-officedocument.wordprocessingml.template | application/vnd.google-apps.document |
| application/pdf | application/vnd.google-apps.document |
| application/vnd.ms-excel.template.macroenabled.12 | application/vnd.google-apps.spreadsheet |
| text/html | application/vnd.google-apps.document |
| application/vnd.openxmlformats-officedocument.presentationml.presentation | application/vnd.google-apps.presentation |
| application/vnd.google-apps.script+json | application/vnd.google-apps.script |
| application/x-vnd.oasis.opendocument.presentation | application/vnd.google-apps.presentation |
| application/vnd.oasis.opendocument.presentation | application/vnd.google-apps.presentation |
| text/csv | application/vnd.google-apps.spreadsheet |
| application/vnd.ms-powerpoint.slideshow.macroenabled.12 | application/vnd.google-apps.presentation |
| text/rtf | application/vnd.google-apps.document |
| application/vnd.oasis.opendocument.spreadsheet | application/vnd.google-apps.spreadsheet |
| image/png | application/vnd.google-apps.document |
| application/vnd.ms-powerpoint.template.macroenabled.12 | application/vnd.google-apps.presentation |
| application/vnd.sun.xml.writer | application/vnd.google-apps.document |
| application/vnd.google-apps.script+text/plain | application/vnd.google-apps.script |
| text/plain | application/vnd.google-apps.document |
| application/x-msmetafile | application/vnd.google-apps.drawing |
| image/bmp | application/vnd.google-apps.document |
| application/vnd.ms-word.document.macroenabled.12 | application/vnd.google-apps.document |
| application/vnd.openxmlformats-officedocument.spreadsheetml.template | application/vnd.google-apps.spreadsheet |
| image/x-png | application/vnd.google-apps.document |
| application/vnd.oasis.opendocument.text | application/vnd.google-apps.document |
| text/richtext | application/vnd.google-apps.document |
| application/vnd.ms-powerpoint.presentation.macroenabled.12 | application/vnd.google-apps.presentation |
| application/vnd.openxmlformats-officedocument.presentationml.template | application/vnd.google-apps.presentation |
| application/vnd.openxmlformats-officedocument.presentationml.slideshow | application/vnd.google-apps.presentation |
| application/vnd.openxmlformats-officedocument.wordprocessingml.document | application/vnd.google-apps.document |
| application/vnd.ms-word.template.macroenabled.12 | application/vnd.google-apps.document |
| application/rtf | application/vnd.google-apps.document |
| image/jpg | application/vnd.google-apps.document |
| application/vnd.ms-excel.sheet.macroenabled.12 | application/vnd.google-apps.spreadsheet |
| image/pjpeg | application/vnd.google-apps.document |
| application/x-vnd.oasis.opendocument.spreadsheet | application/vnd.google-apps.spreadsheet |
| application/msword | application/vnd.google-apps.document |
| application/vnd.ms-powerpoint | application/vnd.google-apps.presentation |
| text/tab-separated-values | application/vnd.google-apps.spreadsheet |
| Parameter | Type | Description | |
|---|---|---|---|
| source | Text | -> | path to remote file |
| target | Text | -> | POSIX path to local file |
| fileID | Text | -> | optional: Google file ID |
| mime | Text | -> | optional: overwrite mime |
| result | Object | <- | result object |
Download one document from server, automatically convert to local file type.
If file already exists it is overwritten.
If source or target contains spaces, encapsulate with quotes (char(34)).
After downloading a file, the result object returns false or true.
Specify file with name (source) for comfort or compatibility with Dropbox/cURL class. Better/Faster to specify file by Google ID. Pass either ID or source, only one.
| From | To |
|---|---|
| application/vnd.google-apps.document | application/rtf, application/vnd.oasis.opendocument.text, text/html, application/pdf, application/epub+zip, application/zip, application/vnd.openxmlformats-officedocument.wordprocessingml.document, text/plain |
| application/vnd.google-apps.spreadsheet | application/x-vnd.oasis.opendocument.spreadsheet, text/tab-separated-values, application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, text/csv, application/zip, application/vnd.oasis.opendocument.spreadsheet |
| application/vnd.google-apps.jam | application/pdf |
| application/vnd.google-apps.script | application/vnd.google-apps.script+json |
| application/vnd.google-apps.presentation | application/vnd.oasis.opendocument.presentation, application/pdf, application/vnd.openxmlformats-officedocument.presentationml.presentation, text/plain |
| application/vnd.google-apps.form | application/zip |
| application/vnd.google-apps.drawing | image/svg+xml, image/png, application/pdf, image/jpeg |
| application/vnd.google-apps.site | text/plain |
| Parameter | Type | Description | |
|---|---|---|---|
| target | Text | -> | path to remote |
| result | Object | <- | result object |
Returns directory listing from remote server.
result.data contains unfiltered answer from server. result.list contains collection, each representing one file/directory.
The answer is parsed and .list collection contains:
- Id // file Id, identifier for all other commands
- path // file name
- type // document type, such as doc, pdf, dir
- Size // size, such as 1.0 KB
- date // date, such as 2022-03-02 14:27:36
| Parameter | Type | Description | |
|---|---|---|---|
| target | Text | -> | path to remote |
| result | Object | <- | result object |
Creates a new directory on remote server.
| Parameter | Type | Description | |
|---|---|---|---|
| target | Text | -> | path to remote |
| force | Boolean | -> | true to delete none empty folders |
| result | Object | <- | result object |
Deletes a directory on remote server.
Note: only empty directories can be deleted. Delete all files before you delete the directory or pass true as second parameter (default false)
| Parameter | Type | Description | |
|---|---|---|---|
| target | Text | -> | path to remote |
| result | Object | <- | result object |
Deletes a file on remote server.
| Parameter | Type | Description | |
|---|---|---|---|
| source | Text | -> | path to remote file |
| target | Text | -> | path to renamed remote file |
| result | Object | <- | result object |
Deletes a file on remote server.
| Parameter | Type | Description | |
|---|---|---|---|
| source | Text | -> | path to remote file |
| target | Text | -> | path to renamed remote file |
| result | Object | <- | result object |
Moves a file on remote server to another directory (and/or rename it)
| Parameter | Type | Description | |
|---|---|---|---|
| command | Text | -> | command |
| result | Object | <- | result object |
Allows to pass any valid gdrive command and execute it. Result is returned directly.
| Parameter | Type | Description | |
|---|---|---|---|
| result | Object | <- | result object |
returns in result.data version information from cURL.
Example:
gdrive: 2.1.1
Golang: go1.17.8
OS/Arch: darwin/amd64
| Parameter | Type | Description | |
|---|---|---|---|
| Path | Text | -> | Path to local dbxcli installation |
Allows to use another gdrive installation.
Precompiled versions for Mac and Windows can be downloaded from: gdrive
| Parameter | Type | Description | |
|---|---|---|---|
| enable | Object | -> | Shared Object with Attribut Stop |
Only useable if included or similar ProgressCallback method is used. If passed it enables the stop button in progress bar, allowing the end user to abort the operation. If Stop button is clicked, attribut of shared object enable.stop is set to true
| Parameter | Type | Description | |
|---|---|---|---|
| async | Boolean | -> | asynchronous execution |
By default all commands are executed synchronously, meaning the command do not return till execution is completed or a timeout occurred. This allows all command to return the result or execution information.
If Asynchronous mode is enabled, all commands returns immediately, not waiting for execution. The result will not include useful informations.
Useful in combination with .stop(), .status() and .wait() calls or with useCallback.
Note: asynchronous only works if the 4D progress continues to run. As long the 4D process is alive, open commands will continue to execute. If the 4D process terminals, all still running FTP operations will end.
sets a maximum execution time for the worker. By default all operations are stopped after 60 seconds, upload or download after 600 seconds. If your operation might take longer, set a longer timeout. The timeout is not considered when asynchronous mode is enabled.
Terminates the execution of a running operation, such as upload or download. Only useful in combination with setAsyncMode.
Returns informations about the execution of a running operation, such as upload or download. Only useful in combination with setAsyncMode.
Return object contains:
| Name | Type | Description |
|---|---|---|
| terminated | boolean | true if command finished execution |
| responseError | Text | error message if command failed |
| response | Text | message if command succeeded |
| exitCode | Text | exit code returned by gdrive |
| errors | collection | optional: execution errors received by 4D |
| Parameter | Type | Description | |
|---|---|---|---|
| seconds | Integer | -> | max time in seconds |
Only useful in combination with setAsyncMode.
If upload/download commands are executed in a 4D form or another 4D worker, execution will automatically continue in background.
If they are executed in the current 4D process and you want to loop for poll for results, use .wait(1) instead of Delay Process.
The command returns after given wait time or before if execution is finished.
| Parameter | Type | Description | |
|---|---|---|---|
| callback | 4D.Function | -> | 4d function to call during progress |
| ID | Text | -> | text to pass to callback method to identify job |
Allows to show a progress bar during long running operations or to get informed when command execution is complete.
The callback method is called whenever a new progress message is available from gdrive and get's 3 parameter passed. The given ID, the progress text and the completeness ratio from 0-100%.
$ftp.useCallback(Formula(ProgressCallback); "Download 4D.dmg")
$ftp.setAsyncMode(True)
$result:=$ftp.download($source; $target)
Repeat
$ftp.wait(1) // needed while our process is running
// wait is not needed if a form would be open or if a worker would handle the job
$status:=$ftp.status()
Until (Bool($status.terminated))Method ProgressCallback
#DECLARE($ID : Text; $message : Text; $value : Integer)
// called from cs.FileTransfer if callback is set via .useCallback()
// $ID is set through code - $message comes from curl
var ProgressBarID : Integer
If (ProgressBarID=0)
ProgressBarID:=Progress New
Progress SET MESSAGE(ProgressBarID; $ID)
End if
If ($value=100)
Progress QUIT(ProgressBarID)
ProgressBarID:=0
Else
Progress SET PROGRESS(ProgressBarID; $value/100)
End if To support the stop button in the progress bar, Storage needs to be used to share progress bar and worker IDs. See example in test_curl - download.
// enable stop button in progress bar
Use (Storage.FileTransfer_Progress)
Storage.FileTransfer_Progress[$progressid]:=New shared object()
End use
$ftp.useCallback(Formula(ProgressCallback); $progressid)After execution of download/upload/etc, check:
If (Bool(Storage.FileTransfer_Progress[$progressid].Stop)) // check stop button if it was set, remove from storage
// user canceled!!Don't forget to remove the object from storage when done. See test_gdrive - download for a full example