From 05d4ee4b5b3b3495d2e47a11643d11a4de73b80d Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Sun, 11 Sep 2016 14:42:11 +0200 Subject: [PATCH] correctly determine file type --- build.xml | 2 ++ src/shpub/Command/Note.php | 43 +++++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/build.xml b/build.xml index dfd3c8c..1a58072 100644 --- a/build.xml +++ b/build.xml @@ -53,6 +53,7 @@ + @@ -61,6 +62,7 @@ + diff --git a/src/shpub/Command/Note.php b/src/shpub/Command/Note.php index d5231a9..b5df8ba 100644 --- a/src/shpub/Command/Note.php +++ b/src/shpub/Command/Note.php @@ -59,37 +59,56 @@ class Command_Note } $files = $command->options['files']; - $fileList = [ + $fileList = $urlList = [ 'audio' => [], - 'photo' => [], + 'image' => [], 'video' => [], ]; - $urls = []; + foreach ($files as $filePath) { - if (file_exists($filePath)) { - $type = 'photo'; - $fileList[$type][] = $filePath; - } else if (strpos($filePath, '://') !== false) { + if (strpos($filePath, '://') !== false) { //url - $urls[] = $filePath; + $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); } } - if (count($urls)) { + foreach ($urlList as $type => $urls) { + if ($type == 'image') { + $type = 'photo'; + } if (count($urls) == 1) { - $req->req->addPostParameter('photo', reset($urls)); - } else { + $req->req->addPostParameter($type, reset($urls)); + } else if (count($urls) > 1) { $n = 0; foreach ($urls as $url) { $req->req->addPostParameter( - 'photo[' . $n++ . ']', reset($urls) + $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) { -- 2.30.2