I'm having problems uploading dropped folder with cURL

Hi script wizzers

I have been crawling through this forum search for snippets and have cobbled together the following scripts.

The problem is, I get a response with it saying that cURL has uploaded ‘0 of xxxxx’ for each file along with other relevant this-is-not-working info.

I have included some display dialogs for debugging but to no avail. Of course I am not a frequent flyer with AS so please be patient.

Any help or advice would be most appreciated :slight_smile:

Heretis:

property serverName : "ftp://ftp.server.com/pix/" -- ¢¢¢ change this to test !!!

on open theFolders
	repeat with i from 1 to length of theFolders
		my walkMe(item i of theFolders)
	end repeat
end open

on walkMe(thisContainer)
	try
		tell application "Finder" to set theFileItems to every item of thisContainer --assuming that thisContainer is a folder.
		repeat with afile2upload in theFileItems
			if ((afile2upload as string) ends with ":") then
				walkMe(afile2upload) --afile2upload looks like a folder
			else
				uploadThis(afile2upload)
			end if
		end repeat
	end try
	display dialog "Seem to have completed upload to " --& serverName
end walkMe


on uploadThis(macFilePath)
	try
		display dialog "About to do " & macFilePath
		set userName to "user" -- ¢¢¢ change this to test !!!
		set userPasswd to "password" -- ¢¢¢ change this to test !!!
		
		set remoteURL to serverName
		
		simpleFtpUpload(remoteURL, macFilePath, userName, userPasswd)
		
	on error theError
		display dialog theError
	end try
	
end uploadThis

