Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Commit

Permalink
File uploads (#42)
Browse files Browse the repository at this point in the history
* Add support for multipart/form-data requests and fix file uploads

* Added support for thread_ts in file uploads
  • Loading branch information
pmishev committed Jul 20, 2020
1 parent b5b453c commit 0241af2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
32 changes: 26 additions & 6 deletions Slack/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public function __construct(Connection $connection)

/**
* @param string $action
* @param array $parameter
* @param array $parameter
* @param bool $multipart Whether to use multipart/form-data or the default application/x-www-form-urlencoded
*
* @return Response|bool
*/
public function send($action, array $parameter = [])
public function send($action, array $parameter = [], $multipart = false)
{
if (!$this->connection->isValid()) {
return false;
Expand All @@ -51,7 +52,8 @@ public function send($action, array $parameter = [])
do {
$response = $this->executeRequest(
$url,
$this->connection->getHttpMethod() === Request::METHOD_POST ? $parsedRequestParams : null
$this->connection->getHttpMethod() === Request::METHOD_POST ? $parsedRequestParams : null,
$multipart
);
$response = Response::parseGuzzleResponse($response, $action);

Expand Down Expand Up @@ -89,18 +91,36 @@ protected function buildUri(Actions\ActionsInterface $action, array $requestPara

/**
* @param Uri $uri
* @param array $params form post values
* @param array $params form post values
* @param bool $multipart Whether to use multipart/form-data or the default application/x-www-form-urlencoded
*
* @return \GuzzleHttp\Psr7\Response
*/
protected function executeRequest(Uri $uri, array $params = null)
protected function executeRequest(Uri $uri, array $params = null, $multipart = false)
{
$guzzle = new GuzzleClient(['verify' => $this->connection->getVerifySsl()]);

if (null !== $params) {
if ($multipart) {
$multipartParams = [];
foreach ($params as $paramName => $paramValue) {
$multipartParams[] = [
'name' => $paramName,
'contents' => $paramValue,
];
}
$postParams = ['multipart' => $multipartParams];
} else {
$postParams = ['form_params' => $params];
}
} else {
$postParams = [];
}

return $guzzle->request(
$this->connection->getHttpMethod(),
$uri,
null !== $params ? ['form_params' => $params] : []
$postParams
);
}
}
2 changes: 1 addition & 1 deletion Slack/Client/Actions/FilesUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FilesUpload implements ActionsInterface
* @var array
*/
protected $parameter = [
'content' => null,
'file' => null,
'filetype' => null,
'filename' => null,
'title' => null,
Expand Down
11 changes: 6 additions & 5 deletions Slack/Messaging.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ public function message($channel, $message, $identity, array $attachments = [],
);
}


/**
* @param string|array $channel # String is DEPRECATED scince v1.3 - Deliver Array
* @param string $title
* @param string $file
* @param string|null $comment
* @param string|null $threadTs
*
* @return Client\Response|false
*/
public function upload($channel, $title, $file, $comment = null)
public function upload($channel, $title, $file, $comment = null, $threadTs = null)
{
if (!file_exists($file)) {
return false;
Expand All @@ -96,13 +96,14 @@ public function upload($channel, $title, $file, $comment = null)
$params['title'] = $title;
$params['initial_comment'] = $comment;
$params['channels'] = implode(',', $channel);
$params['content'] = file_get_contents($file);
$params['fileType'] = mime_content_type($file);
$params['filename'] = basename($file);
$params['file'] = fopen($file, 'r');
$params['thread_ts'] = $threadTs;

return $this->client->send(
Actions::ACTION_FILES_UPLOAD,
$params
$params,
true
);
}
}

0 comments on commit 0241af2

Please sign in to comment.