Video downloads end with .html

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
floam
Posts: 5
Joined: Fri Dec 17, 2010 3:45 am

Video downloads end with .html

Post by floam »

On my system video downloads were getting a text/html Content-Type causing Safari to append .html to the end of the filenames. This quick fix got around that.

Code: Select all

--- video.phpold	2010-12-16 19:35:57.194047323 -0800
+++ video.php	2010-12-16 19:38:27.650321752 -0800
@@ -89,6 +89,7 @@
 {
     $downloadIndex = validInt($_REQUEST['downloadIndex']);
     header( "Content-disposition: attachment; filename=".$videoFiles[$downloadIndex]."; size=".filesize($videoFiles[$downloadIndex]) );
+    header( "Content-Type: application/octet-stream" );
     readfile( $videoFiles[$downloadIndex] );
     exit;
 }
BlankMan
Posts: 147
Joined: Tue Jan 19, 2010 2:53 am
Location: Milwaukee, WI USA

Post by BlankMan »

Opera was doing this to me too, might give this a try, thanks.
mastertheknife
Posts: 678
Joined: Wed Dec 16, 2009 4:32 pm
Location: Israel

Post by mastertheknife »

Complete headers would probably work better.

Code: Select all

--- a/web/skins/classic/views/video.php
+++ b/web/skins/classic/views/video.php
@@ -88,7 +88,15 @@ if ( isset($_REQUEST['deleteIndex']) )
 if ( isset($_REQUEST['downloadIndex']) )
 {
     $downloadIndex = validInt($_REQUEST['downloadIndex']);
-    header( "Content-disposition: attachment; filename=".$videoFiles[$downloadIndex]."; size=".filesize($videoFiles[$downloadIndex]) );
+    header("Pragma: public");
+    header("Expires: 0");
+    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
+    header("Cache-Control: private",false); // required by certain browsers
+    header("Content-Description: File Transfer"); 
+    header("Content-Disposition: attachment; filename=".$videoFiles[$downloadIndex]); 
+    header("Content-Transfer-Encoding: binary"); 
+    header("Content-Type: application/force-download");
+    header("Content-Length: ".filesize($videoFiles[$downloadIndex])); 
     readfile( $videoFiles[$downloadIndex] );
     exit;
 }
EDIT: Fixed missing parenthesis in the code

EDIT 2: Do not use, newer patch in the post below.

mastertheknife.
Last edited by mastertheknife on Sat Feb 05, 2011 6:46 am, edited 1 time in total.
mastertheknife
Posts: 678
Joined: Wed Dec 16, 2009 4:32 pm
Location: Israel

Post by mastertheknife »

I fixed the patch to work for old IE and fixed filenames for Firefox. On my machine the file name was events_1_12487-r1-s1.avi, this is because Firefox doesn't strip paths from the filename but simply replaces them with underscore. I used basename() to strip the path.
Patch was tested on IE6, Opera 10 and Firefox 3.6.

Code: Select all

--- a/web/skins/classic/views/video.php
+++ b/web/skins/classic/views/video.php
@@ -88,7 +88,15 @@ if ( isset($_REQUEST['deleteIndex']) )
 if ( isset($_REQUEST['downloadIndex']) )
 {
     $downloadIndex = validInt($_REQUEST['downloadIndex']);
-    header( "Content-disposition: attachment; filename=".$videoFiles[$downloadIndex]."; size=".filesize($videoFiles[$downloadIndex]) );
+    header("Pragma: public");
+    header("Expires: 0");
+    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
+    header("Cache-Control: private",false); // required by certain browsers
+    header("Content-Description: File Transfer"); 
+    header('Content-Disposition: attachment; filename="'.basename($videoFiles[$downloadIndex]).'"'); // basename is required because the video index contains the path and firefox doesn't strip the path but simply replaces the slashes with an underscore. 
+    header("Content-Transfer-Encoding: binary"); 
+    header("Content-Type: application/force-download");
+    header("Content-Length: ".filesize($videoFiles[$downloadIndex])); 
     readfile( $videoFiles[$downloadIndex] );
     exit;
 }
Reports from other browsers and versions are welcome.

mastertheknife.
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

This is a strange one. In FireFox for me, everything has been working fine and when clicking on the Download link the default actual suggested by FF is to download the file. However applying this change makes FF suggest opening the link rather than downloading it.

That may be by design and other than requiring one more click it doesn't break anything so I will probably apply the patch anyway.
Phil
floam
Posts: 5
Joined: Fri Dec 17, 2010 3:45 am

Re: Video downloads end with .html

Post by floam »

Probably a better idea to use application/octet-stream than application/force-download. The former is the official type for arbitrary byte data and the latter just works as a side effect. Best would be figuring out what is actually being sent and choose the correct MIME, but that's not as trivial.
Post Reply