Merge pull request #6664 from rubencm/ticket/17357

[ticket/17357] Remove file_info from storage
This commit is contained in:
Marc Alexander 2024-09-25 17:07:04 +02:00 committed by GitHub
commit 96df4d1284
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 123 additions and 287 deletions

View file

@ -70,8 +70,6 @@ services:
shared: false shared: false
arguments: arguments:
- '@filesystem' - '@filesystem'
- '@upload_imagesize'
- '@mimetype.guesser'
- '%core.root_path%' - '%core.root_path%'
tags: tags:
- { name: storage.adapter } - { name: storage.adapter }
@ -90,6 +88,7 @@ services:
- '@cache' - '@cache'
- '@config' - '@config'
- '@dbal.conn' - '@dbal.conn'
- '@mimetype.extension_guesser'
- '@storage.avatar' - '@storage.avatar'
- '@symfony_request' - '@symfony_request'
@ -102,6 +101,7 @@ services:
- '@content.visibility' - '@content.visibility'
- '@dbal.conn' - '@dbal.conn'
- '@event_dispatcher' - '@event_dispatcher'
- '@mimetype.extension_guesser'
- '@language' - '@language'
- '@request' - '@request'
- '@storage.attachment' - '@storage.attachment'

View file

@ -173,8 +173,6 @@ class acp_attachments
'img_max_width' => array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,), 'img_max_width' => array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
'img_max_height' => array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,), 'img_max_height' => array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
'img_link_width' => array('lang' => 'IMAGE_LINK_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
'img_link_height' => array('lang' => 'IMAGE_LINK_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
'allow_attachments' => array('lang' => 'ALLOW_ATTACHMENTS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false), 'allow_attachments' => array('lang' => 'ALLOW_ATTACHMENTS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false),
'allow_pm_attach' => array('lang' => 'ALLOW_PM_ATTACHMENTS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false), 'allow_pm_attach' => array('lang' => 'ALLOW_PM_ATTACHMENTS', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false),
@ -197,7 +195,6 @@ class acp_attachments
'img_max' => array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0:9999', 'type' => 'dimension:0:9999', 'explain' => true, 'append' => ' ' . $user->lang['PIXEL']), 'img_max' => array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0:9999', 'type' => 'dimension:0:9999', 'explain' => true, 'append' => ' ' . $user->lang['PIXEL']),
'img_strip_metadata' => array('lang' => 'IMAGE_STRIP_METADATA', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true), 'img_strip_metadata' => array('lang' => 'IMAGE_STRIP_METADATA', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
'img_quality' => array('lang' => 'IMAGE_QUALITY', 'validate' => 'int:50:90', 'type' => 'number:50:90', 'explain' => true, 'append' => ' %'), 'img_quality' => array('lang' => 'IMAGE_QUALITY', 'validate' => 'int:50:90', 'type' => 'number:50:90', 'explain' => true, 'append' => ' %'),
'img_link' => array('lang' => 'IMAGE_LINK_SIZE', 'validate' => 'int:0:9999', 'type' => 'dimension:0:9999', 'explain' => true, 'append' => ' ' . $user->lang['PIXEL']),
) )
); );

View file

