From 03c6c0364363b228d1f651a881c36017099dda9d Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 8 Sep 2016 21:08:22 +0200 Subject: [PATCH] file upload support for notes (single + multi) --- src/shpub/Command/Note.php | 43 ++++++++++++++++++++++++++------ src/shpub/MyHttpRequest2.php | 10 ++++++++ src/shpub/Request.php | 48 +++++++++++++++++++++++++++++++++--- 3 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 src/shpub/MyHttpRequest2.php diff --git a/src/shpub/Command/Note.php b/src/shpub/Command/Note.php index a60422a..d7a709e 100644 --- a/src/shpub/Command/Note.php +++ b/src/shpub/Command/Note.php @@ -16,6 +16,17 @@ class Command_Note 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->addArgument( 'text', [ @@ -28,15 +39,33 @@ class Command_Note public function run($command) { - $data = [ - 'h' => 'entry', - 'content' => $command->args['text'], - ]; + $req = new Request($this->cfg->host, $this->cfg); + $req->req->addPostParameter('h', 'entry'); + $req->req->addPostParameter('content', $command->args['text']); - $body = http_build_query($data); + $files = $command->options['files']; + $fileList = [ + 'audio' => [], + 'photo' => [], + 'video' => [], + ]; + foreach ($files as $filePath) { + if (!file_exists($filePath)) { + Log::err('File does not exist: ' . $filePath); + exit(20); + } + $type = 'photo'; + $fileList[$type][] = $filePath; + } + foreach ($fileList as $type => $filePaths) { + if (count($filePaths) == 1) { + $req->addUpload($type, reset($filePaths)); + } else if (count($filePaths) > 0) { + $req->addUpload($type, $filePaths); + } + } - $req = new Request($this->cfg->host, $this->cfg); - $res = $req->send($body); + $res = $req->send(); $postUrl = $res->getHeader('Location'); echo "Post created at server\n"; echo $postUrl . "\n"; diff --git a/src/shpub/MyHttpRequest2.php b/src/shpub/MyHttpRequest2.php new file mode 100644 index 0000000..8b8b44d --- /dev/null +++ b/src/shpub/MyHttpRequest2.php @@ -0,0 +1,10 @@ +postParams; + } +} \ No newline at end of file diff --git a/src/shpub/Request.php b/src/shpub/Request.php index 37dd6ab..44894c5 100644 --- a/src/shpub/Request.php +++ b/src/shpub/Request.php @@ -6,10 +6,12 @@ class Request public $req; public $cfg; + protected $uploadsInfo = []; + public function __construct($host, $cfg) { $this->cfg = $cfg; - $this->req = new \HTTP_Request2($host->endpoints->micropub, 'POST'); + $this->req = new MyHttpRequest2($host->endpoints->micropub, 'POST'); $this->req->setHeader('User-Agent: shpub'); if (version_compare(PHP_VERSION, '5.6.0', '<')) { //correct ssl validation on php 5.5 is a pain, so disable @@ -20,9 +22,11 @@ class Request $this->req->setHeader('authorization', 'Bearer ' . $host->token); } - public function send($body) + public function send($body = null) { - $this->req->setBody($body); + if ($body !== null) { + $this->req->setBody($body); + } if ($this->cfg->debug) { $this->printCurl(); } @@ -38,6 +42,17 @@ class Request return $res; } + /** + * @param string $fieldName name of file-upload field + * @param string|resource|array $filename full name of local file, + * pointer to open file or an array of files + */ + public function addUpload($fieldName, $filename) + { + $this->uploadsInfo[$fieldName] = $filename; + return $this->req->addUpload($fieldName, $filename); + } + protected function printCurl() { $command = 'curl'; @@ -48,7 +63,32 @@ class Request $caseKey = implode('-', array_map('ucfirst', explode('-', $key))); $command .= ' -H ' . escapeshellarg($caseKey . ': ' . $val); } - $command .= ' --data ' . escapeshellarg($this->req->getBody()); + + $postParams = $this->req->getPostParams(); + + if (count($this->uploadsInfo) == 0) { + foreach ($postParams as $k => $v) { + $command .= ' -d ' . escapeshellarg($k . '=' . $v); + } + } else { + foreach ($postParams as $k => $v) { + $command .= ' -F ' . escapeshellarg($k . '=' . $v); + } + foreach ($this->uploadsInfo as $fieldName => $filename) { + if (!is_array($filename)) { + $command .= ' -F ' . escapeshellarg( + $fieldName . '=@' . $filename + ); + } else { + foreach ($filename as $k => $realFilename) { + $command .= ' -F ' . escapeshellarg( + $fieldName . '[' . $k . ']=@' . $realFilename + ); + } + } + } + } + $command .= ' ' . escapeshellarg((string) $this->req->getUrl()); echo $command . "\n"; -- 2.30.2