11 public function __construct($cfg)
16 public static function opts(\Console_CommandLine $optParser)
18 $cmd = $optParser->addCommand('note');
23 'long_name' => '--files',
24 'description' => 'Files to upload',
25 'help_name' => 'PATH',
26 'action' => 'StoreArray',
33 'long_name' => '--published',
34 'description' => 'Publish date',
35 'help_name' => 'DATE',
36 'action' => 'StoreString',
45 'description' => 'Post text',
50 public function run($command)
52 $req = new Request($this->cfg->host, $this->cfg);
53 $req->req->addPostParameter('h', 'entry');
54 $req->req->addPostParameter('content', $command->args['text']);
55 if ($command->options['published'] !== null) {
56 $req->req->addPostParameter(
57 'published', $command->options['published']
61 $files = $command->options['files'];
62 $fileList = $urlList = [
68 foreach ($files as $filePath) {
69 if (strpos($filePath, '://') !== false) {
71 $mte = new \MIME_Type_Extension();
72 $mimetype = $mte->getMIMEType($filePath);
73 $media = \MIME_Type::getMedia($mimetype);
74 if (!isset($urlList[$media])) {
75 Log::err('File type not allowed: ' . $mimetype);
78 $urlList[$media][] = $filePath;
79 } else if (file_exists($filePath)) {
81 $mimetype = \MIME_Type::autoDetect($filePath);
82 $media = \MIME_Type::getMedia($mimetype);
83 if (!isset($urlList[$media])) {
84 Log::err('File type not allowed: ' . $mimetype);
87 $fileList[$media][] = $filePath;
89 Log::err('File does not exist: ' . $filePath);
93 foreach ($urlList as $type => $urls) {
94 if ($type == 'image') {
97 if (count($urls) == 1) {
98 $req->req->addPostParameter($type, reset($urls));
99 } else if (count($urls) > 1) {
101 foreach ($urls as $url) {
102 $req->req->addPostParameter(
103 $type . '[' . $n++ . ']', reset($urls)
108 foreach ($fileList as $type => $filePaths) {
109 if ($type == 'image') {
112 if (count($filePaths) == 1) {
113 $req->addUpload($type, reset($filePaths));
114 } else if (count($filePaths) > 0) {
115 $req->addUpload($type, $filePaths);
120 $postUrl = $res->getHeader('Location');
121 echo "Post created at server\n";
122 echo $postUrl . "\n";