Merge branch 'dev' of ../rebble.net into dev

This commit is contained in:
Mike Macgirvin 2022-06-22 02:42:08 -07:00
commit c1549be36a
11 changed files with 78 additions and 100 deletions

View file

@ -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']);
}

View file

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

View file

@ -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']);
}

View file

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

View file

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

View file

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

View file

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

42
Code/Storage/Stdio.php Normal file
View file

@ -0,0 +1,42 @@
<?php
namespace Code\Storage;
Class Stdio {
/**
* @brief Pipes $infile to $outfile in $bufsize chunks
*
* @return int bytes written | false
*/
static public function fpipe($infile, $outfile, $bufsize = 65535)
{
$size = false;
$in = fopen($infile, 'rb');
$out = fopen('php://output', 'wb');
if ($in && $out) {
$size = self::pipe_streams($in, $out, $bufsize);
}
fclose($in);
fclose($out);
return $size;
}
/**
* @brief Pipes $in to $out in $bufsize (or 64KB) chunks.
*
* @param resource $in File pointer of input
* @param resource $out File pointer of output
* @param int $bufsize size of chunk, default 65535
* @return number with the size
*/
static public function pipe_streams($in, $out, $bufsize = 65535)
{
$size = 0;
while (!feof($in)) {
$size += fwrite($out, fread($in, $bufsize));
}
return $size;
}
}

View file

@ -2,6 +2,8 @@
namespace Code\Thumbs;
use Code\Storage\Stdio;
class Pdf
{
@ -19,13 +21,7 @@ class Pdf
$tmpfile = $file . '.pdf';
$outfile = $file . '.jpg';
$istream = fopen($file, 'rb');
$ostream = fopen($tmpfile, 'wb');
if ($istream && $ostream) {
pipe_streams($istream, $ostream);
fclose($istream);
fclose($ostream);
}
Stdio::fpipe($file,$tmpfile);
$imagick_path = get_config('system', 'imagick_convert_path');
if ($imagick_path && @file_exists($imagick_path)) {

View file

@ -2,6 +2,8 @@
namespace Code\Thumbs;
use Code\Storage\Stdio;
class Video
{
@ -27,14 +29,8 @@ class Video
$tmpfile = $file . $extension;
$outfile = $file . '.jpg';
$istream = fopen($file, 'rb');
$ostream = fopen($tmpfile, 'wb');
if ($istream && $ostream) {
pipe_streams($istream, $ostream);
fclose($istream);
fclose($ostream);
}
Stdio::fpipe($file,$tmpfile);
/*
* Note: imagick convert may try to call 'ffmpeg' (or other conversion utilities) under
* the covers for this particular operation. If this is not installed or not in the path

View file

@ -18,6 +18,8 @@ use Code\Daemon\Run;
use Code\Lib\Channel;
use Code\Lib\ServiceClass;
use Code\Extend\Hook;
use Code\Storage\Stdio;
require_once('include/permissions.php');
require_once('include/security.php');
@ -870,13 +872,7 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null)
$display_path = ltrim($pathname . '/' . $filename, '/');
if ($src) {
$istream = @fopen($src, 'rb');
$ostream = @fopen($os_basepath . $os_relpath, 'wb');
if ($istream && $ostream) {
pipe_streams($istream, $ostream, 65535);
fclose($istream);
fclose($ostream);
}
Stdio::fpipe($src, $os_basepath . $os_relpath);
}
if (array_key_exists('created', $arr)) {
@ -1870,23 +1866,6 @@ function find_filename_by_hash($channel_id, $attachHash)
return $filename;
}
/**
* @brief Pipes $in to $out in 16KB chunks.
*
* @param resource $in File pointer of input
* @param resource $out File pointer of output
* @param int $bufsize size of chunk, default 16384
* @return number with the size
*/
function pipe_streams($in, $out, $bufsize = 16384)
{
$size = 0;
while (!feof($in)) {
$size += fwrite($out, fread($in, $bufsize));
}
return $size;
}
/**
* @brief Activity for files.
*