[ticket/15276] Update file_info to get size of images

PHPBB3-15276
This commit is contained in:
Rubén Calvo 2017-08-09 22:08:45 +02:00
parent 3c60333725
commit 09856aeb26
9 changed files with 87 additions and 48 deletions

4
phpBB/cache/.htaccess vendored Normal file
View file

@ -0,0 +1,4 @@
<Files *>
Order Allow,Deny
Deny from All
</Files>

10
phpBB/cache/index.htm vendored Normal file
View file

@ -0,0 +1,10 @@
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
</body>
</html>

View file

@ -38,6 +38,7 @@ services:
shared: false shared: false
arguments: arguments:
- '@filesystem' - '@filesystem'
- '@upload_imagesize'
- '%core.root_path%' - '%core.root_path%'
tags: tags:
- { name: storage.adapter } - { name: storage.adapter }

View file

@ -85,15 +85,4 @@ interface adapter_interface
* When the file cannot be copied * When the file cannot be copied
*/ */
public function copy($path_orig, $path_dest); public function copy($path_orig, $path_dest);
/**
* Get file info.
*
* @param string $path The file
*
* @throws \phpbb\storage\exception\not_implemented When the adapter doesnt implement the method
*
* @return \phpbb\storage\file_info Returns file_info object
*/
public function file_properties($path);
} }

View file

@ -18,6 +18,7 @@ use phpbb\storage\exception\exception;
use phpbb\filesystem\exception\filesystem_exception; use phpbb\filesystem\exception\filesystem_exception;
use phpbb\filesystem\filesystem; use phpbb\filesystem\filesystem;
use phpbb\filesystem\helper as filesystem_helper; use phpbb\filesystem\helper as filesystem_helper;
use FastImageSize\FastImageSize;
/** /**
* @internal Experimental * @internal Experimental
@ -31,6 +32,13 @@ class local implements adapter_interface, stream_interface
*/ */
protected $filesystem; protected $filesystem;
/**
* Filesystem component
*
* @var \FastImageSize\FastImageSize
*/
protected $imagesize;
/** /**
* @var string path * @var string path
*/ */
@ -44,9 +52,10 @@ class local implements adapter_interface, stream_interface
/** /**
* Constructor * Constructor
*/ */
public function __construct(filesystem $filesystem, $phpbb_root_path) public function __construct(filesystem $filesystem, FastImageSize $imagesize, $phpbb_root_path)
{ {
$this->filesystem = $filesystem; $this->filesystem = $filesystem;
$this->imagesize = $imagesize;
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
} }
@ -236,14 +245,6 @@ class local implements adapter_interface, stream_interface
} }
} }
/**
* {@inheritdoc}
*/
public function file_properties($path)
{
return [];
}
/** /**
* Get file size. * Get file size.
* *
@ -251,7 +252,7 @@ class local implements adapter_interface, stream_interface
* *
* @throws \phpbb\storage\exception\exception When cannot get size * @throws \phpbb\storage\exception\exception When cannot get size
* *
* @return int File size in bytes * @return array Properties
*/ */
public function file_size($path) public function file_size($path)
{ {
@ -262,16 +263,15 @@ class local implements adapter_interface, stream_interface
throw new exception('STORAGE_CANNOT_GET_FILESIZE'); throw new exception('STORAGE_CANNOT_GET_FILESIZE');
} }
return $size; return ['size' => $size];
} }
/** /**
* Get file mimetype. * Get file mimetype.
* *
* @param string $path The file * @param string $path The file
* *
* @return string Mime type * @return array Properties
*/ */
public function file_mimetype($path) public function file_mimetype($path)
{ {
@ -285,6 +285,51 @@ class local implements adapter_interface, stream_interface
$mimetype = mime_content_type($this->root_path . $path); $mimetype = mime_content_type($this->root_path . $path);
} }
return $mimetype; return ['mimetype' => $mimetype];
}
/**
* Get image dimensions.
*
* @param string $path The file
*
* @return array Properties
*/
protected function image_dimensions($path)
{
$size = $this->imagesize->getImageSize($this->root_path . $path);
// For not supported types like swf
if ($size === false)
{
$imsize = getimagesize($this->root_path . $path);
$size = ['width' => $imsize[0], 'height' => $imsize[1]];
}
return ['image_width' => $size['width'], 'image_height' => $size['height']];
}
/**
* Get image width.
*
* @param string $path The file
*
* @return array Properties
*/
public function file_image_width($path)
{
return $this->image_dimensions($path);
}
/**
* Get image height.
*
* @param string $path The file
*
* @return array Properties
*/
public function file_image_height($path)
{
return $this->image_dimensions($path);
} }
} }

