Clean up from #1631

- Files in the media library can now contain two dots
- Retina Images Support
This commit is contained in:
Samuel Georges 2016-02-27 14:49:38 +11:00
parent 423360b2d2
commit 8e9ae8e53e
2 changed files with 44 additions and 41 deletions

View File

@ -381,35 +381,30 @@ class MediaLibrary
$path = str_replace('\\', '/', $path);
$path = '/'.trim($path, '/');
if ($normalizeOnly)
if ($normalizeOnly) {
return $path;
}
$regexDirectorySeparator = preg_quote(DIRECTORY_SEPARATOR, '/');
$regexDot = preg_quote('.', '/');
$regex = [
/**
* Checks for parent or current directory reference at beginning of path
*/
// Checks for parent or current directory reference at beginning of path
'(^'.$regexDot.'+?'.$regexDirectorySeparator.')',
/**
* Check for parent or current directory reference in middle of path
*/
'('.$regexDirectorySeparator.$regexDot.'+?'.$regexDirectorySeparator.')',
// Check for parent or current directory reference in middle of path
'('.$regexDirectorySeparator.$regexDot.'+?'.$regexDirectorySeparator.')',
/**
* Check for parent or current directory reference at end of path
*/
'('.$regexDirectorySeparator.$regexDot.'+?$)',
// Check for parent or current directory reference at end of path
'('.$regexDirectorySeparator.$regexDot.'+?$)',
];
/**
* Now, let's combine everything to one regex
/*
* Combine everything to one regex
*/
$regex = '/'.implode('|', $regex).'/';
if (preg_match($regex, $path) !== 0 || strpos($path, '//') !== false)
throw new ApplicationException(Lang::get('cms::lang.media.invalid_path', ['path'=>$path]));
if (preg_match($regex, $path) !== 0 || strpos($path, '//') !== false) {
throw new ApplicationException(Lang::get('cms::lang.media.invalid_path', compact('path')));
}
return $path;
}

View File

@ -945,25 +945,6 @@ class MediaManager extends WidgetBase
}
}
/**
* Creates a slug form the string. A modified version of Laravel's Str::slug
* with the main difference that it accepts @-signs
* @param string
* @return string
*/
public static function slug($string)
{
$title = Str::ascii($title);
// Convert all dashes/underscores into separator
$flip = $separator == '-' ? '_' : '-';
$title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
// Remove all characters that are not the separator, letters, numbers, whitespace or @.
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s@]+!u', '', mb_strtolower($title));
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
return trim($title, $separator);
}
protected function checkUploadPostback()
{
$fileName = null;
@ -989,15 +970,14 @@ class MediaManager extends WidgetBase
$fileName = File::name($fileName).'.'.$extension;
/*
* File name contains non-latin characters, attempt to slug the value
*/
* File name contains non-latin characters, attempt to slug the value
*/
if (!$this->validateFileName($fileName)) {
$fileNameSlug = static::slug(File::name($fileName), '-');
$fileName = $fileNameSlug.'.'.$extension;
$fileNameClean = $this->cleanFileName(File::name($fileName));
$fileName = $fileNameClean . '.' . $extension;
}
// See mime type handling in the asset manager
if (!$uploadedFile->isValid()) {
throw new ApplicationException($uploadedFile->getErrorMessage());
}
@ -1018,6 +998,11 @@ class MediaManager extends WidgetBase
}
}
/**
* Validate a proposed media item file name.
* @param string
* @return string
*/
protected function validateFileName($name)
{
if (!preg_match('/^[0-9a-z@\.\s_\-]+$/i', $name)) {
@ -1031,6 +1016,29 @@ class MediaManager extends WidgetBase
return true;
}
/**
* Creates a slug form the string. A modified version of Str::slug
* with the main difference that it accepts @-signs
* @param string
* @return string
*/
protected function cleanFileName($name)
{
$title = Str::ascii($title);
// Convert all dashes/underscores into separator
$flip = $separator = '-';
$title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
// Remove all characters that are not the separator, letters, numbers, whitespace or @.
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s@]+!u', '', mb_strtolower($title));
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
return trim($title, $separator);
}
//
// Cropping
//