tus-php icon indicating copy to clipboard operation
tus-php copied to clipboard

Unable to create resource

Open silent-bury opened this issue 1 year ago • 0 comments
trafficstars

Note: Please report any security issues directly to hello [at] ankit.pl

Describe the bug When the /files interface is requested, it is found that the GET request is not a POST request, resulting in the failure to return 201 and create the corresponding resource, resulting in the error Unable to create resource

environment

  1. PHP Version 8.3
  2. Hyperf Version 3,1
  3. Tus-php Version 2.4

To Reproduce vendor/ankitpokhrel/tus-php/src/Tus/Client.php

public function createWithUpload(string $key, int $bytes = -1): array
    {
        $bytes = $bytes < 0 ? $this->fileSize : $bytes;
        $headers = $this->headers + [
            'Upload-Length' => $this->fileSize,
            'Upload-Key' => $key,
            'Upload-Checksum' => $this->getUploadChecksumHeader(),
            'Upload-Metadata' => $this->getUploadMetadataHeader(),
        ];

        $data = '';
        if ($bytes > 0) {
            $data = $this->getData(0, $bytes);

            $headers += [
                'Content-Type' => self::HEADER_CONTENT_TYPE,
                'Content-Length' => \strlen($data),
            ];
        }

        if ($this->isPartial()) {
            $headers += ['Upload-Concat' => 'partial'];
        }

        try {
            $response = $this->getClient()->post($this->apiPath, [
                'body' => $data,
                'headers' => $headers,
            ]);
        } catch (ClientException $e) {
            $response = $e->getResponse();
        }

        $statusCode = $response->getStatusCode();


        if (HttpResponse::HTTP_CREATED !== $statusCode) {
            throw new FileException('Unable to create resource.');
        }

        $uploadOffset   = $bytes > 0 ? current($response->getHeader('upload-offset')) : 0;
        $uploadLocation = current($response->getHeader('location'));

        $this->getCache()->set($this->getKey(), [
            'location' => $uploadLocation,
            'expires_at' => Carbon::now()->addSeconds($this->getCache()->getTtl())->format($this->getCache()::RFC_7231),
        ]);

        return [
            'location' => $uploadLocation,
            'offset' => $uploadOffset,
        ];
    }

vendor/ankitpokhrel/tus-php/src/Tus/Server.php

 /**
     * Handle all HTTP request.
     *
     * @return HttpResponse|BinaryFileResponse
     */
    public function serve()
    {
        $this->applyMiddleware();

        $requestMethod = $this->getRequest()->method();

        if ( ! \in_array($requestMethod, $this->getRequest()->allowedHttpVerbs(), true)) {
            return $this->response->send(null, HttpResponse::HTTP_METHOD_NOT_ALLOWED);
        }
        $clientVersion = $this->getRequest()->header('Tus-Resumable');

        if (HttpRequest::METHOD_OPTIONS !== $requestMethod && $clientVersion && self::TUS_PROTOCOL_VERSION !== $clientVersion) {
            return $this->response->send(null, HttpResponse::HTTP_PRECONDITION_FAILED, [
                'Tus-Version' => self::TUS_PROTOCOL_VERSION,
            ]);
        }
        $method = 'handle' . ucfirst(strtolower($requestMethod));

        return $this->{$method}();
    }

/app/Controller/UploadFileController.php

public function initServer()
    {

        \TusPhp\Config::set([
            'redis' => [
                'host' => '127.0.0.1',
                'port' => '6379',
                'database' => 0,
                'password' => 123456,
                'auth' => 123456
            ],
            'file' => [
                'dir' => '/CartvuMedia/www/test/cache/',
                'name' => 'tus_php.client.cache',
            ],
        ]);

        $server   = new \TusPhp\Tus\Server('redis');

        $server->setUploadDir('/www/hyperf-bob-solution/test-tus-upload/');

        $response = $server->serve();

       return $response;
    }

  1. /www/hyperf-bob-solution/config/routes.php
 Router::addRoute(["POST"], '/files', 'App\Controller\UploadFileController@initServer');

When I called createWithUpload, I didn't GET the status code 201 I was expecting, but 200, so I found that the method I got in Server.php was get instead of POST

silent-bury avatar Oct 22 '24 09:10 silent-bury