From 9f81b10b061382886a9c7e21ca941c11a6e4bee1 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 20 Sep 2016 10:41:40 +0200 Subject: [PATCH] Add upload command --- src/shpub/Cli.php | 2 ++ src/shpub/Command/Upload.php | 46 ++++++++++++++++++++++++++++++++++++ src/shpub/Request.php | 2 +- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/shpub/Command/Upload.php diff --git a/src/shpub/Cli.php b/src/shpub/Cli.php index 4507155..5689851 100644 --- a/src/shpub/Cli.php +++ b/src/shpub/Cli.php @@ -130,6 +130,8 @@ class Cli Command_Undelete::opts($optParser); Command_Update::opts($optParser); + Command_Upload::opts($optParser); + return $optParser; } diff --git a/src/shpub/Command/Upload.php b/src/shpub/Command/Upload.php new file mode 100644 index 0000000..0a667a1 --- /dev/null +++ b/src/shpub/Command/Upload.php @@ -0,0 +1,46 @@ +cfg = $cfg; + } + + public static function opts(\Console_CommandLine $optParser) + { + $cmd = $optParser->addCommand('upload'); + $cmd->description = 'Directly upload files to the media endpoint'; + $cmd->addArgument( + 'files', + [ + 'optional' => false, + 'multiple' => true, + 'description' => 'File paths', + ] + ); + } + + public function run(\Console_CommandLine_Result $cmdRes) + { + if ($this->cfg->host->endpoints->media == '') { + Log::err('Host as no media endpoint'); + exit(20); + } + + $req = new Request($this->cfg->host, $this->cfg); + + foreach ($cmdRes->args['files'] as $filePath) { + if (!file_exists($filePath)) { + Log::err('File does not exist: ' . $filePath); + exit(20); + } + + $url = $req->uploadToMediaEndpoint($filePath); + Log::info('Uploaded file ' . $filePath); + Log::msg($url); + } + } +} +?> diff --git a/src/shpub/Request.php b/src/shpub/Request.php index 3a4512e..68db98d 100644 --- a/src/shpub/Request.php +++ b/src/shpub/Request.php @@ -154,7 +154,7 @@ class Request /** * @return string URL at media endpoint */ - protected function uploadToMediaEndpoint($fileName) + public function uploadToMediaEndpoint($fileName) { $httpReq = $this->getHttpRequest( $this->host->endpoints->media, $this->host->token -- 2.30.2