mirror of
https://github.com/getgrav/grav.git
synced 2025-02-20 19:56:53 +01:00
Improved Utils::download() method to allow overrides on download name, mime and expires header
This commit is contained in:
parent
fbcaf991aa
commit
e6447f7203
|
|
@ -1,6 +1,8 @@
|
|||
# v1.7.27
|
||||
## 01/04/2022
|
||||
|
||||
1. [](#improved)
|
||||
* Improved `Utils::download()` method to allow overrides on download name, mime and expires header
|
||||
3. [](#bugfix)
|
||||
* Fixed CLI `--env` and `--lang` options having no effect if they aren't added before all the other options
|
||||
|
||||
|
|
|
|||
|
|
@ -653,16 +653,17 @@ abstract class Utils
|
|||
* @param bool $force_download as opposed to letting browser choose if to download or render
|
||||
* @param int $sec Throttling, try 0.1 for some speed throttling of downloads
|
||||
* @param int $bytes Size of chunks to send in bytes. Default is 1024
|
||||
* @param array $options Extra options: [mime, download_name, expires]
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function download($file, $force_download = true, $sec = 0, $bytes = 1024)
|
||||
public static function download($file, $force_download = true, $sec = 0, $bytes = 1024, array $options = [])
|
||||
{
|
||||
if (file_exists($file)) {
|
||||
// fire download event
|
||||
Grav::instance()->fireEvent('onBeforeDownload', new Event(['file' => $file]));
|
||||
Grav::instance()->fireEvent('onBeforeDownload', new Event(['file' => $file, 'options' => &$options]));
|
||||
|
||||
$file_parts = pathinfo($file);
|
||||
$mimetype = static::getMimeByExtension($file_parts['extension']);
|
||||
$mimetype = $options['mime'] ?? static::getMimeByExtension($file_parts['extension']);
|
||||
$size = filesize($file); // File size
|
||||
|
||||
// clean all buffers
|
||||
|
|
@ -680,7 +681,7 @@ abstract class Utils
|
|||
|
||||
if ($force_download) {
|
||||
// output the regular HTTP headers
|
||||
header('Content-Disposition: attachment; filename="' . $file_parts['basename'] . '"');
|
||||
header('Content-Disposition: attachment; filename="' . ($options['download_name'] ?? $file_parts['basename']) . '"');
|
||||
}
|
||||
|
||||
// multipart-download and download resuming support
|
||||
|
|
@ -704,7 +705,7 @@ abstract class Utils
|
|||
header('Content-Length: ' . $size);
|
||||
|
||||
if (Grav::instance()['config']->get('system.cache.enabled')) {
|
||||
$expires = Grav::instance()['config']->get('system.pages.expires');
|
||||
$expires = $options['expires'] ?? Grav::instance()['config']->get('system.pages.expires');
|
||||
if ($expires > 0) {
|
||||
$expires_date = gmdate('D, d M Y H:i:s T', time() + $expires);
|
||||
header('Cache-Control: max-age=' . $expires);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user