public function handle(): Server
	{

		// Check the upload directory
		if (!is_dir($this->uploadDir))
			throw new ThunderTUSException("Invalid upload directory. Path wasn't set, it doesn't exist or it isn't a directory.");

		// Call handlers
		$method = "handle" . $this->request->getMethod();
		if (!method_exists($this, $method))
			throw new ThunderTUSException("Invalid HTTP request method. Not TUS or ThunderTUS compliant.");

		// Check if this server supports the client protocol version
		if ($this->request->getHeaderLine("Tus-Resumable") != self::PROTOCOL_VERSION) {
			$this->response = $this->response->withStatus(412);
			return $this;
		}

		// Gather the filename from the last part of the URL and set the resource location
		$this->apiPath   = rtrim($this->apiPath, "/") . "/";
		$this->uploadDir = rtrim(realpath($this->uploadDir), \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR;
		$url             = $this->request->getUri();
		$this->file      = str_replace(["\\", "/"], "", substr($url, strrpos($url, $this->apiPath) + strlen($this->apiPath)));
		$this->location  = $this->apiPath . $this->file;
		$this->file      = $this->uploadDir . $this->file;

		// Replicate the input stream so we can control its pointer
		$this->stream = fopen('php://temp', 'w+');
		stream_copy_to_stream(fopen($this->streamURI, 'r'), $this->stream);
		rewind($this->stream);

		$this->response = $this->$method();

		return $this;
	}