pnz/tus-hook-handler

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

This package is auto-updated.

Last update: 2024-08-27 02:55:42 UTC


README

一个处理 TUSd HTTP 钩子的快速助手。处理器通过使用事件调度器暴露了两种方法来处理钩子数据并分派 TUSd 事件。

触发事件的示例

由于 TUSd 钩子实现,您的控制器 必须pre-create http 钩子做出适当的响应,以确认上传(响应代码 200),或拒绝它(响应代码 400)。

钩子控制器示例

    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
    {
      ...
    }