mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
Optimize reading of large files when using php-smbclient
This commit is contained in:
parent
4ca899f582
commit
8c89cb8972
3 changed files with 108 additions and 7 deletions
101
src/NativeReadStream.php
Normal file
101
src/NativeReadStream.php
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
||||||
|
* This file is licensed under the Licensed under the MIT license:
|
||||||
|
* http://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Icewind\SMB;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stream optimized for read only usage
|
||||||
|
*/
|
||||||
|
class NativeReadStream extends NativeStream {
|
||||||
|
const CHUNK_SIZE = 1048576; // 1MB chunks
|
||||||
|
/**
|
||||||
|
* @var resource
|
||||||
|
*/
|
||||||
|
private $readBuffer = null;
|
||||||
|
|
||||||
|
private $bufferSize = 0;
|
||||||
|
|
||||||
|
private $pos = 0;
|
||||||
|
|
||||||
|
public function stream_open($path, $mode, $options, &$opened_path) {
|
||||||
|
$this->readBuffer = fopen('php://memory', 'r+');
|
||||||
|
|
||||||
|
return parent::stream_open($path, $mode, $options, $opened_path);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap a stream from libsmbclient-php into a regular php stream
|
||||||
|
*
|
||||||
|
* @param \Icewind\SMB\NativeState $state
|
||||||
|
* @param resource $smbStream
|
||||||
|
* @param string $mode
|
||||||
|
* @param string $url
|
||||||
|
* @return resource
|
||||||
|
*/
|
||||||
|
public static function wrap($state, $smbStream, $mode, $url) {
|
||||||
|
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeReadStream');
|
||||||
|
$context = stream_context_create(array(
|
||||||
|
'nativesmb' => array(
|
||||||
|
'state' => $state,
|
||||||
|
'handle' => $smbStream,
|
||||||
|
'url' => $url
|
||||||
|
)
|
||||||
|
));
|
||||||
|
$fh = fopen('nativesmb://', $mode, false, $context);
|
||||||
|
stream_wrapper_unregister('nativesmb');
|
||||||
|
return $fh;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_read($count) {
|
||||||
|
// php reads 8192 bytes at once
|
||||||
|
// however due to network latency etc, it's faster to read in larger chunks
|
||||||
|
// and buffer the result
|
||||||
|
if (!parent::stream_eof() && $this->bufferSize < $count) {
|
||||||
|
$remaining = $this->readBuffer;
|
||||||
|
$this->readBuffer = fopen('php://memory', 'r+');
|
||||||
|
$this->bufferSize = 0;
|
||||||
|
stream_copy_to_stream($remaining, $this->readBuffer);
|
||||||
|
$this->bufferSize += fwrite($this->readBuffer, parent::stream_read(self::CHUNK_SIZE));
|
||||||
|
fseek($this->readBuffer, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = fread($this->readBuffer, $count);
|
||||||
|
$this->bufferSize -= $count;
|
||||||
|
|
||||||
|
$read = strlen($result);
|
||||||
|
$this->pos += $read;
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_seek($offset, $whence = SEEK_SET) {
|
||||||
|
$result = parent::stream_seek($offset, $whence);
|
||||||
|
if ($result) {
|
||||||
|
$this->readBuffer = fopen('php://memory', 'r+');
|
||||||
|
$this->bufferSize = 0;
|
||||||
|
$this->pos = parent::stream_tell();
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_eof() {
|
||||||
|
return $this->bufferSize <= 0 && parent::stream_eof();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_tell() {
|
||||||
|
return $this->pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_write($data) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stream_truncate($size) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -233,7 +233,7 @@ class NativeShare extends AbstractShare {
|
||||||
public function read($source) {
|
public function read($source) {
|
||||||
$url = $this->buildUrl($source);
|
$url = $this->buildUrl($source);
|
||||||
$handle = $this->state->open($url, 'r');
|
$handle = $this->state->open($url, 'r');
|
||||||
return NativeStream::wrap($this->state, $handle, 'r', $url);
|
return NativeReadStream::wrap($this->state, $handle, 'r', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -20,22 +20,22 @@ class NativeStream implements File {
|
||||||
/**
|
/**
|
||||||
* @var \Icewind\SMB\NativeState
|
* @var \Icewind\SMB\NativeState
|
||||||
*/
|
*/
|
||||||
private $state;
|
protected $state;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var resource
|
* @var resource
|
||||||
*/
|
*/
|
||||||
private $handle;
|
protected $handle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
private $eof = false;
|
protected $eof = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $url;
|
protected $url;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap a stream from libsmbclient-php into a regular php stream
|
* Wrap a stream from libsmbclient-php into a regular php stream
|
||||||
|
|
@ -50,9 +50,9 @@ class NativeStream implements File {
|
||||||
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
|
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
|
||||||
$context = stream_context_create(array(
|
$context = stream_context_create(array(
|
||||||
'nativesmb' => array(
|
'nativesmb' => array(
|
||||||
'state' => $state,
|
'state' => $state,
|
||||||
'handle' => $smbStream,
|
'handle' => $smbStream,
|
||||||
'url' => $url
|
'url' => $url
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
$fh = fopen('nativesmb://', $mode, false, $context);
|
$fh = fopen('nativesmb://', $mode, false, $context);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue