*/
use Drupal\Component\FileSystem\FileSystem as ComponentFileSystem;
-use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\PhpStorage\FileStorage;
use Drupal\Component\Utility\Bytes;
return \Drupal::service('file_system')->validScheme($scheme);
}
-
/**
* Returns the part of a URI after the schema.
*
// HTTP and to https://example.com/bar.jpg when viewing a HTTPS page)
// Both types of relative URIs are characterized by a leading slash, hence
// we can use a single check.
- if (Unicode::substr($uri, 0, 1) == '/') {
+ if (mb_substr($uri, 0, 1) == '/') {
return $uri;
}
else {
$http_host = $host . ':' . $port;
}
- return preg_replace('|^https?://' . $http_host . '|', '', $file_url);
+ return preg_replace('|^https?://' . preg_quote($http_host, '|') . '|', '', $file_url);
}
/**
}
// Attempt to resolve the URIs. This is necessary in certain configurations
// (see above).
- $real_source = drupal_realpath($source) ?: $source;
- $real_destination = drupal_realpath($destination) ?: $destination;
+ $file_system = \Drupal::service('file_system');
+ $real_source = $file_system->realpath($source) ?: $source;
+ $real_destination = $file_system->realpath($destination) ?: $destination;
// Perform the copy operation.
if (!@copy($real_source, $real_destination)) {
\Drupal::logger('file')->error('The specified file %file could not be copied to %destination.', ['%file' => $source, '%destination' => $destination]);
function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_EXISTS_RENAME) {
$original_source = $source;
$logger = \Drupal::logger('file');
+ $file_system = \Drupal::service('file_system');
// Assert that the source file actually exists.
if (!file_exists($source)) {
- // @todo Replace drupal_set_message() calls with exceptions instead.
- drupal_set_message(t('The specified file %file could not be moved/copied because no file by that name exists. Please check that you supplied the correct filename.', ['%file' => $original_source]), 'error');
- if (($realpath = drupal_realpath($original_source)) !== FALSE) {
+ // @todo Replace \Drupal::messenger()->addError() calls with exceptions
+ // instead.
+ \Drupal::messenger()->addError(t('The specified file %file could not be moved/copied because no file by that name exists. Please check that you supplied the correct filename.', ['%file' => $original_source]));
+ if (($realpath = $file_system->realpath($original_source)) !== FALSE) {
$logger->notice('File %file (%realpath) could not be moved/copied because it does not exist.', ['%file' => $original_source, '%realpath' => $realpath]);
}
else {
$destination = file_build_uri(drupal_basename($source));
}
-
// Prepare the destination directory.
if (file_prepare_directory($destination)) {
// The destination is already a directory, so append the source basename.
if (!file_prepare_directory($dirname)) {
// The destination is not valid.
$logger->notice('File %file could not be moved/copied because the destination directory %destination is not configured correctly.', ['%file' => $original_source, '%destination' => $dirname]);
- drupal_set_message(t('The specified file %file could not be moved/copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', ['%file' => $original_source]), 'error');
+ \Drupal::messenger()->addError(t('The specified file %file could not be moved/copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', ['%file' => $original_source]));
return FALSE;
}
}
// Determine whether we can perform this operation based on overwrite rules.
$destination = file_destination($destination, $replace);
if ($destination === FALSE) {
- drupal_set_message(t('The file %file could not be moved/copied because a file by that name already exists in the destination directory.', ['%file' => $original_source]), 'error');
+ \Drupal::messenger()->addError(t('The file %file could not be moved/copied because a file by that name already exists in the destination directory.', ['%file' => $original_source]));
$logger->notice('File %file could not be moved/copied because a file by that name already exists in the destination directory (%destination)', ['%file' => $original_source, '%destination' => $destination]);
return FALSE;
}
// Assert that the source and destination filenames are not the same.
- $real_source = drupal_realpath($source);
- $real_destination = drupal_realpath($destination);
+ $real_source = $file_system->realpath($source);
+ $real_destination = $file_system->realpath($destination);
if ($source == $destination || ($real_source !== FALSE) && ($real_source == $real_destination)) {
- drupal_set_message(t('The specified file %file was not moved/copied because it would overwrite itself.', ['%file' => $source]), 'error');
+ \Drupal::messenger()->addError(t('The specified file %file was not moved/copied because it would overwrite itself.', ['%file' => $source]));
$logger->notice('File %file could not be moved/copied because it would overwrite itself.', ['%file' => $source]);
return FALSE;
}
}
// Attempt to resolve the URIs. This is necessary in certain configurations
// (see above) and can also permit fast moves across local schemes.
- $real_source = drupal_realpath($source) ?: $source;
- $real_destination = drupal_realpath($destination) ?: $destination;
+ $file_system = \Drupal::service('file_system');
+ $real_source = $file_system->realpath($source) ?: $source;
+ $real_destination = $file_system->realpath($destination) ?: $destination;
// Perform the move operation.
if (!@rename($real_source, $real_destination)) {
// Fall back to slow copy and unlink procedure. This is necessary for
* @param $extensions
* A space-separated list of extensions that should not be altered.
* @param $alerts
- * If TRUE, drupal_set_message() will be called to display a message if the
- * file name was changed.
+ * If TRUE, \Drupal::messenger()->addStatus() will be called to display
+ * a message if the file name was changed.
*
* @return string
* The potentially modified $filename.
// Split the filename up by periods. The first part becomes the basename
// the last part the final extension.
$filename_parts = explode('.', $filename);
- $new_filename = array_shift($filename_parts); // Remove file basename.
- $final_extension = array_pop($filename_parts); // Remove final extension.
+ // Remove file basename.
+ $new_filename = array_shift($filename_parts);
+ // Remove final extension.
+ $final_extension = array_pop($filename_parts);
// Loop through the middle parts of the name and add an underscore to the
// end of each section that could be a file extension but isn't in the list
$filename = $new_filename . '.' . $final_extension;
if ($alerts && $original != $filename) {
- drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $filename]));
+ \Drupal::messenger()->addStatus(t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $filename]));
}
}
*
* @param $path
* A string containing either an URI or a file or directory path.
- * @param $callback
+ * @param callable $callback
* (optional) Callback function to run on each file prior to deleting it and
* on each directory prior to traversing it. For example, can be used to
* modify permissions.
return file_unmanaged_delete($path);
}
-
-
/**
* Moves an uploaded file to a new location.
*
// Write the data to a temporary file.
$temp_name = drupal_tempnam('temporary://', 'file');
if (file_put_contents($temp_name, $data) === FALSE) {
- drupal_set_message(t('The file could not be created.'), 'error');
+ \Drupal::messenger()->addError(t('The file could not be created.'));
return FALSE;
}
// performance boost.
if (!isset($options['nomask'])) {
$ignore_directories = Settings::get('file_scan_ignore_directories', []);
- array_walk($ignore_directories, function(&$value) {
+ array_walk($ignore_directories, function (&$value) {
$value = preg_quote($value, '/');
});
$default_nomask = '/^' . implode('|', $ignore_directories) . '$/';