file upload support for notes (single + multi)
[shpub.git] / src / shpub / Command / Note.php
index a60422aad16479498883b2d7b79523aaeaa9b250..d7a709ed164c51be8306e2d80ef8dce8d7c36a2a 100644 (file)
@@ -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";