Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application examples? #139

Open
leplatrem opened this issue Jun 27, 2023 · 6 comments
Open

Application examples? #139

leplatrem opened this issue Jun 27, 2023 · 6 comments

Comments

@leplatrem
Copy link

Is rustus supposed to be exposed to the world or proxied by a "domain-specific" web service?

Did I understand well from this example:

rustus/docs/hooks.md

Lines 852 to 855 in 3206250

print(f"Received: {hook_name}")
if authorization != "Bearer jwt":
raise HTTPException(401)
return None

That hooks could be used to authenticate file uploads?

I think it would be interesting as a narrative example to unroll a whole demo app that leverages rustus. Or give more context on why it was built?
Or for example, answer questions like "if I want to built a WeTransfer clone, what is the intended way to integrate rustus?"

If you're willing to give me a few pointers, I'd be glad to contribute something :)

@s3rius
Copy link
Owner

s3rius commented Jun 27, 2023

HI! And thanks for you question.
Rustus is supposed to be opened to the world. To make it more secure, you can use CORS.
If you have no hooks configured, then everyone can upload anything.

But if you have file hooks or http hooks, then on pre-create hook you can easily abort uploads by either exiting with non-zero status code (if you use file hooks) or return not 200 status code from your application (if you use http hooks) .

When upload is ready you can add post-finish hook to handle events when upload is complete.

I use rustus to upload renedered videos to one of my side-projects.

@router.post("/rustus_hooks")
async def rustus_hooks(
    hook: RustusHook,
    auth: AuthJWT = Depends(),
    hook_name: Optional[HookName] = Header(None),
) -> None:
    """
    Upload stream in database.

    :param hook: hook information.

    :param auth: service to validate authorization header.

    :param hook_name: hook name

    :raises HTTPException: if you dumb enough to simulate rustus.
    """
    auth.jwt_required()
    upload_id = hook.upload.id
    if hook_name == HookName.PRE_CREATE:
        logger.info("Stream upload started.")
        return
    elif hook_name == HookName.POST_TERMINATE:
        logger.info(f"Removing stream for upload_id={upload_id}")
        await Stream.filter(upload_id=hook.upload.id).delete()
    elif hook_name == HookName.POST_FINISH:
        logger.info(f"Stream upload_id={upload_id} successfully uploaded")
        user = await AdminTable.get(username=auth.get_jwt_subject())
        await Stream.create(
            upload_id=hook.upload.id,
            order=await Stream.all().count(),
            path=hook.upload.path,
            created_by=user,
        )
    else:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Are you drunk?",
        )

That's the whole code to handle uploads. I use rustus with these parameters:

    RUSTUS_MAX_BODY_SIZE: "1000000"
    RUSTUS_DIR_STRUCTURE: "{year}/{month}/{day}"
    RUSTUS_TUS_EXTENSIONS: "getting,creation,termination"
    RUSTUS_HOOKS_HTTP_PROXY_HEADERS: "Authorization"
    RUSTUS_HOOKS_HTTP_URLS: "http://{my_service}/rustus_hooks"
    RUSTUS_HOOKS: "pre-create,post-terminate,post-finish"

I hope it helped you.

@leplatrem
Copy link
Author

That definitely helps! Thank you :)

@s3rius
Copy link
Owner

s3rius commented Jul 4, 2023

I'm really glad, you find this project useful.

If it works for you, please consider closing the issue.

@s3rius
Copy link
Owner

s3rius commented Jul 4, 2023

Or maybe before closing it would be nice to add examples section in docs.

@leplatrem
Copy link
Author

Yes I think it would be useful to show a few recipes/patterns in the docs.

I'm thinking one of:

  • minimal example with a tus client
  • authenticated transfer (the above example)
  • wetransfer-like (several files are added in something like a virtual folder)

@s3rius
Copy link
Owner

s3rius commented Jul 4, 2023

Good set of cases. I will try to implement suggested examples and will put them in folder inside this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants