edihasaj / tuspy-fast-api

MIT License
20 stars 9 forks source link

Server side event when a file upload is complete #2

Closed max-l closed 1 year ago

max-l commented 1 year ago

It would be nice to get a server side callback when an upload is complete (last chunk received), in this callback one could move the file from where it's at (currently hardcoded at /tmp/files/ae76a377ce0e44f3897d5542aca8067a) and give it a proper name (for ex. the name on the client machine).

I guess the way to do this currently, would be to use the callback on the client side, and send a request to the server with the file UUID, to move the file.

Also, is there a plan to unhardcode the following values (port = 8000, and FILES_DIR = '/tmp/files') ? :

https://github.com/edihasaj/tuspy-fast-api/blob/main/tusserver/tus.py#L14

https://github.com/edihasaj/tuspy-fast-api/blob/main/tusserver/tus.py#L130

edihasaj commented 1 year ago

Hey Max, thanks a lot for checking in. Can you be more descriptive of the callback on the client side, you mean to upload it in Google drive, etc.?

As for the hardcoded values it was a bit of an issue when I started implementing TUS since I cannot make it parametrized, but of course what I can do is make a way to query some standard app config and if you set them there, then we use that path. This was developed fast, and in every case that I did use this code I did change the things to my likings and business logic.

I will check when I have a bit time maybe by then of the week, though changes are welcomed as well 🥇

max-l commented 1 year ago

@edihasaj thatnks for the quick reply !

For the "unhardcoding", I made a pull request: https://github.com/edihasaj/tuspy-fast-api/pull/3

About the server side callback, it has nothing to do with Googl Drive, I will try to explain myself better, with code !

(note: the following code snippet works with the pull request):


def my_func(completed_file_path):
    print(f"yay ! the file is now here: {completed_file_path}")
    return {"json_for_client_consumption": 123}

app.include_router(
    create_api_router(
        files_dir='/tmp/filezz',
        location='http://127.0.0.1:8000/files',
        max_size=128849018880,
        on_upload_completion=my_func
    ),
    prefix="/files"
)

So, as you can see above, the tus.create_api_router, could take a callback (on_upload_completion), that lets you write server side code when an upload has completed.

Maybe it's not possible to support such a callback, that would be the case if the server has no way of knowing when a chunk is the LAST chunk. I don't know if the protocol makes figuring out if a chunk is the last one.

After some thought, there is an easy workaround if it's not possible, given that the client does have a "upload completed" callback, one can just send an extra REST call to the server to notify it of the completion.

So the on_upload_completion isn't very useful, it would only save an extra call, but it still would be nice, if it's possible.

edihasaj commented 1 year ago

@max-l thanks for the good thinking you put into this. working with callback actually works and in tus we have this PATCH request where we can check the file chunks if they are the same as they should be if so we complete the file uploading at the end if there is a callback declared we can just invoke it. So that's a very good idea of yours. Also I like the way of using create_api_router, we can set the parameters inside to de-clutch our hardcoded variables.