1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-26 23:14:38 +01:00

Merge branch 'v8' into secret_refactoring

This commit is contained in:
Daniil Gentili 2023-09-23 21:06:38 +02:00
commit 127f8ce7d1
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
9 changed files with 68 additions and 53 deletions

View File

@ -154,6 +154,9 @@
</TooManyArguments>
</file>
<file src="src/DataCenter.php">
<DocblockTypeContradiction>
<code>\is_string($dc)</code>
</DocblockTypeContradiction>
<PossiblyUndefinedArrayOffset>
<code>$default[2]</code>
<code>$default[2]</code>
@ -1486,8 +1489,6 @@
</file>
<file src="src/MTProtoTools/FilesLogic.php">
<MissingClosureReturnType>
<code>static function (int $offset, int $size) use ($stream) {</code>
<code>static function (int $offset, int $size) use ($stream, $seekable, $lock) {</code>
<code>static function (string $payload, int $offset) use ($stream, $seekable, $lock) {</code>
</MissingClosureReturnType>
<PossiblyInvalidArgument>

View File

@ -51,7 +51,7 @@ final class API extends AbstractAPI
*
* @var string
*/
public const RELEASE = '8.0.0-beta151';
public const RELEASE = '8.0.0-beta152';
/**
* Secret chat was not found.
*

View File

@ -97,6 +97,9 @@ final class DataCenter
$this->dohWrapper ??= new DoHWrapper($API);
if ($this->getSettings()->hasChanged()) {
foreach ($this->sockets as $dc => $socket) {
if (\is_string($dc)) {
continue;
}
$socket->setExtra($this->API, $dc, $this->generateContexts($dc));
$socket->reconnect();
}

View File

@ -22,6 +22,10 @@ namespace danog\MadelineProto;
/**
* File callback interface.
*
* @template TT
*
* @implements FileCallbackInterface<TT>
*/
final class FileCallback implements FileCallbackInterface
{
@ -34,7 +38,7 @@ final class FileCallback implements FileCallbackInterface
/**
* Construct file callback.
*
* @param mixed $file File to download/upload
* @param TT $file File to download/upload
* @param callable(float, float, float) $callback Callback
*/
public function __construct(public readonly mixed $file, callable $callback)
@ -43,6 +47,8 @@ final class FileCallback implements FileCallbackInterface
}
/**
* Get file.
*
* @return TT
*/
public function getFile(): mixed
{

View File

@ -22,11 +22,15 @@ namespace danog\MadelineProto;
/**
* File callback interface.
*
* @template T
*/
interface FileCallbackInterface
{
/**
* Get file.
*
* @return T
*/
public function getFile(): mixed;
/**

View File

@ -1940,13 +1940,13 @@ abstract class InternalDoc
* The callable must accept two parameters: int $offset, int $size
* The callable must return a string with the contest of the file at the specified offset and size.
*
* @param mixed $callable Callable
* @param integer $size File size
* @param string $mime Mime type
* @param string $fileName File name
* @param callable $cb Callback
* @param boolean $seekable Whether chunks can be fetched out of order
* @param boolean $encrypted Whether to encrypt file for secret chats
* @param callable(int, int): string $callable Callable (offset, length) => data
* @param integer $size File size
* @param string $mime Mime type
* @param string $fileName File name
* @param callable(float, float, float): void $cb Status callback
* @param boolean $seekable Whether chunks can be fetched out of order
* @param boolean $encrypted Whether to encrypt file for secret chats
*
* @return array InputFile constructor
*/

View File

@ -20,12 +20,11 @@ use danog\MadelineProto\FileCallbackInterface;
/**
* @internal
*
* @implements FileCallbackInterface<mixed>
*/
final class FileCallback extends Obj implements FileCallbackInterface
{
/**
* Get file.
*/
public function getFile(): mixed
{
return $this->__call('getFile');

View File

@ -187,25 +187,18 @@ trait Files
* The callable must accept two parameters: int $offset, int $size
* The callable must return a string with the contest of the file at the specified offset and size.
*
* @param mixed $callable Callable
* @param integer $size File size
* @param string $mime Mime type
* @param string $fileName File name
* @param callable $cb Callback
* @param boolean $seekable Whether chunks can be fetched out of order
* @param boolean $encrypted Whether to encrypt file for secret chats
* @param callable(int, int): string $callable Callable (offset, length) => data
* @param integer $size File size
* @param string $mime Mime type
* @param string $fileName File name
* @param callable(float, float, float): void $cb Status callback
* @param boolean $seekable Whether chunks can be fetched out of order
* @param boolean $encrypted Whether to encrypt file for secret chats
*
* @return array InputFile constructor
*/
public function uploadFromCallable(callable $callable, int $size = 0, string $mime = 'application/octet-stream', string $fileName = '', ?callable $cb = null, bool $seekable = true, bool $encrypted = false): array
{
if (\is_object($callable) && $callable instanceof FileCallbackInterface) {
$cb = $callable;
$callable = $callable->getFile();
}
if (!\is_callable($callable)) {
throw new Exception('Invalid callable provided');
}
if ($cb === null) {
$cb = function (float $percent, float $speed, float $time): void {
$this->logger->logger('Upload status: '.$percent.'%', Logger::NOTICE);
@ -256,13 +249,17 @@ trait Files
};
}
$totalSize = 0;
if (!$seekable) {
$nextOffset = 0;
$callable = static function (int $offset, int $size) use ($callable, &$nextOffset): string {
Assert::eq($offset, $nextOffset);
$nextOffset += $size;
return $callable($offset, $size);
};
}
$callable = static function (int $part_num) use (&$totalSize, $size, $file_id, &$part_total_num, $part_size, $callable, $ige): array {
static $offset = 0;
$oldOffset = $offset;
$offset += $part_size;
$bytes = $callable(
$oldOffset,
$part_num * $part_size,
$part_size
);
$totalSize += $bytesLen = \strlen($bytes);
@ -275,9 +272,6 @@ trait Files
}
}
if ($bytes instanceof Future) {
$bytes = $bytes->await();
}
if ($ige) {
$bytes = $ige->encrypt(\str_pad($bytes, $part_size, \chr(0)));
}
@ -287,18 +281,15 @@ trait Files
$resPromises = [];
$start = \microtime(true);
while ($part_num < $part_total_num || !$size) {
$writePromise = async(
$this->methodCallAsyncWrite(...),
$method,
fn () => $callable($part_num),
['heavy' => true, 'datacenter' => &$datacenter]
);
if (!$seekable) {
try {
$writePromise->await();
} catch (StreamEof) {
break;
}
try {
$writePromise = async(
$this->methodCallAsyncWrite(...),
$method,
$seekable ? fn () => $callable($part_num) : $callable($part_num),
['heavy' => true, 'datacenter' => &$datacenter]
);
} catch (StreamEof) {
break;
}
EventLoop::queue(function () use ($writePromise, $cb, $part_num, $size, &$resPromises): void {
$readFuture = $writePromise->await();

View File

@ -399,7 +399,8 @@ trait FilesLogic
}
if ($stream instanceof File) {
$lock = new LocalMutex;
$callable = static function (int $offset, int $size) use ($stream, $seekable, $lock) {
$nextOffset = 0;
$callable = static function (int $offset, int $size) use ($stream, $seekable, $lock, &$nextOffset): string {
/** @var Lock */
$l = $lock->acquire();
try {
@ -407,8 +408,13 @@ trait FilesLogic
while ($stream->tell() !== $offset) {
$stream->seek($offset);
}
} else {
Assert::eq($offset, $nextOffset);
$nextOffset += $size;
}
return $stream->read(null, $size);
$result = $stream->read(null, $size);
\assert($result !== null);
return $result;
} finally {
EventLoop::queue($l->release(...));
}
@ -419,17 +425,22 @@ trait FilesLogic
$stream = ($ctx->getStream());
$created = true;
}
$callable = static function (int $offset, int $size) use ($stream) {
$nextOffset = 0;
$callable = static function (int $offset, int $size) use ($stream, &$nextOffset): string {
if (!$stream instanceof BufferedRawStream) {
throw new \InvalidArgumentException('Invalid stream type');
}
Assert::eq($offset, $nextOffset);
$nextOffset += $size;
$reader = $stream->getReadBuffer($l);
try {
return $reader->bufferRead($size);
$result = $reader->bufferRead($size);
} catch (NothingInTheSocketException $e) {
$reader = $stream->getReadBuffer($size);
return $reader->bufferRead($size);
$result = $reader->bufferRead($size);
}
\assert($result !== null);
return $result;
};
$seekable = false;
}