From ef28bc4902bf76422e766839f520da50441a3eb2 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 7 Apr 2016 18:26:59 +0100
Subject: [PATCH] Fill in video and audio previews.

---
 build/index.php            | 31 ++++++++++++++++++++++++-------
 module_index.json          |  2 +-
 modules/feature-upload.php | 24 ++++++++++++++++++++----
 settings.fragment.php      |  7 ++++---
 4 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/build/index.php b/build/index.php
index 9fe02e3..47648c6 100644
--- a/build/index.php
+++ b/build/index.php
@@ -203,7 +203,7 @@ $settings->upload_allowed_file_types = [
 	"video/mp4",
 	"video/webm",
 	"audio/mp4",
-	"audio/mp3"
+	"audio/mpeg"
 ];
 
 // The default file type for previews.
@@ -221,7 +221,8 @@ $settings->mime_extension_mappings_location = "/etc/mime.types";
 // Used to override the above file if it assigns weird extensions
 // to any mime types.
 $settings->mime_mappings_overrides = [
-	"text/plain" => "txt"
+	"text/plain" => "txt",
+	"audio/mpeg" => "mp3"
 ];
 
 // The minimum and maximum allowed sizes of generated preview images in pixels.
@@ -316,7 +317,7 @@ input[type=button], input[type=submit] { cursor: pointer; }
 .sidebar li:before { content: \"\"; position: absolute; width: 1rem; top: 0.8rem; left: -1.2rem; border-bottom: 2px dashed rgba(50, 50, 50, 0.4); }
 
 .preview { text-align: center; }
-.preview img { max-width: 100%; }
+.preview img, .preview video, .preview audio { max-width: 100%; }
 .image-controls ul { list-style-type: none; margin: 5px; padding: 5px; }
 .image-controls li { display: inline-block; margin: 5px; padding: 5px; }
 .link-display { margin-left: 0.5rem; }
@@ -2674,9 +2675,18 @@ register_module([
 				
 				case "video":
 				case "audio":
-					$im = errorimage("TODO: Proxy the video / audio through");
-					header("image/png");
-					imagepng($im);
+					// Get the file size
+					$filesize = filesize($filepath);
+					
+					// Send some headers
+					header("content-length: $filesize");
+					header("content-type: $mime_type");
+					
+					// Open the file and send it to the user
+					$handle = fopen($filepath, "rb");
+					fpassthru($handle);
+					fclose($handle);
+					exit();
 					break;
 				
 				default:
@@ -2744,8 +2754,15 @@ register_module([
 						break;
 					
 					case "video":
-						$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>";
+						$preview_html .= "\t\t\t<figure class='preview'>
+				<video src='$previewUrl' controls preload='metadata'>Your browser doesn't support HTML5 video, but you can still <a href='$previewUrl'>download it</a> if you'd like.</video>
+			</figure>";
 						break;
+						
+					case "audio":
+						$preview_html .= "\t\t\t<figure class='preview'>
+				<audio src='$previewUrl' controls preload='metadata'>Your browser doesn't support HTML5 audio, but you can still <a href='$previewUrl'>download it</a> if you'd like.</audio>
+			</figure>";
 				}
 				
 				$fileInfo = [];
diff --git a/module_index.json b/module_index.json
index a81e970..542ba0d 100644
--- a/module_index.json
+++ b/module_index.json
@@ -68,7 +68,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File:' prefix.",
         "id": "feature-upload",
-        "lastupdate": 1460046939,
+        "lastupdate": 1460047934,
         "optional": false
     },
     {
diff --git a/modules/feature-upload.php b/modules/feature-upload.php
index f6289d1..636c35d 100644
--- a/modules/feature-upload.php
+++ b/modules/feature-upload.php
@@ -255,9 +255,18 @@ register_module([
 				
 				case "video":
 				case "audio":
-					$im = errorimage("TODO: Proxy the video / audio through");
-					header("image/png");
-					imagepng($im);
+					// Get the file size
+					$filesize = filesize($filepath);
+					
+					// Send some headers
+					header("content-length: $filesize");
+					header("content-type: $mime_type");
+					
+					// Open the file and send it to the user
+					$handle = fopen($filepath, "rb");
+					fpassthru($handle);
+					fclose($handle);
+					exit();
 					break;
 				
 				default:
@@ -325,8 +334,15 @@ register_module([
 						break;
 					
 					case "video":
-						$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>";
+						$preview_html .= "\t\t\t<figure class='preview'>
+				<video src='$previewUrl' controls preload='metadata'>Your browser doesn't support HTML5 video, but you can still <a href='$previewUrl'>download it</a> if you'd like.</video>
+			</figure>";
 						break;
+						
+					case "audio":
+						$preview_html .= "\t\t\t<figure class='preview'>
+				<audio src='$previewUrl' controls preload='metadata'>Your browser doesn't support HTML5 audio, but you can still <a href='$previewUrl'>download it</a> if you'd like.</audio>
+			</figure>";
 				}
 				
 				$fileInfo = [];
diff --git a/settings.fragment.php b/settings.fragment.php
index 3165c44..eca00ae 100644
--- a/settings.fragment.php
+++ b/settings.fragment.php
@@ -200,7 +200,7 @@ $settings->upload_allowed_file_types = [
 	"video/mp4",
 	"video/webm",
 	"audio/mp4",
-	"audio/mp3"
+	"audio/mpeg"
 ];
 
 // The default file type for previews.
@@ -218,7 +218,8 @@ $settings->mime_extension_mappings_location = "/etc/mime.types";
 // Used to override the above file if it assigns weird extensions
 // to any mime types.
 $settings->mime_mappings_overrides = [
-	"text/plain" => "txt"
+	"text/plain" => "txt",
+	"audio/mpeg" => "mp3"
 ];
 
 // The minimum and maximum allowed sizes of generated preview images in pixels.
@@ -313,7 +314,7 @@ input[type=button], input[type=submit] { cursor: pointer; }
 .sidebar li:before { content: \"\"; position: absolute; width: 1rem; top: 0.8rem; left: -1.2rem; border-bottom: 2px dashed rgba(50, 50, 50, 0.4); }
 
 .preview { text-align: center; }
-.preview img { max-width: 100%; }
+.preview img, .preview video, .preview audio { max-width: 100%; }
 .image-controls ul { list-style-type: none; margin: 5px; padding: 5px; }
 .image-controls li { display: inline-block; margin: 5px; padding: 5px; }
 .link-display { margin-left: 0.5rem; }