View file

@ -36,24 +36,7 @@ class file_info
{ {
$this->adapter = $adapter; $this->adapter = $adapter;
$this->path = $path; $this->path = $path;
}
/**
* Load propertys lazily.
*
* @param string path The file path.
*/
protected function fill_properties($path)
{
if ($this->properties === null)
{
$this->properties = []; $this->properties = [];
foreach ($this->adapter->file_properties($this->path) as $name => $value)
{
$this->properties[$name] = $value;
}
}
} }
/** /**
@ -65,8 +48,6 @@ class file_info
*/ */
public function get($name) public function get($name)
{ {
$this->fill_properties($this->path);
if (!isset($this->properties[$name])) if (!isset($this->properties[$name]))
{ {
if (!method_exists($this->adapter, 'file_' . $name)) if (!method_exists($this->adapter, 'file_' . $name))
@ -74,7 +55,7 @@ class file_info
throw new not_implemented(); throw new not_implemented();
} }
$this->properties[$name] = call_user_func([$this->adapter, 'file_' . $name], $this->path); $this->properties = array_merge($this->properties, call_user_func([$this->adapter, 'file_' . $name], $this->path));
} }
return $this->properties[$name]; return $this->properties[$name];

View file

@ -193,6 +193,15 @@ class storage
} }
} }
/**
* Get file info.
*
* @param string $path The file
*
* @throws \phpbb\storage\exception\not_implemented When the adapter doesnt implement the method
*
* @return \phpbb\storage\file_info Returns file_info object
*/
public function file_info($path) public function file_info($path)
{ {
return new file_info($this->adapter, $path); return new file_info($this->adapter, $path);

View file

@ -36,7 +36,7 @@ class phpbb_avatar_manager_test extends \phpbb_database_test_case
->will($this->returnArgument(0)); ->will($this->returnArgument(0));
$filesystem = new \phpbb\filesystem\filesystem(); $filesystem = new \phpbb\filesystem\filesystem();
$adapter = new \phpbb\storage\adapter\local($filesystem, $phpbb_root_path); $adapter = new \phpbb\storage\adapter\local($filesystem, new \FastImageSize\FastImageSize(), $phpbb_root_path);
$adapter->configure(['path' => 'images/avatars/upload']); $adapter->configure(['path' => 'images/avatars/upload']);
$adapter_factory_mock = $this->createMock('\phpbb\storage\adapter_factory'); $adapter_factory_mock = $this->createMock('\phpbb\storage\adapter_factory');
$adapter_factory_mock->expects($this->any()) $adapter_factory_mock->expects($this->any())

View file

@ -24,7 +24,7 @@
$filesystem = new \phpbb\filesystem\filesystem(); $filesystem = new \phpbb\filesystem\filesystem();
$phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR; $phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR;
$this->adapter = new \phpbb\storage\adapter\local($filesystem, $phpbb_root_path); $this->adapter = new \phpbb\storage\adapter\local($filesystem, new \FastImageSize\FastImageSize(), $phpbb_root_path);
$this->adapter->configure(['path' => 'test_path']); $this->adapter->configure(['path' => 'test_path']);
$this->path = $phpbb_root_path . 'test_path/'; $this->path = $phpbb_root_path . 'test_path/';