Last active
May 23, 2022 22:52
-
-
Save taragano/bb6900a7b8240a61a7641152b7689b7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cloudinary credentials: | |
Set Variable [ $cloud_name; Value:"mycloudname" ] | |
Set Variable [ $api_key; Value:1234567890 ] | |
Set Variable [ $api_secret; Value:"abcdabcdabcdabcd" ] | |
# Validate presence of a file in "File Container" | |
If [ IsEmpty ( GetField ( "File Container" ) ) ] | |
Show Custom Dialog [ Title: "Error"; Message: "No image to upload"; Default Button: “OK”, Commit: “Yes”; Button 2: “Cancel”, | |
Commit: “No” ] | |
Exit Script [ Result: "No image to upload" ] | |
End If | |
# Calculate the unix time. NOTE: if you use FileMaker Pro Advanced, you can include a custom function instead of lines 14-32. See https://www.briandunning.com/cf/64 for example: | |
Set Variable [ $offset_text; Value:GetAsText ( | |
GetAsTime ( | |
Get ( CurrentTimestamp ) | |
- Floor ( Get ( CurrentTimeUTCMilliseconds ) / 1000 ) | |
) | |
)] | |
Set Variable [ $colon_position; Value:Position ( $offset_text ; ":" ; 0 ; 1 ) ] | |
Set Variable [ $timezone_offset; Value:GetAsNumber ( Left ( $offset_text ; $colon_position-1 ) ) ] | |
Set Variable [ $timestamp; Value:If ( | |
Date ( (Month (Get ( CurrentTimestamp ))); (Day (Get ( CurrentTimestamp ))); Year ((Get ( CurrentTimestamp ))) ) ≥ (Date (1;1;1970)) | |
and | |
Date ( (Month (Get ( CurrentTimestamp ))); (Day (Get ( CurrentTimestamp ))); Year ((Get ( CurrentTimestamp ))) ) ≤ (Date (7;8;2038)); | |
(Seconds ( Get ( CurrentTimestamp ) )) + | |
(Minute ( Get ( CurrentTimestamp ) ) * 60) + | |
( (Hour (Get ( CurrentTimestamp )) - $timezone_offset) * 3600) + | |
((Date ( (Month (Get ( CurrentTimestamp ))); (Day (Get ( CurrentTimestamp ))); Year ((Get ( CurrentTimestamp ))) ) - (Date (1;1;1970))) * 86400) | |
;-1) | |
] | |
# Prepare payload: | |
Set Variable [ $server; Value:"https://api.cloudinary.com/v1_1/" & $cloud_name & "/image/upload" ] | |
Set Variable [ $file_to_upload; Value:Substitute ( GetAsURLEncoded ( "data:image/jpeg;base64," & Base64Encode ( GetField ( "File | |
Container" ) ) ) ; ["%0D"; ""] ; ["%0A"; ""] ) ] | |
Set Variable [ $public_id; Value:"my_public_id" ] | |
Set Variable [ $signature; Value:HexEncode ( CryptDigest ( "public_id=" & $public_id & "×tamp=" & $timestamp & $api_secret ; "SHA1" ) ) ] | |
Set Variable [ $data; Value:"file=" & $file_to_upload & "&public_id=" & $public_id & "×tamp=" & $timestamp & "&api_key=" & $api_key & "&signature=" & $signature ] | |
Set Variable [ $options; Value:"-d " & "\"" & $data & "\"" ] | |
Show Custom Dialog [ Title: "Uploading"; Message: "Uploading"; Default Button: “OK”, Commit: “Yes”; Button 2: “Cancel”, Commit: | |
“No” ] | |
Set Variable [ $response ] | |
# Perform the upload: | |
Insert from URL [ $response; $server; cURL options: $options ] | |
[ Do not automatically encode URL; Select; No dialog; Verify SSL Certificates ] | |
# Set fields with response | |
Set Field [ Content Management::Image public ID; JSONGetElement ( $response ; "public_id" ) ] | |
Set Field [ Content Management::Image version; JSONGetElement ( $response ; "version" ) ] | |
Set Field [ Content Management::Url; JSONGetElement ( $response ; "secure_url" ) ] | |
Set Field [ Content Management::Full response; $response ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment