Skip to content

Commit 2467d64

Browse files
committed
hooks: add pre-access examples
- add some README.md to guide usage - fix grpc example Makefile proto path
1 parent a8ffb16 commit 2467d64

File tree

11 files changed

+913
-111
lines changed

11 files changed

+913
-111
lines changed

examples/hooks/file/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Run file hook exemple
2+
3+
tusd -hooks-dir=./ -hooks-enabled-events=pre-create,pre-finish,pre-access,post-create,post-receive,post-terminate,post-finish
4+
5+
Adapt enabled-events hooks list for your needs.

examples/hooks/file/pre-access

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# This example demonstrates how to read the hook event details
4+
# from stdin, and output debug messages.
5+
6+
# We use >&2 to write debugging output to stderr. tusd
7+
# will forward these to its stderr. Any output from the
8+
# hook on stdout will be captured by tusd and interpreted
9+
# as a response.
10+
cat /dev/stdin | jq . >&2

examples/hooks/grpc/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
hook_pb2.py: ../../../cmd/tusd/cli/hooks/proto/v2/hook.proto
2-
python3 -m grpc_tools.protoc --proto_path=../../../cmd/tusd/cli/hooks/proto/v2/ hook.proto --python_out=. --grpc_python_out=.
1+
hook_pb2.py: ../../../pkg/hooks/grpc/proto/hook.proto
2+
python3 -m grpc_tools.protoc --proto_path=../../../pkg/hooks/grpc/proto/ hook.proto --python_out=. --grpc_python_out=.

examples/hooks/grpc/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Run grpc hook exemple
2+
3+
python3 server.py
4+
5+
tusd -hooks-grpc=localhost:8000 -hooks-enabled-events=pre-create,pre-finish,pre-access,post-create,post-receive,post-terminate,post-finish
6+
7+
Adapt enabled-events hooks list for your needs.

examples/hooks/grpc/hook_pb2.py

Lines changed: 818 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,50 @@
11
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2-
"""Client and server classes corresponding to protobuf-defined services."""
32
import grpc
43

54
import hook_pb2 as hook__pb2
65

76

87
class HookHandlerStub(object):
9-
"""The hook service definition.
10-
"""
8+
"""The hook service definition.
9+
"""
1110

12-
def __init__(self, channel):
13-
"""Constructor.
11+
def __init__(self, channel):
12+
"""Constructor.
1413
15-
Args:
16-
channel: A grpc.Channel.
17-
"""
18-
self.InvokeHook = channel.unary_unary(
19-
'/v2.HookHandler/InvokeHook',
20-
request_serializer=hook__pb2.HookRequest.SerializeToString,
21-
response_deserializer=hook__pb2.HookResponse.FromString,
22-
)
14+
Args:
15+
channel: A grpc.Channel.
16+
"""
17+
self.InvokeHook = channel.unary_unary(
18+
'/proto.HookHandler/InvokeHook',
19+
request_serializer=hook__pb2.HookRequest.SerializeToString,
20+
response_deserializer=hook__pb2.HookResponse.FromString,
21+
)
2322

2423

