/**
  * Move resource files to the new locations and adjust records.
  *
  * @param Schema $schema
  * @return void
  */
 public function postUp(Schema $schema)
 {
     $resourcesResult = $this->connection->executeQuery('SELECT persistence_object_identifier, sha1, filename FROM typo3_flow_resource_resource');
     while ($resourceInfo = $resourcesResult->fetch(\PDO::FETCH_ASSOC)) {
         $resourcePathAndFilename = FLOW_PATH_DATA . 'Persistent/Resources/' . $resourceInfo['sha1'];
         $newResourcePathAndFilename = FLOW_PATH_DATA . 'Persistent/Resources/' . $resourceInfo['sha1'][0] . '/' . $resourceInfo['sha1'][1] . '/' . $resourceInfo['sha1'][2] . '/' . $resourceInfo['sha1'][3] . '/' . $resourceInfo['sha1'];
         $mediaType = MediaTypes::getMediaTypeFromFilename($resourceInfo['filename']);
         if (file_exists($resourcePathAndFilename)) {
             $md5 = md5_file($resourcePathAndFilename);
             $filesize = filesize($resourcePathAndFilename);
             if (!file_exists(dirname($newResourcePathAndFilename))) {
                 Files::createDirectoryRecursively(dirname($newResourcePathAndFilename));
             }
             $result = @rename($resourcePathAndFilename, $newResourcePathAndFilename);
         } elseif (file_exists($newResourcePathAndFilename)) {
             $md5 = md5_file($newResourcePathAndFilename);
             $filesize = filesize($newResourcePathAndFilename);
             $result = TRUE;
         } else {
             $this->write(sprintf('Error while migrating database for the new resource management: the resource file "%s" (original filename: %s) was not found, but the resource object with uuid %s needs this file.', $resourcePathAndFilename, $resourceInfo['filename'], $resourceInfo['persistence_object_identifier']));
             continue;
         }
         $this->connection->executeUpdate('UPDATE typo3_flow_resource_resource SET collectionname = ?, mediatype = ?, md5 = ?, filesize = ? WHERE persistence_object_identifier = ?', array('persistent', $mediaType, $md5, $filesize, $resourceInfo['persistence_object_identifier']));
         if ($result === FALSE) {
             $this->write(sprintf('Could not move the data file of resource "%s" from its legacy location at %s to the correct location %s.', $resourceInfo['sha1'], $resourcePathAndFilename, $newResourcePathAndFilename));
         }
     }
     $this->connection->exec('ALTER TABLE typo3_flow_resource_resource ALTER md5 SET NOT NULL');
     $this->connection->exec('ALTER TABLE typo3_flow_resource_resource ALTER collectionname SET NOT NULL');
     $this->connection->exec('ALTER TABLE typo3_flow_resource_resource ALTER mediatype SET NOT NULL');
     $this->connection->exec('ALTER TABLE typo3_flow_resource_resource ALTER filesize SET NOT NULL');
 }
 /**
  * Map the given resource to a media model class.
  *
  * @param Resource $resource
  * @param array $additionalProperties Optional properties that can be taken into account for deciding the model class. what you get here can depend on the caller, so you should always fallback to something based on the resource.
  * @return string
  */
 public function map(Resource $resource, array $additionalProperties = array())
 {
     $mediaType = MediaTypes::getMediaTypeFromFilename($resource->getFilename());
     foreach ($this->settings['patterns'] as $pattern => $mappingInformation) {
         if (preg_match($pattern, $mediaType)) {
             return $mappingInformation['className'];
         }
     }
     return $this->settings['default'];
 }
 /**
  * @param array $module
  * @return mixed
  */
 public function indexAction(array $module)
 {
     $moduleRequest = new ActionRequest($this->request);
     $moduleRequest->setArgumentNamespace('moduleArguments');
     $moduleRequest->setControllerObjectName($module['controller']);
     $moduleRequest->setControllerActionName($module['action']);
     if (isset($module['format'])) {
         $moduleRequest->setFormat($module['format']);
     }
     if ($this->request->hasArgument($moduleRequest->getArgumentNamespace()) === true && is_array($this->request->getArgument($moduleRequest->getArgumentNamespace()))) {
         $moduleRequest->setArguments($this->request->getArgument($moduleRequest->getArgumentNamespace()));
     }
     foreach ($this->request->getPluginArguments() as $argumentNamespace => $argument) {
         $moduleRequest->setArgument('--' . $argumentNamespace, $argument);
     }
     $modules = explode('/', $module['module']);
     $moduleConfiguration = Arrays::getValueByPath($this->settings['modules'], implode('.submodules.', $modules));
     $moduleConfiguration['path'] = $module['module'];
     if (!$this->menuHelper->isModuleEnabled($moduleConfiguration['path'])) {
         throw new DisabledModuleException(sprintf('The module "%s" is disabled. You can enable it with the "enabled" flag in Settings.yaml.', $module['module']), 1437148922);
     }
     $moduleBreadcrumb = array();
     $path = array();
     foreach ($modules as $moduleIdentifier) {
         array_push($path, $moduleIdentifier);
         $config = Arrays::getValueByPath($this->settings['modules'], implode('.submodules.', $path));
         $moduleBreadcrumb[implode('/', $path)] = $config;
     }
     $moduleRequest->setArgument('__moduleConfiguration', $moduleConfiguration);
     $moduleResponse = new Response($this->response);
     $this->dispatcher->dispatch($moduleRequest, $moduleResponse);
     if ($moduleResponse->hasHeader('Location')) {
         $this->redirectToUri($moduleResponse->getHeader('Location'), 0, $moduleResponse->getStatusCode());
     } elseif ($moduleRequest->getFormat() !== 'html') {
         $mediaType = MediaTypes::getMediaTypeFromFilename('file.' . $moduleRequest->getFormat());
         if ($mediaType !== 'application/octet-stream') {
             $this->controllerContext->getResponse()->setHeader('Content-Type', $mediaType);
         }
         return $moduleResponse->getContent();
     } else {
         $user = $this->securityContext->getPartyByType('TYPO3\\Neos\\Domain\\Model\\User');
         $sites = $this->menuHelper->buildSiteList($this->controllerContext);
         $this->view->assignMultiple(array('moduleClass' => implode('-', $modules), 'moduleContents' => $moduleResponse->getContent(), 'title' => $moduleRequest->hasArgument('title') ? $moduleRequest->getArgument('title') : $moduleConfiguration['label'], 'rootModule' => array_shift($modules), 'submodule' => array_shift($modules), 'moduleConfiguration' => $moduleConfiguration, 'moduleBreadcrumb' => $moduleBreadcrumb, 'user' => $user, 'modules' => $this->menuHelper->buildModuleList($this->controllerContext), 'sites' => $sites));
     }
 }
 /**
  * @test
  * @dataProvider filenamesAndMediaTypes
  */
 public function getMediaTypeFromFilenameMapsFilenameOrExtensionToMediaType($filename, $expectedMediaType)
 {
     $this->assertSame($expectedMediaType, MediaTypes::getMediaTypeFromFilename($filename));
 }
 /**
  * Returns the Media Type for this resource
  *
  * @return string The IANA Media Type
  * @api
  */
 public function getMediaType()
 {
     if ($this->mediaType === null) {
         return MediaTypes::getMediaTypeFromFilename($this->filename);
     } else {
         return $this->mediaType;
     }
 }
 /**
  * Explicitly sets the content of the request body
  *
  * In most cases, content is just a string representation of the request body.
  * In order to reduce memory consumption for uploads and other big data, it is
  * also possible to pass a stream resource. The easies way to convert a local file
  * into a stream resource is probably: $resource = fopen('file://path/to/file', 'rb');
  *
  * @param string|resource $content The body content, for example arguments of a PUT request, or a stream resource
  * @return void
  * @api
  */
 public function setContent($content)
 {
     if (is_resource($content) && get_resource_type($content) === 'stream' && stream_is_local($content)) {
         $streamMetaData = stream_get_meta_data($content);
         $this->headers->set('Content-Length', filesize($streamMetaData['uri']));
         $this->headers->set('Content-Type', MediaTypes::getMediaTypeFromFilename($streamMetaData['uri']));
     }
     parent::setContent($content);
 }
 /**
  * Returns the Media Type for this resource
  *
  * @return string The IANA Media Type
  * @api
  */
 public function getMediaType()
 {
     return MediaTypes::getMediaTypeFromFilename('x.' . $this->getFileExtension());
 }
 /**
  * Set the suggested filename of this Object
  *
  * @param string $filename
  * @return void
  */
 public function setFilename($filename)
 {
     $pathInfo = UnicodeFunctions::pathinfo($filename);
     $extension = isset($pathInfo['extension']) ? '.' . strtolower($pathInfo['extension']) : '';
     $this->filename = $pathInfo['filename'] . $extension;
     $this->mediaType = MediaTypes::getMediaTypeFromFilename($this->filename);
 }