Oooops!
Actually I forgot about this already ;-)
Lagi Pittas wrote:ftp_Rawlist() works in a brain dead way - what were they thinking it's more code to create it and more code to decipher the results, a lose/lose.
Right, it's a PITA. And worse, LC's "put URL myFTPDirectoryURL" gives the same crap as result. With the icing of the cake of not working on Android at all where I need it ...
I wanted to document my solution anyways, so here we go:
What I wanted to do was only to check if a local file (of an Android app) is outdated and if it is, downloading the fresh version from the FTP. To do this I now use a .php script (one in each directory, they need the directory name in the script ...). Since having login data in a PHP script gives me bad feelings, I use a special ftp User that can't do anything but "list" the semi-public directory part only.
The PHP script:
Code: Select all
<?php
$c = ftp_connect('localhost');
ftp_login($c, 'USER', 'PASSWORD');
$data = ftp_rawlist($c, '/DIRECTORY');
print_r($data);
?>
PHP output (be sure to have correct USER, PASSWORD and DIRECTORY in the script):
Code: Select all
Array
(
[0] => drwxr-xr-x 2 xxxxXXXX yyyyYYYY 4096 Feb 18 00:35 .
[1] => drwxr-xr-x 7 xxxxXXXX yyyyYYYY 4096 Feb 5 10:25 ..
[2] => -rw-r--r-- 1 xxxxXXXX yyyyYYYY 84754 Feb 16 16:14 MM_Ordermaker.rev
[3] => -rw-r--r-- 1 xxxxXXXX yyyyYYYY 30093 Feb 6 12:51 ckommprint.rev
[3] => -rw-r--r-- 1 xxxxXXXX yyyyYYYY 139 Feb 5 10:27 fLIST.php
)
To compare, the LC version "
put URL myFTPDirectoryURL", if invoked in a supported environment, returns that:
Code: Select all
drwxr-xr-x 2 xxxxXXXX yyyyYYYY 4096 Feb 18 00:35 .
drwxr-xr-x 7 xxxxXXXX yyyyYYYY 4096 Feb 5 10:25 ..
-rw-r--r-- 1 xxxxXXXX yyyyYYYY 84754 Feb 16 16:14 MM_Ordermaker.rev
-rw-r--r-- 1 xxxxXXXX yyyyYYYY 30093 Feb 6 12:51 ckommprint.rev
-rw-r--r-- 1 xxxxXXXX yyyyYYYY 139 Feb 5 10:27 fLIST.php
Obviously both are the output of the same unixoid shell command, just wrapped differently.
Additional difficulty:
Since I want to compare to local files I want to use "
the detailed files" too, that gives a quite different output:
Code: Select all
ckommprint.rev,30093,,1486384526,1486402901,1486405661,,,,666,
MM_Ordermaker.rev,84754,,1486715345,1487257875,1487268402,,,,666
So the function takes, as input, either above .PHP output, or the output of the above "detailed files" call, with "true" as parameter "isLocal":
Code: Select all
function clearLIST myData, isLocal -- Goal: to get nice dir listings
if not isLocal then -- this is the output of the .php call
delete line 1 to 2 of myData
delete last line of myData
repeat until offset(" ", myData) = 0 -- at first, kill double spaces in the data
replace " " with " " in myData -- might not be neccessary, but in any case ...
end repeat
convert the date to dateitems
put char 1 to 4 of it into myYear -- the current year to use as dummy
set itemdel to space -- a suitable delimiter
repeat for each line myLine in myData
delete char 1 to (offset(">",myLine)+12) of myLine -- removing junk
if (char 1 of myLine <> "1") OR (myLine = empty) then next repeat -- ignore all but files
convert "Mon, " & item -3 of myLine & " " & item -4 of myLine & " " & \
myYear & " 00:00:00 +0000" from internet date to dateitems -- date magick :)
put char 6 to (offset(comma,it,8)+7) of it into fileDate
replace comma with "-" in fileDate -- now we have a nice, comparable SQL date fragment :)
put (item -1 of myLine & tab & item -5 of myLine & tab & \
fileDate & CR) after myVar -- creating output variable
end repeat
else -- This is the result of a "detailed files" call
set itemdel to comma
repeat for each line myLine in myData
get item 5 of myLine
convert it from seconds to dateitems
put char 6 to (offset(comma,it,8)+7) of it into fileDate
replace comma with "-" in fileDate -- now we have a nice, comparable SQL date fragment :)
put (item 1 of myLine & tab & item 2 of myLine & tab & \
fileDate & CR) after myVar -- creating output variable
end repeat
end if
delete last char of myVar -- cleaning up
return myVar -- done:
-- output: {fList2.php 201 1-6} // Filename tab ByteSize tab DatePart return
end clearLIST
The output looks like this:
Code: Select all
-- .PHP output:
MM_Ordermaker.rev 84754 2-16
ckommprint.rev 30093 2-6
fLIST.php 139 2-5
-- "detailed files" output:
ckommprint.rev 30093 2-6
MM_Ordermaker.rev 84754 2-16
Now I can compare. Using this construction:
Code: Select all
set the defaultfolder to gPath
put clearlist(the detailed files,true) into myLocList -- load LISTs to compare if new stuff to dl'
put clearLIST(URL ("http://WhereEver.biz/MM_Setup/" & UserDir & "/fLIST.php")) into myRemList
...
put line lineoffset(item i of myFile & ".rev",myLocList) of myLocList into myLoc
put line lineoffset(item i of myFile & ".rev",myRemList)of myRemList into myRem
...
if ((item 2 of myLoc <> item 2 of myRem) OR \ -- size is different?
(item 3 of of myRem > item 3 of of myLoc)) then -- remote file is newer?
I determine what to replace. Works sufficiently well - after all there's rarely a file change w/o a change in the byte size.
Lagi Pittas wrote:Basically as I understand it If the File was created in the current year you get the time returned but not the year, and if the creation date was in the last year you don't get the time but get the year.
Oops. Canned madness, family pack. Anyways, I ignore this time/ year fragment already ;-))
Took me a lot of hair pulling, this. Especially the testing on the Android device (my first real Android job) where I had to find out the hard way how poor LCs Android implementation actually is :/
Anyways. I changed the subject, may this save some poor Android dev some trouble, some day!
Have fun!
Edit: Maybe a Mod can push this over to the Android section where it would suit better? Thx!