Is there a way to download a file?

Okay, so I’ve followed the tutorial for using Cloud Lua and was able to get responses using coronium.input and coronium.output.  But I’m still at a loss on where to access the files that were uploaded via coronium:uploadFile.  My concern is that the documentation for os.remove() says “You can only remove files in the DocumentsDirectory and TemporaryDirectory”.  I would greatly appreciate any help.

I would really like to be able to delete the file using Cloud Lua… but if that’s not possi nm let, does Coronium support something like this?

Deleting an object will not delete any file that is referenced by any of its columns.

Files are static assets and may be referenced from different places. For example, your app may have cached an object with a reference to this file. Even if the original object has been deleted from the server, cached data should continue working without impacting the user experience in most cases.

If you still want to delete a file, you can do so through the REST API. You will need to provide the master key in order to be allowed to delete a file. Note that the name of the file must be the name in the response of the upload operation, rather than the original filename.

curl -X DELETE \
-H “X-Parse-Application-Id: <YOUR_APPLICATION_ID>” \
-H “X-Parse-Master-Key: <YOUR_MASTER_KEY>” \
https://api.parse.com/1/files/<FILE_NAME>

Hi,

Let me spin up a Coronium instance and I can help you better. When you upload a file you should receive its identifier/url, generally you’d want to store this data so you can easily access it again. The file uploads are logged in MySQL so thats one place to find paths, but…if you know the filename then its not too hard to do.

The documents you refer to in regards to “DocumentsDirectory”, etc. have to do directly with Corona SDK, not Cloud Lua.

Again, let me start up an instance so I can help you better.

Cheers.

You are awesome!  I truly appreciate your help.  

I am tracking the url, to include the file name when I upload the file.  Later the file is downloaded with the returned url by another user.  When the transaction is complete, the user that created the file will need to be able to delete the file… that last bit is the only piece missing.  

Please let me know whatever information you need… and Thanks again.

Wait, one sec. So I’m clear, you want to delete the file from the clients device? Or from the actual Coronium server?

Cheers.

I need it so that the user can delete the file, or call a function that exists in Cloud Lua that will delete the file.  I can write the code to make this automatic or done at the press of a button from the user side.  But the initiation of the deletion needs to start from a user.

There are 2 different actions here (and locations). I’m trying to determine whether you would like to remove the file from both the server and the clients device (after download of course), or just the server, or just the client (Corona SDK)?

Depending on which you want to do, you may not need to do anything with Cloud Lua.

Let me know.

I’m already deleting the file in the device after download using os.remove.  What I need is to be able to delete the file on the server.

Ok, perfect. I’ll get back to you shortly. :slight_smile:

Thanks

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?