X-Git-Url: https://git.cweiske.de/shpub.git/blobdiff_plain/05d4ee4b5b3b3495d2e47a11643d11a4de73b80d..059c6ab77b8e63210b9afc266d62c9f543d4d7ae:/src/shpub/Command/Note.php diff --git a/src/shpub/Command/Note.php b/src/shpub/Command/Note.php index b5df8ba..c7683f3 100644 --- a/src/shpub/Command/Note.php +++ b/src/shpub/Command/Note.php @@ -1,42 +1,12 @@ cfg = $cfg; - } - public static function opts(\Console_CommandLine $optParser) { $cmd = $optParser->addCommand('note'); - $cmd->addOption( - 'files', - array( - 'short_name' => '-f', - 'long_name' => '--files', - 'description' => 'Files to upload', - 'help_name' => 'PATH', - 'action' => 'StoreArray', - 'default' => [], - ) - ); - $cmd->addOption( - 'published', - array( - 'long_name' => '--published', - 'description' => 'Publish date', - 'help_name' => 'DATE', - 'action' => 'StoreString', - 'default' => null, - ) - ); + static::optsGeneric($cmd); $cmd->addArgument( 'text', [ @@ -47,79 +17,17 @@ class Command_Note ); } - public function run($command) + public function run(\Console_CommandLine_Result $cmdRes) { $req = new Request($this->cfg->host, $this->cfg); - $req->req->addPostParameter('h', 'entry'); - $req->req->addPostParameter('content', $command->args['text']); - if ($command->options['published'] !== null) { - $req->req->addPostParameter( - 'published', $command->options['published'] - ); - } - - $files = $command->options['files']; - $fileList = $urlList = [ - 'audio' => [], - 'image' => [], - 'video' => [], - ]; - - foreach ($files as $filePath) { - if (strpos($filePath, '://') !== false) { - //url - $mte = new \MIME_Type_Extension(); - $mimetype = $mte->getMIMEType($filePath); - $media = \MIME_Type::getMedia($mimetype); - if (!isset($urlList[$media])) { - Log::err('File type not allowed: ' . $mimetype); - exit(20); - } - $urlList[$media][] = $filePath; - } else if (file_exists($filePath)) { - //file - $mimetype = \MIME_Type::autoDetect($filePath); - $media = \MIME_Type::getMedia($mimetype); - if (!isset($urlList[$media])) { - Log::err('File type not allowed: ' . $mimetype); - exit(20); - } - $fileList[$media][] = $filePath; - } else { - Log::err('File does not exist: ' . $filePath); - exit(20); - } - } - foreach ($urlList as $type => $urls) { - if ($type == 'image') { - $type = 'photo'; - } - if (count($urls) == 1) { - $req->req->addPostParameter($type, reset($urls)); - } else if (count($urls) > 1) { - $n = 0; - foreach ($urls as $url) { - $req->req->addPostParameter( - $type . '[' . $n++ . ']', reset($urls) - ); - } - } - } - foreach ($fileList as $type => $filePaths) { - if ($type == 'image') { - $type = 'photo'; - } - if (count($filePaths) == 1) { - $req->addUpload($type, reset($filePaths)); - } else if (count($filePaths) > 0) { - $req->addUpload($type, $filePaths); - } - } + $req->setType('entry'); + $req->addProperty('content', $cmdRes->args['text']); + $this->handleGenericOptions($cmdRes, $req); $res = $req->send(); $postUrl = $res->getHeader('Location'); - echo "Post created at server\n"; - echo $postUrl . "\n"; + Log::info('Post created at server'); + Log::msg($postUrl); } } ?>