Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 627 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

A client asked about uploading via SFTP and using rename command. There is a trick to use quote commands to rename the file when the uploaded finished. We like to avoid having a half uploaded file, so we upload to a temporary file first and then rename it.

Take a look on this script:

Set Variable [ $curl ; Value: MBS("CURL.New") ] 
Set Variable [ $result ; Value: MBS( "CURL.SetOptionURL"; $curl; CURL Test::URL & "temp.jpg") ] 
# login via password
Set Variable [ $result ; Value: MBS( "CURL.SetOptionPassword"; $curl; CURL Test::Password) ] 
Set Variable [ $result ; Value: MBS( "CURL.SetOptionUsername"; $curl; CURL Test::Name) ] 
Set Variable [ $result ; Value: MBS( "CURL.SetOptionSSHAuthTypes"; $curl; 2+8 ) ] 
# data to upload from container field
Set Variable [ $result ; Value: MBS("CURL.SetOptionUpload"; $curl; 1) ] 
Set Variable [ $result ; Value: MBS("CURL.SetInputFile"; $curl; CURL Test::Image) ] 
# Require TLS 1.2 for FTP over SSL:
Set Variable [ $result ; Value: MBS( "CURL.SetOptionUseSSL"; $curl; 3 ) ] 
Set Variable [ $result ; Value: MBS( "CURL.SetOptionSSLVersion"; $curl; 6 ) ] 
# download existing file, rename after upload
Set Variable [ $result ; Value: MBS( "CURL.SetOptionPostQuote"; $curl; "*rm image.jpg"; "rename temp.jpg image.jpg" ) ] 
# see progress in debug messages
Set Variable [ $result ; Value: MBS( "CURL.SetDebugWithProgress"; $curl; 1 ) ] 
# perform it
Set Field [ CURL Test::Result ; MBS("CURL.Perform"; $curl) ] 
Set Field [ CURL Test::debug ; MBS("CURL.GetDebugMessages"; $curl) ] 
Set Field [ CURL Test::Output ; MBS("CURL.GetResultAsText"; $curl) ] 
Set Variable [ $result ; Value: MBS("CURL.Release"; $curl) ] 

The key element here is that we pass commands with CURL.SetOptionPostQuote to run after the transfer. The "rm" command deletes the file if it exists. To avoid CURL reporting an error, we prefix it with a star to ignore the error, if the file is not there. The rename command then changes the file name. If you like, you can use quotes here around file names to have it work with special characters in the file name. Especially do that if the user enters the file name and make sure they don't include quote characters themselves and introduce their own commands here!

 

As you see we build the URL from the given server URL, e.g. "sftp://testserver.com/test/". This may include the path to the right folder. But usually you start without a folder to look where you end up being. For a recent user, you use an URL without a folder and you end up in the root folder of the machine. Not perfect as we want to be in the folder for the website, so we need to use a full path there like "/clients/testserver/web/". Another account on the same server starts in the user home folder for that user, so we are inside "/clients/testserver" already and don't need that path in the URL. The result is that "sftp://testserver.com/test/" with one account is the same folder as "sftp://testserver.com/clients/testserver/test/" in the other account because of the different root folders used!

 

Directory listing can be done with a download fron an URL ending with a slash as in this script:

Set Variable [ $curl ; Value: MBS("CURL.New") ] 
Set Variable [ $result ; Value: MBS( "CURL.SetOptionURL"; $curl; "sftp://testserver.com/" ) ] 
# login via password
Set Variable [ $result ; Value: MBS( "CURL.SetOptionPassword"; $curl; CURL Test::Password) ] 
Set Variable [ $result ; Value: MBS( "CURL.SetOptionUsername"; $curl; CURL Test::Name) ] 
Set Variable [ $result ; Value: MBS( "CURL.SetOptionSSHAuthTypes"; $curl; 2+8 ) ] 
# Require TLS 1.2 for FTP over SSL:
Set Variable [ $result ; Value: MBS( "CURL.SetOptionUseSSL"; $curl; 3 ) ] 
Set Variable [ $result ; Value: MBS( "CURL.SetOptionSSLVersion"; $curl; 6 ) ] 
# perform it
Set Field [ CURL Test::Result ; MBS("CURL.Perform"; $curl) ] 
Set Field [ CURL Test::debug ; MBS("CURL.GetDebugMessages"; $curl) ] 
Set Field [ CURL Test::Output ; MBS("CURL.GetResultAsText"; $curl) ] 
Set Variable [ $result ; Value: MBS("CURL.Release"; $curl) ] 

This provides the output of the "ls" command on the server. Use CURL.SetOptionDirListOnly with value 1 to only get a file list.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.