Detecting success with network.download

I’m using network.download to grab a series of files from a website. The problem is these are files created every 15 minutes, and are named with time and date as part of name. eg. radar_201301271215.gif

I can’t always guarantee that the image is there. I’m trying to use event.status to detect success (code 200) but this doesn’t always seem to work. Sometimes I’m left with blank or empty images.

I don’t think there is a way to check for filesize on the images in system.TemporaryDirectory, nor a way to check on filesize at the end of download.

Any ideas? [import]uid: 120570 topic_id: 35385 reply_id: 335385[/import]

Hi @conor1.

There are a couple of things that you may or may not be doing. Check the event.isError value. I’m not sure you would get a event.status of 200 (or any event.status) on isError, but you should check it. It’s possible that you might try to grab the file while the server is writing it, which would generate in a 0 byte or short file that would still return a 200 status code. Programming around this race condition is a bit of a challenge.

Now once you get the file, you can use LFS to get the file size and if it’s too short (you should have a good idea on what size your radar images are) then delete it and try again.
[import]uid: 199310 topic_id: 35385 reply_id: 140707[/import]

Like Rob said, probably the first thing to do is check the event.isError when the server calls back with the result… If it asynchronous, ie; your app just wants to check whenever, like the instant the user hits a button (and you don’t want to wait for a laggy or missed server response), then maybe something like:

