Is there a way to download a file?

Hi,

So I’m pretty sure this will work with your version. It’s a couple of previously undocumented methods.

Make sure to always include a filename, or you risk any number of issues. Even if the file does not exist, as long as you have a filename in there, you won’t have any random deletions. And don’t use the * (asterisk) character ever, unless you know what you want from it. Used incorrectly and you could wipe your files clean (probably why this was undocumented).

I’d highly recommend using a separate identifier (like the MySQL ID) as opposed to sending up the filepath directly so that you can check the data for validity.

You will need to make a cloud file, can be called whatever you’d like, I went with remove.lua :

[lua]

–Coronium Cloud / remove.lua

local input = coronium.input()

local fullpath = coronium.io.getPath( input.baseDir, input.filename )
coronium.io.deleteFile( fullpath )

coronium.output( coronium.answer( ‘removed’ … input.filename ) )[/lua]

Usage:

[lua]

–Corona SDK / main.lua

coronium:run( ‘remove’, { filename = ‘the-previously-stored-filename.ext’, baseDir = ‘files’ } )

[/lua]

By ID

If you decide to use an ID instead of the path (which you should), it might look like this:

[lua]

coronium:run( ‘remove’, { id = ‘some_id’, baseDir = ‘files’ } )[/lua]

You’d then pull the record, get the path, and remove the file.

If all of your files reside in the same directory, you can hard-code that in the cloud Lua, and leave it out on your call:

[lua]

coronium:run( ‘remove’, { id = ‘some_id’ } )

[/lua]

You’d of course need to make the proper adjustments in your cloud file:

[lua]

local input = coronium.input()

local id = input.id

local baseDir = ‘files’

–look up

local q = “SELECT filename FROM _files WHERE id=”…input.id

local answer = coronium.mysql.query( { database = ‘_files’ }, q )

local rec = answer.result[1]

local filename = rec.filename

local fullpath = coronium.io.getPath( baseDir, filename )

coronium.io.deleteFile( fullpath )

coronium.output( ‘file deleted - all done’ )

[/lua]

Hope that helps (and more importantly, works).

Cheers.

Awesome… thanks a million… I’ll work on this tonight and let you know how it goes.  

So I can’t get it to work with using the filename.  Based on your recommendation, I’m going to try to do it by objectId anyways.  But there’s one problem.  I could never get the coronium:uploadfile function to return an objectId, even though the documentation says it should return one.  I figured out, via trial and error, that I could get it to return a result.file… which is the objectId and filename combined.  

Do you know of a way to get only the objectId returned, or to extract just the objectId from the result.file that is returned?

Using filename - When using filename, I don’t get any errors and I do get the return I expect from io.output… but the file is not being deleted.

Using ObjectId - I can’t get the query that runs on the server to return a result… it always returns nil.

Using id - using just the numerical id like I do locally in sqlite has the same problem as using the objectId, it returns nil on the query.  

In all cases, I tried just about every combination I could think of, specifically, I true answer.result[1] and just answer.result for the query return.  No joy on any combination I could think of.  

Here is the code I’m at now using filename because it seems closer to working… but it is pretty much back to what you provided:

[lua]

local input = coronium.input()

local fullpath = coronium.io.getPath( input.baseDir, input.filename )

coronium.io.deleteFile( fullpath )

coronium.output( coronium.answer( 'removed ’ … input.filename ) )

[/lua]

[lua]

local deleteId = “49d025645737d7c91a31237180443.zip”

coronium:run( ‘remove’, { filename = deleteId, baseDir = ‘files’ }, onDeleteFile )

[/lua]

Any other thoughts or ideas?

Hi,

That’s odd. Well the file upload does not return on objectId, its the file key (which is the filename id).

The docs were incorrect (I believe they should be fixed now, may need a refresh http://docs.coronium.io/en/latest/client/Files/#examples)..)

The general flow is to upload, store the identifier to a record of your choice. This is how you would associate an objectId.

Also, I have to apologize, this project is a bit older, so I have not been in the code for some time.

So my question is, what version are your running? I did the tests on 1.92.4

Let me know.

We are running 1.92.5.

The return I’m getting with event.result.file is a rather long objectId along with the filename that was just uploaded to include the file extension. I use this to build the download link… works great! Is that the unique identifier you’re referring to?

Also, I’m basing that version of Coronium off another post made by Charles Work, whom is helping me on the project and is the one that installed the coronium instance. Is there a way to verify the version installed?

Hi,

Yes, that is the uri for the file. Its was copied from the way Parse did it. I’m not really a big fan of how it turned out.

Since we are dealing with removing files now, I am going to be only responding in this thread https://forums.coronalabs.com/topic/62514-how-to-delete-a-file/ to keep some sanity.

Will run some more tests.

Cheers.

Roger that, I will start following that topic.

Just a heads up, I logged in to Coronium from my phone and it says 1.93.1 in bottom left corner. Does that indicates the version we are running?

Have you tired network.download()?

https://docs.coronalabs.com/api/library/network/download.html

EDIT: I’m dumb. Sorry for the useless post :frowning:

Hi,

If you’ve uploaded the file using the Coronium module, you should receive a path to that file in the return (generally you would associate this with some data record). And then you would use network.download as suggested.

Cheers.

Useless post? I don’t think so! Thanks, Alex. I didn’t think of network.download.

Thanks. :slight_smile:

And another thing: I think I found a bug in your code.
The function below is returning nil. Can you confirm that?
–File is done uploading.
local function onFileUpload( event )
if not event.error then
print( event.result.objectId )
end
end

I’ll take a look. I assume you’re running all the latest versions?

The first version I used was almost a year ago, and this issue was already there. In the rush of the year I forgot to point it out to you and focused on other things.

 I did download the latest version yesterday. 

I know this post is a bit old, but I found the solution to event.result.objectId.  It will return nil everytime… but if you look for event.result.file, you will get what you’re looking for.

For my app, I’ve figured out how to output to a txt file, zip that file, upload to Coronium, get the download link returned, download, unzip, extract from text.  The only thing I’m lacking now is the ability to delete the .zip file that was uploaded on the server.  Does anyone out there know how to do that?  I’m assuming it something involving network.request, but I’m really have trouble understanding that call and figuring out how to delete a file.

Any help would be greatly appreciated.

Here’s the code I’m trying to run… maybe it’s something simple:

[lua]

local function networkListener( event )

     if ( event.isError ) then

     elseif ( event.phase == “began” ) then

     elseif ( event.phase == “ended” ) then    

     end

end

network.request(downloadLink, “DELETE”, networkListener)

[/lua]

Hi,

You can’t delete files from the client for security reasons. You would need to do that in cloud Lua, and call from the client.

Check out Lua’s os.remove(filename)

Deletes the file or directory with the given name. Directories must be empty to be removed. If this function fails, it returns nil, plus a string describing the error.

Hope that helps.

I think this will help quite a bit, but I’ll have to get familiar with the concept of Cloud Lua.  My understanding is that you can create a .lua file on the server, or upload one… and call a function on that .lua file using:

[lua]:run( functionName, functionParameters, callback )[/lua]

But I’m not sure how the directory works on the cloud.  What would be the path to access files I uploaded using coronium:uploadFile?

So I’m thinking the function in Cloud Lua would be very simple something like this:

[lua]

local function deleteChallengeFunction(fileToDelete, directoryIn)

    os.remove( system.pathForFile( fileToDelete, directoryIn ) )

end 

[/lua]

I know there’s probably much more to it than this.  I’ll keep researching… but any help would be greatly appreciated.