lumi icon indicating copy to clipboard operation
lumi copied to clipboard

Support for File

Open tanmoysrt opened this issue 3 years ago • 7 comments

Currently, Lum supports communication over JSON only. But in real life, we need to upload files and also download files from Server. So we will bring that support

Tasks -

  • [x] **Send File To Client: ** Any function can return an instance of a file from any function. In the backend, we need to check the return type and if that is file, need to set the appropriate headers and send the file
  • [ ] Receive File From Client: : To start work on this feature set, we need to first complete this issue #8 . After then we can work on this. When the file will receive we will cast it to file and pass it to function

tanmoysrt avatar Nov 29 '22 13:11 tanmoysrt

@Tanmoy741127 , about your first point: would it be possible to use Path objects to serve files/folders to the user? I imagine something like this would be useful:

from pathlib import Path

def get_file(name) -> Path:
    return Path(name)

dosisod avatar Dec 21 '22 02:12 dosisod

@dosisod working on that. User can return an instance of file like you show.

tanmoysrt avatar Dec 21 '22 04:12 tanmoysrt

@dosisod added support for serve filesin latest version. Check the README file. Give it a try and provide feedback please.

tanmoysrt avatar Dec 22 '22 08:12 tanmoysrt

Will do! I'll take a look at it in the next couple of days

dosisod avatar Dec 23 '22 06:12 dosisod

@dosisod sure

tanmoysrt avatar Dec 23 '22 06:12 tanmoysrt

.

tanmoysrt avatar Jan 08 '23 17:01 tanmoysrt

Sorry for the delay on this! A couple notes:

  1. Files passed to open() should be checked to ensure that they are from inside the directory Lumi is being ran in. If this isn't checked, somebody could steal files from anywhere on the file system:
from lumi import Lumi

def index(file: str):
    return open(file)
$ curl -X POST localhost:8080/index -d '{"file":"/home/user/.ssh/id_rsa"}' -H "Content-Type: application/json"
<RSA KEY>

You can check this with the following:

from pathlib import Path

if Path("some/file/name").is_relative_to(Path.cwd()):
    # handle file
else:
    # return 403 or 404 error
  1. In the README you use the rb mode to open the file. Is there any difference between r (the default) and rb? I didn't see a difference in my testing.

Thanks!

dosisod avatar Jan 12 '23 02:01 dosisod