From abcd456461f7c37b0d0d323c36dca7ccf476babe Mon Sep 17 00:00:00 2001 From: Mike Macgirvin Date: Wed, 22 Jun 2022 02:36:53 -0700 Subject: [PATCH] add Code\Storage\Stdio - start with pipe_streams() refactor --- Code/Module/Attach.php | 16 +++++---------- Code/Module/Ca.php | 9 ++------ Code/Module/Getfile.php | 33 +++++++++--------------------- Code/Module/Photo.php | 16 +++++---------- Code/Module/Xp.php | 9 ++------ Code/Storage/Directory.php | 2 +- Code/Storage/File.php | 2 +- Code/Storage/Stdio.php | 42 ++++++++++++++++++++++++++++++++++++++ Code/Thumbs/Pdf.php | 10 +++------ Code/Thumbs/Video.php | 12 ++++------- include/attach.php | 27 +++--------------------- 11 files changed, 78 insertions(+), 100 deletions(-) create mode 100644 Code/Storage/Stdio.php diff --git a/Code/Module/Attach.php b/Code/Module/Attach.php index 92ead58cc..5119c7fe0 100644 --- a/Code/Module/Attach.php +++ b/Code/Module/Attach.php @@ -3,6 +3,7 @@ namespace Code\Module; use Code\Web\Controller; +use Code\Storage\Stdio; require_once('include/security.php'); require_once('include/attach.php'); @@ -39,17 +40,10 @@ class Attach extends Controller header('Content-Disposition: attachment; filename="' . $r['data']['filename'] . '"'); if (intval($r['data']['os_storage'])) { $fname = dbunescbin($r['data']['content']); - if (strpos($fname, 'store') !== false) { - $istream = fopen($fname, 'rb'); - } else { - $istream = fopen('store/' . $c[0]['channel_address'] . '/' . $fname, 'rb'); - } - $ostream = fopen('php://output', 'wb'); - if ($istream && $ostream) { - pipe_streams($istream, $ostream); - fclose($istream); - fclose($ostream); - } + Stdio::fpipe((strpos($fname, 'store') !== false) + ? $fname + : 'store/' . $channel['channel_address'] . '/' . $fname, + 'php://output'); } else { echo dbunescbin($r['data']['content']); } diff --git a/Code/Module/Ca.php b/Code/Module/Ca.php index 9df6c8cf5..6c9f2b378 100644 --- a/Code/Module/Ca.php +++ b/Code/Module/Ca.php @@ -7,6 +7,7 @@ namespace Code\Module; use Code\Web\Controller; +use Code\Storage\Stdio; /** * Ca @@ -47,13 +48,7 @@ class Ca extends Controller . '; max-age=' . $cache . ';' ); - $infile = fopen($path, 'rb'); - $outfile = fopen('php://output', 'wb'); - if ($infile && $outfile) { - pipe_streams($infile, $outfile); - } - fclose($infile); - fclose($outfile); + Stdio::fpipe($path,'php://output'); killme(); } diff --git a/Code/Module/Getfile.php b/Code/Module/Getfile.php index 6717d525b..f2d4c496a 100644 --- a/Code/Module/Getfile.php +++ b/Code/Module/Getfile.php @@ -5,7 +5,8 @@ namespace Code\Module; use Code\Web\Controller; use Code\Web\HTTPSig; use Code\Lib\Channel; - +use Code\Storage\Stdio; + /** * module: getfile * @@ -95,17 +96,10 @@ class Getfile extends Controller if (intval($r[0]['os_storage'])) { $fname = dbunescbin($r[0]['content']); - if (strpos($fname, 'store') !== false) { - $istream = fopen($fname, 'rb'); - } else { - $istream = fopen('store/' . $channel['channel_address'] . '/' . $fname, 'rb'); - } - $ostream = fopen('php://output', 'wb'); - if ($istream && $ostream) { - pipe_streams($istream, $ostream); - fclose($istream); - fclose($ostream); - } + Stdio::fpipe((strpos($fname, 'store') !== false) + ? $fname + : 'store/' . $channel['channel_address'] . '/' . $fname, + 'php://output'); } else { echo dbunescbin($r[0]['content']); } @@ -125,17 +119,10 @@ class Getfile extends Controller header('Content-Disposition: attachment; filename="' . $r['data']['filename'] . '"'); if (intval($r['data']['os_storage'])) { $fname = dbunescbin($r['data']['content']); - if (strpos($fname, 'store') !== false) { - $istream = fopen($fname, 'rb'); - } else { - $istream = fopen('store/' . $channel['channel_address'] . '/' . $fname, 'rb'); - } - $ostream = fopen('php://output', 'wb'); - if ($istream && $ostream) { - pipe_streams($istream, $ostream); - fclose($istream); - fclose($ostream); - } + Stdio::fpipe((strpos($fname, 'store') !== false) + ? $fname + : 'store/' . $channel['channel_address'] . '/' . $fname, + 'php://output'); } else { echo dbunescbin($r['data']['content']); } diff --git a/Code/Module/Photo.php b/Code/Module/Photo.php index aaac93a0b..ddcf82b7a 100644 --- a/Code/Module/Photo.php +++ b/Code/Module/Photo.php @@ -10,6 +10,7 @@ use Code\Lib\Channel; use Code\Lib\LDSignatures; use Code\Web\HTTPSig; use Code\Extend\Hook; +use Code\Storage\Stdio; require_once('include/security.php'); require_once('include/attach.php'); @@ -306,17 +307,10 @@ class Photo extends Controller // If it's a file resource, stream it. if ($streaming && $channel) { - if (strpos($streaming, 'store') !== false) { - $istream = fopen($streaming, 'rb'); - } else { - $istream = fopen('store/' . $channel['channel_address'] . '/' . $streaming, 'rb'); - } - $ostream = fopen('php://output', 'wb'); - if ($istream && $ostream) { - pipe_streams($istream, $ostream); - fclose($istream); - fclose($ostream); - } + Stdio::fpipe((strpos($streaming, 'store') !== false) + ? $streaming + : 'store/' . $channel['channel_address'] . '/' . $streaming, + 'php://output'); } else { echo $data; } diff --git a/Code/Module/Xp.php b/Code/Module/Xp.php index c20aeabe4..c7311fc72 100644 --- a/Code/Module/Xp.php +++ b/Code/Module/Xp.php @@ -3,6 +3,7 @@ namespace Code\Module; use Code\Web\Controller; use Code\Lib\Channel; +use Code\Storage\Stdio; class Xp extends Controller { @@ -51,13 +52,7 @@ class Xp extends Controller $smaxage = intval($cache / 12); header('Cache-Control: s-maxage=' . $smaxage . '; max-age=' . $cache . ';'); - $infile = fopen($path, 'rb'); - $outfile = fopen('php://output', 'wb'); - if ($infile && $outfile) { - pipe_streams($infile, $outfile); - } - fclose($infile); - fclose($outfile); + Stdio::fpipe($path,'php://output'); killme(); } diff --git a/Code/Storage/Directory.php b/Code/Storage/Directory.php index a73e29314..3978a0f65 100644 --- a/Code/Storage/Directory.php +++ b/Code/Storage/Directory.php @@ -290,7 +290,7 @@ class Directory extends DAV\Node implements DAV\ICollection, DAV\IQuota, DAV\IMo if (is_resource($data)) { $fp = fopen($f, 'wb'); if ($fp) { - pipe_streams($data, $fp); + Stdio::pipe_streams($data, $fp); fclose($fp); } $size = filesize($f); diff --git a/Code/Storage/File.php b/Code/Storage/File.php index 99e8d69f4..92bd22d60 100644 --- a/Code/Storage/File.php +++ b/Code/Storage/File.php @@ -203,7 +203,7 @@ class File extends DAV\Node implements DAV\IFile { if (is_resource($data)) { $fp = fopen($f,'wb'); if ($fp) { - pipe_streams($data,$fp); + Stdio::pipe_streams($data,$fp); fclose($fp); } } diff --git a/Code/Storage/Stdio.php b/Code/Storage/Stdio.php new file mode 100644 index 000000000..b58718e8e --- /dev/null +++ b/Code/Storage/Stdio.php @@ -0,0 +1,42 @@ +