diff --git a/phpBB/includes/acp/acp_storage.php b/phpBB/includes/acp/acp_storage.php index b2647843f1..4348fb3b21 100644 --- a/phpBB/includes/acp/acp_storage.php +++ b/phpBB/includes/acp/acp_storage.php @@ -158,7 +158,7 @@ class acp_storage } } - // If there is no errors + // If there is no changes trigger_error($this->lang->lang('STORAGE_NO_CHANGES') . adm_back_link($this->u_action), E_USER_WARNING); } diff --git a/phpBB/install/schemas/schema_data.sql b/phpBB/install/schemas/schema_data.sql index 14c63fab72..a3af830817 100644 --- a/phpBB/install/schemas/schema_data.sql +++ b/phpBB/install/schemas/schema_data.sql @@ -289,10 +289,13 @@ INSERT INTO phpbb_config (config_name, config_value) VALUES ('exts_composer_enab INSERT INTO phpbb_config (config_name, config_value) VALUES ('exts_composer_purge_on_remove', '1'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('storage\attachment\provider', 'phpbb\storage\provider\local'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('storage\attachment\config\path', 'files'); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('storage\attachment\config\depth', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('storage\avatar\provider', 'phpbb\storage\provider\local'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('storage\avatar\config\path', 'images/avatars/upload'); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('storage\avatar\config\depth', '0'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('storage\backup\provider', 'phpbb\storage\provider\local'); INSERT INTO phpbb_config (config_name, config_value) VALUES ('storage\backup\config\path', 'store'); +INSERT INTO phpbb_config (config_name, config_value) VALUES ('storage\backup\config\depth', '0'); INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('cache_last_gc', '0', 1); INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('cron_lock', '0', 1); diff --git a/phpBB/language/en/acp/storage.php b/phpBB/language/en/acp/storage.php index 638df2177e..8d20a24db7 100644 --- a/phpBB/language/en/acp/storage.php +++ b/phpBB/language/en/acp/storage.php @@ -52,6 +52,7 @@ $lang = array_merge($lang, array( // Local adapter 'STORAGE_ADAPTER_LOCAL_NAME' => 'Local', 'STORAGE_ADAPTER_LOCAL_OPTION_PATH' => 'Path', + 'STORAGE_ADAPTER_LOCAL_OPTION_DEPTH' => 'Depth', // Form validation 'STORAGE_UPDATE_SUCCESSFUL' => 'All storage types were successfully updated.', diff --git a/phpBB/phpbb/db/migration/data/v330/storage_adapter_local_depth.php b/phpBB/phpbb/db/migration/data/v330/storage_adapter_local_depth.php new file mode 100644 index 0000000000..2da4c1954a --- /dev/null +++ b/phpBB/phpbb/db/migration/data/v330/storage_adapter_local_depth.php @@ -0,0 +1,44 @@ + +* @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\v330; + +class storage_adapter_local_depth extends \phpbb\db\migration\migration +{ + static public function depends_on() + { + return array( + '\phpbb\db\migration\data\v330\storage_attachment', + '\phpbb\db\migration\data\v330\storage_avatar', + '\phpbb\db\migration\data\v330\storage_backup', + ); + } + + public function update_data() + { + return array( + array('if', array( + ($this->config['storage\\attachment\\provider'] == \phpbb\storage\provider\local::class), + array('config.add', array('storage\\attachment\\config\\depth', '0')), + )), + array('if', array( + ($this->config['storage\\avatar\\provider'] == \phpbb\storage\provider\local::class), + array('config.add', array('storage\\avatar\\config\\depth', '0')), + )), + array('if', array( + ($this->config['storage\\backup\\provider'] == \phpbb\storage\provider\local::class), + array('config.add', array('storage\\backup\\config\\depth', '0')), + )), + ); + } +} diff --git a/phpBB/phpbb/storage/adapter/local.php b/phpBB/phpbb/storage/adapter/local.php index 16ddebad91..bfcdebc565 100644 --- a/phpBB/phpbb/storage/adapter/local.php +++ b/phpBB/phpbb/storage/adapter/local.php @@ -62,6 +62,22 @@ class local implements adapter_interface, stream_interface */ protected $path; + /* + * Subdirectories depth + * + * Instead of storing all folders in the same directory, they can be divided + * into smaller directories. The variable describes the number of subdirectories + * to be used for storing the files. For example: + * depth = 0 -> /images/avatars/upload/my_avatar.jpg + * depth = 2 -> /images/avatars/upload/d9/8c/my_avatar.jpg + * This is for those who have problems storing a large number of files in + * a single directory. + * More info: https://tracker.phpbb.com/browse/PHPBB3-15371 + * + * @var int dir_depth + */ + protected $dir_depth; + /** * Constructor */ @@ -85,6 +101,7 @@ class local implements adapter_interface, stream_interface $this->path = $options['path']; $this->root_path = $this->phpbb_root_path . $options['path']; + $this->dir_depth = (int) $options['depth']; } /** @@ -101,7 +118,7 @@ class local implements adapter_interface, stream_interface try { - $this->filesystem->dump_file($this->root_path . $path, $content); + $this->filesystem->dump_file($this->root_path . $this->get_path($path) . $this->get_filename($path), $content); } catch (filesystem_exception $e) { @@ -119,7 +136,7 @@ class local implements adapter_interface, stream_interface throw new exception('STORAGE_FILE_NO_EXIST', $path); } - $content = @file_get_contents($this->root_path . $path); + $content = @file_get_contents($this->root_path . $this->get_path($path) . $this->get_filename($path)); if ($content === false) { @@ -134,7 +151,7 @@ class local implements adapter_interface, stream_interface */ public function exists($path) { - return $this->filesystem->exists($this->root_path . $path); + return $this->filesystem->exists($this->root_path . $this->get_path($path) . $this->get_filename($path)); } /** @@ -144,12 +161,14 @@ class local implements adapter_interface, stream_interface { try { - $this->filesystem->remove($this->root_path . $path); + $this->filesystem->remove($this->root_path . $this->get_path($path) . $this->get_filename($path)); } catch (filesystem_exception $e) { throw new exception('STORAGE_CANNOT_DELETE', $path, array(), $e); } + + $this->remove_empty_dirs($path); } /** @@ -161,12 +180,14 @@ class local implements adapter_interface, stream_interface try { - $this->filesystem->rename($this->root_path . $path_orig, $this->root_path . $path_dest, false); + $this->filesystem->rename($this->root_path . $this->get_path($path_orig) . $this->get_filename($path_orig), $this->root_path . $this->get_path($path_dest) . $this->get_filename($path_dest), false); } catch (filesystem_exception $e) { throw new exception('STORAGE_CANNOT_RENAME', $path_orig, array(), $e); } + + $this->remove_empty_dirs($path_orig); } /** @@ -178,7 +199,7 @@ class local implements adapter_interface, stream_interface try { - $this->filesystem->copy($this->root_path . $path_orig, $this->root_path . $path_dest, false); + $this->filesystem->copy($this->root_path . $this->get_path($path_orig) . $this->get_filename($path_orig), $this->root_path . $this->get_path($path_dest) . $this->get_filename($path_dest), false); } catch (filesystem_exception $e) { @@ -212,7 +233,7 @@ class local implements adapter_interface, stream_interface */ protected function ensure_directory_exists($path) { - $path = dirname($this->root_path . $path); + $path = dirname($this->root_path . $this->get_path($path) . $this->get_filename($path)); $path = filesystem_helper::make_path_relative($path, $this->root_path); if (!$this->exists($path)) @@ -221,12 +242,68 @@ class local implements adapter_interface, stream_interface } } + /** + * Removes the directory tree ascending until it finds a non empty directory. + * + * @param string $path The file path + */ + protected function remove_empty_dirs($path) + { + $dirpath = dirname($this->root_path . $path); + $filepath = dirname($this->root_path . $this->get_path($path) . $this->get_filename($path)); + $path = filesystem_helper::make_path_relative($filepath, $dirpath); + + do + { + $parts = explode('/', $path); + $parts = array_slice($parts, 0, -1); + $path = implode('/', $parts); + } + while ($path && @rmdir($dirpath . '/' . $path)); + } + + /** + * Get the path to the file, appending subdirectories for directory depth + * if $dir_depth > 0. + * + * @param string $path The file path + */ + protected function get_path($path) + { + $dirname = dirname($path); + + $hash = md5(basename($path)); + + $parts = str_split($hash, 2); + $parts = array_slice($parts, 0, $this->dir_depth); + + // Create path + $path = $dirname . DIRECTORY_SEPARATOR; + + if (!empty($parts)) + { + $path .= implode(DIRECTORY_SEPARATOR, $parts) . DIRECTORY_SEPARATOR; + } + + return $path; + } + + /** + * To be used in other PR + * + * @param string $path The file path + */ + protected function get_filename($path) + { + return basename($path); + } + /** * {@inheritdoc} */ public function read_stream($path) { - $stream = @fopen($this->root_path . $path, 'rb'); + $stream = @fopen($this->root_path . $this->get_path($path) . $this->get_filename($path), 'rb'); if (!$stream) { @@ -241,12 +318,14 @@ class local implements adapter_interface, stream_interface */ public function write_stream($path, $resource) { + $this->ensure_directory_exists($path); + if ($this->exists($path)) { throw new exception('STORAGE_FILE_EXISTS', $path); } - $stream = @fopen($this->root_path . $path, 'w+b'); + $stream = @fopen($this->root_path . $this->get_path($path) . $this->get_filename($path), 'w+b'); if (!$stream) { @@ -271,7 +350,7 @@ class local implements adapter_interface, stream_interface */ public function file_size($path) { - $size = filesize($this->root_path . $path); + $size = filesize($this->root_path . $this->get_path($path) . $this->get_filename($path)); if ($size === null) { @@ -290,7 +369,7 @@ class local implements adapter_interface, stream_interface */ public function file_mimetype($path) { - return ['mimetype' => $this->mimetype_guesser->guess($this->root_path . $path)]; + return ['mimetype' => $this->mimetype_guesser->guess($this->root_path . $this->get_path($path) . $this->get_filename($path))]; } /** @@ -302,12 +381,12 @@ class local implements adapter_interface, stream_interface */ protected function image_dimensions($path) { - $size = $this->imagesize->getImageSize($this->root_path . $path); + $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 . $path); + $imsize = getimagesize($this->root_path . $this->get_path($path) . $this->get_filename($path)); $size = ['width' => $imsize[0], 'height' => $imsize[1]]; } diff --git a/phpBB/phpbb/storage/provider/local.php b/phpBB/phpbb/storage/provider/local.php index 7ec94d3d2e..09e5bf9ac4 100644 --- a/phpBB/phpbb/storage/provider/local.php +++ b/phpBB/phpbb/storage/provider/local.php @@ -36,7 +36,10 @@ class local implements provider_interface */ public function get_options() { - return ['path' => array('type' => 'text')]; + return [ + 'path' => array('type' => 'text'), + 'depth' => array('type' => 'text'), + ]; } /** diff --git a/tests/storage/adapter/local_test.php b/tests/storage/adapter/local_test.php index b478ce4009..a4092e303b 100644 --- a/tests/storage/adapter/local_test.php +++ b/tests/storage/adapter/local_test.php @@ -17,28 +17,22 @@ protected $path; + protected $filesystem; + public function setUp() { parent::setUp(); - $filesystem = new \phpbb\filesystem\filesystem(); + $this->filesystem = new \phpbb\filesystem\filesystem(); $phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR; - $this->adapter = new \phpbb\storage\adapter\local($filesystem, new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path); - $this->adapter->configure(['path' => 'test_path']); + $this->adapter = new \phpbb\storage\adapter\local($this->filesystem, new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path); + $this->adapter->configure(['path' => 'test_path', 'depth' => 2]); $this->path = $phpbb_root_path . 'test_path/'; mkdir($this->path); } - public function data_test_exists() - { - yield [$this->path . '../README.md', true]; - yield [$this->path . 'nonexistent_file.php', false]; - yield [$this->path . '../phpBB/phpbb', true]; - yield [$this->path . 'nonexistent/folder', false]; - } - public function tearDown() { $this->adapter = null; @@ -48,68 +42,82 @@ public function test_put_contents() { $this->adapter->put_contents('file.txt', 'abc'); - $this->assertTrue(file_exists($this->path . 'file.txt')); - $this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc'); - unlink($this->path . 'file.txt'); + $this->assertTrue(file_exists($this->path . '3d/8e/file.txt')); + $this->assertEquals(file_get_contents($this->path . '3d/8e/file.txt'), 'abc'); + unlink($this->path . '3d/8e/file.txt'); + rmdir($this->path . '3d/8e'); + rmdir($this->path . '3d'); } public function test_get_contents() { - file_put_contents($this->path . 'file.txt', 'abc'); + mkdir($this->path . '3d/8e', 0777, true); + file_put_contents($this->path . '3d/8e/file.txt', 'abc'); $this->assertEquals($this->adapter->get_contents('file.txt'), 'abc'); - unlink($this->path . 'file.txt'); + unlink($this->path . '3d/8e/file.txt'); + rmdir($this->path . '3d/8e'); + rmdir($this->path . '3d'); } - /** - * @dataProvider data_test_exists - */ - public function test_exists($path, $expected) + public function test_exists() { - $this->assertSame($expected, $this->adapter->exists($path)); + mkdir($this->path . '3d/8e', 0777, true); + touch($this->path . '3d/8e/file.txt'); + $this->assertTrue($this->adapter->exists('file.txt')); + $this->assertFalse($this->adapter->exists('3d/8e/file.txt')); + unlink($this->path . '3d/8e/file.txt'); + rmdir($this->path . '3d/8e'); + rmdir($this->path . '3d'); } public function test_delete_file() { - file_put_contents($this->path . 'file.txt', ''); - $this->assertTrue(file_exists($this->path . 'file.txt')); + mkdir($this->path . '3d/8e', 0777, true); + touch($this->path . '3d/8e/file.txt'); + $this->assertTrue(file_exists($this->path . '3d/8e/file.txt')); $this->adapter->delete('file.txt'); - $this->assertFalse(file_exists($this->path . 'file.txt')); - } - - public function test_delete_folder() - { - mkdir($this->path . 'path/to/dir', 0777, true); - $this->assertTrue(file_exists($this->path . 'path/to/dir')); - $this->adapter->delete('path'); - $this->assertFalse(file_exists($this->path . 'path/to/dir')); + $this->assertFalse(file_exists($this->path . '3d/8e/file.txt')); + $this->assertFalse(file_exists($this->path . '3d')); } public function test_rename() { - file_put_contents($this->path . 'file.txt', ''); + mkdir($this->path . '3d/8e', 0777, true); + touch($this->path . '3d/8e/file.txt'); $this->adapter->rename('file.txt', 'file2.txt'); - $this->assertFalse(file_exists($this->path . 'file.txt')); - $this->assertTrue(file_exists($this->path . 'file2.txt')); - unlink($this->path . 'file2.txt'); + $this->assertFalse(file_exists($this->path . '3d/8e/file.txt')); + $this->assertTrue(file_exists($this->path . '27/36/file2.txt')); + $this->assertFalse(file_exists($this->path . '3d')); + unlink($this->path . '27/36/file2.txt'); + rmdir($this->path . '27/36'); + rmdir($this->path . '27'); } public function test_copy() { - file_put_contents($this->path . 'file.txt', 'abc'); + mkdir($this->path . '3d/8e', 0777, true); + file_put_contents($this->path . '3d/8e/file.txt', 'abc'); $this->adapter->copy('file.txt', 'file2.txt'); - $this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc'); - $this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc'); - unlink($this->path . 'file.txt'); - unlink($this->path . 'file2.txt'); + $this->assertEquals(file_get_contents($this->path . '3d/8e/file.txt'), 'abc'); + $this->assertEquals(file_get_contents($this->path . '27/36/file2.txt'), 'abc'); + unlink($this->path . '3d/8e/file.txt'); + rmdir($this->path . '3d/8e'); + rmdir($this->path . '3d'); + unlink($this->path . '27/36/file2.txt'); + rmdir($this->path . '27/36'); + rmdir($this->path . '27'); } public function test_read_stream() { - file_put_contents($this->path . 'file.txt', ''); + mkdir($this->path . '3d/8e', 0777, true); + touch($this->path . '3d/8e/file.txt'); $stream = $this->adapter->read_stream('file.txt'); $this->assertTrue(is_resource($stream)); fclose($stream); - unlink($this->path . 'file.txt'); + unlink($this->path . '3d/8e/file.txt'); + rmdir($this->path . '3d/8e'); + rmdir($this->path . '3d'); } public function test_write_stream() @@ -118,8 +126,11 @@ $stream = fopen($this->path . 'file.txt', 'rb'); $this->adapter->write_stream('file2.txt', $stream); fclose($stream); - $this->assertEquals(file_get_contents($this->path . 'file2.txt'), 'abc'); + $this->assertEquals(file_get_contents($this->path . '27/36/file2.txt'), 'abc'); unlink($this->path . 'file.txt'); - unlink($this->path . 'file2.txt'); + unlink($this->path . '27/36/file2.txt'); + rmdir($this->path . '27/36'); + rmdir($this->path . '27'); } + }