lapis icon indicating copy to clipboard operation
lapis copied to clipboard

How to write file response?

Open Bobby19 opened this issue 7 years ago • 2 comments

Hi guys, i found an example with file upload. But i have no idea how to implement file download.

I want to check some request in my app.lua script and if user has enough rights answer with binary data. These files might be very big, around 100Mb.

Thank you.

Bobby19 avatar Sep 21 '16 17:09 Bobby19

responses served from lapis are buffered in memory before being sent the client, so serving large files in lapis isn't a good idea since each request would take that much memory. Where are you serving the file from? Is it on your local disk?

I recommend letting nginx serve the file, and you can use Lua to restrict access to that endpoint. You can create a new location in your nginx config and then use one of the _by_lua directives.

leafo avatar Sep 21 '16 18:09 leafo

Yep, these files are on my local disk. /data/www/file_storage/

Yes, i think it is a good idea to let nginx serving these files. I am going to write something like:

      content_by_lua '
        local my_module = require("my_module")
        local full_file_path = my_module.get_file_path(username, file_guid) --get_file_path will check the username and find file in db.
      ';

And then, i need somehow ask nginx to use this file as response. I know that this question is not about lapis, at all. But, could you share a link which describes such technic? And, i don't want to make these file links static in order to avoid unauthorized downloads. Please. I am newbie, so i can spend a few days searching for such manual :)

Thank you!

Bobby19 avatar Sep 21 '16 19:09 Bobby19