1
0
mirror of https://github.com/misterunknown/ifm.git synced 2025-08-09 17:46:31 +02:00

Fix image preview when root_dir is not the DocumentRoot.

This commit is contained in:
Marco Dickert
2017-12-11 11:34:07 +01:00
parent 9d21de458c
commit afc50f5744
4 changed files with 75 additions and 30 deletions

View File

@@ -1549,7 +1549,7 @@ function IFM( params ) {
item.download.action = "download"; item.download.action = "download";
item.download.icon = "icon icon-download"; item.download.icon = "icon icon-download";
} }
if( item.icon.indexOf( 'file-image' ) !== -1 && self.config.isDocroot ) { if( item.icon.indexOf( 'file-image' ) !== -1 ) {
item.tooltip = 'data-toggle="tooltip"'; item.tooltip = 'data-toggle="tooltip"';
} }
if( self.config.extract && self.inArray( item.ext, ["zip","tar","tgz","tar.gz","tar.xz","tar.bz2"] ) ) { if( self.config.extract && self.inArray( item.ext, ["zip","tar","tgz","tar.gz","tar.xz","tar.bz2"] ) ) {
@@ -1668,7 +1668,10 @@ function IFM( params ) {
title: function() { title: function() {
var item = self.fileCache.find( x => x.guid == $(this).attr('id') ); var item = self.fileCache.find( x => x.guid == $(this).attr('id') );
var tooltip = document.createElement( 'img' ); var tooltip = document.createElement( 'img' );
tooltip.src = encodeURI( self.pathCombine( self.currentDir, item.name ) ).replace( '#', '%23' ).replace( '?', '%3F' ); if( self.config.isDocroot )
tooltip.src = encodeURI( self.pathCombine( self.currentDir, item.name ) ).replace( '#', '%23' ).replace( '?', '%3F' );
else
tooltip.src = self.api + "?api=proxy&dir=" + encodeURIComponent( self.currentDir ) + "&filename=" + encodeURIComponent( item.name );
tooltip.classList.add( 'imgpreview' ); tooltip.classList.add( 'imgpreview' );
return tooltip; return tooltip;
}, },
@@ -3279,6 +3282,7 @@ function IFM( params ) {
case "searchItems": $this->searchItems( $_REQUEST ); break; case "searchItems": $this->searchItems( $_REQUEST ); break;
case "getFolderTree": $this->getFolderTree( $_REQUEST ); break; case "getFolderTree": $this->getFolderTree( $_REQUEST ); break;
case "createArchive": $this->createArchive( $_REQUEST ); break; case "createArchive": $this->createArchive( $_REQUEST ); break;
case "proxy": $this->downloadFile( $_REQUEST, false ); break;
default: default:
$this->jsonResponse( array( "status" => "ERROR", "message" => "Invalid api action given" ) ); $this->jsonResponse( array( "status" => "ERROR", "message" => "Invalid api action given" ) );
break; break;
@@ -3657,7 +3661,7 @@ function IFM( params ) {
} }
// provides a file for downloading // provides a file for downloading
private function downloadFile( array $d ) { private function downloadFile( array $d, $forceDL=true ) {
if( $this->config['download'] != 1 ) if( $this->config['download'] != 1 )
$this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) ); $this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) );
elseif( $this->config['showhtdocs'] != 1 && ( substr( $d['filename'], 0, 3 ) == ".ht" || substr( $d['filename'],0,3 ) == ".ht" ) ) elseif( $this->config['showhtdocs'] != 1 && ( substr( $d['filename'], 0, 3 ) == ".ht" || substr( $d['filename'],0,3 ) == ".ht" ) )
@@ -3666,7 +3670,10 @@ function IFM( params ) {
$this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) ); $this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) );
else { else {
$this->chDirIfNecessary( $d["dir"] ); $this->chDirIfNecessary( $d["dir"] );
$this->fileDownload( $d['filename'] ); if( ! is_file( $d['filename' ] ) )
http_response_code( 404 );
else
$this->fileDownload( array( "file" => $d['filename'], "forceDL" => $forceDL ) );
} }
} }
@@ -3793,7 +3800,7 @@ function IFM( params ) {
else else
$d['filename'] = basename( getcwd() ); $d['filename'] = basename( getcwd() );
} }
$this->fileDownload( $dfile, $d['filename'] . ".zip" ); $this->fileDownload( array( "file" => $dfile, "name" => $d['filename'] . ".zip" ) );
} catch ( Exception $e ) { } catch ( Exception $e ) {
echo $this->l['error'] . " " . $e->getMessage(); echo $this->l['error'] . " " . $e->getMessage();
} finally { } finally {
@@ -4236,16 +4243,24 @@ function IFM( params ) {
else return true; else return true;
} }
private function fileDownload( $file, $name="" ) { private function fileDownload( array $options ) {
if( $options['forceDL'] )
$content_type = "application/octet-stream";
else
$content_type = mime_content_type( $options['file'] );
if( ! isset( $options['name'] ) || trim( $options['name'] ) == "" )
$options['name'] = basename( $options['file'] );
header( 'Content-Description: File Transfer' ); header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/octet-stream' ); header( 'Content-Type: ' . $content_type );
header( 'Content-Disposition: attachment; filename="' . ( trim( $name ) == "" ? basename( $file ) : $name ) . '"' ); header( 'Content-Disposition: attachment; filename="' . $options['name'] . '"' );
header( 'Expires: 0' ); header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate' ); header( 'Cache-Control: must-revalidate' );
header( 'Pragma: public' ); header( 'Pragma: public' );
header( 'Content-Length: ' . filesize( $file ) ); header( 'Content-Length: ' . filesize( $options['file'] ) );
$file_stream = fopen( $file, 'rb' ); $file_stream = fopen( $options['file'], 'rb' );
$stdout_stream = fopen('php://output', 'wb'); $stdout_stream = fopen('php://output', 'wb');
stream_copy_to_stream($file_stream, $stdout_stream); stream_copy_to_stream($file_stream, $stdout_stream);

35
ifm.php
View File

@@ -1549,7 +1549,7 @@ function IFM( params ) {
item.download.action = "download"; item.download.action = "download";
item.download.icon = "icon icon-download"; item.download.icon = "icon icon-download";
} }
if( item.icon.indexOf( 'file-image' ) !== -1 && self.config.isDocroot ) { if( item.icon.indexOf( 'file-image' ) !== -1 ) {
item.tooltip = 'data-toggle="tooltip"'; item.tooltip = 'data-toggle="tooltip"';
} }
if( self.config.extract && self.inArray( item.ext, ["zip","tar","tgz","tar.gz","tar.xz","tar.bz2"] ) ) { if( self.config.extract && self.inArray( item.ext, ["zip","tar","tgz","tar.gz","tar.xz","tar.bz2"] ) ) {
@@ -1668,7 +1668,10 @@ function IFM( params ) {
title: function() { title: function() {
var item = self.fileCache.find( x => x.guid == $(this).attr('id') ); var item = self.fileCache.find( x => x.guid == $(this).attr('id') );
var tooltip = document.createElement( 'img' ); var tooltip = document.createElement( 'img' );
tooltip.src = encodeURI( self.pathCombine( self.currentDir, item.name ) ).replace( '#', '%23' ).replace( '?', '%3F' ); if( self.config.isDocroot )
tooltip.src = encodeURI( self.pathCombine( self.currentDir, item.name ) ).replace( '#', '%23' ).replace( '?', '%3F' );
else
tooltip.src = self.api + "?api=proxy&dir=" + encodeURIComponent( self.currentDir ) + "&filename=" + encodeURIComponent( item.name );
tooltip.classList.add( 'imgpreview' ); tooltip.classList.add( 'imgpreview' );
return tooltip; return tooltip;
}, },
@@ -3279,6 +3282,7 @@ function IFM( params ) {
case "searchItems": $this->searchItems( $_REQUEST ); break; case "searchItems": $this->searchItems( $_REQUEST ); break;
case "getFolderTree": $this->getFolderTree( $_REQUEST ); break; case "getFolderTree": $this->getFolderTree( $_REQUEST ); break;
case "createArchive": $this->createArchive( $_REQUEST ); break; case "createArchive": $this->createArchive( $_REQUEST ); break;
case "proxy": $this->downloadFile( $_REQUEST, false ); break;
default: default:
$this->jsonResponse( array( "status" => "ERROR", "message" => "Invalid api action given" ) ); $this->jsonResponse( array( "status" => "ERROR", "message" => "Invalid api action given" ) );
break; break;
@@ -3657,7 +3661,7 @@ function IFM( params ) {
} }
// provides a file for downloading // provides a file for downloading
private function downloadFile( array $d ) { private function downloadFile( array $d, $forceDL=true ) {
if( $this->config['download'] != 1 ) if( $this->config['download'] != 1 )
$this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) ); $this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) );
elseif( $this->config['showhtdocs'] != 1 && ( substr( $d['filename'], 0, 3 ) == ".ht" || substr( $d['filename'],0,3 ) == ".ht" ) ) elseif( $this->config['showhtdocs'] != 1 && ( substr( $d['filename'], 0, 3 ) == ".ht" || substr( $d['filename'],0,3 ) == ".ht" ) )
@@ -3666,7 +3670,10 @@ function IFM( params ) {
$this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) ); $this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) );
else { else {
$this->chDirIfNecessary( $d["dir"] ); $this->chDirIfNecessary( $d["dir"] );
$this->fileDownload( $d['filename'] ); if( ! is_file( $d['filename' ] ) )
http_response_code( 404 );
else
$this->fileDownload( array( "file" => $d['filename'], "forceDL" => $forceDL ) );
} }
} }
@@ -3793,7 +3800,7 @@ function IFM( params ) {
else else
$d['filename'] = basename( getcwd() ); $d['filename'] = basename( getcwd() );
} }
$this->fileDownload( $dfile, $d['filename'] . ".zip" ); $this->fileDownload( array( "file" => $dfile, "name" => $d['filename'] . ".zip" ) );
} catch ( Exception $e ) { } catch ( Exception $e ) {
echo $this->l['error'] . " " . $e->getMessage(); echo $this->l['error'] . " " . $e->getMessage();
} finally { } finally {
@@ -4236,16 +4243,24 @@ function IFM( params ) {
else return true; else return true;
} }
private function fileDownload( $file, $name="" ) { private function fileDownload( array $options ) {
if( $options['forceDL'] )
$content_type = "application/octet-stream";
else
$content_type = mime_content_type( $options['file'] );
if( ! isset( $options['name'] ) || trim( $options['name'] ) == "" )
$options['name'] = basename( $options['file'] );
header( 'Content-Description: File Transfer' ); header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/octet-stream' ); header( 'Content-Type: ' . $content_type );
header( 'Content-Disposition: attachment; filename="' . ( trim( $name ) == "" ? basename( $file ) : $name ) . '"' ); header( 'Content-Disposition: attachment; filename="' . $options['name'] . '"' );
header( 'Expires: 0' ); header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate' ); header( 'Cache-Control: must-revalidate' );
header( 'Pragma: public' ); header( 'Pragma: public' );
header( 'Content-Length: ' . filesize( $file ) ); header( 'Content-Length: ' . filesize( $options['file'] ) );
$file_stream = fopen( $file, 'rb' ); $file_stream = fopen( $options['file'], 'rb' );
$stdout_stream = fopen('php://output', 'wb'); $stdout_stream = fopen('php://output', 'wb');
stream_copy_to_stream($file_stream, $stdout_stream); stream_copy_to_stream($file_stream, $stdout_stream);

View File

@@ -135,7 +135,7 @@ function IFM( params ) {
item.download.action = "download"; item.download.action = "download";
item.download.icon = "icon icon-download"; item.download.icon = "icon icon-download";
} }
if( item.icon.indexOf( 'file-image' ) !== -1 && self.config.isDocroot ) { if( item.icon.indexOf( 'file-image' ) !== -1 ) {
item.tooltip = 'data-toggle="tooltip"'; item.tooltip = 'data-toggle="tooltip"';
} }
if( self.config.extract && self.inArray( item.ext, ["zip","tar","tgz","tar.gz","tar.xz","tar.bz2"] ) ) { if( self.config.extract && self.inArray( item.ext, ["zip","tar","tgz","tar.gz","tar.xz","tar.bz2"] ) ) {
@@ -254,7 +254,10 @@ function IFM( params ) {
title: function() { title: function() {
var item = self.fileCache.find( x => x.guid == $(this).attr('id') ); var item = self.fileCache.find( x => x.guid == $(this).attr('id') );
var tooltip = document.createElement( 'img' ); var tooltip = document.createElement( 'img' );
tooltip.src = encodeURI( self.pathCombine( self.currentDir, item.name ) ).replace( '#', '%23' ).replace( '?', '%3F' ); if( self.config.isDocroot )
tooltip.src = encodeURI( self.pathCombine( self.currentDir, item.name ) ).replace( '#', '%23' ).replace( '?', '%3F' );
else
tooltip.src = self.api + "?api=proxy&dir=" + encodeURIComponent( self.currentDir ) + "&filename=" + encodeURIComponent( item.name );
tooltip.classList.add( 'imgpreview' ); tooltip.classList.add( 'imgpreview' );
return tooltip; return tooltip;
}, },

View File

@@ -271,6 +271,7 @@ f00bar;
case "searchItems": $this->searchItems( $_REQUEST ); break; case "searchItems": $this->searchItems( $_REQUEST ); break;
case "getFolderTree": $this->getFolderTree( $_REQUEST ); break; case "getFolderTree": $this->getFolderTree( $_REQUEST ); break;
case "createArchive": $this->createArchive( $_REQUEST ); break; case "createArchive": $this->createArchive( $_REQUEST ); break;
case "proxy": $this->downloadFile( $_REQUEST, false ); break;
default: default:
$this->jsonResponse( array( "status" => "ERROR", "message" => "Invalid api action given" ) ); $this->jsonResponse( array( "status" => "ERROR", "message" => "Invalid api action given" ) );
break; break;
@@ -649,7 +650,7 @@ f00bar;
} }
// provides a file for downloading // provides a file for downloading
private function downloadFile( array $d ) { private function downloadFile( array $d, $forceDL=true ) {
if( $this->config['download'] != 1 ) if( $this->config['download'] != 1 )
$this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) ); $this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) );
elseif( $this->config['showhtdocs'] != 1 && ( substr( $d['filename'], 0, 3 ) == ".ht" || substr( $d['filename'],0,3 ) == ".ht" ) ) elseif( $this->config['showhtdocs'] != 1 && ( substr( $d['filename'], 0, 3 ) == ".ht" || substr( $d['filename'],0,3 ) == ".ht" ) )
@@ -658,7 +659,10 @@ f00bar;
$this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) ); $this->jsonResponse( array( "status" => "ERROR", "message" => $this->l['nopermissions'] ) );
else { else {
$this->chDirIfNecessary( $d["dir"] ); $this->chDirIfNecessary( $d["dir"] );
$this->fileDownload( $d['filename'] ); if( ! is_file( $d['filename' ] ) )
http_response_code( 404 );
else
$this->fileDownload( array( "file" => $d['filename'], "forceDL" => $forceDL ) );
} }
} }
@@ -785,7 +789,7 @@ f00bar;
else else
$d['filename'] = basename( getcwd() ); $d['filename'] = basename( getcwd() );
} }
$this->fileDownload( $dfile, $d['filename'] . ".zip" ); $this->fileDownload( array( "file" => $dfile, "name" => $d['filename'] . ".zip" ) );
} catch ( Exception $e ) { } catch ( Exception $e ) {
echo $this->l['error'] . " " . $e->getMessage(); echo $this->l['error'] . " " . $e->getMessage();
} finally { } finally {
@@ -1228,16 +1232,24 @@ f00bar;
else return true; else return true;
} }
private function fileDownload( $file, $name="" ) { private function fileDownload( array $options ) {
if( $options['forceDL'] )
$content_type = "application/octet-stream";
else
$content_type = mime_content_type( $options['file'] );
if( ! isset( $options['name'] ) || trim( $options['name'] ) == "" )
$options['name'] = basename( $options['file'] );
header( 'Content-Description: File Transfer' ); header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/octet-stream' ); header( 'Content-Type: ' . $content_type );
header( 'Content-Disposition: attachment; filename="' . ( trim( $name ) == "" ? basename( $file ) : $name ) . '"' ); header( 'Content-Disposition: attachment; filename="' . $options['name'] . '"' );
header( 'Expires: 0' ); header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate' ); header( 'Cache-Control: must-revalidate' );
header( 'Pragma: public' ); header( 'Pragma: public' );
header( 'Content-Length: ' . filesize( $file ) ); header( 'Content-Length: ' . filesize( $options['file'] ) );
$file_stream = fopen( $file, 'rb' ); $file_stream = fopen( $options['file'], 'rb' );
$stdout_stream = fopen('php://output', 'wb'); $stdout_stream = fopen('php://output', 'wb');
stream_copy_to_stream($file_stream, $stdout_stream); stream_copy_to_stream($file_stream, $stdout_stream);