function doesExist(theFile) -- checks for file we got from server in temporary dir  
 local retVal = false   
 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(" -- 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 the file that server returned...  
 retVal = true -- Tell the caller that the file DOES exist...  
 end  
 io.close(fh) -- close the file...  
 end  
 end  
  
 return retVal  
end  

Oh, also, I found out about the 404 file not found html issue with my file server by using the corona simulator on the mac. You can open the sandbox, and see your apps temporary directory… All the files it creates as it runs, and open them with your mac apps to look them over. Quite handy. Don’t use the windows simulator, but I’d imagine you can do the same thing. [import]uid: 79933 topic_id: 35385 reply_id: 140710[/import]

mpappas,

That does the trick, many thanks.
I think the network.download might be a little hitandmiss on a phone. I think I’m getting a 200 response and still missing the image. Hard to be sure on the phone. On simulator, network response is so fast It’s not a fair test.

I had missed the seek(“end”) call, so thanks for revealing it. [import]uid: 120570 topic_id: 35385 reply_id: 140720[/import]

Hi @conor1.

There are a couple of things that you may or may not be doing. Check the event.isError value. I’m not sure you would get a event.status of 200 (or any event.status) on isError, but you should check it. It’s possible that you might try to grab the file while the server is writing it, which would generate in a 0 byte or short file that would still return a 200 status code. Programming around this race condition is a bit of a challenge.

Now once you get the file, you can use LFS to get the file size and if it’s too short (you should have a good idea on what size your radar images are) then delete it and try again.
[import]uid: 199310 topic_id: 35385 reply_id: 140707[/import]

Like Rob said, probably the first thing to do is check the event.isError when the server calls back with the result… If it asynchronous, ie; your app just wants to check whenever, like the instant the user hits a button (and you don’t want to wait for a laggy or missed server response), then maybe something like:

function doesExist(theFile) -- checks for file we got from server in temporary dir  
 local retVal = false   
 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(" -- 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 the file that server returned...  
 retVal = true -- Tell the caller that the file DOES exist...  
 end  
 io.close(fh) -- close the file...  
 end  
 end  
  
 return retVal  
end  

Oh, also, I found out about the 404 file not found html issue with my file server by using the corona simulator on the mac. You can open the sandbox, and see your apps temporary directory… All the files it creates as it runs, and open them with your mac apps to look them over. Quite handy. Don’t use the windows simulator, but I’d imagine you can do the same thing. [import]uid: 79933 topic_id: 35385 reply_id: 140710[/import]

mpappas,

That does the trick, many thanks.
I think the network.download might be a little hitandmiss on a phone. I think I’m getting a 200 response and still missing the image. Hard to be sure on the phone. On simulator, network response is so fast It’s not a fair test.

I had missed the seek(“end”) call, so thanks for revealing it. [import]uid: 120570 topic_id: 35385 reply_id: 140720[/import]

Hi,

Is there a way to get the size of a folder created in my DocumentsDirectory and where I download my files?

TIA

Alberto. [import]uid: 44013 topic_id: 35385 reply_id: 143300[/import]

You can use the LFS module to get the file sizes.

See this tutorial:

http://www.coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/

[import]uid: 199310 topic_id: 35385 reply_id: 143395[/import]

Thanks Rob!

Following that examples, I tried this to get the size of a sub-folder:

local file\_path = system.pathForFile( "proba", system.DocumentsDirectory )  
  
local file\_attr = lfs.attributes( file\_path )  
local size = file\_attr.size  
  
print( "Sub-folder size: "..size )  
  

where “proba” is my sub-folder in DocumentsDirectory

I get 238 when the sub-folder’s size is 139 Kb
Do you know what am I doing wrong?
Is it possible to get directly the sub-folder’s size instead of adding the sizes of the files in the sub-folder? [import]uid: 44013 topic_id: 35385 reply_id: 143445[/import]

You are wanting to get the size of all the files in the folder? Well that’s different than getting the file size of the directory.

Unix file systems directory size grows as more directory entries are added, but when the files are removed, the directory size does not go down, so it didn’t make much sense that you wanted the file size of the directory.

If you want the sum of the size of each file in the directory, then that’s different. You will have to loop over each file in the directory (which LFS can give you the list of files) and then get the stat information for each file and add it up.

Keep in mind too, that size may be in bytes, where as the OS may report it in 512byte blocks,
[import]uid: 199310 topic_id: 35385 reply_id: 143528[/import]

Hi Rob!

Thanks a lot!, I think your information is very useful!

I’m going to loop over each file in the directory and add it up.

Alberto. [import]uid: 44013 topic_id: 35385 reply_id: 143565[/import]

Hi,

Is there a way to get the size of a folder created in my DocumentsDirectory and where I download my files?

TIA

Alberto. [import]uid: 44013 topic_id: 35385 reply_id: 143300[/import]

You can use the LFS module to get the file sizes.

See this tutorial:

http://www.coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/

[import]uid: 199310 topic_id: 35385 reply_id: 143395[/import]

Thanks Rob!

Following that examples, I tried this to get the size of a sub-folder:

local file\_path = system.pathForFile( "proba", system.DocumentsDirectory )  
  
local file\_attr = lfs.attributes( file\_path )  
local size = file\_attr.size  
  
print( "Sub-folder size: "..size )  
  

where “proba” is my sub-folder in DocumentsDirectory

I get 238 when the sub-folder’s size is 139 Kb
Do you know what am I doing wrong?
Is it possible to get directly the sub-folder’s size instead of adding the sizes of the files in the sub-folder? [import]uid: 44013 topic_id: 35385 reply_id: 143445[/import]

You are wanting to get the size of all the files in the folder? Well that’s different than getting the file size of the directory.

Unix file systems directory size grows as more directory entries are added, but when the files are removed, the directory size does not go down, so it didn’t make much sense that you wanted the file size of the directory.

If you want the sum of the size of each file in the directory, then that’s different. You will have to loop over each file in the directory (which LFS can give you the list of files) and then get the stat information for each file and add it up.

Keep in mind too, that size may be in bytes, where as the OS may report it in 512byte blocks,
[import]uid: 199310 topic_id: 35385 reply_id: 143528[/import]

Hi Rob!

Thanks a lot!, I think your information is very useful!

I’m going to loop over each file in the directory and add it up.

Alberto. [import]uid: 44013 topic_id: 35385 reply_id: 143565[/import]

Hi,

Is there a way to get the size of a folder created in my DocumentsDirectory and where I download my files?

TIA

Alberto. [import]uid: 44013 topic_id: 35385 reply_id: 143300[/import]

You can use the LFS module to get the file sizes.

See this tutorial:

http://www.coronalabs.com/blog/2012/05/08/luafilesystem-lfs-tutorial/

[import]uid: 199310 topic_id: 35385 reply_id: 143395[/import]

Thanks Rob!

Following that examples, I tried this to get the size of a sub-folder:

local file\_path = system.pathForFile( "proba", system.DocumentsDirectory )  
  
local file\_attr = lfs.attributes( file\_path )  
local size = file\_attr.size  
  
print( "Sub-folder size: "..size )  
  

where “proba” is my sub-folder in DocumentsDirectory

I get 238 when the sub-folder’s size is 139 Kb
Do you know what am I doing wrong?
Is it possible to get directly the sub-folder’s size instead of adding the sizes of the files in the sub-folder? [import]uid: 44013 topic_id: 35385 reply_id: 143445[/import]