Skip to content

Commit

Permalink
Fixes for importing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jawngee committed Apr 25, 2017
1 parent a7713da commit 7c66878
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 12 deletions.
82 changes: 78 additions & 4 deletions classes/tasks/ilab-s3-import-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/tasks/wp-background-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ public function cancel_process() {
*
* @return mixed
*/
abstract protected function task( $item );
abstract public function task( $item );

}
}
55 changes: 55 additions & 0 deletions classes/tools/imgix/ilab-media-imgix-tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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']))
Expand Down Expand Up @@ -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 <img> tags, as they are redundant.
$html = get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt );

}

return $html;
}
}
61 changes: 55 additions & 6 deletions classes/tools/s3/ilab-media-s3-tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ILabMediaS3Tool extends ILabMediaToolBase {
private $settingsError = false;

private $uploadedDocs = [];
private $pdfInfo = [];

private $cacheControl = null;
private $expires = null;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 7c66878

Please sign in to comment.