From 7c668787c5b4341faa013614235f35283958ee57 Mon Sep 17 00:00:00 2001 From: Jon Gilkison Date: Tue, 25 Apr 2017 19:47:26 +0700 Subject: [PATCH] Fixes for importing. --- classes/tasks/ilab-s3-import-process.php | 82 ++++++++++++++++++- classes/tasks/wp-background-process.php | 2 +- classes/tools/imgix/ilab-media-imgix-tool.php | 55 +++++++++++++ classes/tools/s3/ilab-media-s3-tool.php | 61 ++++++++++++-- composer.json | 3 +- 5 files changed, 191 insertions(+), 12 deletions(-) diff --git a/classes/tasks/ilab-s3-import-process.php b/classes/tasks/ilab-s3-import-process.php index e21c07c9..f27b4cee 100644 --- a/classes/tasks/ilab-s3-import-process.php +++ b/classes/tasks/ilab-s3-import-process.php @@ -26,24 +26,98 @@ class ILABS3ImportProcess extends WP_Background_Process { protected $action = 'ilab_s3_import_process'; - protected function task($item) { + public function task($item) { $index = $item['index']; $post_id = $item['post']; + $isDocument = false; + update_option('ilab_s3_import_current', $index+1); $data = wp_get_attachment_metadata($post_id); + if ($data) { + error_log("IMPORT DATA: ".json_encode($data, JSON_PRETTY_PRINT)); + } else { + error_log("IMPORT DATA DOES NOT EXIST."); + } if (empty($data)) { - $file = get_attached_file($post_id); - $file = _wp_relative_upload_path($file); + $isDocument = true; + $post_mime = get_post_mime_type($post_id); + $upload_file = get_attached_file($post_id); + $file = _wp_relative_upload_path($upload_file); $data = [ 'file' => $file ]; + if (file_exists($upload_file)) { + $mime = trim(`file --mime-type -b {$upload_file}`); + if ($mime == 'image/vnd.adobe.photoshop') { + $mime = 'application/vnd.adobe.photoshop'; + } + + $data['ilab-mime'] = $mime; + if ($mime != $post_mime) { + wp_update_post(['ID'=>$post_id, 'post_mime_type' => $mime]); + } + $imagesize = getimagesize( $upload_file ); + if ($imagesize) { + if (file_is_displayable_image($upload_file)) { + $data['width'] = $imagesize[0]; + $data['height'] = $imagesize[1]; + $data['sizes']=[ + 'full' => [ + 'file' => $data['file'], + 'width' => $data['width'], + 'height' => $data['height'] + ] + ]; + + $isDocument = false; + } + } + + if ($mime == 'application/pdf') { + $renderPDF = apply_filters('ilab_imgix_render_pdf', false); + + try { + $parser = new \Smalot\PdfParser\Parser(); + $pdf = $parser->parseFile($upload_file); + $pages = $pdf->getPages(); + if (count($pages)>0) { + $page = $pages[0]; + $details = $page->getDetails(); + if (isset($details['MediaBox'])) { + $data['width'] = $details['MediaBox'][2]; + $data['height'] = $details['MediaBox'][3]; + + if ($renderPDF) { + $data['sizes']=[ + 'full' => [ + 'file' => $data['file'], + 'width' => $data['width'], + 'height' => $data['height'] + ] + ]; + + $isDocument = false; + } + } + } + } catch (Exception $ex) { + error_log("PDF Exception: ".$ex->getMessage()); + } + } + } + + error_log("FOUND DATA: ".json_encode($data, JSON_PRETTY_PRINT)); } $s3tool = ILabMediaToolsManager::instance()->tools['s3']; $data = $s3tool->updateAttachmentMetadata($data, $post_id); - wp_update_attachment_metadata($post_id, $data); + if ($isDocument) { + update_post_meta($post_id, 'ilab_s3_info', $data); + } else { + wp_update_attachment_metadata($post_id, $data); + } return false; } diff --git a/classes/tasks/wp-background-process.php b/classes/tasks/wp-background-process.php index f9a5cf00..1646c281 100755 --- a/classes/tasks/wp-background-process.php +++ b/classes/tasks/wp-background-process.php @@ -501,7 +501,7 @@ public function cancel_process() { * * @return mixed */ - abstract protected function task( $item ); + abstract public function task( $item ); } } diff --git a/classes/tools/imgix/ilab-media-imgix-tool.php b/classes/tools/imgix/ilab-media-imgix-tool.php index 6d7b5c2d..77e08cc0 100644 --- a/classes/tools/imgix/ilab-media-imgix-tool.php +++ b/classes/tools/imgix/ilab-media-imgix-tool.php @@ -182,6 +182,7 @@ public function setup() do_action('ilab_imgix_setup'); add_filter('imgix_build_srcset_url',[$this,'buildSrcSetURL'],0,3); + add_filter('media_send_to_editor', [$this,'mediaSendToEditor'], 0, 3); } public function buildMpeg4($value, $postId, $size) { @@ -205,6 +206,28 @@ function prepareAttachmentForJS( $response, $attachment, $meta ) if (!$response || empty($response) || !isset($response['sizes'])) return $response; + if ($this->renderPDF && isset($meta['s3'])) { + if ($attachment->post_mime_type == 'application/pdf') { + $response['type'] = 'image'; + $response['mime'] = 'image/pdf'; + if (isset($meta['width'])) { + $response['width'] = round($meta['width']); + } + + if (isset($meta['height'])) { + $response['height'] = round($meta['height']); + } + + if (isset($response['width']) && isset($response['height'])) { + $response['orientation'] = ($response['width'] > $response['height']) ? 'landscape' : 'portrait'; + } + } + + if (!isset($response['s3']['privacy'])) { + $response['s3']['privacy'] = $this->privacy; + } + } + foreach($response['sizes'] as $key => $sizeInfo) { $res = $this->buildImgixImage($response['id'],$key); if (is_array($res)) { @@ -392,6 +415,10 @@ private function buildImgixImage($id,$size, $params=null, $skipParams=false, $me if ($size=='full' && !$newSize) { + if (!isset($meta['width']) || !isset($meta['height'])) { + return false; + } + if (!$params) { if (isset($meta['imgix-params'])) @@ -1113,4 +1140,32 @@ public function fileIsDisplayableImage($result, $path) { return $result; } + + public function mediaSendToEditor($html, $id, $attachment) { + if (!$this->renderPDF) { + return $html; + } + + $mime = get_post_mime_type($id); + if ($mime == 'application/pdf') { + $align = isset( $attachment['align'] ) ? $attachment['align'] : 'none'; + $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium'; + $alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : ''; + + // No whitespace-only captions. + $caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : ''; + if ( '' === trim( $caption ) ) { + $caption = ''; + } + + $url = empty( $attachment['url'] ) ? '' : $attachment['url']; + $rel = ( strpos( $url, 'attachment_id') || get_attachment_link( $id ) == $url ); + + $title = ''; // We no longer insert title tags into tags, as they are redundant. + $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ); + + } + + return $html; + } } diff --git a/classes/tools/s3/ilab-media-s3-tool.php b/classes/tools/s3/ilab-media-s3-tool.php index 422a33a8..a984bce0 100644 --- a/classes/tools/s3/ilab-media-s3-tool.php +++ b/classes/tools/s3/ilab-media-s3-tool.php @@ -35,6 +35,7 @@ class ILabMediaS3Tool extends ILabMediaToolBase { private $settingsError = false; private $uploadedDocs = []; + private $pdfInfo = []; private $cacheControl = null; private $expires = null; @@ -380,16 +381,41 @@ public function updateAttachmentMetadata($data,$id) if ($this->skipUpdate) { return $data; } - + if (!$data) { return $data; } + $mime = (isset($data['ilab-mime'])) ? $data['ilab-mime'] : null; + if ($mime) { + unset($data['ilab-mime']); + } + if (!isset($data['file'])) { - $mime = get_post_mime_type($id); - $renderPDF = apply_filters('ilab_imgix_render_pdf', false); - if (($mime == 'application/pdf') && (!$renderPDF)) { - unset($data['sizes']); + if (!$mime) { + $mime = get_post_mime_type($id); + } + + if ($mime == 'application/pdf') { + $renderPDF = apply_filters('ilab_imgix_render_pdf', false); + + if (!$renderPDF) { + unset($data['sizes']); + } + + $s3Info = get_post_meta($id, 'ilab_s3_info', true); + if ($s3Info) { + $pdfInfo = $this->pdfInfo[$s3Info['file']]; + $data['width'] = $pdfInfo['width']; + $data['height'] = $pdfInfo['height']; + $data['file'] = $s3Info['s3']['key']; + $data['s3'] = $s3Info['s3']; + if ($renderPDF) { + $data['sizes']['full']['file'] = $s3Info['s3']['key']; + $data['sizes']['full']['width'] = $data['width']; + $data['sizes']['full']['height'] = $data['height']; + } + } } return $data; } @@ -402,7 +428,10 @@ public function updateAttachmentMetadata($data,$id) return $data; } - $mime = wp_get_image_mime($upload_path.'/'.$data['file']); + if (!$mime) { + $mime = wp_get_image_mime($upload_path.'/'.$data['file']); + } + if ($mime && in_array($mime, $this->ignoredMimeTypes)) { return $data; } @@ -477,6 +506,26 @@ public function handleUpload($upload, $context='upload') { $file = trim(str_replace($upload_path,'',$pi['dirname']),'/').'/'.$pi['basename']; + if (($upload['type']=='application/pdf') && file_exists($upload_path.'/'.$file)) { + try { + $parser = new \Smalot\PdfParser\Parser(); + $pdf = $parser->parseFile($upload_path.'/'.$file); + $pages = $pdf->getPages(); + if (count($pages)>0) { + $page = $pages[0]; + $details = $page->getDetails(); + if (isset($details['MediaBox'])) { + $data = []; + $data['width'] = $details['MediaBox'][2]; + $data['height'] = $details['MediaBox'][3]; + $this->pdfInfo[$upload_path.'/'.$file] = $data; + } + } + } catch (Exception $ex) { + + } + } + $upload = $this->processFile($s3, $upload_path, $file, $upload); if (isset($upload['s3'])) { if ($this->docCdn) diff --git a/composer.json b/composer.json index e13a0d5e..5e88c651 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ ], "require" : { "imgix/imgix-php": "^1.0", - "ilab/ilab-aws-sdk": "^0.1.0" + "ilab/ilab-aws-sdk": "^0.1.0", + "smalot/pdfparser": "^0.10.0" } }