1
0
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:
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

@@ -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
*

View File

@@ -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;
}
}

View File

@@ -67,20 +67,20 @@ class adapter_factory
$options[$definition] = $this->config['storage\\' . $storage_name . '\\config\\' . $definition];
}
return $this->get_with_options($storage_name, $options);
return $this->get_with_options($storage_name, $provider_class, $options);
}
/**
* Obtains a configured adapters for a given storage with custom options
* Obtains a configured adapters with custom options
*
* @param string $storage_name
* @param string $provider_class
* @param array $options
*
* @return mixed
*/
public function get_with_options(string $storage_name, array $options): mixed
public function get_with_options(string $storage_name, string $provider_class, array $options): mixed
{
$provider_class = $this->config['storage\\' . $storage_name . '\\provider'];
$provider = $this->providers->get_by_class($provider_class);
if (!$provider->is_available())
@@ -89,6 +89,7 @@ class adapter_factory
}
$adapter = $this->adapters->get_by_class($provider->get_adapter_class());
$options['storage'] = $storage_name;
$adapter->configure($options);
return $adapter;

View File

@@ -293,11 +293,11 @@ class attachment extends controller
/**
* Remove non valid characters https://github.com/symfony/http-foundation/commit/c7df9082ee7205548a97031683bc6550b5dc9551
*/
protected function filenameFallback($filename)
protected function filenameFallback($filename): string
{
$filename = preg_replace(['/[^\x20-\x7e]/', '/%/', '/\//', '/\\\\/'], '', $filename);
$filename = (string) preg_replace(['/[^\x20-\x7e]/', '/%/', '/\//', '/\\\\/'], '', $filename);
return (!empty($filename)) ?: 'File';
return !empty($filename) ? $filename : 'File';
}
/**
@@ -305,7 +305,7 @@ class attachment extends controller
*/
protected function prepare(StreamedResponse $response, string $file): void
{
$response->setPrivate(); // By default should be private, but make sure of it
$response->setPrivate(); // By default, should be private, but make sure of it
parent::prepare($response, $file);
}
@@ -445,7 +445,7 @@ class attachment extends controller
if (!$url)
{
return ($this->config['secure_allow_empty_referer']) ? true : false;
return (bool) $this->config['secure_allow_empty_referer'];
}
// Split URL into domain and script part
@@ -453,13 +453,13 @@ class attachment extends controller
if ($url === false)
{
return ($this->config['secure_allow_empty_referer']) ? true : false;
return (bool) $this->config['secure_allow_empty_referer'];
}
$hostname = $url['host'];
unset($url);
$allowed = ($this->config['secure_allow_deny']) ? false : true;
$allowed = !$this->config['secure_allow_deny'];
$iplist = array();
if (($ip_ary = @gethostbynamel($hostname)) !== false)

View File

@@ -90,7 +90,7 @@ class avatar extends controller
}
$ext = substr(strrchr($file, '.'), 1);
$file = (int) $file;
$file = (int) $file; // This removes the timestamp leaving only the user id
return $this->config['avatar_salt'] . '_' . ($avatar_group ? 'g' : '') . $file . '.' . $ext;
}

View File

@@ -159,7 +159,7 @@ class controller
@set_time_limit(0);
$fp = $this->storage->read_stream($file);
$fp = $this->storage->read($file);
// Close db connection
$this->file_gc();
@@ -173,7 +173,7 @@ class controller
flush();
// Terminate script to avoid the execution of terminate events
// This avoid possible errors with db connection closed
// This avoids possible errors with db connection closed
exit;
});

View File

