private static function infoToJson(FileSystemInfo $info)
 {
     $isDir = $info instanceof DirectoryInfo;
     $virtualPath = Path::getVirtualPath($info->getFullName(), Config::getRootDir());
     $json = array('name' => $info->getName(), 'path' => strlen($virtualPath) > 1 ? rtrim($virtualPath, '/') : $virtualPath, 'size' => $isDir ? 0 : $info->getSize(), 'last_access_time' => DateTimeUtils::toString($info->getLastAccessTime()), 'last_write_time' => DateTimeUtils::toString($info->getLastWriteTime()), 'is_dir' => $isDir);
     return $json;
 }
Example #2
0
 private function setTimestampHelper(FileSystemInfo $info, $timestamp)
 {
     $parentInfo = $info;
     $rootDirInfo = new DirectoryInfo(Config::getRootDir());
     $endDirPath = $rootDirInfo->getParent()->getFullName();
     do {
         $this->cache->save($parentInfo->getFullName(), $timestamp);
         if ($parentInfo instanceof FileInfo) {
             $parentInfo = $parentInfo->getDirectory();
         } else {
             $parentInfo = $parentInfo->getParent();
         }
     } while ($parentInfo != null && strcasecmp($parentInfo->getFullName(), $endDirPath) != 0);
 }
Example #3
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;
 }
Example #4
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;
 }