1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-26 01:43:45 +02:00

[ticket/17361] Rewrite storage

PHPBB-17361
This commit is contained in:
Ruben Calvo
2024-11-24 00:15:10 +01:00
parent 688dbb0837
commit e9c445925b
24 changed files with 407 additions and 762 deletions

View File

@@ -13,8 +13,6 @@
namespace phpbb\storage;
use phpbb\cache\driver\driver_interface as cache;
use phpbb\db\driver\driver_interface as db;
use phpbb\storage\adapter\adapter_interface;
use phpbb\storage\exception\storage_exception;
@@ -28,48 +26,33 @@ class storage
*/
protected $adapter;
/**
* @var db
*/
protected $db;
/**
* Cache driver
* @var cache
*/
protected $cache;
/**
* @var adapter_factory
*/
protected $factory;
/**
* @var file_tracker
*/
protected $file_tracker;
/**
* @var string
*/
protected $storage_name;
/**
* @var string
*/
protected $storage_table;
/**
* Constructor
*
* @param db $db
* @param cache $cache
* @param adapter_factory $factory
* @param file_tracker $file_tracker
* @param string $storage_name
* @param string $storage_table
*/
public function __construct(db $db, cache $cache, adapter_factory $factory, string $storage_name, string $storage_table)
public function __construct(adapter_factory $factory, file_tracker $file_tracker, string $storage_name)
{
$this->db = $db;
$this->cache = $cache;
$this->factory = $factory;
$this->file_tracker = $file_tracker;
$this->storage_name = $storage_name;
$this->storage_table = $storage_table;
}
/**
@@ -98,57 +81,48 @@ class storage
}
/**
* Dumps content into a file
* Reads a file as a stream
*
* @param string $path The file to be written to.
* @param string $content The data to write into the file.
*
* @throws storage_exception When the file already exists
* When the file cannot be written
*/
public function put_contents(string $path, string $content): void
{
if ($this->exists($path))
{
throw new storage_exception('STORAGE_FILE_EXISTS', $path);
}
$this->get_adapter()->put_contents($path, $content);
$this->track_file($path);
}
/**
* Read the contents of a file
*
* @param string $path The file to read
*
* @return string Returns file contents
* @param string $path File to read
*
* @return resource Returns a file pointer
* @throws storage_exception When the file doesn't exist
* When cannot read file contents
* When unable to open file
*
*/
public function get_contents(string $path): string
public function read(string $path)
{
if (!$this->exists($path))
{
throw new storage_exception('STORAGE_FILE_NO_EXIST', $path);
}
return $this->get_adapter()->get_contents($path);
return $this->get_adapter()->read($path);
}
/**
* Checks the existence of files or directories
* Writes a new file using a stream
*
* @param string $path file/directory to check
* @param bool $full_check check in the filesystem too
* @param string $path The target file
* @param resource $resource The resource
*
* @return bool Returns true if the file/directory exist, false otherwise
* @throws storage_exception When the file exist
* When target file cannot be created
*/
public function exists(string $path, bool $full_check = false): bool
public function write(string $path, $resource): void
{
return ($this->is_tracked($path) && (!$full_check || $this->get_adapter()->exists($path)));
if ($this->exists($path))
{
throw new storage_exception('STORAGE_FILE_EXISTS', $path);
}
if (!is_resource($resource))
{
throw new storage_exception('STORAGE_INVALID_RESOURCE');
}
$size = $this->get_adapter()->write($path, $resource);
$this->file_tracker->track_file($this->storage_name, $path, $size);
}
/**
@@ -167,231 +141,19 @@ class storage
}
$this->get_adapter()->delete($path);
$this->untrack_file($path);
$this->file_tracker->untrack_file($this->get_name(), $path);
}
/**
* Rename a file or a directory
* Checks the existence of files or directories
*
* @param string $path_orig The original file/direcotry
* @param string $path_dest The target file/directory
* @param string $path file/directory to check
*
* @throws storage_exception When the file doesn't exist
* When target exists
* When file/directory cannot be renamed
* @return bool Returns true if the file/directory exist, false otherwise
*/
public function rename(string $path_orig, string $path_dest): void
public function exists(string $path): bool
{
if (!$this->exists($path_orig))
{
throw new storage_exception('STORAGE_FILE_NO_EXIST', $path_orig);
}
if ($this->exists($path_dest))
{
throw new storage_exception('STORAGE_FILE_EXISTS', $path_dest);
}
$this->get_adapter()->rename($path_orig, $path_dest);
$this->track_rename($path_orig, $path_dest);
}
/**
* Copies a file
*
* @param string $path_orig The original filename
* @param string $path_dest The target filename
*
* @throws storage_exception When the file doesn't exist
* When target exists
* When the file cannot be copied
*/
public function copy(string $path_orig, string $path_dest): void
{
if (!$this->exists($path_orig))
{
throw new storage_exception('STORAGE_FILE_NO_EXIST', $path_orig);
}
if ($this->exists($path_dest))
{
throw new storage_exception('STORAGE_FILE_EXISTS', $path_dest);
}
$this->get_adapter()->copy($path_orig, $path_dest);
$this->track_file($path_dest);
}
/**
* Reads a file as a stream
*
* @param string $path File to read
*
* @return resource Returns a file pointer
* @throws storage_exception When the file doesn't exist
* When unable to open file
*
*/
public function read_stream(string $path)
{
if (!$this->exists($path))
{
throw new storage_exception('STORAGE_FILE_NO_EXIST', $path);
}
$stream = null;
$adapter = $this->get_adapter();
if ($adapter instanceof stream_interface)
{
$stream = $adapter->read_stream($path);
}
else
{
// Simulate the stream
$stream = fopen('php://temp', 'w+b');
fwrite($stream, $adapter->get_contents($path));
rewind($stream);
}
return $stream;
}
/**
* Writes a new file using a stream
*
* @param string $path The target file
* @param resource $resource The resource
*
* @throws storage_exception When the file exist
* When target file cannot be created
*/
public function write_stream(string $path, $resource): void
{
if ($this->exists($path))
{
throw new storage_exception('STORAGE_FILE_EXISTS', $path);
}
if (!is_resource($resource))
{
throw new storage_exception('STORAGE_INVALID_RESOURCE');
}
$adapter = $this->get_adapter();
if ($adapter instanceof stream_interface)
{
$adapter->write_stream($path, $resource);
$this->track_file($path);
}
else
{
// Simulate the stream
$adapter->put_contents($path, stream_get_contents($resource));
}
}
/**
* Track file in database
*
* @param string $path The target file
* @param bool $update Update file size when already tracked
*/
public function track_file(string $path, bool $update = false): void
{
if (!$this->get_adapter()->exists($path))
{
throw new storage_exception('STORAGE_FILE_NO_EXIST', $path);
}
$sql_ary = array(
'file_path' => $path,
'storage' => $this->get_name(),
);
// Get file, if exist update filesize, if not add new record
$sql = 'SELECT * 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);
if (!$row)
{
$sql_ary['filesize'] = $this->get_adapter()->file_size($path);
$sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql);
}
else if ($update)
{
$sql = 'UPDATE ' . $this->storage_table . '
SET filesize = ' . $this->get_adapter()->file_size($path) . '
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
$this->db->sql_query($sql);
}
$this->cache->destroy('_storage_' . $this->get_name() . '_totalsize');
$this->cache->destroy('_storage_' . $this->get_name() . '_numfiles');
}
/**
* Untrack file
*
* @param string $path The target file
*/
public function untrack_file($path)
{
$sql_ary = array(
'file_path' => $path,
'storage' => $this->get_name(),
);
$sql = 'DELETE FROM ' . $this->storage_table . '
WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary);
$this->db->sql_query($sql);
$this->cache->destroy('_storage_' . $this->get_name() . '_totalsize');
$this->cache->destroy('_storage_' . $this->get_name() . '_numfiles');
}
/**
* Check if a file is tracked
*
* @param string $path The file
*
* @return bool True if file is tracked
*/
public function is_tracked(string $path): bool
{
$sql_ary = array(
'file_path' => $path,
'storage' => $this->get_name(),
);
$sql = 'SELECT file_id 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;
}
/**
* Rename tracked file
*
* @param string $path_orig The original file/direcotry
* @param string $path_dest The target file/directory
*/
protected function track_rename(string $path_orig, string $path_dest): void
{
$sql = 'UPDATE ' . $this->storage_table . "
SET file_path = '" . $this->db->sql_escape($path_dest) . "'
WHERE file_path = '" . $this->db->sql_escape($path_orig) . "'
AND storage = '" . $this->db->sql_escape($this->get_name()) . "'";
$this->db->sql_query($sql);
return $this->file_tracker->is_tracked($this->get_name(), $path);
}
/**
@@ -400,73 +162,30 @@ class storage
* @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
{
$sql_ary = array(
'file_path' => $path,
'storage' => $this->get_name(),
);
return $this->file_tracker->file_size($this->get_name(), $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);
/**
* Return the number of files stored in this storage
*
* @return int Number of files.
*/
public function total_files(): int
{
return $this->file_tracker->total_files($this->get_name());
}
/**
* Get total storage size
*
* @return int Size in bytes
* @return float Size in bytes
*/
public function get_size(): int
public function total_size(): float
{
$total_size = $this->cache->get('_storage_' . $this->get_name() . '_totalsize');
if ($total_size === false)
{
$sql = 'SELECT SUM(filesize) AS totalsize
FROM ' . $this->storage_table . "
WHERE storage = '" . $this->db->sql_escape($this->get_name()) . "'";
$result = $this->db->sql_query($sql);
$total_size = (int) $this->db->sql_fetchfield('totalsize');
$this->cache->put('_storage_' . $this->get_name() . '_totalsize', $total_size);
$this->db->sql_freeresult($result);
}
return (int) $total_size;
}
/**
* Get number of storage files
*
* @return int Number of files
*/
public function get_num_files(): int
{
$number_files = $this->cache->get('_storage_' . $this->get_name() . '_numfiles');
if ($number_files === false)
{
$sql = 'SELECT COUNT(file_id) AS numfiles
FROM ' . $this->storage_table . "
WHERE storage = '" . $this->db->sql_escape($this->get_name()) . "'";
$result = $this->db->sql_query($sql);
$number_files = (int) $this->db->sql_fetchfield('numfiles');
$this->cache->put('_storage_' . $this->get_name() . '_numfiles', $number_files);
$this->db->sql_freeresult($result);
}
return (int) $number_files;
return $this->file_tracker->total_size($this->get_name());
}
/**