Add upload command
authorChristian Weiske <cweiske@cweiske.de>
Tue, 20 Sep 2016 08:41:40 +0000 (10:41 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 20 Sep 2016 08:41:40 +0000 (10:41 +0200)
src/shpub/Cli.php
src/shpub/Command/Upload.php [new file with mode: 0644]
src/shpub/Request.php

index 4507155..5689851 100644 (file)
@@ -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 (file)
index 0000000..0a667a1
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+namespace shpub;
+
+class Command_Upload
+{
+    public function __construct(Config $cfg)
+    {
+        $this->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);
+        }
+    }
+}
+?>
index 3a4512e..68db98d 100644 (file)
@@ -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