[ticket/15371] Split uploaded files into subdirectories

PHPBB3-15371
This commit is contained in:
Rubén Calvo 2017-09-21 20:56:56 +02:00
parent 449b930c5e
commit 5edb8f0b5b
5 changed files with 106 additions and 15 deletions

View file

@ -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); trigger_error($this->lang->lang('STORAGE_NO_CHANGES') . adm_back_link($this->u_action), E_USER_WARNING);
} }

View file

@ -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 ('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\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\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\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\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\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\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 ('cache_last_gc', '0', 1);
INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('cron_lock', '0', 1); INSERT INTO phpbb_config (config_name, config_value, is_dynamic) VALUES ('cron_lock', '0', 1);

View file

@ -0,0 +1,44 @@
<?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\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')),
)),
);
}
}

View file

@ -62,6 +62,11 @@ class local implements adapter_interface, stream_interface
*/ */
protected $path; protected $path;
/*
* @var int dir_depth
*/
protected $dir_depth;
/** /**
* Constructor * Constructor
*/ */
@ -85,6 +90,7 @@ class local implements adapter_interface, stream_interface
$this->path = $options['path']; $this->path = $options['path'];
$this->root_path = $this->phpbb_root_path . $options['path']; $this->root_path = $this->phpbb_root_path . $options['path'];
$this->dir_depth = (int) $options['depth'];
} }
/** /**
@ -101,7 +107,7 @@ class local implements adapter_interface, stream_interface
try try
{ {
$this->filesystem->dump_file($this->root_path . $path, $content); $this->filesystem->dump_file($this->root_path . $this->get_subfolder($path) . $path, $content);
} }
catch (filesystem_exception $e) catch (filesystem_exception $e)
{ {
@ -119,7 +125,7 @@ class local implements adapter_interface, stream_interface
throw new exception('STORAGE_FILE_NO_EXIST', $path); 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_subfolder($path) . $path);
if ($content === false) if ($content === false)
{ {
@ -134,7 +140,7 @@ class local implements adapter_interface, stream_interface
*/ */
public function exists($path) public function exists($path)
{ {
return $this->filesystem->exists($this->root_path . $path); return $this->filesystem->exists($this->root_path . $this->get_subfolder($path) . $path);
} }
/** /**
@ -144,12 +150,14 @@ class local implements adapter_interface, stream_interface
{ {
try try
{ {
$this->filesystem->remove($this->root_path . $path); $this->filesystem->remove($this->root_path . $this->get_subfolder($path) . $path);
} }
catch (filesystem_exception $e) catch (filesystem_exception $e)
{ {
throw new exception('STORAGE_CANNOT_DELETE', $path, array(), $e); throw new exception('STORAGE_CANNOT_DELETE', $path, array(), $e);
} }
$this->remove_empty_dirs($path);
} }
/** /**
@ -161,12 +169,14 @@ class local implements adapter_interface, stream_interface
try try
{ {
$this->filesystem->rename($this->root_path . $path_orig, $this->root_path . $path_dest, false); $this->filesystem->rename($this->root_path . $this->get_subfolder($path) . $path_orig, $this->root_path . $this->get_subfolder($path) . $path_dest, false);
} }
catch (filesystem_exception $e) catch (filesystem_exception $e)
{ {
throw new exception('STORAGE_CANNOT_RENAME', $path_orig, array(), $e); throw new exception('STORAGE_CANNOT_RENAME', $path_orig, array(), $e);
} }
$this->remove_empty_dirs($path_orig);
} }
/** /**
@ -178,7 +188,7 @@ class local implements adapter_interface, stream_interface
try try
{ {
$this->filesystem->copy($this->root_path . $path_orig, $this->root_path . $path_dest, false); $this->filesystem->copy($this->root_path . $this->get_subfolder($path) . $path_orig, $this->root_path . $this->get_subfolder($path) . $path_dest, false);
} }
catch (filesystem_exception $e) catch (filesystem_exception $e)
{ {
@ -212,7 +222,7 @@ class local implements adapter_interface, stream_interface
*/ */
protected function ensure_directory_exists($path) protected function ensure_directory_exists($path)
{ {
$path = dirname($this->root_path . $path); $path = dirname($this->root_path . $this->get_subfolder($path) . $path);
$path = filesystem_helper::make_path_relative($path, $this->root_path); $path = filesystem_helper::make_path_relative($path, $this->root_path);
if (!$this->exists($path)) if (!$this->exists($path))
@ -221,12 +231,41 @@ 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)
{
$path = dirname($this->root_path . $this->get_subfolder($path) . $path);
$path = filesystem_helper::make_path_relative($path, $this->root_path);
do
{
$parts = explode(DIRECTORY_SEPARATOR, $path);
$parts = array_slice($parts, 0, -1);
$path = implode(DIRECTORY_SEPARATOR, $parts);
}
while (@rmdir($this->root_path . $path));
}
protected function get_subfolder($path)
{
$hash = md5($path);
$parts = str_split($hash, 2);
$parts = array_slice($parts, 0, $this->dir_depth);
return implode(DIRECTORY_SEPARATOR, $parts) . DIRECTORY_SEPARATOR;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function read_stream($path) public function read_stream($path)
{ {
$stream = @fopen($this->root_path . $path, 'rb'); $stream = @fopen($this->root_path . $this->get_subfolder($path) . $path, 'rb');
if (!$stream) if (!$stream)
{ {
@ -241,12 +280,14 @@ class local implements adapter_interface, stream_interface
*/ */
public function write_stream($path, $resource) public function write_stream($path, $resource)
{ {
$this->ensure_directory_exists($path);
if ($this->exists($path)) if ($this->exists($path))
{ {
throw new exception('STORAGE_FILE_EXISTS', $path); throw new exception('STORAGE_FILE_EXISTS', $path);
} }
$stream = @fopen($this->root_path . $path, 'w+b'); $stream = @fopen($this->root_path . $this->get_subfolder($path) . $path, 'w+b');
if (!$stream) if (!$stream)
{ {
@ -271,7 +312,7 @@ class local implements adapter_interface, stream_interface
*/ */
public function file_size($path) public function file_size($path)
{ {
$size = filesize($this->root_path . $path); $size = filesize($this->root_path . $this->get_subfolder($path) . $path);
if ($size === null) if ($size === null)
{ {
@ -290,7 +331,7 @@ class local implements adapter_interface, stream_interface
*/ */
public function file_mimetype($path) 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_subfolder($path) . $path)];
} }
/** /**
@ -302,12 +343,12 @@ class local implements adapter_interface, stream_interface
*/ */
protected function image_dimensions($path) protected function image_dimensions($path)
{ {
$size = $this->imagesize->getImageSize($this->root_path . $path); $size = $this->imagesize->getImageSize($this->root_path . $this->get_subfolder($path) . $path);
// For not supported types like swf // For not supported types like swf
if ($size === false) if ($size === false)
{ {
$imsize = getimagesize($this->root_path . $path); $imsize = getimagesize($this->root_path . $this->get_subfolder($path) . $path);
$size = ['width' => $imsize[0], 'height' => $imsize[1]]; $size = ['width' => $imsize[0], 'height' => $imsize[1]];
} }

View file

@ -36,7 +36,10 @@ class local implements provider_interface
*/ */
public function get_options() public function get_options()
{ {
return ['path' => array('type' => 'text')]; return [
'path' => array('type' => 'text'),
'depth' => array('type' => 'text'),
];
} }
/** /**