public function getMetadata(Application $app, Request $request, $path) { $realPath = Path::getRealPath($path, Config::getRootDir()); if (!Path::exists($realPath)) { return new Response('', Response::HTTP_NOT_FOUND); } if ($request->getMethod() === 'HEAD') { return new Response('', Response::HTTP_OK); } $lastWriteTime = $response = $this->getLastWriteTime($app, $realPath); if (($response = $this->hasCachedResponse($request, $lastWriteTime)) !== false) { return $response; } $searchOptions = $request->query->getBoolean('contents') ? FileSystemInfoSerializer::TOP_DIRECTORY_ONLY : FileSystemInfoSerializer::WITHOUT_CONTENT; $info = Path::getInfo($realPath); $json = FileSystemInfoSerializer::toJson($info, $searchOptions); $response = $app->json($json); $response->setLastModified($lastWriteTime); $response->setPublic(); return $response; }
public function putFile(Application $app, Request $request, $path) { $realPath = Path::getRealPath($path, Config::getRootDir()); if (Path::exists($realPath) && Path::isDir($realPath)) { return $this->createResponse(Response::HTTP_BAD_REQUEST, "Directory '{$path}' already exists"); } if ($request->query->getBoolean('is_dir') || $request->request->getBoolean('is_dir')) { if (Path::exists($realPath)) { return $this->createResponse(Response::HTTP_BAD_REQUEST, "Directory '{$path}' already exists"); } mkdir($realPath); } else { $content = $request->getContent(true); if (!is_resource($content)) { return $this->createResponse(Response::HTTP_BAD_REQUEST, "Request has incorrect body"); } $resourceType = get_resource_type($content); if ($resourceType != 'stream') { return $this->createResponse(Response::HTTP_BAD_REQUEST, "Request has incorrect body"); } $newFile = fopen($realPath, 'w'); try { stream_copy_to_stream($content, $newFile); } finally { fclose($newFile); } } $info = Path::getInfo($realPath); $app['fsrapi.cache']->setTimestampForInfo($info, $info->getLastWriteTime()); $response = new Response(); $response->headers->addCacheControlDirective('no-cache', true); $response->headers->addCacheControlDirective('no-store', true); return $response; }
public function testExists() { $this->assertTrue(Path::exists(__FILE__)); $this->assertFalse(Path::exists(__FILE__ . '12345')); }