Exemplo n.º 1
0
/**
 * Check if folder is available or create it.
 *
 * @param <string> $dir
 *    Folder to check
 */
function _humble_check_dir($dir)
{
    // Normalize directory name
    $dir = file_stream_wrapper_uri_normalize($dir);
    // Create directory (if not exist)
    file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
}
Exemplo n.º 2
0
/**
 * Parses a url or embedded code into a unique URI.
 *
 * @param string $url
 *   The original URL or embed code to parse.
 *
 * @return array
 *   The unique URI for the file, based on its stream wrapper, or NULL.
 *
 * @see media_parse_to_file()
 * @see media_add_from_url_validate()
 */
function hook_media_parse($url)
{
    // Only parse URLs from our website of choice: examplevideo.com
    if (substr($url, 0, 27) == 'http://www.examplevideo.com') {
        // Each video has a 5 digit ID, i.e. http://www.examplevideo.com/12345
        // Grab the ID and use it in our URI.
        $id = substr($url, 28, 33);
        return file_stream_wrapper_uri_normalize('examplevideo://video/' . $id);
    }
}
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     // Create target directory if necessary
     $destination = \Drupal::config('system.file')->get('default_scheme') . '://plupload-test';
     file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
     $saved_files = array();
     foreach ($form_state->getValue('plupload') as $uploaded_file) {
         $file_uri = file_stream_wrapper_uri_normalize($destination . '/' . $uploaded_file['name']);
         // Move file without creating a new 'file' entity.
         file_unmanaged_move($uploaded_file['tmppath'], $file_uri);
         // @todo: When https://www.drupal.org/node/2245927 is resolved,
         // use a helper to save file to file_managed table
         $saved_files[] = $file_uri;
     }
     if (!empty($saved_files)) {
         drupal_set_message('Files uploaded correctly: ' . implode(', ', $saved_files) . '.', 'status');
     }
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function buildUrl($path, $clean_urls = NULL)
 {
     $uri = $this->buildUri($path);
     // The token query is added even if the
     // 'image.settings:allow_insecure_derivatives' configuration is TRUE, so
     // that the emitted links remain valid if it is changed back to the default
     // FALSE. However, sites which need to prevent the token query from being
     // emitted at all can additionally set the
     // 'image.settings:suppress_itok_output' configuration to TRUE to achieve
     // that (if both are set, the security token will neither be emitted in the
     // image derivative URL nor checked for in
     // \Drupal\image\ImageStyleInterface::deliver()).
     $token_query = array();
     if (!\Drupal::config('image.settings')->get('suppress_itok_output')) {
         // The passed $path variable can be either a relative path or a full URI.
         $original_uri = file_uri_scheme($path) ? file_stream_wrapper_uri_normalize($path) : file_build_uri($path);
         $token_query = array(IMAGE_DERIVATIVE_TOKEN => $this->getPathToken($original_uri));
     }
     if ($clean_urls === NULL) {
         // Assume clean URLs unless the request tells us otherwise.
         $clean_urls = TRUE;
         try {
             $request = \Drupal::request();
             $clean_urls = RequestHelper::isCleanUrl($request);
         } catch (ServiceNotFoundException $e) {
         }
     }
     // If not using clean URLs, the image derivative callback is only available
     // with the script path. If the file does not exist, use Url::fromUri() to
     // ensure that it is included. Once the file exists it's fine to fall back
     // to the actual file path, this avoids bootstrapping PHP once the files are
     // built.
     if ($clean_urls === FALSE && file_uri_scheme($uri) == 'public' && !file_exists($uri)) {
         $directory_path = file_stream_wrapper_get_instance_by_uri($uri)->getDirectoryPath();
         return Url::fromUri('base:' . $directory_path . '/' . file_uri_target($uri), array('absolute' => TRUE, 'query' => $token_query))->toString();
     }
     $file_url = file_create_url($uri);
     // Append the query string with the token, if necessary.
     if ($token_query) {
         $file_url .= (strpos($file_url, '?') !== FALSE ? '&' : '?') . UrlHelper::buildQuery($token_query);
     }
     return $file_url;
 }
Exemplo n.º 5
0
 /**
  * Show query arguments, called by the AJAX link in the query log.
  */
 function queryLogArguments($request_id = NULL, $qid = NULL)
 {
     if (!is_numeric($request_id)) {
         throw new AccessDeniedHttpException();
     }
     $path = "temporary://devel_querylog/{$request_id}.txt";
     $path = file_stream_wrapper_uri_normalize($path);
     $output = $this->t('No arguments log found.');
     if (file_exists($path)) {
         $queries = Json::decode(file_get_contents($path));
         if ($queries !== FALSE && isset($queries[$qid])) {
             $query = $queries[$qid];
             $conn = Database::getConnection();
             $quoted = array();
             foreach ((array) $query['args'] as $key => $val) {
                 $quoted[$key] = is_null($val) ? 'NULL' : $conn->quote($val);
             }
             $output = strtr($query['query'], $quoted);
         }
     }
     $GLOBALS['devel_shutdown'] = FALSE;
     return new Response($output);
 }
