123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php
- namespace Typemill\Controllers;
- use Slim\Http\Request;
- use Slim\Http\Response;
- use Typemill\Models\ProcessImage;
- use Typemill\Models\ProcessFile;
- use Typemill\Controllers\BlockApiController;
- use \URLify;
- class MediaApiController extends ContentController
- {
- public function getMediaLibImages(Request $request, Response $response, $args)
- {
- # get params from call
- $this->params = $request->getParams();
- $this->uri = $request->getUri();
- $imageProcessor = new ProcessImage($this->settings['images']);
- if(!$imageProcessor->checkFolders('images'))
- {
- return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
- }
-
- $imagelist = $imageProcessor->scanMediaFlat();
- return $response->withJson(['images' => $imagelist]);
- }
- public function getMediaLibFiles(Request $request, Response $response, $args)
- {
- # get params from call
- $this->params = $request->getParams();
- $this->uri = $request->getUri();
- $fileProcessor = new ProcessFile();
- if(!$fileProcessor->checkFolders())
- {
- return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
- }
-
- $filelist = $fileProcessor->scanFilesFlat();
- return $response->withJson(['files' => $filelist]);
- }
- public function getImage(Request $request, Response $response, $args)
- {
- # get params from call
- $this->params = $request->getParams();
- $this->uri = $request->getUri();
- $this->setStructure($draft = true, $cache = false);
- $imageProcessor = new ProcessImage($this->settings['images']);
- if(!$imageProcessor->checkFolders('images'))
- {
- return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
- }
- $imageDetails = $imageProcessor->getImageDetails($this->params['name'], $this->structure);
-
- if($imageDetails)
- {
- return $response->withJson(['image' => $imageDetails]);
- }
-
- return $response->withJson(['errors' => 'Image not found or image name not valid.'], 404);
- }
- public function getFile(Request $request, Response $response, $args)
- {
- # get params from call
- $this->params = $request->getParams();
- $this->uri = $request->getUri();
- $this->setStructure($draft = true, $cache = false);
- $fileProcessor = new ProcessFile();
- if(!$fileProcessor->checkFolders())
- {
- return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
- }
- $fileDetails = $fileProcessor->getFileDetails($this->params['name'], $this->structure);
- if($fileDetails)
- {
- return $response->withJson(['file' => $fileDetails]);
- }
- return $response->withJson(['errors' => 'file not found or file name invalid'],404);
- }
- public function createImage(Request $request, Response $response, $args)
- {
- # get params from call
- $this->params = $request->getParams();
- $this->uri = $request->getUri();
-
- $imageProcessor = new ProcessImage($this->settings['images']);
-
- if(!$imageProcessor->checkFolders('images'))
- {
- return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
- }
- if($imageProcessor->createImage($this->params['image'], $this->params['name'], $this->settings['images']))
- {
- return $response->withJson(['name' => 'media/live/' . $imageProcessor->getFullName(),'errors' => false]);
- }
- return $response->withJson(['errors' => 'could not store image to temporary folder']);
- }
- public function uploadFile(Request $request, Response $response, $args)
- {
- # get params from call
- $this->params = $request->getParams();
- $this->uri = $request->getUri();
- # make sure only allowed filetypes are uploaded
- $finfo = finfo_open( FILEINFO_MIME_TYPE );
- $mtype = finfo_file( $finfo, $this->params['file'] );
- finfo_close( $finfo );
- $allowedMimes = $this->getAllowedMtypes();
- if(!in_array($mtype, $allowedMimes))
- {
- return $response->withJson(array('errors' => 'File-type is not allowed'));
- }
- $fileProcessor = new ProcessFile();
- if(!$fileProcessor->checkFolders())
- {
- return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
- }
-
- $fileinfo = $fileProcessor->storeFile($this->params['file'], $this->params['name']);
- if($fileinfo)
- {
- return $response->withJson(['errors' => false, 'info' => $fileinfo]);
- }
- return $response->withJson(['errors' => 'could not store file to temporary folder'],500);
- }
-
- public function publishImage(Request $request, Response $response, $args)
- {
- $params = $request->getParsedBody();
- $imageProcessor = new ProcessImage($this->settings['images']);
- if(!$imageProcessor->checkFolders())
- {
- return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
- }
-
- if($imageProcessor->publishImage())
- {
- $request = $request->withParsedBody($params);
-
- $block = new BlockApiController($this->c);
- if($params['new'])
- {
- return $block->addBlock($request, $response, $args);
- }
- return $block->updateBlock($request, $response, $args);
- }
- return $response->withJson(['errors' => 'could not store image to media folder'],500);
- }
- public function publishFile(Request $request, Response $response, $args)
- {
- $params = $request->getParsedBody();
- $fileProcessor = new ProcessFile();
- if(!$fileProcessor->checkFolders())
- {
- return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
- }
-
- if($fileProcessor->publishFile())
- {
- $request = $request->withParsedBody($params);
-
- $block = new BlockApiController($this->c);
- if($params['new'])
- {
- return $block->addBlock($request, $response, $args);
- }
- return $block->updateBlock($request, $response, $args);
- }
- return $response->withJson(['errors' => 'could not store file to media folder'],500);
- }
- public function deleteImage(Request $request, Response $response, $args)
- {
- # get params from call
- $this->params = $request->getParams();
- $this->uri = $request->getUri();
- if(!isset($this->params['name']))
- {
- return $response->withJson(['errors' => 'image name is missing'],500);
- }
- $imageProcessor = new ProcessImage($this->settings['images']);
- if(!$imageProcessor->checkFolders())
- {
- return $response->withJson(['errors' => 'Please check if your media-folder exists and all folders inside are writable.'], 500);
- }
- if($imageProcessor->deleteImage($this->params['name']))
- {
- return $response->withJson(['errors' => false]);
- }
- return $response->withJson(['errors' => 'Oops, looks like we could not delete all sizes of that image.'], 500);
- }
- public function deleteFile(Request $request, Response $response, $args)
- {
- # get params from call
- $this->params = $request->getParams();
- $this->uri = $request->getUri();
- if(!isset($this->params['name']))
- {
- return $response->withJson(['errors' => 'file name is missing'],500);
- }
- $fileProcessor = new ProcessFile();
- if($fileProcessor->deleteFile($this->params['name']))
- {
- return $response->withJson(['errors' => false]);
- }
- return $response->withJson(['errors' => 'could not delete the file'],500);
- }
- public function saveVideoImage(Request $request, Response $response, $args)
- {
- /* get params from call */
- $this->params = $request->getParams();
- $this->uri = $request->getUri();
- $class = false;
- $imageUrl = $this->params['markdown'];
-
- if(strpos($imageUrl, 'https://www.youtube.com/watch?v=') !== false)
- {
- $videoID = str_replace('https://www.youtube.com/watch?v=', '', $imageUrl);
- $videoID = strpos($videoID, '&') ? substr($videoID, 0, strpos($videoID, '&')) : $videoID;
- $class = 'youtube';
- }
- if(strpos($imageUrl, 'https://youtu.be/') !== false)
- {
- $videoID = str_replace('https://youtu.be/', '', $imageUrl);
- $videoID = strpos($videoID, '?') ? substr($videoID, 0, strpos($videoID, '?')) : $videoID;
- $class = 'youtube';
- }
-
- if($class == 'youtube')
- {
- $videoURLmaxres = 'https://i1.ytimg.com/vi/' . $videoID . '/maxresdefault.jpg';
- $videoURL0 = 'https://i1.ytimg.com/vi/' . $videoID . '/0.jpg';
- }
- $ctx = stream_context_create(array(
- 'https' => array(
- 'timeout' => 1
- )
- )
- );
-
- $imageData = @file_get_contents($videoURLmaxres, 0, $ctx);
- if($imageData === false)
- {
- $imageData = @file_get_contents($videoURL0, 0, $ctx);
- if($imageData === false)
- {
- return $response->withJson(array('errors' => 'could not get the video image'));
- }
- }
-
- $imageData64 = 'data:image/jpeg;base64,' . base64_encode($imageData);
- $desiredSizes = ['live' => ['width' => 560, 'height' => 315]];
- $imageProcessor = new ProcessImage($this->settings['images']);
- if(!$imageProcessor->checkFolders())
- {
- return $response->withJson(['errors' => ['message' => 'Please check if your media-folder exists and all folders inside are writable.']], 500);
- }
- $tmpImage = $imageProcessor->createImage($imageData64, $desiredSizes);
-
- if(!$tmpImage)
- {
- return $response->withJson(array('errors' => 'could not create temporary image'));
- }
-
- $imageUrl = $imageProcessor->publishImage($desiredSizes, $videoID);
- if($imageUrl)
- {
- $this->params['markdown'] = '{#' . $videoID. ' .' . $class . '}';
- $request = $request->withParsedBody($this->params);
-
- $block = new BlockApiController($this->c);
- if($params['new'])
- {
- return $block->addBlock($request, $response, $args);
- }
- return $block->updateBlock($request, $response, $args);
- }
-
- return $response->withJson(array('errors' => 'could not store the preview image'));
- }
- private function getAllowedMtypes()
- {
- return array(
- 'application/zip',
- 'application/gzip',
- 'application/vnd.rar',
- 'application/vnd.visio',
- 'application/vnd.ms-excel',
- 'application/vnd.ms-powerpoint',
- 'application/vnd.ms-word.document.macroEnabled.12',
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
- 'application/vnd.apple.keynote',
- 'application/vnd.apple.mpegurl',
- 'application/vnd.apple.numbers',
- 'application/vnd.apple.pages',
- 'application/vnd.amazon.mobi8-ebook',
- 'application/epub+zip',
- 'application/pdf',
- 'image/png',
- 'image/jpeg',
- 'image/gif',
- 'image/svg+xml',
- 'font/*',
- 'audio/mpeg',
- 'audio/mp4',
- 'audio/ogg',
- 'video/mpeg',
- 'video/mp4',
- 'video/ogg',
- );
- }
- }
|