Graby\Graby::handleMimeAction PHP Method

handleMimeAction() private method

These action can be exclude or link to handle custom content (like image, video, pdf, etc ..).
private handleMimeAction ( array $mimeInfo, string $effectiveUrl, string $body = '' ) : array | null
$mimeInfo array From getMimeActionInfo() function
$effectiveUrl string Current content url
$body string Content from the response
return array | null
    private function handleMimeAction($mimeInfo, $effectiveUrl, $body = '')
    {
        if (!isset($mimeInfo['action'])) {
            return;
        }
        $infos = array('status' => 200, 'title' => $mimeInfo['name'], 'language' => '', 'html' => '', 'url' => $effectiveUrl, 'content_type' => $mimeInfo['mime'], 'open_graph' => array());
        switch ($mimeInfo['action']) {
            case 'exclude':
                throw new \Exception(sprintf('This is url "%s" is blocked by mime action.', $effectiveUrl));
            case 'link':
                $infos['html'] = '<a href="' . $effectiveUrl . '">Download ' . $mimeInfo['name'] . '</a>';
                if ($mimeInfo['type'] == 'image') {
                    $infos['html'] = '<a href="' . $effectiveUrl . '"><img src="' . $effectiveUrl . '" alt="' . $mimeInfo['name'] . '" /></a>';
                }
                if ($mimeInfo['mime'] == 'application/pdf') {
                    $parser = new PdfParser();
                    $pdf = $parser->parseFile($effectiveUrl);
                    // tiny hack to avoid character like �
                    $html = mb_convert_encoding(nl2br($pdf->getText()), 'UTF-8', 'UTF-8');
                    // strip away unwanted chars (that usualy came from PDF extracted content)
                    // @see http://www.phpwact.org/php/i18n/charsets#common_problem_areas_with_utf-8
                    $html = preg_replace('/[^\\x{0009}\\x{000a}\\x{000d}\\x{0020}-\\x{D7FF}\\x{E000}-\\x{FFFD}]+/u', ' ', $html);
                    $infos['html'] = $html;
                    // update title in case of details are present
                    $details = $pdf->getDetails();
                    // Title can be a string or an array with one key
                    if (isset($details['Title'])) {
                        if (is_array($details['Title']) && isset($details['Title'][0]) && '' !== trim($details['Title'][0])) {
                            $infos['title'] = $details['Title'][0];
                        } elseif (is_string($details['Title']) && '' !== trim($details['Title'])) {
                            $infos['title'] = $details['Title'];
                        }
                    }
                }
                if ($mimeInfo['mime'] == 'text/plain') {
                    $infos['html'] = '<pre>' . $body . '</pre>';
                }
                return $infos;
        }
        return;
    }