Пример #1
0
 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;
 }
Пример #2
0
 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;
 }
Пример #3
0
 public function testGetRealPath()
 {
     $virtualPath = '/';
     $result = Path::getRealPath($virtualPath);
     $expected = $virtualPath;
     $this->assertEquals($expected, $result);
     $virtualPath = '/files';
     $result = Path::getRealPath($virtualPath);
     $expected = $virtualPath;
     $this->assertEquals($expected, $result);
     $rootDir = '/';
     $virtualPath = '/';
     $result = Path::getRealPath($virtualPath, $rootDir);
     $expected = $rootDir;
     $this->assertEquals($expected, $result);
     $rootDir = '/';
     $virtualPath = '/files';
     $result = Path::getRealPath($virtualPath, $rootDir);
     $expected = '/files';
     $this->assertEquals($expected, $result);
     $rootDir = __DIR__;
     $virtualPath = '/';
     $result = Path::getRealPath($virtualPath, $rootDir);
     $expected = $rootDir . '/';
     $this->assertEquals($expected, $result);
     $rootDir = __DIR__ . '/';
     $virtualPath = '/files';
     $result = Path::getRealPath($virtualPath, $rootDir);
     $expected = $rootDir . 'files';
     $this->assertEquals($expected, $result);
 }