pnz/tus-hook-handler

dev-master / 0.1.x-dev 2018-04-25 18:01 UTC

This package is auto-updated.

Last update: 2024-10-27 03:26:34 UTC


README

Quick helper to handle TUSd HTTP hooks. The handler exposes two methods to process the hook data and to dispatch TUSd events by using and EventDispatcher.

Example of triggered events:

Due to the TUSd hook implementation, your controller MUST respond with a proper response to the pre-create http hook, to confirm the upload (response code 200), or to deny it (response code 400).

Example of the hook controller:

    public function tusdHookAction(Request $request): Response
    {
        try {
            $data = $this->tusHookHandler->buildHookData($request);
        } catch (TusException $e) {
            // Do not proceed, as the the request is invalid.
            // The TUSd server will abort the upload
            return new Response('Invalid request: '.$e->getMessage(), 400);
        }

        if (HookData::HOOK_PRE_CREATE === $data->hookName) {
            if (!$this->isValidUpload($data)) {
                // Return a failure response, TUSd server will abort the upload
                return new Response('Invalid request: invalid token', 400);
            }
        }

        // Let the handler dispatch the event
        $this->tusHookHandler->handleHook($data);

        // Return an empty response, TUSd server will handle it as a positive answer.
        return new Response();
    }

    private function isValidUpload(HookData $data): bool
    {
      ...
    }