2020-06-12 19:31:10 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-12 19:31:10 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2020-06-12 19:31:10 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\Files\File;
|
2021-12-15 15:44:58 +00:00
|
|
|
use Config\Mimes;
|
2021-05-06 14:00:48 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! function_exists('download_file')) {
|
2021-12-15 15:44:58 +00:00
|
|
|
function download_file(string $fileUrl, string $mimetype = ''): File
|
2021-05-06 14:00:48 +00:00
|
|
|
{
|
2024-06-05 18:46:23 +00:00
|
|
|
$fileExtension = pathinfo(parse_url($fileUrl, PHP_URL_PATH), PATHINFO_EXTENSION);
|
2021-12-15 15:44:58 +00:00
|
|
|
$extension = $fileExtension === '' ? Mimes::guessExtensionFromType($mimetype) : $fileExtension;
|
2021-05-06 14:00:48 +00:00
|
|
|
$tmpFilename =
|
|
|
|
time() .
|
|
|
|
'_' .
|
|
|
|
bin2hex(random_bytes(10)) .
|
|
|
|
'.' .
|
2021-12-15 15:44:58 +00:00
|
|
|
$extension;
|
2024-06-05 18:46:23 +00:00
|
|
|
$tmpfilePath = WRITEPATH . 'uploads/' . $tmpFilename;
|
|
|
|
|
|
|
|
$file = fopen($tmpfilePath, 'w');
|
|
|
|
$ch = curl_init();
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $fileUrl);
|
|
|
|
|
|
|
|
// output directly to file
|
|
|
|
curl_setopt($ch, CURLOPT_FILE, $file);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['User-Agent: Castopod/' . CP_VERSION]);
|
|
|
|
|
|
|
|
// follow redirects up to 20, like Apple Podcasts
|
2024-12-17 15:06:08 +00:00
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
2024-06-05 18:46:23 +00:00
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
|
|
|
|
|
|
|
|
curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
fclose($file);
|
2021-05-06 14:00:48 +00:00
|
|
|
|
2024-06-05 18:46:23 +00:00
|
|
|
return new File($tmpfilePath);
|
2020-06-12 19:31:10 +00:00
|
|
|
}
|
|
|
|
}
|