Exemplo n.º 6
0
 /**
  * Helper function to get all the images from the configured folder.
  */
 private function getImages()
 {
     if ($files = cache_get('devel_image_provider_local')) {
         return $files->data;
     }
     $files = array();
     $count = 1;
     // Limiting number of images to find to 100.
     // @TODO: add this as a setting.
     $max_count = 100;
     // Remove trailing slash.
     $dir = rtrim($this->settings['devel_image_provider_path'], '/');
     if (is_dir($dir) && ($handle = opendir($dir))) {
         while (FALSE !== ($filename = readdir($handle)) && $count <= $max_count) {
             $path = "{$dir}/{$filename}";
             if ($filename[0] != '.' && preg_match('/.*\\.(jpg|jpeg|png)$/i', $filename) && ($image = image_get_info($path))) {
                 $file = new stdClass();
                 $file->uri = file_stream_wrapper_uri_normalize($path);
                 $file->filename = $filename;
                 $file->filemime = $image['mime_type'];
                 $file->name = pathinfo($filename, PATHINFO_FILENAME);
                 $files[$file->uri] = $file;
                 $count++;
             }
         }
         closedir($handle);
     }
     cache_set('devel_image_provider_local', $files, 'cache', CACHE_TEMPORARY);
     return $files;
 }
Exemplo n.º 7
0
 /**
  * Show query arguments, called by the AJAX link in the query log.
  */
 function queryLogArguments($request_id = NULL, $qid = NULL)
 {
     if (!is_numeric($request_id)) {
         throw new AccessDeniedHttpException();
     }
     $path = "temporary://devel_querylog/{$request_id}.txt";
     $path = file_stream_wrapper_uri_normalize($path);
     $queries = json_decode(file_get_contents($path));
     $query = $queries[$qid];
     $conn = \Drupal\Core\Database\Database::getConnection();
     $quoted = array();
     foreach ((array) $query->args as $key => $val) {
         $quoted[$key] = is_null($val) ? 'NULL' : $conn->quote($val);
     }
     $output = strtr($query->query, $quoted);
     $GLOBALS['devel_shutdown'] = FALSE;
     return new Response($output);
 }
Exemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function fileEntityFromUri($uri, AccountProxyInterface $user)
 {
     $uri = file_stream_wrapper_uri_normalize($uri);
     $file_info = new \SplFileInfo($uri);
     // Begin building file entity.
     $values = ['uid' => $user->id(), 'status' => 0, 'filename' => $file_info->getFilename(), 'uri' => $uri, 'filesize' => $file_info->getSize(), 'filemime' => $this->mimeTypeGuesser->guess($uri)];
     /** @var \Drupal\file\FileInterface $file */
     $file = $this->entityManager->getStorage('file')->create($values);
     return $file;
 }
Exemplo n.º 9
0
 /**
  * Render API callback: Validates the managed_file element.
  *
  * Note: based on plupload_element_validate().
  */
 public static function validatePlUploadFile(&$element, FormStateInterface $form_state, &$complete_form)
 {
     foreach ($element['#value'] as $file_info) {
         // Here we create a $file object for a file that doesn't exist yet,
         // because saving the file to its destination is done in a submit handler.
         // Using tmp path will give validators access to the actual file on disk and
         // filesize information. We manually modify filename and mime to allow
         // extension checks.
         $destination = \Drupal::config('system.file')->get('default_scheme') . '://' . $file_info['name'];
         $destination = file_stream_wrapper_uri_normalize($destination);
         $file = entity_create('file', array('uri' => $file_info['tmppath'], 'uid' => \Drupal::currentUser()->id(), 'status' => FILE_STATUS_PERMANENT, 'filename' => drupal_basename($destination), 'filemime' => \Drupal::service('file.mime_type.guesser')->guess($destination)));
         foreach (file_validate($file, $element['#upload_validators']) as $error_message) {
             $message = t('The specified file %name could not be uploaded.', array('%name' => $file->getFilename()));
             $concatenated_message = $message . ' ' . $error_message;
             $form_state->setError($element, $concatenated_message);
         }
     }
 }
Exemplo n.º 10
0
 /**
  * Constructs a URI to a location given a relative path.
  *
  * @param string $scheme
  *   A valid stream wrapper, such as 'public' or 'private'
  *
  * @param $path
  *   The path component that should come after the stream wrapper.
  *
  * @return string
  */
 public static function buildFileUri($scheme, $path) {
   $uri = $scheme . '://' . $path;
   return file_stream_wrapper_uri_normalize($uri);
 }