2524
class HookHandlerServicer(object):
26-
"""The hook service definition.
25+
"""The hook service definition.
26+
"""
27+
28+
def InvokeHook(self, request, context):
29+
"""InvokeHook is invoked for every hook that is executed. HookRequest contains the
30+
corresponding information about the hook type, the involved upload, and
31+
causing HTTP request.
32+
The return value HookResponse allows to stop or reject an upload, as well as modifying
33+
the HTTP response. See the documentation for HookResponse for more details.
2734
"""
28-
29-
def InvokeHook(self, request, context):
30-
"""InvokeHook is invoked for every hook that is executed. HookRequest contains the
31-
corresponding information about the hook type, the involved upload, and
32-
causing HTTP request.
33-
The return value HookResponse allows to stop or reject an upload, as well as modifying
34-
the HTTP response. See the documentation for HookResponse for more details.
35-
"""
36-
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
37-
context.set_details('Method not implemented!')
38-
raise NotImplementedError('Method not implemented!')
35+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
36+
context.set_details('Method not implemented!')
37+
raise NotImplementedError('Method not implemented!')
3938

4039

4140
def add_HookHandlerServicer_to_server(servicer, server):
42-
rpc_method_handlers = {
43-
'InvokeHook': grpc.unary_unary_rpc_method_handler(
44-
servicer.InvokeHook,
45-
request_deserializer=hook__pb2.HookRequest.FromString,
46-
response_serializer=hook__pb2.HookResponse.SerializeToString,
47-
),
48-
}
49-
generic_handler = grpc.method_handlers_generic_handler(
50-
'v2.HookHandler', rpc_method_handlers)
51-
server.add_generic_rpc_handlers((generic_handler,))
52-
53-
54-
# This class is part of an EXPERIMENTAL API.
55-
class HookHandler(object):
56-
"""The hook service definition.
57-
"""
58-
59-
@staticmethod
60-
def InvokeHook(request,
61-
target,
62-
options=(),
63-
channel_credentials=None,
64-
call_credentials=None,
65-
insecure=False,
66-
compression=None,
67-
wait_for_ready=None,
68-
timeout=None,
69-
metadata=None):
70-
return grpc.experimental.unary_unary(request, target, '/v2.HookHandler/InvokeHook',
71-
hook__pb2.HookRequest.SerializeToString,
72-
hook__pb2.HookResponse.FromString,
73-
options, channel_credentials,
74-
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
41+
rpc_method_handlers = {
42+
'InvokeHook': grpc.unary_unary_rpc_method_handler(
43+
servicer.InvokeHook,
44+
request_deserializer=hook__pb2.HookRequest.FromString,
45+
response_serializer=hook__pb2.HookResponse.SerializeToString,
46+
),
47+
}
48+
generic_handler = grpc.method_handlers_generic_handler(
49+
'proto.HookHandler', rpc_method_handlers)
50+
server.add_generic_rpc_handlers((generic_handler,))

examples/hooks/grpc/server.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ def InvokeHook(self, hook_request, context):
3737
hook_response.changeFileInfo.metaData['filename'] = metaData['filename']
3838
hook_response.changeFileInfo.metaData['creation_time'] = time.ctime()
3939

40+
41+
# Example: Use the pre-access hook to print each upload access
42+
if hook_request.type == 'pre-access':
43+
mode = hook_request.event.access.mode
44+
id = hook_request.event.access.files[0].id
45+
size = hook_request.event.access.files[0].size
46+
print(f'Access {id} (mode={mode}, size={size} bytes)')
47+
4048
# Example: Use the post-finish hook to print information about a completed upload,
4149
# including its storage location.
4250
if hook_request.type == 'post-finish':

examples/hooks/http/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Run http hook exemple
2+
3+
python3 server.py
4+
5+
tusd -hooks-http=http://localhost:8000 -hooks-enabled-events=pre-create,pre-finish,pre-access,post-create,post-receive,post-terminate,post-finish
6+
7+
Adapt enabled-events hooks list for your needs.

examples/hooks/http/server.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def do_POST(self):
5050
'creation_time': time.ctime(),
5151
}
5252

53+
# Example: Use the pre-access hook to print each upload access
54+
if hook_request['Type'] == 'pre-access':
55+
mode = hook_request['Event']['Access']['Mode']
56+
id = hook_request['Event']['Access']['Files'][0]['ID']
57+
size = hook_request['Event']['Access']['Files'][0]['Size']
58+
print(f'Access {id} (mode={mode}, size={size} bytes)')
5359

5460
# Example: Use the post-finish hook to print information about a completed upload,
5561
# including its storage location.

examples/hooks/plugin/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Run plugin hook exemple
2+
3+
make
4+
5+
tusd -hooks-http=./hook_handler -hooks-enabled-events=pre-create,pre-finish,pre-access,post-create,post-receive,post-terminate,post-finish
6+
7+
Adapt enabled-events hooks list for your needs.

0 commit comments

Comments
 (0)