on simpleFtpUpload(remoteURL, macFilePath, userName, userPasswd)
	-- version 1.2, Dan Shockley (http://www.danshockley.com)
	-- uses curl to do the upload
	-- remoteURL is the complete ftp url to the remote destination directory
	try
		if userName is "" then
			set userName to "user"
			set userPasswd to "password"
			-- no email address, change if desired
		end if
		--display dialog "About to setup cURL instruction "
		-- get parentFolder
		if (macFilePath as string) ends with ":" then -- it is a folder itself
			set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
			set parentFolder to (text items 1 thru -3 of (macFilePath as string)) as string
			set AppleScript's text item delimiters to od
		else -- it is just a file
			set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
			set parentFolder to (text items 1 thru -2 of (macFilePath as string)) as string
			set AppleScript's text item delimiters to od
		end if
		
		display dialog "About to do POSIX instruction..." & return & "set localPosixPath to quoted form of POSIX path of alias macFilePath"
		--set localPosixPath to quoted form of POSIX path of alias macFilePath
		--set localPosixPath to POSIX path of macFilePath --(POSIX path of (macFilePath as alias))
		set localPosixPath to (POSIX path of (macFilePath as alias))
		
		display dialog "About to do basename shell script for uploadFileName..." & return & localPosixPath
		set uploadFileName to do shell script "basename " & localPosixPath
		display dialog "Just done basename shell script for localPosixPath..." & return & (uploadFileName as text)
		set uploadDirectory to quoted form of POSIX path of parentFolder
		
		-- curl --upload-file SOMEFILE ftp://SERVER.com --user MYUSER:THEPASSWD
		-- must be IN the directory of the file you want to upload
		set myCommand to "cd " & uploadDirectory & ";" & "curl --upload-file "
		set myCommand to myCommand & quoted form of uploadFileName
		set myCommand to myCommand & " " & remoteURL
		set myCommand to myCommand & " --user " & userName & ":" & userPasswd
		set myCommand to myCommand & " " & "--write-out " & "%{size_upload}"
		set myCommand to myCommand & " --quote -quit"
		
		-- output is the 'size_upload' result from curl
		
		set uploadResult to do shell script myCommand
		
		return uploadResult -- size uploaded in kilobytes
	on error errMsg number errNum
		error "simpleFtpUpload FAILED: " & errMsg number errNum
	end try
end simpleFtpUpload

Kind regards, nodrog.

Hi Scripters

I guess what I am asking is if someone with ftp access could try this script by saving the script as an application, and dropping a folder with one or two files inside it and see if it can be made to work (i.e. upload those same files to the specified folder on the ftp server without error).

I think it is very close but not quite there.

The lines that will need to be changed are marked with “-- ¢¢¢ change this to test !!!”

Hope this clarifies my post :slight_smile:

Kind regards, nodrog.

Model: 2 GHz Intel Core Duo
AppleScript: 1.10.7
Browser: Firefox 3.0.5
Operating System: Mac OS X (10.4)

Hi,

actually this should do the job. I tested it successfully with my server data
The script uses the shell find command to find all files recursively in the dropped folders.
The find command can also execute the upload of the found files via curl in the same line.
There’s a little check whether the dropped item is a folder


property serverName : "ftp://ftp.server.com/pix/" -- ¢¢¢ change this to test !!!
property userName : "user" -- ¢¢¢ change this to test !!!
property userPasswd : "password" -- ¢¢¢ change this to test !!!

on open theFolders
	set myCommand to "curl " & serverName & " -u " & userName & ":" & userPasswd & " -T {} \\;"
	repeat with oneFolder in theFolders
		set {folder:Fo, package folder:Pa} to info for oneFolder
		if Fo and not Pa then
			do shell script "find " & quoted form of POSIX path of oneFolder & " -type f ! -name '.*' -print -exec " & myCommand
		end if
	end repeat
end open

Edit: Important note: As in your original script new files overwrite existing files on the server with the same file name !

Hi StefanK – this message edited to make the script now work properly as per StefanK’s advice

Thanks for you learned and efficient response to my query.

Sadly, I do not have the same response as you seem to to get with your script.

My unmodified attempt seemed to do something though suspiciously quickly. When I checked the server, no files appeared.

I reset the permissions on the URL site to ensure that nothing was being blocked by the server (using cyberduck).

I then added a display dialog to show some feedback. This is the listing I used:


property serverName : "ftp://ftp.server.com/public_html/server.com/pix/" --"ftp://ftp.server.com/public_html/server.com/pix" -- ¢¢¢ change this line to test !!! 
--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==
--  ADDED A SLASH ABOVE AS PER STEFANK'S ADVICE AND... VOILA!!! SUCCESS AT LAST  -- (WHAT AN AWESOME DUDE STEFANK IS!!!) --
-- From this "ftp://ftp.server.com/public_html/server.com/pix" ....... to this "ftp://ftp.server.com/public_html/server.com/pix/"
--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==
property userName : "username" -- ¢¢¢ change this line to test !!!
property userPasswd : "password" -- ¢¢¢ change this line to test !!!

on open theFolders
	set myCommand to "curl " & serverName & " -u " & userName & ":" & userPasswd & " -T {} \\;"
	repeat with oneFolder in theFolders
		set {folder:Fo, package folder:Pa} to info for oneFolder
		if Fo and not Pa then
			display dialog "Attempting to upload " & oneFolder
			try
				do shell script "find " & quoted form of POSIX path of oneFolder & " -type f ! -name '.*' -print -exec " & myCommand
			on error errMsg number errNum
				error "stefanKsFabulousFtpUpload FAILED: " & errMsg number errNum
			end try
		end if
	end repeat
end open

Dialog correctly mentions dragged folder name. Short pause… ended.

Added a try and on error trap…

Again, sadly, no files were uploaded, & no intelligence returned suggesting a possible reason.

Any clues?

Kind regards, nodrog.

the path in serverName must end with a slash (/)

Hi StefanK

Thank you for your help. This absolutely did the trick. Your attention to detail is sublime :wink:

You should receive a knighthood shortly, I sent it via American Express. Queen Elizabeth will be sending you a telegram also to confirm this.

Do you have a preference for your sword or will Excalibur do? The Beefeaters are prying Excalibur from King Arthur’s sarcophagus as we speak.

Excellent work you have done here, most appreciated and simply awesome.

:smiley:

Kind regards, nodrog.