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\Channel\Channel; |
10
|
|
|
use Pnz\MattermostClient\Model\Channel\ChannelMember; |
11
|
|
|
use Pnz\MattermostClient\Model\Channel\ChannelMembers; |
12
|
|
|
use Pnz\MattermostClient\Model\Channel\ChannelStats; |
13
|
|
|
use Pnz\MattermostClient\Model\File\FileMetadata; |
14
|
|
|
use Pnz\MattermostClient\Model\Post\Posts; |
|
|
|
|
15
|
|
|
use Pnz\MattermostClient\Model\Status; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
|
18
|
|
|
final class Files extends HttpApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Upload a File to a channel. |
22
|
|
|
* |
23
|
|
|
* |
24
|
|
|
* @see: https://api.mattermost.com/v4/#tag/files%2Fpaths%2F~1files%2Fpost |
25
|
|
|
* |
26
|
|
|
* @return FileInfo|ResponseInterface |
27
|
|
|
*/ |
28
|
|
|
public function sendFile(string $fileContents, string $channelId, string $clientId = null) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
if (empty($fileContents)) { |
31
|
|
|
throw new InvalidArgumentException('File contents can not be empty'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (empty($channelId)) { |
35
|
|
|
throw new InvalidArgumentException('ChannelID can not be empty'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
$headers = []; |
40
|
|
|
$multipartStreamBuilder = new MultipartStreamBuilder(); |
41
|
|
|
|
42
|
|
|
// Add channelID |
43
|
|
|
$multipartStreamBuilder->addResource('channel_id', $channelId); |
44
|
|
|
// Add file contents |
45
|
|
|
$multipartStreamBuilder->addResource('files', base64_encode(substr($fileContents, 0, 100))); |
46
|
|
|
// Add client id |
47
|
|
|
$multipartStreamBuilder->addResource('client_ids', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'); |
48
|
|
|
|
49
|
|
|
$multipartStream = $multipartStreamBuilder->build(); |
50
|
|
|
$headers['Content-Type'] = 'multipart/form-data; boundary='.$multipartStreamBuilder->getBoundary(); |
51
|
|
|
$multipartStreamBuilder->reset(); |
52
|
|
|
|
53
|
|
|
//var_dump($fileContents, $multipartStream->getContents(), $headers); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$response = $this->httpPostRaw(sprintf('/files', $channelId), $multipartStream, $headers); |
56
|
|
|
|
57
|
|
|
var_dump($response->getBody()->getContents()); |
|
|
|
|
58
|
|
|
|
59
|
|
|
// return $this->handleResponse($response, Posts::class); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a file metadata. |
65
|
|
|
* |
66
|
|
|
* @param string $fileId |
67
|
|
|
* |
68
|
|
|
* @see https://api.mattermost.com/v4/#tag/files%2Fpaths%2F~1files~1%7Bfile_id%7D~1info%2Fget |
69
|
|
|
* |
70
|
|
|
* @return FileMetadata|ResponseInterface |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function getFileMetadata(string $fileId) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
if (empty($fileId)) { |
75
|
|
|
throw new InvalidArgumentException('FileID can not be empty'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$response = $this->httpGet(sprintf('/files/%s', $fileId)); |
79
|
|
|
|
80
|
|
|
return $this->handleResponse($response, FileMetadata::class); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: