GitHub - terminusdb/tus: TUS protocol for resumable file uploads via HTTP (original) (raw)

TUS in swipl

TUS protocol for resumable file uploads via HTTP in swipl (see:TUS)

This package implements both the server handlers and a simple client.

The package implements the core protocol along with four extensions:

Options

Options for the server can be set with the set_tus_options/1predicate which has the following options:

For instance, to set a larger client chunk size one can call:

:- set_tus_options([tus_client_chunk_size(33_554_432)]).

Examples

Server

To use the TUS server with the HTTP library in swipl you can simply add a tus_dispatch call to your http_handler from a chosen endpoint as follows:

:- http_handler(root(files), tus_dispatch, [ methods([options,head,post,patch,delete]), prefix ]).

In order to determine the location of a given resource when passed the resource URL in other contexts you can use the utility predicatetus_uri_resource(URI, Resource) together withtus_resource_path(Resource, Resource_Path). In this way once a client has uploaded a resource it can be referred to in client-server communications, and be moved, modified or manipulated by server code.

Domains and Authorization in Server

The server does not manage authorization, but it is possible to use authorization to provide isolation of user data by providing thetus_dispatch/2 predicate with options which include[domain(Domain)].

You can use any authorization system you'd like (basic, JWT etc.) to guard access to this domain token and the domain token will be used to ensure isolation.

The following code shows how you might add a handler which carries with it a domain after authorization. There is a more complete example in the tests in prolog/tus.pl.

:- meta_predicate auth_wrapper(2,?). auth_wrapper(Goal,Request) :- authorize(Request, Domain), call(Goal, [domain(Domain)], Request).

spawn_auth_server(URL, Port) :- random_between(49152, 65535, Port), http_server(http_dispatch, [port(Port), workers(1)]), http_handler(root(files), auth_wrapper(tus_dispatch), [ methods([options,head,post,patch,delete]), prefix ]), format(atom(URL), 'http://127.0.0.1:~d/files', [Port]).

Client

The client invocation requires only the endpoint of interest and a file and can be invoked thus:

tus_upload(Example, URL, Resource)

The Resource variable gives back a resource handle that the server can use to refer to the resource in subsequent communication.

TODO

The remaining extensions would be desirable:

In addition the Creation extension should be checked from the Options call before initiating an upload.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0