Aww what the heck, these two are mostly complete, and pretty generic. Ones calls back, the other either returns the file pronto, or delays the callback until when the file does arrive… If you can figure out how to use them, then your halfway done
[lua]
local pendingDownloads = {} – Files we’ve called to download, but haven’t arrived yet.
local function downloadComplete(event) – Calls back original source, clears the pending flag…
if( event.url ~= nil ) then
pendingDownloadsevent.url – Call the completion listener…
pendingDownloads[event.url] = nil – And remove the pending flag…
end
return true
end
–
– download file takes a full url as arg, and checks actual filename at end to see if it is in cache already. If not, sends the request.
– NOTE: uses system tempDirectory for storiing/caching files… When complete, calls back completionListener.
–
function downloadFile(fileLocation, completionListener)
– print(" – downloadFile() – fileLocation == “, fileLocation)
– Let’s get the base filename, without path…
local fileName = getBaseFilename(fileLocation)
– print( " — Found the base fileName, string is: " … fileName )
– Now determine if the file is already cached…
if( cacheFileExists(fileName) ) then
local tempEvent = {}
tempEvent.response = fileLocation
tempEvent.phase = “ended”
tempEvent.url = fileLocation
– print(” **** Found the cache file, calling back ***")
completionListener(tempEvent) – Simulate OS callback event struct…
else
if( pendingDownloads[fileLocation] ~= nil ) then – We’re still waiting for it??
– print(" **** DOWNLOAD STILL PENDING FOR: “, fileLocation)
return
else – else not pending or chaced, start the downoload…
pendingDownloads[fileLocation] = completionListener
network.download( fileLocation, “GET”, downloadComplete, fileName ,system.TemporaryDirectory )
– print(” ************************ Downloading actual file ==", fileLocation)
end
end
end
[/lua]
and a couple of the little support functions:
[lua]
– cacheFileExists() – returns true if file exists in app Temp folder, false if not… Done this way because some web servers return large 404 files which count as files… so filesize must be greater than 512 to count/be able to tell…
function cacheFileExists(theFile)
local retVal = false – By default, file doesn’t exist
local path = system.pathForFile( theFile, system.TemporaryDirectory )
if( path ) then
– print("***** Path for File ==", path)
local fh, errStr = io.open( path, “r” )
if( fh ) then
local fileLength = fh:seek(“end”)
– print(" – cache file fileLength == ", fileLength )
if( fileLength > 512 ) then – Our server returns an html file with message 404 error that is about 400 bytes long if graphic file requested is NOT there… so check for length of file server returned… Your mileage may vary.
retVal = true – Tell the caller that the file DOES exist…
end
io.close(fh) – close the file…
end
end
return retVal
end
function getBaseFilename(fileLocation) – Gets the base filename from a long path string ( the stuff after the last slash \ )
– Let’s get the base filename, without path…
local startChar=1
local endChar=1
local fileName = fileLocation
– String find is giving me a hard time searching from the end, so we will whittle it down from the beginning…
– I really should read up and practice with the string library
– This entire function could probably be replaced with 1 line of code.
while ( startChar ~= nil ) do
startChar, endChar = string.find(fileName, “/”)
– print(" – found char at position: “, startChar)
if( startChar ~= nil ) then
fileName = string.sub(fileName, startChar+1, -1) – Get the sub to the end of the string
– print(” - fileName = ", fileName)
end
end
return fileName
end
[/lua]