Skip to content

Commit 8cc07d8

Browse files
committed
Revise file upload logic.
1 parent 89baf4f commit 8cc07d8

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/TelegramFile.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace NotificationChannels\Telegram;
44

5-
use Illuminate\Support\Facades\View;
65
use JsonSerializable;
7-
use NotificationChannels\Telegram\Traits\HasSharedLogic;
6+
use Illuminate\Support\Facades\View;
87
use Psr\Http\Message\StreamInterface;
8+
use NotificationChannels\Telegram\Traits\HasSharedLogic;
99

1010
/**
1111
* Class TelegramFile.
@@ -18,7 +18,7 @@ class TelegramFile implements JsonSerializable
1818
public $type = 'document';
1919

2020
/**
21-
* @param string $content
21+
* @param string $content
2222
*
2323
* @return self
2424
*/
@@ -30,7 +30,7 @@ public static function create(string $content = ''): self
3030
/**
3131
* Message constructor.
3232
*
33-
* @param string $content
33+
* @param string $content
3434
*/
3535
public function __construct(string $content = '')
3636
{
@@ -41,7 +41,7 @@ public function __construct(string $content = '')
4141
/**
4242
* Notification message (Supports Markdown).
4343
*
44-
* @param string $content
44+
* @param string $content
4545
*
4646
* @return $this
4747
*/
@@ -57,26 +57,29 @@ public function content(string $content): self
5757
*
5858
* Generic method to attach files of any type based on API.
5959
*
60-
* @param string|resource|StreamInterface $file
61-
* @param string $type
62-
* @param string|null $filename
60+
* @param string|resource|StreamInterface $file
61+
* @param string $type
62+
* @param string|null $filename
6363
*
6464
* @return $this
6565
*/
6666
public function file($file, string $type, string $filename = null): self
6767
{
6868
$this->type = $type;
6969

70-
$isLocalFile = $this->isReadableFile($file);
71-
72-
if ($filename !== null || $isLocalFile) {
73-
$this->payload['file'] = [
74-
'filename' => $filename,
75-
'name' => $type,
76-
'contents' => $isLocalFile ? fopen($file, 'rb') : $file,
77-
];
78-
} else {
70+
if (is_string($file) && !$this->isReadableFile($file)) {
7971
$this->payload[$type] = $file;
72+
73+
return $this;
74+
}
75+
76+
$this->payload['file'] = [
77+
'name' => $type,
78+
'contents' => is_resource($file) ? $file : fopen($file, 'rb'),
79+
];
80+
81+
if ($filename !== null) {
82+
$this->payload['file']['filename'] = $filename;
8083
}
8184

8285
return $this;
@@ -87,7 +90,7 @@ public function file($file, string $type, string $filename = null): self
8790
*
8891
* Use this method to send photos.
8992
*
90-
* @param string $file
93+
* @param string $file
9194
*
9295
* @return $this
9396
*/
@@ -102,7 +105,7 @@ public function photo(string $file): self
102105
* Use this method to send audio files, if you want Telegram clients to display them in the music player.
103106
* Your audio must be in the .mp3 format.
104107
*
105-
* @param string $file
108+
* @param string $file
106109
*
107110
* @return $this
108111
*/
@@ -116,8 +119,8 @@ public function audio(string $file): self
116119
*
117120
* Use this method to send general files.
118121
*
119-
* @param string $file
120-
* @param string|null $filename
122+
* @param string $file
123+
* @param string|null $filename
121124
*
122125
* @return $this
123126
*/
@@ -131,7 +134,7 @@ public function document(string $file, string $filename = null): self
131134
*
132135
* Use this method to send video files, Telegram clients support mp4 videos.
133136
*
134-
* @param string $file
137+
* @param string $file
135138
*
136139
* @return $this
137140
*/
@@ -145,7 +148,7 @@ public function video(string $file): self
145148
*
146149
* Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound).
147150
*
148-
* @param string $file
151+
* @param string $file
149152
*
150153
* @return $this
151154
*/
@@ -160,7 +163,7 @@ public function animation(string $file): self
160163
* Use this method to send audio files, if you want Telegram clients to display the file as a playable voice
161164
* message. For this to work, your audio must be in an .ogg file encoded with OPUS.
162165
*
163-
* @param string $file
166+
* @param string $file
164167
*
165168
* @return $this
166169
*/
@@ -175,7 +178,7 @@ public function voice(string $file): self
175178
* Telegram clients support rounded square mp4 videos of up to 1 minute long.
176179
* Use this method to send video messages.
177180
*
178-
* @param string $file
181+
* @param string $file
179182
*
180183
* @return $this
181184
*/
@@ -188,9 +191,9 @@ public function videoNote(string $file): self
188191
* Attach a view file as the content for the notification.
189192
* Supports Laravel blade template.
190193
*
191-
* @param string $view
192-
* @param array $data
193-
* @param array $mergeData
194+
* @param string $view
195+
* @param array $data
196+
* @param array $mergeData
194197
*
195198
* @return $this
196199
*/
@@ -237,7 +240,7 @@ public function toMultipart(): array
237240
/**
238241
* Determine if it's a regular and readable file.
239242
*
240-
* @param string $file
243+
* @param string $file
241244
*
242245
* @return bool
243246
*/

0 commit comments

Comments
 (0)