1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-11-22 16:33:00 +00:00

Overhaul file preview/display to support multiple filetypes.

This commit is contained in:
Starbeamrainbowlabs 2016-04-07 17:37:11 +01:00
parent 4e599569ed
commit 18012c82fe
5 changed files with 162 additions and 55 deletions

View file

@ -17,6 +17,9 @@
- Fixed the downloader - Fixed the downloader
- Fixed an issue with the recent changes page and redirects causing a large number of warnings - Fixed an issue with the recent changes page and redirects causing a large number of warnings
# Notes
- Test the etag code!
## v0.10 ## v0.10
### Added ### Added

View file

@ -199,7 +199,11 @@ $settings->upload_allowed_file_types = [
"image/jpeg", "image/jpeg",
"image/png", "image/png",
"image/gif", "image/gif",
"image/webp" "image/webp",
"video/mp4",
"video/webm",
"audio/mp4",
"audio/mp3"
]; ];
// The default file type for previews. // The default file type for previews.
@ -2631,8 +2635,6 @@ register_module([
$output_mime = $_GET["type"]; $output_mime = $_GET["type"];
/// ETag handling /// /// ETag handling ///
// TODO: Test this
error_log("Note to self: Please remember to test this etag code!");
// Generate the etag and send it to the client // Generate the etag and send it to the client
$preview_etag = sha1("$output_mime|$target_size|$filepath|$mime_type"); $preview_etag = sha1("$output_mime|$target_size|$filepath|$mime_type");
$allheaders = getallheaders(); $allheaders = getallheaders();
@ -2653,68 +2655,118 @@ register_module([
} }
/// ETag handling end /// /// ETag handling end ///
$preview_image = new Imagick(); $preview = new Imagick();
switch(substr($mime_type, 0, strpos($mime_type, "/"))) switch(substr($mime_type, 0, strpos($mime_type, "/")))
{ {
case "image": case "image":
$preview_image->readImage($filepath); $preview->readImage($filepath);
break; break;
case "application": case "application":
if($mime_type == "application/pdf") if($mime_type == "application/pdf")
{ {
$preview_image = new imagick(); $preview = new imagick();
$preview_image->readImage("{$filepath}[0]"); $preview->readImage("{$filepath}[0]");
$preview_image->setResolution(300,300); $preview->setResolution(300,300);
$preview_image->setImageColorspace(255); $preview->setImageColorspace(255);
break; break;
} }
case "video":
case "audio":
$im = errorimage("TODO: Proxy the video / audio through");
header("image/png");
imagepng($im);
break;
default: default:
http_response_code(501); http_response_code(501);
$preview_image = errorimage("Unrecognised file type '$mime_type'."); $preview = errorimage("Unrecognised file type '$mime_type'.");
header("content-type: image/png"); header("content-type: image/png");
imagepng($preview_image); imagepng($preview);
exit(); exit();
} }
// Scale the image down to the target size // Scale the image down to the target size
$preview_image->resizeImage($target_size, $target_size, imagick::FILTER_LANCZOS, 1, true); $preview->resizeImage($target_size, $target_size, imagick::FILTER_LANCZOS, 1, true);
// Send the completed preview image to the user // Send the completed preview image to the user
header("content-type: $output_mime"); header("content-type: $output_mime");
header("x-generation-time: " . (microtime(true) - $start_time) . "s"); header("x-generation-time: " . (microtime(true) - $start_time) . "s");
$preview_image->setImageFormat(substr($output_mime, strpos($output_mime, "/") + 1)); $preview->setImageFormat(substr($output_mime, strpos($output_mime, "/") + 1));
echo($preview_image->getImageBlob()); echo($preview->getImageBlob());
}); });
/*
* ██████ ██████ ███████ ██ ██ ██ ███████ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██████ █████ ██ ██ ██ █████ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██
* ██ ██ ██ ███████ ████ ██ ███████ ███ ███
*
* ██████ ██ ███████ ██████ ██ █████ ██ ██ ███████ ██████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ███████ ██████ ██ ███████ ████ █████ ██████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██ ███████ ██ ███████ ██ ██ ██ ███████ ██ ██
*/
page_renderer::register_part_preprocessor(function(&$parts) { page_renderer::register_part_preprocessor(function(&$parts) {
global $pageindex, $env, $settings; global $pageindex, $env, $settings;
// Todo add the preview to the top of the page here, but only if the current action is view and we are on a page that is a file // Don't do anything if the action isn't view
if($env->action !== "view")
return;
if(isset($pageindex->{$env->page}->uploadedfile) and $pageindex->{$env->page}->uploadedfile == true) if(isset($pageindex->{$env->page}->uploadedfile) and $pageindex->{$env->page}->uploadedfile == true)
{ {
// We are looking at a page that is paired with an uploaded file // We are looking at a page that is paired with an uploaded file
$filepath = $pageindex->{$env->page}->uploadedfilepath; $filepath = $pageindex->{$env->page}->uploadedfilepath;
$mime_type = $pageindex->{$env->page}->uploadedfilemime; $mime_type = $pageindex->{$env->page}->uploadedfilemime;
$dimensions = getimagesize($env->storage_prefix . $filepath); $dimensions = getimagesize($env->storage_prefix . $filepath);
$fileTypeDisplay = substr($mime_type, 0, strpos($mime_type, "/"));
$previewUrl = "?action=preview&size=$settings->default_preview_size&page=" . rawurlencode($env->page);
$preview_html = "";
error_log($fileTypeDisplay);
switch($fileTypeDisplay)
{
case "image":
$preview_sizes = [ 256, 512, 768, 1024, 1440 ]; $preview_sizes = [ 256, 512, 768, 1024, 1440 ];
$preview_html = "<figure class='preview'> $preview_html .= "\t\t\t<figure class='preview'>
<img src='?action=preview&size=$settings->default_preview_size&page=" . rawurlencode($env->page) . "' /> <img src='$previewUrl' />
<nav class='image-controls'> <nav class='image-controls'>
<ul><li><a href='" . ($env->storage_prefix == "./" ? $filepath : "?action=preview&size=original&page=" . rawurlencode($env->page)) . "'>&#x01f304; Original image</a></li> <ul><li><a href='" . ($env->storage_prefix == "./" ? $filepath : "?action=preview&size=original&page=" . rawurlencode($env->page)) . "'>&#x01f304; Original image</a></li>
<li>Other Sizes: "; <li>Other Sizes: ";
foreach($preview_sizes as $size) foreach($preview_sizes as $size)
$preview_html .= "<a href='?action=preview&page=" . rawurlencode($env->page) . "&size=$size'>$size" . "px</a> "; $preview_html .= "<a href='?action=preview&page=" . rawurlencode($env->page) . "&size=$size'>$size" . "px</a> ";
$preview_html .= "</li></ul></nav> $preview_html .= "</li></ul></nav>
</figure> </figure>";
<h2>File Information</h2> break;
<table><tr><th>Name</th><td>" . str_replace("File/", "", $filepath) . "</td>
<tr><th>Type</th><td>$mime_type</td></tr> case "video":
<tr><th>Size</th><td>" . human_filesize(filesize($filepath)) . "</td></tr>"; $preview_html .= "<video src='$previewUrl' controls preload='metadata'>Your browser doesn't support the HTML5 video tag, but you can still <a href='$previewUrl'>download it</a> if you'd like.</video>";
if(substr($mime_type, strpos($mime_type, "/")) == "image") break;
$preview_html .= "<tr><th>Original dimensions</th><td>$dimensions[0] x $dimensions[1]</td></tr>"; }
$preview_html .= "<tr><th>Uploaded by</th><td>" . $pageindex->{$env->page}->lasteditor . "</td></tr></table>
<h2>Description</h2>"; $fileInfo = [];
$fileInfo["Name"] = str_replace("File/", "", $filepath);
$fileInfo["Type"] = $mime_type;
$fileInfo["Size"] = human_filesize(filesize($filepath));
switch($fileTypeDisplay)
{
case "image":
$fileInfo["Original dimensions"] = "$dimensions[0] x $dimensions[1]";
break;
}
$fileInfo["Uploaded by"] = $pageindex->{$env->page}->lasteditor;
$preview_html .= "\t\t\t<h2>File Information</h2>
<table>";
foreach ($fileInfo as $displayName => $displayValue)
{
$preview_html .= "<tr><th>$displayName</th><td>$displayValue</td></tr>\n";
}
$preview_html .= "</table>";
$parts["{content}"] = str_replace("</h1>", "</h1>\n$preview_html", $parts["{content}"]); $parts["{content}"] = str_replace("</h1>", "</h1>\n$preview_html", $parts["{content}"]);
} }

View file

@ -68,7 +68,7 @@
"author": "Starbeamrainbowlabs", "author": "Starbeamrainbowlabs",
"description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.", "description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.",
"id": "feature-upload", "id": "feature-upload",
"lastupdate": 1459973604, "lastupdate": 1460046939,
"optional": false "optional": false
}, },
{ {

View file

@ -216,8 +216,6 @@ register_module([
$output_mime = $_GET["type"]; $output_mime = $_GET["type"];
/// ETag handling /// /// ETag handling ///
// TODO: Test this
error_log("Note to self: Please remember to test this etag code!");
// Generate the etag and send it to the client // Generate the etag and send it to the client
$preview_etag = sha1("$output_mime|$target_size|$filepath|$mime_type"); $preview_etag = sha1("$output_mime|$target_size|$filepath|$mime_type");
$allheaders = getallheaders(); $allheaders = getallheaders();
@ -238,68 +236,118 @@ register_module([
} }
/// ETag handling end /// /// ETag handling end ///
$preview_image = new Imagick(); $preview = new Imagick();
switch(substr($mime_type, 0, strpos($mime_type, "/"))) switch(substr($mime_type, 0, strpos($mime_type, "/")))
{ {
case "image": case "image":
$preview_image->readImage($filepath); $preview->readImage($filepath);
break; break;
case "application": case "application":
if($mime_type == "application/pdf") if($mime_type == "application/pdf")
{ {
$preview_image = new imagick(); $preview = new imagick();
$preview_image->readImage("{$filepath}[0]"); $preview->readImage("{$filepath}[0]");
$preview_image->setResolution(300,300); $preview->setResolution(300,300);
$preview_image->setImageColorspace(255); $preview->setImageColorspace(255);
break; break;
} }
case "video":
case "audio":
$im = errorimage("TODO: Proxy the video / audio through");
header("image/png");
imagepng($im);
break;
default: default:
http_response_code(501); http_response_code(501);
$preview_image = errorimage("Unrecognised file type '$mime_type'."); $preview = errorimage("Unrecognised file type '$mime_type'.");
header("content-type: image/png"); header("content-type: image/png");
imagepng($preview_image); imagepng($preview);
exit(); exit();
} }
// Scale the image down to the target size // Scale the image down to the target size
$preview_image->resizeImage($target_size, $target_size, imagick::FILTER_LANCZOS, 1, true); $preview->resizeImage($target_size, $target_size, imagick::FILTER_LANCZOS, 1, true);
// Send the completed preview image to the user // Send the completed preview image to the user
header("content-type: $output_mime"); header("content-type: $output_mime");
header("x-generation-time: " . (microtime(true) - $start_time) . "s"); header("x-generation-time: " . (microtime(true) - $start_time) . "s");
$preview_image->setImageFormat(substr($output_mime, strpos($output_mime, "/") + 1)); $preview->setImageFormat(substr($output_mime, strpos($output_mime, "/") + 1));
echo($preview_image->getImageBlob()); echo($preview->getImageBlob());
}); });
/*
* ██████ ██████ ███████ ██ ██ ██ ███████ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██████ █████ ██ ██ ██ █████ ██ ██
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██
* ██ ██ ██ ███████ ████ ██ ███████ ███ ███
*
* ██████ ██ ███████ ██████ ██ █████ ██ ██ ███████ ██████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██ ███████ ██████ ██ ███████ ████ █████ ██████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██ ███████ ██ ███████ ██ ██ ██ ███████ ██ ██
*/
page_renderer::register_part_preprocessor(function(&$parts) { page_renderer::register_part_preprocessor(function(&$parts) {
global $pageindex, $env, $settings; global $pageindex, $env, $settings;
// Todo add the preview to the top of the page here, but only if the current action is view and we are on a page that is a file // Don't do anything if the action isn't view
if($env->action !== "view")
return;
if(isset($pageindex->{$env->page}->uploadedfile) and $pageindex->{$env->page}->uploadedfile == true) if(isset($pageindex->{$env->page}->uploadedfile) and $pageindex->{$env->page}->uploadedfile == true)
{ {
// We are looking at a page that is paired with an uploaded file // We are looking at a page that is paired with an uploaded file
$filepath = $pageindex->{$env->page}->uploadedfilepath; $filepath = $pageindex->{$env->page}->uploadedfilepath;
$mime_type = $pageindex->{$env->page}->uploadedfilemime; $mime_type = $pageindex->{$env->page}->uploadedfilemime;
$dimensions = getimagesize($env->storage_prefix . $filepath); $dimensions = getimagesize($env->storage_prefix . $filepath);
$fileTypeDisplay = substr($mime_type, 0, strpos($mime_type, "/"));
$previewUrl = "?action=preview&size=$settings->default_preview_size&page=" . rawurlencode($env->page);
$preview_html = "";
error_log($fileTypeDisplay);
switch($fileTypeDisplay)
{
case "image":
$preview_sizes = [ 256, 512, 768, 1024, 1440 ]; $preview_sizes = [ 256, 512, 768, 1024, 1440 ];
$preview_html = "<figure class='preview'> $preview_html .= "\t\t\t<figure class='preview'>
<img src='?action=preview&size=$settings->default_preview_size&page=" . rawurlencode($env->page) . "' /> <img src='$previewUrl' />
<nav class='image-controls'> <nav class='image-controls'>
<ul><li><a href='" . ($env->storage_prefix == "./" ? $filepath : "?action=preview&size=original&page=" . rawurlencode($env->page)) . "'>&#x01f304; Original image</a></li> <ul><li><a href='" . ($env->storage_prefix == "./" ? $filepath : "?action=preview&size=original&page=" . rawurlencode($env->page)) . "'>&#x01f304; Original image</a></li>
<li>Other Sizes: "; <li>Other Sizes: ";
foreach($preview_sizes as $size) foreach($preview_sizes as $size)
$preview_html .= "<a href='?action=preview&page=" . rawurlencode($env->page) . "&size=$size'>$size" . "px</a> "; $preview_html .= "<a href='?action=preview&page=" . rawurlencode($env->page) . "&size=$size'>$size" . "px</a> ";
$preview_html .= "</li></ul></nav> $preview_html .= "</li></ul></nav>
</figure> </figure>";
<h2>File Information</h2> break;
<table><tr><th>Name</th><td>" . str_replace("File/", "", $filepath) . "</td>
<tr><th>Type</th><td>$mime_type</td></tr> case "video":
<tr><th>Size</th><td>" . human_filesize(filesize($filepath)) . "</td></tr>"; $preview_html .= "<video src='$previewUrl' controls preload='metadata'>Your browser doesn't support the HTML5 video tag, but you can still <a href='$previewUrl'>download it</a> if you'd like.</video>";
if(substr($mime_type, strpos($mime_type, "/")) == "image") break;
$preview_html .= "<tr><th>Original dimensions</th><td>$dimensions[0] x $dimensions[1]</td></tr>"; }
$preview_html .= "<tr><th>Uploaded by</th><td>" . $pageindex->{$env->page}->lasteditor . "</td></tr></table>
<h2>Description</h2>"; $fileInfo = [];
$fileInfo["Name"] = str_replace("File/", "", $filepath);
$fileInfo["Type"] = $mime_type;
$fileInfo["Size"] = human_filesize(filesize($filepath));
switch($fileTypeDisplay)
{
case "image":
$fileInfo["Original dimensions"] = "$dimensions[0] x $dimensions[1]";
break;
}
$fileInfo["Uploaded by"] = $pageindex->{$env->page}->lasteditor;
$preview_html .= "\t\t\t<h2>File Information</h2>
<table>";
foreach ($fileInfo as $displayName => $displayValue)
{
$preview_html .= "<tr><th>$displayName</th><td>$displayValue</td></tr>\n";
}
$preview_html .= "</table>";
$parts["{content}"] = str_replace("</h1>", "</h1>\n$preview_html", $parts["{content}"]); $parts["{content}"] = str_replace("</h1>", "</h1>\n$preview_html", $parts["{content}"]);
} }

View file

@ -196,7 +196,11 @@ $settings->upload_allowed_file_types = [
"image/jpeg", "image/jpeg",
"image/png", "image/png",
"image/gif", "image/gif",
"image/webp" "image/webp",
"video/mp4",
"video/webm",
"audio/mp4",
"audio/mp3"
]; ];
// The default file type for previews. // The default file type for previews.