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|resource|StreamInterface|null $file The file contents to send |
22
|
|
|
* @param string|null $clientId A unique identifier for the file that will be returned in the response |
23
|
|
|
* |
24
|
|
|
* @return FileUploadInfo|ResponseInterface |
25
|
|
|
*/ |
26
|
|
|
public function sendFile($file, string $filename, string $channelId, string $clientId = null) |
27
|
|
|
{ |
28
|
|
|
if (!($file instanceof StreamInterface || \is_resource($file) || \is_string($file))) { |
29
|
|
|
throw new InvalidArgumentException('File must be a string, resource or StreamInterface'); |
30
|
|
|
} |
31
|
|
|
if (empty($filename)) { |
32
|
|
|
throw new InvalidArgumentException('File filename can not be empty'); |
33
|
|
|
} |
34
|
|
|
if (empty($channelId)) { |
35
|
|
|
throw new InvalidArgumentException('ChannelID can not be empty'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$multipartStreamBuilder = new MultipartStreamBuilder(); |
39
|
|
|
$multipartStreamBuilder->addResource('channel_id', $channelId); |
40
|
|
|
$multipartStreamBuilder->addResource('files', $file, ['filename' => $filename]); |
41
|
|
|
|
42
|
|
|
if ($clientId) { |
43
|
|
|
// Add client id |
44
|
|
|
$multipartStreamBuilder->addResource('client_ids', $clientId); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$headers = ['Content-Type' => 'multipart/form-data; boundary='.$multipartStreamBuilder->getBoundary()]; |
48
|
|
|
$multipartStream = $multipartStreamBuilder->build(); |
49
|
|
|
|
50
|
|
|
$response = $this->httpPostRaw('/files', $multipartStream, $headers); |
51
|
|
|
|
52
|
|
|
return $this->handleResponse($response, FileUploadInfo::class); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get metadata for a file. |
57
|
|
|
* |
58
|
|
|
* @param string $fileId The ID of the file info to get |
59
|
|
|
* |
60
|
|
|
* @see https://api.mattermost.com/v4/#tag/files%2Fpaths%2F~1files~1%7Bfile_id%7D~1info%2Fget |
61
|
|
|
* |
62
|
|
|
* @return FileInfo|ResponseInterface |
63
|
|
|
*/ |
64
|
|
|
public function getFileInfo(string $fileId) |
65
|
|
|
{ |
66
|
|
|
if (empty($fileId)) { |
67
|
|
|
throw new InvalidArgumentException('FileID can not be empty'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$response = $this->httpGet(sprintf('/files/%s/info', $fileId)); |
71
|
9 |
|
|
72
|
|
|
return $this->handleResponse($response, FileInfo::class); |
73
|
9 |
|
} |
74
|
1 |
|
|
75
|
|
|
/** |
76
|
|
|
* Gets a file that has been uploaded previously. |
77
|
8 |
|
* |
78
|
|
|
* @param string $fileId The ID of the file to get |
79
|
8 |
|
*/ |
80
|
|
|
public function getFile(string $fileId): StreamInterface |
81
|
|
|
{ |
82
|
|
|
if (empty($fileId)) { |
83
|
|
|
throw new InvalidArgumentException('FileID can not be empty'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$response = $this->httpGet(sprintf('/files/%s', $fileId)); |
87
|
9 |
|
|
88
|
|
|
$status = $response->getStatusCode(); |
89
|
9 |
|
if (200 !== $status && 201 !== $status) { |
90
|
1 |
|
$this->handleErrors($response); |
91
|
|
|
} |
92
|
|
|
|
93
|
8 |
|
$response->getBody()->rewind(); |
94
|
|
|
|
95
|
8 |
|
return $response->getBody(); |
96
|
8 |
|
} |
97
|
7 |
|
|
98
|
|
|
/** |
99
|
|
|
* Gets a public link for a file that can be accessed without logging into Mattermost. |
100
|
1 |
|
* |
101
|
|
|
* @param string $fileId The ID of the file to get a link for |
102
|
1 |
|
*/ |
103
|
|
|
public function getFileLink(string $fileId): string |
104
|
|
|
{ |
105
|
|
|
if (empty($fileId)) { |
106
|
|
|
throw new InvalidArgumentException('FileID can not be empty'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$response = $this->httpGet(sprintf('/files/%s/link', $fileId)); |
110
|
9 |
|
|
111
|
|
|
if (200 !== $response->getStatusCode() && 201 !== $response->getStatusCode()) { |
112
|
9 |
|
$this->handleErrors($response); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
return (string) $response->getBody(); |
116
|
8 |
|
} |
117
|
|
|
} |
118
|
|
|
|