1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Pnz\MattermostClient\Api; |
6
|
|
|
|
7
|
|
|
use Http\Message\MultipartStream\MultipartStreamBuilder; |
8
|
|
|
use Pnz\MattermostClient\Exception\InvalidArgumentException; |
9
|
|
|
use Pnz\MattermostClient\Model\File\FileInfo; |
10
|
|
|
use Pnz\MattermostClient\Model\File\FileUploadInfo; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Psr\Http\Message\StreamInterface; |
13
|
|
|
|
14
|
|
|
final class FilesApi extends HttpApi |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Uploads a file that can later be attached to a post. |
18
|
|
|
* |
19
|
|
|
* @see: https://api.mattermost.com/v4/#tag/files%2Fpaths%2F~1files%2Fpost |
20
|
|
|
* |
21
|
|
|
* @param string $fileContents The file contents to send |
22
|
|
|
* @param string $filename The filename to be used |
23
|
|
|
* @param string $channelId The ID of the channel that this file will be uploaded to |
24
|
|
|
* @param string|null $clientId A unique identifier for the file that will be returned in the response |
25
|
|
|
* |
26
|
|
|
* @return FileUploadInfo|ResponseInterface |
27
|
|
|
*/ |
28
|
|
|
public function sendFile(string $fileContents, string $filename, string $channelId, string $clientId = null) |
29
|
|
|
{ |
30
|
|
|
if (empty($fileContents) || empty($filename)) { |
31
|
|
|
throw new InvalidArgumentException('File contents and filename can not be empty'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (empty($channelId)) { |
35
|
|
|
throw new InvalidArgumentException('ChannelID can not be empty'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$headers = []; |
39
|
|
|
$multipartStreamBuilder = new MultipartStreamBuilder(); |
40
|
|
|
|
41
|
|
|
// Add channelID |
42
|
|
|
$multipartStreamBuilder->addResource('channel_id', $channelId); |
43
|
|
|
// Add file contents |
44
|
|
|
$multipartStreamBuilder->addResource('files', substr($fileContents, 0, 100), [ |
45
|
|
|
'filename' => $filename, |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
if ($clientId) { |
49
|
|
|
// Add client id |
50
|
|
|
$multipartStreamBuilder->addResource('client_ids', $clientId); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$multipartStream = $multipartStreamBuilder->build(); |
54
|
|
|
$headers['Content-Type'] = 'multipart/form-data; boundary='.$multipartStreamBuilder->getBoundary(); |
55
|
|
|
$multipartStreamBuilder->reset(); |
56
|
|
|
|
57
|
|
|
$response = $this->httpPostRaw('/files', $multipartStream, $headers); |
58
|
|
|
|
59
|
|
|
return $this->handleResponse($response, FileUploadInfo::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get metadata for a file. |
64
|
|
|
* |
65
|
|
|
* @param string $fileId The ID of the file info to get |
66
|
|
|
* |
67
|
|
|
* @see https://api.mattermost.com/v4/#tag/files%2Fpaths%2F~1files~1%7Bfile_id%7D~1info%2Fget |
68
|
|
|
* |
69
|
|
|
* @return FileInfo|ResponseInterface |
70
|
|
|
*/ |
71
|
9 |
|
public function getFileInfo(string $fileId) |
72
|
|
|
{ |
73
|
9 |
|
if (empty($fileId)) { |
74
|
1 |
|
throw new InvalidArgumentException('FileID can not be empty'); |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
$response = $this->httpGet(sprintf('/files/%s/info', $fileId)); |
78
|
|
|
|
79
|
8 |
|
return $this->handleResponse($response, FileInfo::class); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets a file that has been uploaded previously. |
84
|
|
|
* |
85
|
|
|
* @param string $fileId The ID of the file to get |
86
|
|
|
* |
87
|
|
|
* @return StreamInterface |
88
|
|
|
*/ |
89
|
9 |
|
public function getFile(string $fileId) |
90
|
|
|
{ |
91
|
9 |
|
if (empty($fileId)) { |
92
|
1 |
|
throw new InvalidArgumentException('FileID can not be empty'); |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
$response = $this->httpGet(sprintf('/files/%s', $fileId)); |
96
|
|
|
|
97
|
8 |
|
$status = $response->getStatusCode(); |
98
|
8 |
|
if (200 !== $status && 201 !== $status) { |
99
|
7 |
|
$this->handleErrors($response); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$response->getBody()->rewind(); |
103
|
|
|
|
104
|
1 |
|
return $response->getBody(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Gets a public link for a file that can be accessed without logging into Mattermost. |
109
|
|
|
* |
110
|
|
|
* @param string $fileId The ID of the file to get a link for |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
9 |
|
public function getFileLink(string $fileId) |
115
|
|
|
{ |
116
|
9 |
|
if (empty($fileId)) { |
117
|
1 |
|
throw new InvalidArgumentException('FileID can not be empty'); |
118
|
|
|
} |
119
|
|
|
|
120
|
8 |
|
$response = $this->httpGet(sprintf('/files/%s/link', $fileId)); |
121
|
|
|
|
122
|
8 |
|
if (200 !== $response->getStatusCode() && 201 !== $response->getStatusCode()) { |
123
|
7 |
|
$this->handleErrors($response); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return (string) $response->getBody(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|