@@ -36,7 +36,7 @@ class storage_exception extends runtime_exception
*
* @return string
*/
public function get_filename()
public function get_filename(): string
{
$parameters = $this->get_parameters();
return $parameters['filename'];

View File

@@ -0,0 +1,198 @@
<?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\cache\driver\driver_interface as cache;
use phpbb\db\driver\driver_interface as db;
use phpbb\storage\exception\storage_exception;
class file_tracker
{
/**
* @var db
*/
protected $db;
/**
* Cache driver
* @var cache
*/
protected $cache;
/**
* @var string
*/
protected $storage_table;
/**
* Constructor
*
* @param db $db
* @param cache $cache
* @param string $storage_table
*/
public function __construct(db $db, cache $cache, string $storage_table)
{
$this->db = $db;
$this->cache = $cache;
$this->storage_table = $storage_table;
}
/**
* Track file in database
*
* @param string $storage Storage name
* @param string $path The target file
* @param int $size Size in bytes
*/
public function track_file(string $storage, string $path, int $size): void
{
$sql_ary = array(
'file_path' => $path,
'storage' => $storage,
'filesize' => $size,
);
$sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql);
$this->cache->destroy('_storage_' . $storage . '_totalsize');
$this->cache->destroy('_storage_' . $storage . '_numfiles');
}
/**
* Untrack file
*
* @param string $storage Storage name
* @param string $path The target file
*/
public function untrack_file(string $storage, $path): void
{
$sql_ary = array(
'file_path' => $path,
'storage' => $storage,
);
$sql = 'DELETE FROM ' . $this->storage_table . '
WHERE ' . $this->db->sql_build_array('DELETE', $sql_ary);
$this->db->sql_query($sql);
$this->cache->destroy('_storage_' . $storage . '_totalsize');
$this->cache->destroy('_storage_' . $storage . '_numfiles');
}
/**
* Check if a file is tracked
*
* @param string $storage Storage name
* @param string $path The file
*
* @return bool True if file is tracked
*/
public function is_tracked(string $storage, string $path): bool
{
$sql_ary = array(
'file_path' => $path,
'storage' => $storage,
);
$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;
}
/**
* 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 $storage, string $path): int
{
$sql_ary = array(
'file_path' => $path,
'storage' => $storage,
);
$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 (int) $row['filesize'];
}
/**
* Get number of tracked storage files for a storage
*
* @param string $storage Storage name
*
* @return int Number of files
*/
public function total_files(string $storage): int
{
$number_files = $this->cache->get('_storage_' . $storage. '_numfiles');
if ($number_files === false)
{
$sql = 'SELECT COUNT(file_id) AS numfiles
FROM ' . $this->storage_table . "
WHERE storage = '" . $this->db->sql_escape($storage) . "'";
$result = $this->db->sql_query($sql);
$number_files = $this->db->sql_fetchfield('numfiles');
$this->cache->put('_storage_' . $storage . '_numfiles', $number_files);
$this->db->sql_freeresult($result);
}
return (int) $number_files;
}
/**
* Get total storage size
*
* @param string $storage Storage name
*
* @return float Size in bytes
*/
public function total_size(string $storage): float
{
$total_size = $this->cache->get('_storage_' . $storage . '_totalsize');
if ($total_size === false)
{
$sql = 'SELECT SUM(filesize) AS totalsize
FROM ' . $this->storage_table . "
WHERE storage = '" . $this->db->sql_escape($storage) . "'";
$result = $this->db->sql_query($sql);
$total_size = $this->db->sql_fetchfield('totalsize');
$this->cache->put('_storage_' . $storage . '_totalsize', $total_size);
$this->db->sql_freeresult($result);
}
return (float) $total_size;
}
}

View File

@@ -129,7 +129,7 @@ class helper
$options[$definition] = $this->state_helper->new_definition_value($storage_name, $definition);
}
$adapters[$storage_name] = $this->adapter_factory->get_with_options($storage_name, $options);
$adapters[$storage_name] = $this->adapter_factory->get_with_options($storage_name, $provider_class, $options);
}
return $adapters[$storage_name];
@@ -193,8 +193,8 @@ class helper
$current_adapter = $this->get_current_adapter($storage_name);
$new_adapter = $this->get_new_adapter($storage_name);
$stream = $current_adapter->read_stream($file);
$new_adapter->write_stream($file, $stream);
$stream = $current_adapter->read($file);
$new_adapter->write($file, $stream);
if (is_resource($stream))
{

View File

@@ -18,7 +18,7 @@ class local implements provider_interface
/**
* {@inheritdoc}
*/
public function get_name()
public function get_name(): string
{
return 'local';
}
@@ -34,7 +34,7 @@ class local implements provider_interface
/**
* {@inheritdoc}
*/
public function get_options()
public function get_options(): array
{
return [
'path' => [
@@ -47,7 +47,7 @@ class local implements provider_interface
/**
* {@inheritdoc}
*/
public function is_available()
public function is_available(): bool
{
return true;
}

View File

@@ -20,7 +20,7 @@ interface provider_interface
*
* @return string
*/
public function get_name();
public function get_name(): string;
/**
* Gets adapter class
@@ -34,12 +34,12 @@ interface provider_interface
*
* @return array Configuration keys
*/
public function get_options();
public function get_options(): array;
/**
* Return true if the adapter is available
*
* @return bool
*/
public function is_available();
public function is_available(): bool;
}

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());
}
/**

View File

@@ -1,41 +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;
interface stream_interface
{
/**
* Reads a file as a stream
*
* @param string $path File to read
*
* @return resource Returns a file pointer
* @throws storage_exception When unable to open file
*/
public function read_stream(string $path);
/**
* Writes a new file using a stream
*
* @param string $path The target file
* @param resource $resource The resource
*
* @return void
* @throws storage_exception When target file exists
* When target file cannot be created
*/
public function write_stream(string $path, $resource): void;
}