@ -1234,19 +1234,7 @@ function parse_attachments($forum_id, &$message, &$attachments, &$update_count_a
{ {
if ($config['img_display_inlined']) if ($config['img_display_inlined'])
{ {
if ($config['img_link_width'] || $config['img_link_height']) $display_cat = attachment_category::IMAGE;
{
try
{
$file_info = $storage_attachment->file_info($filename);
$display_cat = ($file_info->image_width <= $config['img_link_width'] && $file_info->image_height <= $config['img_link_height']) ? attachment_category::IMAGE : attachment_category::NONE;
}
catch (\Exception $e)
{
$display_cat = attachment_category::NONE;
}
}
} }
else else
{ {

View file

@ -361,8 +361,6 @@ class phpbb_questionnaire_phpbb_data_provider
'hot_threshold' => true, 'hot_threshold' => true,
'img_create_thumbnail' => true, 'img_create_thumbnail' => true,
'img_display_inlined' => true, 'img_display_inlined' => true,
'img_link_height' => true,
'img_link_width' => true,
'img_max_height' => true, 'img_max_height' => true,
'img_max_thumb_width' => true, 'img_max_thumb_width' => true,
'img_max_width' => true, 'img_max_width' => true,

View file

@ -1676,8 +1676,6 @@ function phpbb_import_attach_config()
$config->set('img_display_inlined', $attach_config['img_display_inlined']); $config->set('img_display_inlined', $attach_config['img_display_inlined']);
$config->set('img_max_width', $attach_config['img_max_width']); $config->set('img_max_width', $attach_config['img_max_width']);
$config->set('img_max_height', $attach_config['img_max_height']); $config->set('img_max_height', $attach_config['img_max_height']);
$config->set('img_link_width', $attach_config['img_link_width']);
$config->set('img_link_height', $attach_config['img_link_height']);
$config->set('img_create_thumbnail', $attach_config['img_create_thumbnail']); $config->set('img_create_thumbnail', $attach_config['img_create_thumbnail']);
$config->set('img_max_thumb_width', 400); $config->set('img_max_thumb_width', 400);
$config->set('img_min_thumb_filesize', $attach_config['img_min_thumb_filesize']); $config->set('img_min_thumb_filesize', $attach_config['img_min_thumb_filesize']);

View file

@ -155,8 +155,6 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('hot_threshold', '2
INSERT INTO phpbb_config (config_name, config_value) VALUES ('icons_path', 'images/icons'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('icons_path', 'images/icons');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_create_thumbnail', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_create_thumbnail', '0');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_display_inlined', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_display_inlined', '1');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_link_height', '0');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_link_width', '0');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_max_height', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_max_height', '0');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_max_thumb_width', '400'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_max_thumb_width', '400');
INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_max_width', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('img_max_width', '0');

View file

@ -111,8 +111,6 @@ $lang = array_merge($lang, array(
'GO_TO_EXTENSIONS' => 'Go to extension management screen', 'GO_TO_EXTENSIONS' => 'Go to extension management screen',
'GROUP_NAME' => 'Group name', 'GROUP_NAME' => 'Group name',
'IMAGE_LINK_SIZE' => 'Image link dimensions',
'IMAGE_LINK_SIZE_EXPLAIN' => 'Display image attachment as an inline text link if image is larger than this. To disable this behaviour, set the values to 0px by 0px.',
'IMAGE_QUALITY' => 'Quality of uploaded image attachments (JPEG only)', 'IMAGE_QUALITY' => 'Quality of uploaded image attachments (JPEG only)',
'IMAGE_QUALITY_EXPLAIN' => 'Specify value between 50% (smaller file size) and 90% (higher quality). Quality higher than 90% increases filesize and is disabled. Setting only applies if maximum image dimensions are set to a value other than 0px by 0px.', 'IMAGE_QUALITY_EXPLAIN' => 'Specify value between 50% (smaller file size) and 90% (higher quality). Quality higher than 90% increases filesize and is disabled. Setting only applies if maximum image dimensions are set to a value other than 0px by 0px.',
'IMAGE_STRIP_METADATA' => 'Strip image metadata (JPEG only)', 'IMAGE_STRIP_METADATA' => 'Strip image metadata (JPEG only)',

View file

@ -0,0 +1,41 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db\migration\data\v400;
use phpbb\db\migration\migration;
class remove_img_link extends migration
{
public function effectively_installed()
{
return !$this->config->offsetExists('img_link_width')
&& !$this->config->offsetExists('img_link_height');
}
public static function depends_on()
{
return [
'\phpbb\db\migration\data\v400\dev'
];
}
public function update_data()
{
return [
['config.remove', ['img_link_width']],
['config.remove', ['img_link_height']],
];
}
}

View file

@ -20,7 +20,7 @@ interface adapter_interface
/** /**
* Set adapter parameters * Set adapter parameters
* *
* @param array options Storage-specific options. * @param array $options options Storage-specific options.
*/ */
public function configure(array $options): void; public function configure(array $options): void;
@ -82,13 +82,15 @@ interface adapter_interface
public function copy(string $path_orig, string $path_dest): void; public function copy(string $path_orig, string $path_dest): void;
/** /**
* Get direct link * Get file size in bytes
* *
* @param string $path The file * @param string $path The file
* *
* @return string Returns link. * @return int Size in bytes.
*
* @throws storage_exception When unable to retrieve file size
*/ */
public function get_link(string $path): string; public function file_size(string $path): int;
/** /**
* Get space available in bytes * Get space available in bytes

View file

@ -18,8 +18,6 @@ use phpbb\storage\exception\storage_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 phpbb\mimetype\guesser;
use FastImageSize\FastImageSize;
/** /**
* Experimental * Experimental
@ -33,20 +31,6 @@ class local implements adapter_interface, stream_interface
*/ */
protected $filesystem; protected $filesystem;
/**
* FastImageSize
*
* @var FastImageSize
*/
protected $imagesize;
/**
* Mimetype Guesser component
*
* @var guesser
*/
protected $mimetype_guesser;
/** /**
* @var string path * @var string path
*/ */
@ -77,15 +61,11 @@ class local implements adapter_interface, stream_interface
* Constructor * Constructor
* *
* @param filesystem $filesystem * @param filesystem $filesystem
* @param FastImageSize $imagesize
* @param guesser $mimetype_guesser
* @param string $phpbb_root_path * @param string $phpbb_root_path
*/ */
public function __construct(filesystem $filesystem, FastImageSize $imagesize, guesser $mimetype_guesser, string $phpbb_root_path) public function __construct(filesystem $filesystem, string $phpbb_root_path)
{ {
$this->filesystem = $filesystem; $this->filesystem = $filesystem;
$this->imagesize = $imagesize;
$this->mimetype_guesser = $mimetype_guesser;
$this->phpbb_root_path = $phpbb_root_path; $this->phpbb_root_path = $phpbb_root_path;
} }
@ -294,15 +274,9 @@ class local implements adapter_interface, stream_interface
} }
/** /**
* Get file size * {@inheritdoc}
*
* @param string $path The file
*
* @return array Properties
*
* @throws storage_exception When cannot get size
*/ */
public function file_size(string $path): array public function file_size(string $path): int
{ {
$size = @filesize($this->root_path . $this->get_path($path) . $this->get_filename($path)); $size = @filesize($this->root_path . $this->get_path($path) . $this->get_filename($path));
@ -311,72 +285,7 @@ class local implements adapter_interface, stream_interface
throw new storage_exception('STORAGE_CANNOT_GET_FILESIZE'); throw new storage_exception('STORAGE_CANNOT_GET_FILESIZE');
} }
return ['size' => $size]; return $size;
}
/**
* Get file mimetype
*
* @param string $path The file
*
* @return array Properties
*/
public function file_mimetype(string $path): array
{
return ['mimetype' => $this->mimetype_guesser->guess($this->root_path . $this->get_path($path) . $this->get_filename($path))];
}
/**
* Get image dimensions
*
* @param string $path The file
*
* @return array Properties
*/
protected function image_dimensions(string $path): array
{
$size = $this->imagesize->getImageSize($this->root_path . $this->get_path($path) . $this->get_filename($path));
// For not supported types like swf
if ($size === false)
{
$imsize = getimagesize($this->root_path . $this->get_path($path) . $this->get_filename($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(string $path): array
{
return $this->image_dimensions($path);
}
/**
* Get image height
*
* @param string $path The file
*
* @return array Properties
*/
public function file_image_height(string $path): array
{
return $this->image_dimensions($path);
}
/**
* {@inheritdoc}
*/
public function get_link(string $path): string
{
return generate_board_url() . '/' . $this->path . $path;
} }
/** /**

View file

@ -22,6 +22,7 @@ use phpbb\db\driver\driver_interface;
use phpbb\event\dispatcher_interface; use phpbb\event\dispatcher_interface;
use phpbb\exception\http_exception; use phpbb\exception\http_exception;
use phpbb\language\language; use phpbb\language\language;
use phpbb\mimetype\extension_guesser;
use phpbb\request\request; use phpbb\request\request;
use phpbb\storage\storage; use phpbb\storage\storage;
use phpbb\user; use phpbb\user;
@ -66,15 +67,16 @@ class attachment extends controller
* @param content_visibility $content_visibility * @param content_visibility $content_visibility
* @param driver_interface $db * @param driver_interface $db
* @param dispatcher_interface $dispatcher * @param dispatcher_interface $dispatcher
* @param extension_guesser $extension_guesser
* @param language $language * @param language $language
* @param request $request * @param request $request
* @param storage $storage * @param storage $storage
* @param symfony_request $symfony_request * @param symfony_request $symfony_request
* @param user $user * @param user $user
*/ */
public function __construct(auth $auth, service $cache, config $config, content_visibility $content_visibility, driver_interface $db, dispatcher_interface $dispatcher, language $language, request $request, storage $storage, symfony_request $symfony_request, user $user) public function __construct(auth $auth, service $cache, config $config, content_visibility $content_visibility, driver_interface $db, dispatcher_interface $dispatcher, extension_guesser $extension_guesser, language $language, request $request, storage $storage, symfony_request $symfony_request, user $user)
{ {
parent::__construct($cache, $db, $storage, $symfony_request); parent::__construct($cache, $db, $extension_guesser, $storage, $symfony_request);
$this->auth = $auth; $this->auth = $auth;
$this->config = $config; $this->config = $config;

View file

@ -16,6 +16,7 @@ namespace phpbb\storage\controller;
use phpbb\cache\service; use phpbb\cache\service;
use phpbb\config\config; use phpbb\config\config;
use phpbb\db\driver\driver_interface; use phpbb\db\driver\driver_interface;
use phpbb\mimetype\extension_guesser;
use phpbb\storage\storage; use phpbb\storage\storage;
use Symfony\Component\HttpFoundation\Request as symfony_request; use Symfony\Component\HttpFoundation\Request as symfony_request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -39,12 +40,13 @@ class avatar extends controller
* @param service $cache * @param service $cache
* @param config $config * @param config $config
* @param driver_interface $db * @param driver_interface $db
* @param extension_guesser $extension_guesser
* @param storage $storage * @param storage $storage
* @param symfony_request $symfony_request * @param symfony_request $symfony_request
*/ */
public function __construct(service $cache, config $config, driver_interface $db, storage $storage, symfony_request $symfony_request) public function __construct(service $cache, config $config, driver_interface $db, extension_guesser $extension_guesser, storage $storage, symfony_request $symfony_request)
{ {
parent::__construct($cache, $db, $storage, $symfony_request); parent::__construct($cache, $db , $extension_guesser, $storage, $symfony_request);
$this->config = $config; $this->config = $config;
} }

View file

@ -16,6 +16,7 @@ namespace phpbb\storage\controller;
use phpbb\cache\service; use phpbb\cache\service;
use phpbb\db\driver\driver_interface; use phpbb\db\driver\driver_interface;
use phpbb\exception\http_exception; use phpbb\exception\http_exception;
use phpbb\mimetype\extension_guesser;
use phpbb\storage\exception\storage_exception; use phpbb\storage\exception\storage_exception;
use phpbb\storage\storage; use phpbb\storage\storage;
use Symfony\Component\HttpFoundation\Request as symfony_request; use Symfony\Component\HttpFoundation\Request as symfony_request;
@ -33,6 +34,9 @@ class controller
/** @var driver_interface */ /** @var driver_interface */
protected $db; protected $db;
/** @var extension_guesser */
protected $extension_guesser;
/** @var storage */ /** @var storage */
protected $storage; protected $storage;
@ -47,10 +51,11 @@ class controller
* @param storage $storage * @param storage $storage
* @param symfony_request $symfony_request * @param symfony_request $symfony_request
*/ */
public function __construct(service $cache, driver_interface $db, storage $storage, symfony_request $symfony_request) public function __construct(service $cache, driver_interface $db, extension_guesser $extension_guesser, storage $storage, symfony_request $symfony_request)
{ {
$this->cache = $cache; $this->cache = $cache;
$this->db = $db; $this->db = $db;
$this->extension_guesser = $extension_guesser;
$this->storage = $storage; $this->storage = $storage;
$this->symfony_request = $symfony_request; $this->symfony_request = $symfony_request;
} }
@ -124,14 +129,12 @@ class controller
*/ */
protected function prepare(StreamedResponse $response, string $file): void protected function prepare(StreamedResponse $response, string $file): void
{ {
$file_info = $this->storage->file_info($file);
// Add Content-Type header // Add Content-Type header
if (!$response->headers->has('Content-Type')) if (!$response->headers->has('Content-Type'))
{ {
try try
{ {
$content_type = $file_info->get('mimetype'); $content_type = $this->extension_guesser->guess($file);
} }
catch (storage_exception $e) catch (storage_exception $e)
{ {
@ -146,7 +149,7 @@ class controller
{ {
try try
{ {
$response->headers->set('Content-Length', $file_info->get('size')); $response->headers->set('Content-Length', $this->storage->file_size($file));
} }
catch (storage_exception $e) catch (storage_exception $e)
{ {

View file

@ -1,85 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\storage;
use phpbb\storage\exception\storage_exception;
use phpbb\storage\adapter\adapter_interface;
class file_info
{
/**
* @var adapter_interface
*/
protected $adapter;
/**
* Path of the file
*
* @var string
*/
protected $path;
/**
* Stores the properties of $path file, so dont have to be consulted multiple times.
* For example, when you need the width of an image, using getimagesize() you get
* both dimensions, so you store both here, and when you get the height, you dont have
* to call getimagesize() again
*
* @var array
*/
protected $properties;
/**
* Constructor
*
* @param adapter_interface $adapter
* @param string $path
*/
public function __construct(adapter_interface $adapter, $path)
{
$this->adapter = $adapter;
$this->path = $path;
$this->properties = [];
}
/**
* Load propertys lazily
*
* @param string $name The property name.
*
* @return string Returns the property value
*/
public function get($name)
{
if (!isset($this->properties[$name]))
{
if (!method_exists($this->adapter, 'file_' . $name))
{
throw new storage_exception('STORAGE_METHOD_NOT_IMPLEMENTED');
}
$this->properties = array_merge($this->properties, call_user_func([$this->adapter, 'file_' . $name], $this->path));
}
return $this->properties[$name];
}
/**
* Alias of \phpbb\storage\file_info->get()
*/
public function __get($name)
{
return $this->get($name);
}
}

View file

@ -59,11 +59,11 @@ class storage
* *
* @param db $db * @param db $db
* @param cache $cache * @param cache $cache
* @param adapter_factory $factory * @param adapter_factory $factory
* @param string $storage_name * @param string $storage_name
* @param string $storage_table * @param string $storage_table
*/ */
public function __construct(db $db, cache $cache, adapter_factory $factory, $storage_name, $storage_table) public function __construct(db $db, cache $cache, adapter_factory $factory, string $storage_name, string $storage_table)
{ {
$this->db = $db; $this->db = $db;
$this->cache = $cache; $this->cache = $cache;
@ -77,7 +77,7 @@ class storage
* *
* @return string * @return string
*/ */
public function get_name() public function get_name(): string
{ {
return $this->storage_name; return $this->storage_name;
} }
@ -87,7 +87,7 @@ class storage
* *
* @return adapter_interface * @return adapter_interface
*/ */
protected function get_adapter() protected function get_adapter(): mixed
{ {
if ($this->adapter === null) if ($this->adapter === null)
{ {
@ -106,7 +106,7 @@ class storage
* @throws storage_exception When the file already exists * @throws storage_exception When the file already exists
* When the file cannot be written * When the file cannot be written
*/ */
public function put_contents($path, $content) public function put_contents(string $path, string $content): void
{ {
if ($this->exists($path)) if ($this->exists($path))
{ {
@ -128,7 +128,7 @@ class storage
* When cannot read file contents * When cannot read file contents
* *
*/ */
public function get_contents($path) public function get_contents(string $path): string
{ {
if (!$this->exists($path)) if (!$this->exists($path))
{ {
@ -146,7 +146,7 @@ class storage
* *
* @return bool Returns true if the file/directory exist, false otherwise * @return bool Returns true if the file/directory exist, false otherwise
*/ */
public function exists($path, $full_check = false) public function exists(string $path, bool $full_check = false): bool
{ {
return ($this->is_tracked($path) && (!$full_check || $this->get_adapter()->exists($path))); return ($this->is_tracked($path) && (!$full_check || $this->get_adapter()->exists($path)));
} }
@ -154,12 +154,12 @@ class storage
/** /**
* Removes files or directories * Removes files or directories
* *
* @param string $path file/directory to remove * @param string $path file/directory to remove
* *
* @throws storage_exception When removal fails * @throws storage_exception When removal fails
* When the file doesn't exist * When the file doesn't exist
*/ */
public function delete($path) public function delete(string $path): void
{ {
if (!$this->exists($path)) if (!$this->exists($path))
{ {
@ -173,14 +173,14 @@ class storage
/** /**
* Rename a file or a directory * Rename a file or a directory
* *
* @param string $path_orig The original file/direcotry * @param string $path_orig The original file/direcotry
* @param string $path_dest The target file/directory * @param string $path_dest The target file/directory
* *
* @throws storage_exception When the file doesn't exist * @throws storage_exception When the file doesn't exist
* When target exists * When target exists
* When file/directory cannot be renamed * When file/directory cannot be renamed
*/ */
public function rename($path_orig, $path_dest) public function rename(string $path_orig, string $path_dest): void
{ {
if (!$this->exists($path_orig)) if (!$this->exists($path_orig))
{ {
@ -199,14 +199,14 @@ class storage
/** /**
* Copies a file * Copies a file
* *
* @param string $path_orig The original filename * @param string $path_orig The original filename
* @param string $path_dest The target filename * @param string $path_dest The target filename
* *
* @throws storage_exception When the file doesn't exist * @throws storage_exception When the file doesn't exist
* When target exists * When target exists
* When the file cannot be copied * When the file cannot be copied
*/ */
public function copy($path_orig, $path_dest) public function copy(string $path_orig, string $path_dest): void
{ {
if (!$this->exists($path_orig)) if (!$this->exists($path_orig))
{ {
@ -225,14 +225,14 @@ class storage
/** /**
* Reads a file as a stream * Reads a file as a stream
* *
* @param string $path File to read * @param string $path File to read
* *
* @return resource Returns a file pointer * @return resource Returns a file pointer
* @throws storage_exception When the file doesn't exist * @throws storage_exception When the file doesn't exist
* When unable to open file * When unable to open file
* *
*/ */
public function read_stream($path) public function read_stream(string $path)
{ {
if (!$this->exists($path)) if (!$this->exists($path))
{ {
@ -260,13 +260,13 @@ class storage
/** /**
* Writes a new file using a stream * Writes a new file using a stream
* *
* @param string $path The target file * @param string $path The target file
* @param resource $resource The resource * @param resource $resource The resource
* *
* @throws storage_exception When the file exist * @throws storage_exception When the file exist
* When target file cannot be created * When target file cannot be created
*/ */
public function write_stream($path, $resource) public function write_stream(string $path, $resource): void
{ {
if ($this->exists($path)) if ($this->exists($path))
{ {
@ -295,10 +295,10 @@ class storage
/** /**
* Track file in database * Track file in database
* *
* @param string $path The target file * @param string $path The target file
* @param bool $update Update file size when already tracked * @param bool $update Update file size when already tracked
*/ */
public function track_file($path, $update = false) public function track_file(string $path, bool $update = false): void
{ {
if (!$this->get_adapter()->exists($path)) if (!$this->get_adapter()->exists($path))
{ {
@ -319,20 +319,15 @@ class storage
if (!$row) if (!$row)
{ {
// Don't call the file_info method, because it check's if the file is tracked $sql_ary['filesize'] = $this->get_adapter()->file_size($path);
// and is not (for now). This method check if the file exists using the adapter
// at the beginning.
$file = new file_info($this->get_adapter(), $path);
$sql_ary['filesize'] = $file->size;
$sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql); $this->db->sql_query($sql);
} }
else if ($update) else if ($update)
{ {
$file = $this->file_info($path);
$sql = 'UPDATE ' . $this->storage_table . ' $sql = 'UPDATE ' . $this->storage_table . '
SET filesize = ' . $file->size . ' SET filesize = ' . $this->get_adapter()->file_size($path) . '
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary); WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
$this->db->sql_query($sql); $this->db->sql_query($sql);
} }
@ -364,11 +359,11 @@ class storage
/** /**
* Check if a file is tracked * Check if a file is tracked
* *
* @param string $path The file * @param string $path The file
* *
* @return bool True if file is tracked * @return bool True if file is tracked
*/ */
public function is_tracked($path) public function is_tracked(string $path): bool
{ {
$sql_ary = array( $sql_ary = array(
'file_path' => $path, 'file_path' => $path,
@ -381,16 +376,16 @@ class storage
$row = $this->db->sql_fetchrow($result); $row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);
return ($row) ? true : false; return $row !== false;
} }
/** /**
* Rename tracked file * Rename tracked file
* *
* @param string $path_orig The original file/direcotry * @param string $path_orig The original file/direcotry
* @param string $path_dest The target file/directory * @param string $path_dest The target file/directory
*/ */
protected function track_rename($path_orig, $path_dest) protected function track_rename(string $path_orig, string $path_dest): void
{ {
$sql = 'UPDATE ' . $this->storage_table . " $sql = 'UPDATE ' . $this->storage_table . "
SET file_path = '" . $this->db->sql_escape($path_dest) . "' SET file_path = '" . $this->db->sql_escape($path_dest) . "'
@ -400,36 +395,28 @@ class storage
} }
/** /**
* Get file info * Get file size in bytes
* *
* @param string $path The file * @param string $path The file
* *
* @return \phpbb\storage\file_info Returns file_info object * @return int Size in bytes.
* @throws storage_exception When the adapter doesn't implement the method
* When the file doesn't exist
* *
* @throws storage_exception When unable to retrieve file size
*/ */
public function file_info($path) public function file_size(string $path): int
{ {
if (!$this->exists($path)) $sql_ary = array(
{ 'file_path' => $path,
throw new storage_exception('STORAGE_FILE_NO_EXIST', $path); 'storage' => $this->get_name(),
} );
return new file_info($this->get_adapter(), $path); $sql = 'SELECT filesize FROM ' . $this->storage_table . '
} WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
$result = $this->db->sql_query($sql);
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
/** return $row !== false && !empty($row['filesize']) ? $row['filesize'] : $this->get_adapter()->file_size($path);
* Get direct link
*
* @param string $path The file
*
* @return string Returns link.
*
*/
public function get_link($path)
{
return $this->get_adapter()->get_link($path);
} }
/** /**
@ -437,7 +424,7 @@ class storage
* *
* @return int Size in bytes * @return int Size in bytes
*/ */
public function get_size() public function get_size(): int
{ {
$total_size = $this->cache->get('_storage_' . $this->get_name() . '_totalsize'); $total_size = $this->cache->get('_storage_' . $this->get_name() . '_totalsize');
@ -462,7 +449,7 @@ class storage
* *
* @return int Number of files * @return int Number of files
*/ */
public function get_num_files() public function get_num_files(): int
{ {
$number_files = $this->cache->get('_storage_' . $this->get_name() . '_numfiles'); $number_files = $this->cache->get('_storage_' . $this->get_name() . '_numfiles');
@ -493,4 +480,5 @@ class storage
{ {
return $this->get_adapter()->free_space(); return $this->get_adapter()->free_space();
} }
} }

View file

@ -86,16 +86,16 @@ class phpbb_attachment_delete_test extends \phpbb_database_test_case
public function data_attachment_unlink() public function data_attachment_unlink()
{ {
return array( return array(
array(true, true, true), array(true, true),
array(true, false, false), array(false, false),
array(true, true, false, true), array(true, false, true),
); );
} }
/** /**
* @dataProvider data_attachment_unlink * @dataProvider data_attachment_unlink
*/ */
public function test_attachment_delete_success($remove_success, $exists_success, $expected, $throw_exception = false) public function test_attachment_delete_success($exists_success, $expected, $throw_exception = false)
{ {
$this->storage = $this->createMock('\phpbb\storage\storage'); $this->storage = $this->createMock('\phpbb\storage\storage');
if ($throw_exception) if ($throw_exception)
@ -107,8 +107,7 @@ class phpbb_attachment_delete_test extends \phpbb_database_test_case
else else
{ {
$this->storage->expects($this->any()) $this->storage->expects($this->any())
->method('delete') ->method('delete');
->willReturn($remove_success);
} }
$this->storage->expects($this->any()) $this->storage->expects($this->any())
->method('exists') ->method('exists')

View file

@ -33,8 +33,6 @@ class phpbb_local_test_case extends phpbb_test_case
$this->adapter = new local( $this->adapter = new local(
$this->filesystem, $this->filesystem,
new FastImageSize(),
new guesser(array(new extension_guesser)),
$phpbb_root_path $phpbb_root_path
); );