Skip to content

Commit

Permalink
[Assets] Support document (PDF) version preview (pimcore#13858)
Browse files Browse the repository at this point in the history
* support document (PDF) version preview
* add version date as download file name suffix
* use ExtJS to format date to have same display logic as in versions panel
  • Loading branch information
BlackbitDevs authored Jan 11, 2023
1 parent 03a07aa commit af29ff9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,8 @@ public function showVersionAction(Request $request): Response
if (!$version) {
throw $this->createNotFoundException('Version not found');
}

/** @var Asset $asset */
$asset = $version->loadData();

if (!$asset->isAllowed('versions')) {
Expand All @@ -1120,6 +1122,7 @@ public function showVersionAction(Request $request): Response
'@PimcoreAdmin/admin/asset/show_version_' . strtolower($asset->getType()) . '.html.twig',
[
'asset' => $asset,
'version' => $version,
'loader' => $loader,
]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
{% include '@PimcoreAdmin/admin/asset/show_version_unknown.html.twig' %}
{% if asset.getMimeType() == 'application/pdf' %}
{% set tempFile = asset.getTemporaryFile() %}
{% set dataUri = pimcore_asset_version_preview(tempFile) %}

<div style="display: flex; width: 100%; height: 100%; flex-direction: column; overflow: hidden;">
<iframe src="{{ dataUri }}" frameborder="0" style="flex-grow: 1; border: none; margin: 0; padding: 0;"></iframe>
</div>
{% else %}
{% include '@PimcoreAdmin/admin/asset/show_version_unknown.html.twig' %}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,31 @@
vertical-align: middle;
width: 400px;
}
</style>

</head>

<body>

<table id="wrapper" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
Sorry, no preview available
Sorry, no preview available<br>
{% set tempFile = asset.getTemporaryFile() %}
{% set dataUri = pimcore_asset_version_preview(tempFile) %}

<a href="{{ dataUri }}" download="{{ asset.getFilename() }}" id="download-version-link">{{ 'download'|trans([],'admin') }}</a>
</td>
</tr>
</table>

<script>
document.addEventListener('DOMContentLoaded', function () {
{% set filenamePrefix = asset.getFilename()|split('.')|slice(0,-1)|join('.') %}
{% set filenameSuffix = asset.getFilename()|split('.')|last %}
let versionDate = Ext.Date.format(new Date({{ version.getDate() * 1000 }}), 'Y-m-d-H-i-s');
document.getElementById('download-version-link').setAttribute('download', "{{ filenamePrefix }}-" + versionDate + ".{{ filenameSuffix }}");
}, false);
</script>

<script src="/bundles/pimcoreadmin/js/../extjs/js/ext-all.js" {{ pimcore_csp.getNonceHtmlAttribute()|raw }}></script>
</body>
</html>
17 changes: 17 additions & 0 deletions lib/Twig/Extension/HelpersExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\File;
use Pimcore\Twig\Extension\Templating\PimcoreUrl;
use Pimcore\Video;
use Symfony\Component\Mime\MimeTypes;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
Expand Down Expand Up @@ -58,6 +59,7 @@ public function getFunctions(): array
}),
new TwigFunction('pimcore_file_extension', [File::class, 'getFileExtension']),
new TwigFunction('pimcore_image_version_preview', [$this, 'getImageVersionPreview']),
new TwigFunction('pimcore_asset_version_preview', [$this, 'getAssetVersionPreview']),
new TwigFunction('pimcore_breach_attack_random_content', [$this, 'breachAttackRandomContent'], [
'is_safe' => ['html'],
]),
Expand Down Expand Up @@ -104,6 +106,21 @@ public function getImageVersionPreview(string $file): string
return $dataUri;
}

/**
* @param string $file
*
* @return string
*
* @throws \Exception
*/
public function getAssetVersionPreview($file)
{
$dataUri = 'data:'.MimeTypes::getDefault()->guessMimeType($file).';base64,'.base64_encode(file_get_contents($file));
unlink($file);

return $dataUri;
}

/**
* @return string
*
Expand Down

0 comments on commit af29ff9

Please sign in to comment.