-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15689 from snipe/better_handle_inline_files
Better handle inline files in file listing
- Loading branch information
Showing
20 changed files
with
325 additions
and
945 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1123,6 +1123,7 @@ public static function filetype_icon($filename) | |
'png' => 'far fa-image', | ||
'webp' => 'far fa-image', | ||
'avif' => 'far fa-image', | ||
'svg' => 'fas fa-vector-square', | ||
// word | ||
'doc' => 'far fa-file-word', | ||
'docx' => 'far fa-file-word', | ||
|
@@ -1135,7 +1136,7 @@ public static function filetype_icon($filename) | |
//Text | ||
'txt' => 'far fa-file-alt', | ||
'rtf' => 'far fa-file-alt', | ||
'xml' => 'far fa-file-alt', | ||
'xml' => 'fas fa-code', | ||
// Misc | ||
'pdf' => 'far fa-file-pdf', | ||
'lic' => 'far fa-save', | ||
|
@@ -1148,41 +1149,7 @@ public static function filetype_icon($filename) | |
return 'far fa-file'; | ||
} | ||
|
||
public static function show_file_inline($filename) | ||
{ | ||
$extension = substr(strrchr($filename, '.'), 1); | ||
|
||
if ($extension) { | ||
switch ($extension) { | ||
case 'jpg': | ||
case 'jpeg': | ||
case 'gif': | ||
case 'png': | ||
case 'webp': | ||
case 'avif': | ||
return true; | ||
break; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Generate a random encrypted password. | ||
* | ||
* @author Wes Hulette <[email protected]> | ||
* | ||
* @since 5.0.0 | ||
* | ||
* @return string | ||
*/ | ||
public static function generateEncyrptedPassword(): string | ||
{ | ||
return bcrypt(self::generateUnencryptedPassword()); | ||
} | ||
|
||
/** | ||
* Get a random unencrypted password. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
use Illuminate\Http\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
use Illuminate\Contracts\Filesystem\FileNotFoundException; | ||
class StorageHelper | ||
{ | ||
public static function downloader($filename, $disk = 'default') : BinaryFileResponse | RedirectResponse | StreamedResponse | ||
|
@@ -25,4 +26,64 @@ public static function downloader($filename, $disk = 'default') : BinaryFileResp | |
return Storage::disk($disk)->download($filename); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* This determines the file types that should be allowed inline and checks their fileinfo extension | ||
* to determine that they are safe to display inline. | ||
* | ||
* @author <A. Gianotto> [<[email protected]]> | ||
* @since v7.0.14 | ||
* @param $file_with_path | ||
* @return bool | ||
*/ | ||
public static function allowSafeInline($file_with_path) { | ||
|
||
$allowed_inline = [ | ||
'pdf', | ||
'svg', | ||
'jpg', | ||
'gif', | ||
'svg', | ||
'avif', | ||
'webp', | ||
'png', | ||
]; | ||
|
||
|
||
// The file exists and is allowed to be displayed inline | ||
if (Storage::exists($file_with_path) && (in_array(pathinfo($file_with_path, PATHINFO_EXTENSION), $allowed_inline))) { | ||
return true; | ||
} | ||
return false; | ||
|
||
} | ||
|
||
/** | ||
* Decide whether to show the file inline or download it. | ||
*/ | ||
public static function showOrDownloadFile($file, $filename) { | ||
|
||
$headers = []; | ||
|
||
if (request('inline') == 'true') { | ||
|
||
$headers = [ | ||
'Content-Disposition' => 'inline', | ||
]; | ||
|
||
// This is NOT allowed as inline - force it to be displayed as text in the browser | ||
if (self::allowSafeInline($file) != true) { | ||
$headers = array_merge($headers, ['Content-Type' => 'text/plain']); | ||
} | ||
} | ||
|
||
// Everything else seems okay, but the file doesn't exist on the server. | ||
if (Storage::missing($file)) { | ||
throw new FileNotFoundException(); | ||
} | ||
|
||
return Storage::download($file, $filename, $headers); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.