2023-07-04 18:19:06 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2023-08-13 11:40:35 +02:00
|
|
|
/**
|
|
|
|
* This file is part of MadelineProto.
|
|
|
|
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU Affero General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License along with MadelineProto.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Daniil Gentili <daniil@daniil.it>
|
|
|
|
* @copyright 2016-2023 Daniil Gentili <daniil@daniil.it>
|
|
|
|
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
|
|
|
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
|
|
|
*/
|
|
|
|
|
2023-07-04 18:19:06 +02:00
|
|
|
namespace danog\MadelineProto\EventHandler;
|
|
|
|
|
2023-08-19 17:33:47 +02:00
|
|
|
use Amp\ByteStream\ReadableStream;
|
2023-07-04 19:48:23 +02:00
|
|
|
use danog\MadelineProto\MTProto;
|
2023-09-24 00:48:03 +02:00
|
|
|
use danog\MadelineProto\EventHandler\AbstractMedia;
|
2023-07-04 19:48:23 +02:00
|
|
|
|
2023-07-04 18:19:06 +02:00
|
|
|
/**
|
|
|
|
* Represents a generic media.
|
|
|
|
*/
|
2023-09-24 00:48:03 +02:00
|
|
|
abstract class Media extends AbstractMedia
|
2023-07-04 18:19:06 +02:00
|
|
|
{
|
|
|
|
/** Media filesize */
|
|
|
|
public readonly int $size;
|
|
|
|
|
|
|
|
/** Media file name */
|
|
|
|
public readonly string $fileName;
|
|
|
|
|
2023-07-04 19:48:23 +02:00
|
|
|
/** Media file extension */
|
|
|
|
public readonly string $fileExt;
|
|
|
|
|
2023-07-04 18:19:06 +02:00
|
|
|
/** Media creation date */
|
2023-07-10 10:12:46 +02:00
|
|
|
public readonly int $creationDate;
|
2023-07-04 18:19:06 +02:00
|
|
|
|
|
|
|
/** Media MIME type */
|
|
|
|
public readonly string $mimeType;
|
|
|
|
|
|
|
|
/** Time-to-live of media */
|
|
|
|
public readonly ?int $ttl;
|
|
|
|
|
2023-07-04 19:48:23 +02:00
|
|
|
/** @var list<array> Thumbnails */
|
2023-07-04 18:19:06 +02:00
|
|
|
public readonly array $thumbs;
|
|
|
|
|
2023-07-04 19:48:23 +02:00
|
|
|
/** @var list<array> Video thumbnails */
|
2023-07-04 18:19:06 +02:00
|
|
|
public readonly array $videoThumbs;
|
|
|
|
|
|
|
|
/** Whether the media should be hidden behind a spoiler */
|
|
|
|
public readonly bool $spoiler;
|
|
|
|
|
2023-07-11 19:43:31 +02:00
|
|
|
/** File ID in bot API format (always present even for users) */
|
|
|
|
public readonly string $botApiFileId;
|
|
|
|
|
|
|
|
/** Unique file ID in bot API format (always present even for users) */
|
|
|
|
public readonly string $botApiFileUniqueId;
|
|
|
|
|
|
|
|
/** @internal Media location */
|
|
|
|
public readonly array $location;
|
2023-07-04 19:48:23 +02:00
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
public function __construct(
|
|
|
|
MTProto $API,
|
|
|
|
array $rawMedia,
|
2023-07-11 19:43:31 +02:00
|
|
|
|
|
|
|
/** Whether this media is protected */
|
|
|
|
public readonly bool $protected
|
2023-07-04 19:48:23 +02:00
|
|
|
) {
|
|
|
|
parent::__construct($API);
|
|
|
|
[
|
|
|
|
'name' => $name,
|
|
|
|
'ext' => $this->fileExt,
|
|
|
|
'mime' => $this->mimeType,
|
|
|
|
'size' => $this->size,
|
|
|
|
'InputFileLocation' => $this->location
|
|
|
|
] = $API->getDownloadInfo($rawMedia);
|
2023-07-16 16:00:09 +02:00
|
|
|
$this->fileName = $name.$this->fileExt;
|
2023-07-04 19:48:23 +02:00
|
|
|
|
2023-07-11 19:43:31 +02:00
|
|
|
[
|
|
|
|
'file_id' => $this->botApiFileId,
|
|
|
|
'file_unique_id' => $this->botApiFileUniqueId
|
|
|
|
] = $API->extractBotAPIFile($API->MTProtoToBotAPI($rawMedia));
|
|
|
|
|
2023-07-10 10:12:46 +02:00
|
|
|
$this->creationDate = ($rawMedia['document'] ?? $rawMedia['photo'])['date'];
|
2023-07-04 19:48:23 +02:00
|
|
|
$this->ttl = $rawMedia['ttl_seconds'] ?? null;
|
|
|
|
$this->spoiler = $rawMedia['spoiler'] ?? false;
|
|
|
|
}
|
2023-07-10 10:12:46 +02:00
|
|
|
|
2023-07-16 14:55:00 +02:00
|
|
|
/**
|
|
|
|
* Gets a download link for any file up to 4GB.
|
|
|
|
*
|
|
|
|
* @param string|null $scriptUrl Optional path to custom download script (not needed when running via web)
|
|
|
|
*/
|
|
|
|
public function getDownloadLink(?string $scriptUrl = null): string
|
|
|
|
{
|
2023-07-25 18:44:30 +02:00
|
|
|
return $this->getClient()->getDownloadLink($this, $scriptUrl);
|
2023-07-16 14:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-08-19 17:33:47 +02:00
|
|
|
/**
|
|
|
|
* Get a readable amp stream with the file contents.
|
|
|
|
*
|
2023-09-01 15:04:04 +02:00
|
|
|
* @param (callable(float, float, float): void)|null $cb Progress callback
|
2023-08-19 17:33:47 +02:00
|
|
|
*/
|
2023-08-21 18:48:54 +02:00
|
|
|
public function getStream(?callable $cb = null, int $offset = 0, int $end = -1): ReadableStream
|
2023-08-19 17:33:47 +02:00
|
|
|
{
|
2023-08-21 18:48:54 +02:00
|
|
|
return $this->getClient()->downloadToReturnedStream($this, $cb, $offset, $end);
|
2023-08-19 17:33:47 +02:00
|
|
|
}
|
2023-09-01 15:26:32 +02:00
|
|
|
|
|
|
|
/**
|
2023-09-01 19:05:20 +02:00
|
|
|
* Download the media to working directory or passed path.
|
2023-09-24 00:48:03 +02:00
|
|
|
*
|
|
|
|
* @param string $dir Directory where to download the file
|
2023-09-24 19:56:55 +02:00
|
|
|
* @param (callable(float, float, float): void)|null $cb Progress callback
|
2023-09-24 00:48:03 +02:00
|
|
|
* @return string
|
2023-09-01 15:26:32 +02:00
|
|
|
*/
|
2023-09-24 00:48:03 +02:00
|
|
|
public function downloadToDir(?string $dir = null, ?callable $cb = null): string
|
2023-09-01 15:26:32 +02:00
|
|
|
{
|
2023-09-24 00:48:03 +02:00
|
|
|
$dir ??= \getcwd();
|
|
|
|
return $this->getClient()->downloadToDir($this, $dir, $cb);
|
2023-09-02 14:52:07 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Download the media to file.
|
2023-09-24 00:48:03 +02:00
|
|
|
*
|
|
|
|
* @param string $file Downloaded file path
|
2023-09-24 19:56:55 +02:00
|
|
|
* @param (callable(float, float, float): void)|null $cb Progress callback
|
2023-09-24 00:48:03 +02:00
|
|
|
* @return string
|
2023-09-02 14:52:07 +02:00
|
|
|
*/
|
2023-09-24 00:48:03 +02:00
|
|
|
public function downloadToFile(string $file, ?callable $cb = null): string
|
2023-09-02 14:52:07 +02:00
|
|
|
{
|
2023-09-24 10:10:54 +02:00
|
|
|
return $this->getClient()->downloadToFile($this, $file, $cb);
|
2023-09-24 00:48:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
public function jsonSerialize(): mixed
|
|
|
|
{
|
|
|
|
$v = parent::jsonSerialize();
|
|
|
|
unset($v['API'], $v['session'], $v['location']);
|
|
|
|
return $v;
|
2023-09-01 15:26:32 +02:00
|
|
|
}
|
2023-07-04 18:19:06 +02:00
|
|
|
}
|