[ticket/15342] Track stream write and move tracking to separate methods

PHPBB3-15342
This commit is contained in:
Rubén Calvo 2018-02-15 09:33:12 +01:00
parent 5a1b4f559d
commit e9dd717b8b

View file

@ -99,15 +99,7 @@ class storage
try try
{ {
$this->get_adapter()->put_contents($path, $content); $this->get_adapter()->put_contents($path, $content);
$this->track_file($path);
$sql_ary = array(
'file_path' => $path,
'storage' => $this->get_name(),
'filesize' => strlen($content),
);
$sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql);
} }
catch (\Exception $e) catch (\Exception $e)
{ {
@ -155,15 +147,7 @@ class storage
try try
{ {
$this->get_adapter()->delete($path); $this->get_adapter()->delete($path);
$this->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);
} }
catch (\Exception $e) catch (\Exception $e)
{ {
@ -185,20 +169,8 @@ class storage
try try
{ {
$this->get_adapter()->rename($path_orig, $path_dest); $this->get_adapter()->rename($path_orig, $path_dest);
$this->untrack_file($path_orig);
$sql_ary1 = array( $this->track_file($path_dest);
'file_path' => $path_dest,
);
$sql_ary2 = array(
'file_path' => $path_orig,
'storage' => $this->get_name(),
);
$sql = 'UPDATE ' . $this->storage_table . '
SET ' . $this->db->sql_build_array('UPDATE', $sql_ary1) . '
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary2);
$this->db->sql_query($sql);
} }
catch (\Exception $e) catch (\Exception $e)
{ {
@ -220,26 +192,7 @@ class storage
try try
{ {
$this->get_adapter()->copy($path_orig, $path_dest); $this->get_adapter()->copy($path_orig, $path_dest);
$this->track_file($path_dest);
$sql_ary = array(
'file_path' => $path_orig,
'storage' => $this->get_name(),
);
$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);
$sql_ary = array(
'file_path' => $path_dest,
'storage' => $this->get_name(),
'filesize' => (int) $row['filesize'],
);
$sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql);
} }
catch (\Exception $e) catch (\Exception $e)
{ {
@ -291,7 +244,17 @@ class storage
if ($adapter instanceof stream_interface) if ($adapter instanceof stream_interface)
{ {
$adapter->write_stream($path, $resource); $adapter->write_stream($path, $resource);
$this->track_file($path); // Not sure if here, or after close the file
}
else
{
// Simulate the stream
$adapter->put_contents($path, stream_get_contents($resource));
}
}
protected function track_file($path, $update = false)
{
$sql_ary = array( $sql_ary = array(
'file_path' => $path, 'file_path' => $path,
'storage' => $this->get_name(), 'storage' => $this->get_name(),
@ -304,30 +267,36 @@ class storage
$row = $this->db->sql_fetchrow($result); $row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);
if ($row) if (!$row)
{ {
//$sql = 'UPDATE ' . $this->storage_table . ' $file = $this->file_info($path);
// SET filesize = filesize + ' . strlen($content) . ' $sql_ary['filesize'] = $file->size;
// WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
//$this->db->sql_query($sql);
}
else
{
//$sql_ary['filesize'] = strlen($content);
$sql_ary['filesize'] = 0;
$sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary); $sql = 'INSERT INTO ' . $this->storage_table . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql); $this->db->sql_query($sql);
} }
else if ($update)
}
else
{ {
// Simulate the stream $file = $this->file_info($path);
$adapter->put_contents($path, stream_get_contents($resource)); $sql = 'UPDATE ' . $this->storage_table . '
SET filesize = ' . $file->size . '
WHERE ' . $this->db->sql_build_array('SELECT', $sql_ary);
$this->db->sql_query($sql);
} }
} }
protected 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);
}
/** /**
* Get file info. * Get file info.
* *