Skip to content

Commit

Permalink
Added methods to control the name of attached file (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
axl1232 authored Oct 29, 2021
1 parent 98c4d36 commit a60ef29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Message implements ArrayableInterface
private ?string $username = null;
private bool $tts = false;
private ?string $file = null;
private ?string $filename = null;

private SplObjectStorage $embeds;

Expand Down Expand Up @@ -80,6 +81,18 @@ public function setFile(?string $file): self
return $this;
}

public function getFilename(): ?string
{
return $this->filename;
}

public function setFilename(?string $filename): self
{
$this->filename = $filename;

return $this;
}

public function getEmbeds(): SplObjectStorage
{
return $this->embeds;
Expand Down Expand Up @@ -133,6 +146,7 @@ public function toArray(): array

if ($this->file !== null) {
$data['file'] = $this->file;
$data['filename'] = $this->filename ?? basename($this->file);
}

return $data;
Expand Down
6 changes: 4 additions & 2 deletions src/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public function send(Message $message): int
try {
if (isset($data['file'])) {
$file = $data['file'];
unset($data['file']);
$filename = $data['filename'];

unset($data['file'], $data['filename']);

$options[CURLOPT_HTTPHEADER] = ['Content-type: multipart/form-data'];
$options[CURLOPT_POSTFIELDS] = [
'file' => new CURLFile($file, '', basename($file)),
'file' => new CURLFile($file, '', $filename),
'payload_json' => json_encode($data, JSON_THROW_ON_ERROR),
];
} else {
Expand Down

0 comments on commit a60ef29

Please sign in to comment.