1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slack-cli package. |
5
|
|
|
* |
6
|
|
|
* (c) Cas Leentfaar <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CL\SlackCli\Command; |
13
|
|
|
|
14
|
|
|
use CL\Slack\Payload\FilesUploadPayload; |
15
|
|
|
use CL\Slack\Payload\FilesUploadPayloadResponse; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Cas Leentfaar <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class FilesUploadCommand extends AbstractApiCommand |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
parent::configure(); |
29
|
|
|
|
30
|
|
|
$this->setName('files:upload'); |
31
|
|
|
$this->setDescription('Create or upload an existing file to Slack'); |
32
|
|
|
$this->addOption('path', 'p', InputOption::VALUE_REQUIRED, 'The path to the file to upload'); |
33
|
|
|
$this->addOption('content', 'c', InputOption::VALUE_REQUIRED, 'The raw content of the file to upload (alternative for `--path`)'); |
34
|
|
|
$this->addOption('filetype', 'ft', InputOption::VALUE_REQUIRED, 'Slack-internal file type identifier (e.g. `php`)'); |
35
|
|
|
$this->addOption('filename', 'fn', InputOption::VALUE_REQUIRED, 'Filename of the file'); |
36
|
|
|
$this->addOption('title', null, InputOption::VALUE_REQUIRED, 'Title of the file'); |
37
|
|
|
$this->addOption('initial-comment', null, InputOption::VALUE_REQUIRED, 'Initial comment to add to the file'); |
38
|
|
|
$this->addOption('channels', null, InputOption::VALUE_REQUIRED, 'Comma-separated list of channel IDs to share the file into'); |
39
|
|
|
$this->setHelp(<<<EOT |
40
|
|
|
The <info>files:upload</info> command allows you to create or upload an existing file. |
41
|
|
|
|
42
|
|
|
The type of data in the file will be intuited from the filename and the magic bytes in the file, for supported formats. |
43
|
|
|
Using the `--filetype` option will override this behavior (if a valid type is given). |
44
|
|
|
|
45
|
|
|
The file can also be shared directly into channels on upload, by specifying the `--channels` option. |
46
|
|
|
Channel IDs should be comma separated if there is more than one. |
47
|
|
|
|
48
|
|
|
For more information about the related API method, check out the official documentation: |
49
|
|
|
<comment>https://api.slack.com/methods/files.upload</comment> |
50
|
|
|
EOT |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return FilesUploadPayload |
56
|
|
|
*/ |
57
|
|
|
protected function createPayload() |
58
|
|
|
{ |
59
|
|
|
$payload = new FilesUploadPayload(); |
60
|
|
|
|
61
|
|
|
if ($this->input->getOption('path')) { |
62
|
|
|
$content = file_get_contents($this->input->getOption('path')); |
63
|
|
|
} elseif ($this->input->getOption('content')) { |
64
|
|
|
$content = $this->input->getOption('content'); |
65
|
|
|
} else { |
66
|
|
|
throw new \LogicException('Either the `--path` or the `--content` option must be used'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$payload->setContent($content); |
70
|
|
|
$payload->setChannels($this->input->getOption('channels')); |
71
|
|
|
$payload->setFilename($this->input->getOption('filename')); |
72
|
|
|
$payload->setFileType($this->input->getOption('filetype')); |
73
|
|
|
$payload->setTitle($this->input->getOption('title')); |
74
|
|
|
|
75
|
|
|
return $payload; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
* |
81
|
|
|
* @param FilesUploadPayloadResponse $payloadResponse |
82
|
|
|
*/ |
83
|
|
|
protected function handleResponse($payloadResponse) |
84
|
|
|
{ |
85
|
|
|
if ($payloadResponse->isOk()) { |
86
|
|
|
$this->writeOk('Successfully upload file to Slack:'); |
87
|
|
|
$file = $payloadResponse->getFile(); |
88
|
|
|
$this->renderKeyValueTable($file); |
89
|
|
|
} else { |
90
|
|
|
$this->writeError(sprintf('Failed to upload file: %s', lcfirst($payloadResponse->getErrorExplanation()))); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|