Some of the data storage services are ready to securely read and store files and images from your Scriptcase system. This option also saves a local file to increase your reading time, and in case the local file is deleted, a copy will be working in the cloud!
Creating the Control application
1 – Create a Control type application
2 – We must create the necessary fields for uploading the files and the path of the directory to be saved. Fields: files , parent.
Creating the configuration fields
3 – We will create a new field called “ api_gateway ” that will allow the selection of the API used. The field will be of the type “Select” and we will create a manual lookup. The available APIs will be: Dropbox , Amazon S3 and Google Drive.
4 – Each of the types of API available requires additional configuration information. We will create the fields that should fill this information.
Dropbox : apikey, apisecret, accesstoken
Amazon S3 : apikey, apisecret, api_region, api_bucket
Google Drive : appname, jsonoauth, authcode, tokencode
Note 1 : The apikey and apisecret fields will be used for both Dropbox and Amazon S3.
Creating the control methods
We must now create control methods that will hide unnecessary fields according to the type of API that is selected, as well as a method for sending connection information to the API.
5 – The hide_fields method will hide the configuration fields when starting the application.
hide_fields
Hide_fields code :
sc_field_display({jsonoauth}, off);
sc_field_display({authcode}, off);
sc_field_display({tokencode}, off);
sc_field_display({api_region}, off);
sc_field_display({api_bucket}, off);
sc_field_display({appname}, off);
The hide_fields function will be called in the onScriptInit event.
6 – The show_settings method will display the fields according to the selected API.
show_settings
Show_settings code :
if($gateway_value == 'dropbox'){
sc_field_display({jsonoauth}, off);
sc_field_display({authcode}, off);
sc_field_display({tokencode}, off);
sc_field_display({api_region}, off);
sc_field_display({api_bucket}, off);
sc_field_display({appname}, off);
sc_field_display({apikey}, on);
sc_field_display({apisecret}, on);
sc_field_display({accesstoken}, on);
}
else if($gateway_value == 'drive'){
sc_field_display({apikey}, off);
sc_field_display({apisecret}, off);
sc_field_display({accesstoken}, off);
sc_field_display({api_region}, off);
sc_field_display({api_bucket}, off);
sc_field_display({appname}, on);
sc_field_display({jsonoauth}, on);
sc_field_display({authcode}, on);
sc_field_display({tokencode}, on);
}
else if($gateway_value == 's3'){
sc_field_display({jsonoauth}, off);
sc_field_display({authcode}, off);
sc_field_display({accesstoken}, off);
sc_field_display({appname}, off);
sc_field_display({apikey}, on);
sc_field_display({apisecret}, on);
sc_field_display({api_region}, on);
sc_field_display({api_bucket}, on);
}
Note : Use the option to add parameters to the function to add the gateway_value parameter
The show_settings function will be called in an ajax onChange event in the api_gateway field .
7 – The upload_file method will be used to send the connection information with the API.
upload_file
Upload_file code :
if($field_gateway == 'dropbox'){
sc_api_upload([
'settings' => [
'gateway' => 'dropbox',
'api_key' => {apikey},
'api_secret' => {apisecret},
'access_token' => {accesstoken},
],
'file' => {files},
'parents' => {parent},
]);
}
else if($field_gateway == 'drive'){
sc_api_upload([
'settings' => [
'app_name' => {appname},
'gateway' => 'drive',
'json_oauth' => {jsonoauth},
'auth_code' => {authcode},
'token_code' => {tokencode},
],
'file' => {files},
'parents' => {parent},
]);
}
else if($field_gateway == 's3'){
sc_api_upload([
'settings' => [
'app_name' => 'scriptcase',
'gateway' => 's3',
'api_key' => {apikey},
'api_secret' => {apisecret},
'region' => {api_region},
'bucket' => {api_bucket},
],
'file' => {files},
'parents' => {parent},
]);
}
Note : Use the option to add parameters to the function to add the gateway_value parameter
The upload_file method will be called in the onValitySuccess event
8 – Save and run the application.
Comment this post