mirror of
https://github.com/phpbb/phpbb.git
synced 2025-07-30 21:40:43 +02:00
[ticket/17361] Rewrite storage
PHPBB-17361
This commit is contained in:
@@ -25,32 +25,26 @@ interface adapter_interface
|
||||
public function configure(array $options): void;
|
||||
|
||||
/**
|
||||
* Dumps content into a file
|
||||
* Reads a file as a stream
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $content
|
||||
* @throws storage_exception When the file cannot be written
|
||||
* @param string $path File to read
|
||||
*
|
||||
* @return resource Returns a file pointer
|
||||
* @throws storage_exception When unable to open file
|
||||
*/
|
||||
public function put_contents(string $path, string $content): void;
|
||||
public function read(string $path);
|
||||
|
||||
/**
|
||||
* Read the contents of a file
|
||||
* Writes a new file using a stream
|
||||
*
|
||||
* @param string $path The file to read
|
||||
* @param string $path The target file
|
||||
* @param resource $resource The resource
|
||||
*
|
||||
* @return string Returns file contents
|
||||
* @throws storage_exception When cannot read file contents
|
||||
* @return int Returns the number of bytes written
|
||||
* @throws storage_exception When target file exists
|
||||
* When target file cannot be created
|
||||
*/
|
||||
public function get_contents(string $path): string;
|
||||
|
||||
/**
|
||||
* Checks the existence of files or directories
|
||||
*
|
||||
* @param string $path file/directory to check
|
||||
*
|
||||
* @return bool Returns true if the file/directory exist, false otherwise.
|
||||
*/
|
||||
public function exists(string $path): bool;
|
||||
public function write(string $path, $resource): int;
|
||||
|
||||
/**
|
||||
* Removes files or directories
|
||||
@@ -61,37 +55,6 @@ interface adapter_interface
|
||||
*/
|
||||
public function delete(string $path): void;
|
||||
|
||||
/**
|
||||
* Rename a file or a directory
|
||||
*
|
||||
* @param string $path_orig The original file/direcotry
|
||||
* @param string $path_dest The target file/directory
|
||||
*
|
||||
* @throws storage_exception When file/directory cannot be renamed
|
||||
*/
|
||||
public function rename(string $path_orig, string $path_dest): void;
|
||||
|
||||
/**
|
||||
* Copies a file
|
||||
*
|
||||
* @param string $path_orig The original filename
|
||||
* @param string $path_dest The target filename
|
||||
*
|
||||
* @throws storage_exception When the file cannot be copied
|
||||
*/
|
||||
public function copy(string $path_orig, string $path_dest): void;
|
||||
|
||||
/**
|
||||
* Get file size in bytes
|
||||
*
|
||||
* @param string $path The file
|
||||
*
|
||||
* @return int Size in bytes.
|
||||
*
|
||||
* @throws storage_exception When unable to retrieve file size
|
||||
*/
|
||||
public function file_size(string $path): int;
|
||||
|
||||
/**
|
||||
* Get space available in bytes
|
||||
*
|
||||
|
@@ -13,7 +13,6 @@
|
||||
|
||||
namespace phpbb\storage\adapter;
|
||||
|
||||
use phpbb\storage\stream_interface;
|
||||
use phpbb\storage\exception\storage_exception;
|
||||
use phpbb\filesystem\exception\filesystem_exception;
|
||||
use phpbb\filesystem\filesystem;
|
||||
@@ -22,7 +21,7 @@ use phpbb\filesystem\helper as filesystem_helper;
|
||||
/**
|
||||
* Experimental
|
||||
*/
|
||||
class local implements adapter_interface, stream_interface
|
||||
class local implements adapter_interface
|
||||
{
|
||||
/**
|
||||
* Filesystem component
|
||||
@@ -47,16 +46,6 @@ class local implements adapter_interface, stream_interface
|
||||
*/
|
||||
protected $root_path;
|
||||
|
||||
/**
|
||||
* Relative path from $phpbb_root_path to the storage folder
|
||||
* Always finish with slash (/) character
|
||||
* Example:
|
||||
* - images/avatars/upload/
|
||||
*
|
||||
* @var string path
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@@ -71,176 +60,20 @@ class local implements adapter_interface, stream_interface
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
*
|
||||
*/
|
||||
public function configure(array $options): void
|
||||
{
|
||||
$this->path = $options['path'];
|
||||
|
||||
if (substr($this->path, -1, 1) !== '/')
|
||||
{
|
||||
$this->path = $this->path . '/';
|
||||
}
|
||||
|
||||
$this->root_path = filesystem_helper::realpath($this->phpbb_root_path . $options['path']) . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function put_contents(string $path, string $content): void
|
||||
public function read(string $path)
|
||||
{
|
||||
$this->ensure_directory_exists($path);
|
||||
|
||||
try
|
||||
{
|
||||
$this->filesystem->dump_file($this->root_path . $this->get_path($path) . $this->get_filename($path), $content);
|
||||
}
|
||||
catch (filesystem_exception $e)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_WRITE_FILE', $path, array(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get_contents(string $path): string
|
||||
{
|
||||
$content = @file_get_contents($this->root_path . $this->get_path($path) . $this->get_filename($path));
|
||||
|
||||
if ($content === false)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_READ_FILE', $path);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function exists(string $path): bool
|
||||
{
|
||||
return $this->filesystem->exists($this->root_path . $this->get_path($path) . $this->get_filename($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete(string $path): void
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->filesystem->remove($this->root_path . $this->get_path($path) . $this->get_filename($path));
|
||||
}
|
||||
catch (filesystem_exception $e)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_DELETE', $path, array(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rename(string $path_orig, string $path_dest): void
|
||||
{
|
||||
$this->ensure_directory_exists($path_dest);
|
||||
|
||||
try
|
||||
{
|
||||
$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 storage_exception('STORAGE_CANNOT_RENAME', $path_orig, array(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function copy(string $path_orig, string $path_dest): void
|
||||
{
|
||||
$this->ensure_directory_exists($path_dest);
|
||||
|
||||
try
|
||||
{
|
||||
$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)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_COPY', $path_orig, array(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a directory recursively.
|
||||
*
|
||||
* @param string $path The directory path
|
||||
*
|
||||
* @throws storage_exception On any directory creation failure
|
||||
*/
|
||||
protected function create_dir(string $path): void
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->filesystem->mkdir($this->root_path . $path);
|
||||
}
|
||||
catch (filesystem_exception $e)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_CREATE_DIR', $path, array(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the directory of a file exists.
|
||||
*
|
||||
* @param string $path The file path
|
||||
*
|
||||
* @throws storage_exception On any directory creation failure
|
||||
*/
|
||||
protected function ensure_directory_exists(string $path): void
|
||||
{
|
||||
$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))
|
||||
{
|
||||
$this->create_dir($path);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the file
|
||||
*
|
||||
* @param string $path The file path
|
||||
* @return string
|
||||
*/
|
||||
protected function get_path(string $path): string
|
||||
{
|
||||
$dirname = dirname($path);
|
||||
$dirname = ($dirname != '.') ? $dirname . DIRECTORY_SEPARATOR : '';
|
||||
|
||||
return $dirname;
|
||||
}
|
||||
|
||||
/**
|
||||
* To be used in other PR
|
||||
*
|
||||
* @param string $path The file path
|
||||
* @return string
|
||||
*/
|
||||
protected function get_filename(string $path): string
|
||||
{
|
||||
return basename($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read_stream(string $path)
|
||||
{
|
||||
$stream = @fopen($this->root_path . $this->get_path($path) . $this->get_filename($path), 'rb');
|
||||
$stream = @fopen($this->root_path . $path, 'rb');
|
||||
|
||||
if (!$stream)
|
||||
{
|
||||
@@ -253,60 +86,52 @@ class local implements adapter_interface, stream_interface
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function write_stream(string $path, $resource): void
|
||||
public function write(string $path, $resource): int
|
||||
{
|
||||
$this->ensure_directory_exists($path);
|
||||
|
||||
$stream = @fopen($this->root_path . $this->get_path($path) . $this->get_filename($path), 'w+b');
|
||||
$stream = @fopen($this->root_path . $path, 'w+b');
|
||||
|
||||
if (!$stream)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_CREATE_FILE', $path);
|
||||
}
|
||||
|
||||
if (stream_copy_to_stream($resource, $stream) === false)
|
||||
if (($size = stream_copy_to_stream($resource, $stream)) === false)
|
||||
{
|
||||
fclose($stream);
|
||||
throw new storage_exception('STORAGE_CANNOT_COPY_RESOURCE');
|
||||
}
|
||||
|
||||
fclose($stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function file_size(string $path): int
|
||||
{
|
||||
$size = @filesize($this->root_path . $this->get_path($path) . $this->get_filename($path));
|
||||
|
||||
if ($size === null)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_GET_FILESIZE');
|
||||
}
|
||||
|
||||
return $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete(string $path): void
|
||||
{
|
||||
try
|
||||
{
|
||||
$this->filesystem->remove($this->root_path . $path);
|
||||
}
|
||||
catch (filesystem_exception $e)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_DELETE', $path, array(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function free_space(): float
|
||||
{
|
||||
if (function_exists('disk_free_space'))
|
||||
{
|
||||
$free_space = @disk_free_space($this->root_path);
|
||||
|
||||
if ($free_space === false)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_GET_FREE_SPACE');
|
||||
}
|
||||
}
|
||||
else
|
||||
if (!function_exists('disk_free_space') || ($free_space = @disk_free_space($this->root_path)) === false)
|
||||
{
|
||||
throw new storage_exception('STORAGE_CANNOT_GET_FREE_SPACE');
|
||||
}
|
||||
|
||||
return $free_space;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user