MediaApiController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace Typemill\Controllers;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Typemill\Models\ProcessImage;
  6. use Typemill\Models\ProcessFile;
  7. use Typemill\Controllers\BlockApiController;
  8. use \URLify;
  9. class MediaApiController extends ContentController
  10. {
  11. public function getMediaLibImages(Request $request, Response $response, $args)
  12. {
  13. # get params from call
  14. $this->params = $request->getParams();
  15. $this->uri = $request->getUri();
  16. $imageProcessor = new ProcessImage($this->settings['images']);
  17. if(!$imageProcessor->checkFolders('images'))
  18. {
  19. return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
  20. }
  21. $imagelist = $imageProcessor->scanMediaFlat();
  22. return $response->withJson(['images' => $imagelist]);
  23. }
  24. public function getMediaLibFiles(Request $request, Response $response, $args)
  25. {
  26. # get params from call
  27. $this->params = $request->getParams();
  28. $this->uri = $request->getUri();
  29. $fileProcessor = new ProcessFile();
  30. if(!$fileProcessor->checkFolders())
  31. {
  32. return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
  33. }
  34. $filelist = $fileProcessor->scanFilesFlat();
  35. return $response->withJson(['files' => $filelist]);
  36. }
  37. public function getImage(Request $request, Response $response, $args)
  38. {
  39. # get params from call
  40. $this->params = $request->getParams();
  41. $this->uri = $request->getUri();
  42. $this->setStructure($draft = true, $cache = false);
  43. $imageProcessor = new ProcessImage($this->settings['images']);
  44. if(!$imageProcessor->checkFolders('images'))
  45. {
  46. return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
  47. }
  48. $imageDetails = $imageProcessor->getImageDetails($this->params['name'], $this->structure);
  49. if($imageDetails)
  50. {
  51. return $response->withJson(['image' => $imageDetails]);
  52. }
  53. return $response->withJson(['errors' => 'Image not found or image name not valid.'], 404);
  54. }
  55. public function getFile(Request $request, Response $response, $args)
  56. {
  57. # get params from call
  58. $this->params = $request->getParams();
  59. $this->uri = $request->getUri();
  60. $this->setStructure($draft = true, $cache = false);
  61. $fileProcessor = new ProcessFile();
  62. if(!$fileProcessor->checkFolders())
  63. {
  64. return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
  65. }
  66. $fileDetails = $fileProcessor->getFileDetails($this->params['name'], $this->structure);
  67. if($fileDetails)
  68. {
  69. return $response->withJson(['file' => $fileDetails]);
  70. }
  71. return $response->withJson(['errors' => 'file not found or file name invalid'],404);
  72. }
  73. public function createImage(Request $request, Response $response, $args)
  74. {
  75. # get params from call
  76. $this->params = $request->getParams();
  77. $this->uri = $request->getUri();
  78. $imageProcessor = new ProcessImage($this->settings['images']);
  79. if(!$imageProcessor->checkFolders('images'))
  80. {
  81. return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
  82. }
  83. if($imageProcessor->createImage($this->params['image'], $this->params['name'], $this->settings['images']))
  84. {
  85. return $response->withJson(['name' => 'media/live/' . $imageProcessor->getFullName(),'errors' => false]);
  86. }
  87. return $response->withJson(['errors' => 'could not store image to temporary folder']);
  88. }
  89. public function uploadFile(Request $request, Response $response, $args)
  90. {
  91. # get params from call
  92. $this->params = $request->getParams();
  93. $this->uri = $request->getUri();
  94. # make sure only allowed filetypes are uploaded
  95. $finfo = finfo_open( FILEINFO_MIME_TYPE );
  96. $mtype = finfo_file( $finfo, $this->params['file'] );
  97. finfo_close( $finfo );
  98. $allowedMimes = $this->getAllowedMtypes();
  99. if(!in_array($mtype, $allowedMimes))
  100. {
  101. return $response->withJson(array('errors' => 'File-type is not allowed'));
  102. }
  103. $fileProcessor = new ProcessFile();
  104. if(!$fileProcessor->checkFolders())
  105. {
  106. return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
  107. }
  108. $fileinfo = $fileProcessor->storeFile($this->params['file'], $this->params['name']);
  109. if($fileinfo)
  110. {
  111. return $response->withJson(['errors' => false, 'info' => $fileinfo]);
  112. }
  113. return $response->withJson(['errors' => 'could not store file to temporary folder'],500);
  114. }
  115. public function publishImage(Request $request, Response $response, $args)
  116. {
  117. $params = $request->getParsedBody();
  118. $imageProcessor = new ProcessImage($this->settings['images']);
  119. if(!$imageProcessor->checkFolders())
  120. {
  121. return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
  122. }
  123. if($imageProcessor->publishImage())
  124. {
  125. $request = $request->withParsedBody($params);
  126. $block = new BlockApiController($this->c);
  127. if($params['new'])
  128. {
  129. return $block->addBlock($request, $response, $args);
  130. }
  131. return $block->updateBlock($request, $response, $args);
  132. }
  133. return $response->withJson(['errors' => 'could not store image to media folder'],500);
  134. }
  135. public function publishFile(Request $request, Response $response, $args)
  136. {
  137. $params = $request->getParsedBody();
  138. $fileProcessor = new ProcessFile();
  139. if(!$fileProcessor->checkFolders())
  140. {
  141. return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
  142. }
  143. if($fileProcessor->publishFile())
  144. {
  145. $request = $request->withParsedBody($params);
  146. $block = new BlockApiController($this->c);
  147. if($params['new'])
  148. {
  149. return $block->addBlock($request, $response, $args);
  150. }
  151. return $block->updateBlock($request, $response, $args);
  152. }
  153. return $response->withJson(['errors' => 'could not store file to media folder'],500);
  154. }
  155. public function deleteImage(Request $request, Response $response, $args)
  156. {
  157. # get params from call
  158. $this->params = $request->getParams();
  159. $this->uri = $request->getUri();
  160. if(!isset($this->params['name']))
  161. {
  162. return $response->withJson(['errors' => 'image name is missing'],500);
  163. }
  164. $imageProcessor = new ProcessImage($this->settings['images']);
  165. if(!$imageProcessor->checkFolders())
  166. {
  167. return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
  168. }
  169. if($imageProcessor->deleteImage($this->params['name']))
  170. {
  171. return $response->withJson(['errors' => false]);
  172. }
  173. return $response->withJson(['errors' => 'Oops, looks like we could not delete all sizes of that image.'], 500);
  174. }
  175. public function deleteFile(Request $request, Response $response, $args)
  176. {
  177. # get params from call
  178. $this->params = $request->getParams();
  179. $this->uri = $request->getUri();
  180. if(!isset($this->params['name']))
  181. {
  182. return $response->withJson(['errors' => 'file name is missing'],500);
  183. }
  184. $fileProcessor = new ProcessFile();
  185. if($fileProcessor->deleteFile($this->params['name']))
  186. {
  187. return $response->withJson(['errors' => false]);
  188. }
  189. return $response->withJson(['errors' => 'could not delete the file'],500);
  190. }
  191. public function saveVideoImage(Request $request, Response $response, $args)
  192. {
  193. /* get params from call */
  194. $this->params = $request->getParams();
  195. $this->uri = $request->getUri();
  196. $class = false;
  197. $imageUrl = $this->params['markdown'];
  198. if(strpos($imageUrl, 'https://www.youtube.com/watch?v=') !== false)
  199. {
  200. $videoID = str_replace('https://www.youtube.com/watch?v=', '', $imageUrl);
  201. $videoID = strpos($videoID, '&') ? substr($videoID, 0, strpos($videoID, '&')) : $videoID;
  202. $class = 'youtube';
  203. }
  204. if(strpos($imageUrl, 'https://youtu.be/') !== false)
  205. {
  206. $videoID = str_replace('https://youtu.be/', '', $imageUrl);
  207. $videoID = strpos($videoID, '?') ? substr($videoID, 0, strpos($videoID, '?')) : $videoID;
  208. $class = 'youtube';
  209. }
  210. if($class == 'youtube')
  211. {
  212. $videoURLmaxres = 'https://i1.ytimg.com/vi/' . $videoID . '/maxresdefault.jpg';
  213. $videoURL0 = 'https://i1.ytimg.com/vi/' . $videoID . '/0.jpg';
  214. }
  215. $ctx = stream_context_create(array(
  216. 'https' => array(
  217. 'timeout' => 1
  218. )
  219. )
  220. );
  221. $imageData = @file_get_contents($videoURLmaxres, 0, $ctx);
  222. if($imageData === false)
  223. {
  224. $imageData = @file_get_contents($videoURL0, 0, $ctx);
  225. if($imageData === false)
  226. {
  227. return $response->withJson(array('errors' => 'could not get the video image'));
  228. }
  229. }
  230. $imageData64 = 'data:image/jpeg;base64,' . base64_encode($imageData);
  231. $desiredSizes = ['live' => ['width' => 560, 'height' => 315]];
  232. $imageProcessor = new ProcessImage($this->settings['images']);
  233. if(!$imageProcessor->checkFolders())
  234. {
  235. return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
  236. }
  237. $tmpImage = $imageProcessor->createImage($imageData64, $desiredSizes);
  238. if(!$tmpImage)
  239. {
  240. return $response->withJson(array('errors' => 'could not create temporary image'));
  241. }
  242. $imageUrl = $imageProcessor->publishImage($desiredSizes, $videoID);
  243. if($imageUrl)
  244. {
  245. $this->params['markdown'] = '![' . $class . '-video](' . $imageUrl . ' "click to load video"){#' . $videoID. ' .' . $class . '}';
  246. $request = $request->withParsedBody($this->params);
  247. $block = new BlockApiController($this->c);
  248. if($params['new'])
  249. {
  250. return $block->addBlock($request, $response, $args);
  251. }
  252. return $block->updateBlock($request, $response, $args);
  253. }
  254. return $response->withJson(array('errors' => 'could not store the preview image'));
  255. }
  256. private function getAllowedMtypes()
  257. {
  258. return array(
  259. 'application/zip',
  260. 'application/gzip',
  261. 'application/vnd.rar',
  262. 'application/vnd.visio',
  263. 'application/vnd.ms-excel',
  264. 'application/vnd.ms-powerpoint',
  265. 'application/vnd.ms-word.document.macroEnabled.12',
  266. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  267. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  268. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  269. 'application/vnd.apple.keynote',
  270. 'application/vnd.apple.mpegurl',
  271. 'application/vnd.apple.numbers',
  272. 'application/vnd.apple.pages',
  273. 'application/vnd.amazon.mobi8-ebook',
  274. 'application/epub+zip',
  275. 'application/pdf',
  276. 'image/png',
  277. 'image/jpeg',
  278. 'image/gif',
  279. 'image/svg+xml',
  280. 'font/*',
  281. 'audio/mpeg',
  282. 'audio/mp4',
  283. 'audio/ogg',
  284. 'video/mpeg',
  285. 'video/mp4',
  286. 'video/ogg',
  287. );
  288. }
  289. }