Skip to content

Commit

Permalink
Merge pull request #446 from nutgram/fix-inputfile-usages
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed May 7, 2023
2 parents 8e0d37f + ed1cb5a commit ef2ca3c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Telegram/Endpoints/CustomEndpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public function sendChunkedVoice(

protected function sendChunkedMedia(
string $endpoint,
mixed $media,
InputFile|string $media,
string $param,
array $opt = [],
$clientOpt = []
Expand Down
14 changes: 0 additions & 14 deletions src/Telegram/Types/Input/InputMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,4 @@ abstract class InputMedia extends BaseType
* Type of the result
*/
public InputMediaType $type;

/**
* File to send. Pass a file_id to send a file that exists on the Telegram servers (recommended),
* pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>”
* to upload a new one using multipart/form-data under <file_attach_name> name.
* @see https://core.telegram.org/bots/api#sending-files More info on Sending Files
* @var string|resource $media
*/
public mixed $media;

/**
* Optional. Caption of the photo to be sent, 0-1024 characters after entities parsing
*/
public ?string $caption = null;
}
5 changes: 3 additions & 2 deletions src/Telegram/Types/Input/InputMediaAnimation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SergiX44\Hydrator\Annotation\ArrayType;
use SergiX44\Nutgram\Telegram\Properties\InputMediaType;
use SergiX44\Nutgram\Telegram\Properties\ParseMode;
use SergiX44\Nutgram\Telegram\Types\Internal\InputFile;
use SergiX44\Nutgram\Telegram\Types\Message\MessageEntity;

/**
Expand All @@ -21,7 +22,7 @@ class InputMediaAnimation extends InputMedia
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public mixed $media;
public InputFile|string $media;

/**
* Optional.
Expand All @@ -33,7 +34,7 @@ class InputMediaAnimation extends InputMedia
* Thumbnails can't be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public mixed $thumbnail = null;
public InputFile|string|null $thumbnail = null;

/**
* Optional.
Expand Down
5 changes: 3 additions & 2 deletions src/Telegram/Types/Input/InputMediaAudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SergiX44\Hydrator\Annotation\ArrayType;
use SergiX44\Nutgram\Telegram\Properties\InputMediaType;
use SergiX44\Nutgram\Telegram\Properties\ParseMode;
use SergiX44\Nutgram\Telegram\Types\Internal\InputFile;
use SergiX44\Nutgram\Telegram\Types\Message\MessageEntity;

/**
Expand All @@ -21,7 +22,7 @@ class InputMediaAudio extends InputMedia
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public mixed $media;
public InputFile|string $media;

/**
* Optional.
Expand All @@ -33,7 +34,7 @@ class InputMediaAudio extends InputMedia
* Thumbnails can't be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public mixed $thumbnail = null;
public InputFile|string|null $thumbnail = null;

/**
* Optional.
Expand Down
5 changes: 3 additions & 2 deletions src/Telegram/Types/Input/InputMediaDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SergiX44\Hydrator\Annotation\ArrayType;
use SergiX44\Nutgram\Telegram\Properties\InputMediaType;
use SergiX44\Nutgram\Telegram\Properties\ParseMode;
use SergiX44\Nutgram\Telegram\Types\Internal\InputFile;
use SergiX44\Nutgram\Telegram\Types\Message\MessageEntity;

/**
Expand All @@ -21,7 +22,7 @@ class InputMediaDocument extends InputMedia
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public mixed $media;
public InputFile|string $media;

/**
* Optional.
Expand All @@ -33,7 +34,7 @@ class InputMediaDocument extends InputMedia
* Thumbnails can't be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public mixed $thumbnail = null;
public InputFile|string|null $thumbnail = null;

/**
* Optional.
Expand Down
3 changes: 2 additions & 1 deletion src/Telegram/Types/Input/InputMediaPhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SergiX44\Hydrator\Annotation\ArrayType;
use SergiX44\Nutgram\Telegram\Properties\InputMediaType;
use SergiX44\Nutgram\Telegram\Properties\ParseMode;
use SergiX44\Nutgram\Telegram\Types\Internal\InputFile;
use SergiX44\Nutgram\Telegram\Types\Message\MessageEntity;

/**
Expand All @@ -21,7 +22,7 @@ class InputMediaPhoto extends InputMedia
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public mixed $media;
public InputFile|string $media;

/**
* Optional.
Expand Down
5 changes: 3 additions & 2 deletions src/Telegram/Types/Input/InputMediaVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SergiX44\Hydrator\Annotation\ArrayType;
use SergiX44\Nutgram\Telegram\Properties\InputMediaType;
use SergiX44\Nutgram\Telegram\Properties\ParseMode;
use SergiX44\Nutgram\Telegram\Types\Internal\InputFile;
use SergiX44\Nutgram\Telegram\Types\Message\MessageEntity;

/**
Expand All @@ -21,7 +22,7 @@ class InputMediaVideo extends InputMedia
* Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet, or pass “attach://<file_attach_name>” to upload a new one using multipart/form-data under <file_attach_name> name.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public mixed $media;
public InputFile|string $media;

/**
* Optional.
Expand All @@ -33,7 +34,7 @@ class InputMediaVideo extends InputMedia
* Thumbnails can't be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>.
* {@see https://core.telegram.org/bots/api#sending-files More information on Sending Files »}
*/
public mixed $thumbnail = null;
public InputFile|string|null $thumbnail = null;

/**
* Optional.
Expand Down
23 changes: 14 additions & 9 deletions src/Telegram/Types/Internal/InputFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,38 @@ class InputFile
protected ?string $filename;

/**
* @param mixed $resource
* @param string|null $filename
* @param resource|string $resource Resource or path to file
* @param string|null $filename Filename
*/
public function __construct($resource, ?string $filename = null)
public function __construct(mixed $resource, ?string $filename = null)
{
$this->filename = $filename;
if (is_resource($resource)) {
$this->resource = $resource;
} elseif (is_string($resource) && file_exists($resource)) {
$this->resource = fopen($resource, 'rb+');
$res = fopen($resource, 'rb+');
if ($res === false) {
throw new \InvalidArgumentException('Invalid resource specified.');
}

$this->resource = $res;
} else {
throw new \InvalidArgumentException('Invalid resource specified.');
}
}

/**
* @param resource $resource
* @param string|null $filename
* @param resource|string $resource Resource or path to file
* @param string|null $filename Filename
* @return InputFile
*/
public static function make($resource, ?string $filename = null): InputFile
public static function make(mixed $resource, ?string $filename = null): InputFile
{
return new self($resource, $filename);
}

/**
* @param string|null $filename
* @param string|null $filename
* @return InputFile
*/
public function filename(?string $filename): InputFile
Expand All @@ -65,7 +70,7 @@ public function getResource()
/**
* @return string
*/
public function getFilename()
public function getFilename(): string
{
$metadata = stream_get_meta_data($this->resource);

Expand Down

0 comments on commit ef2ca3c

Please sign in to comment.