mirror of
https://code.castopod.org/adaures/castopod
synced 2025-05-24 02:52:01 +00:00
fix(import): add extension when downloading file without + truncate slug if too long
This commit is contained in:
parent
d86315ed94
commit
c5f18bb6dc
@ -131,9 +131,9 @@ class Mimes
|
|||||||
'rar' => ['application/vnd.rar', 'application/x-rar', 'application/rar', 'application/x-rar-compressed'],
|
'rar' => ['application/vnd.rar', 'application/x-rar', 'application/rar', 'application/x-rar-compressed'],
|
||||||
'mid' => 'audio/midi',
|
'mid' => 'audio/midi',
|
||||||
'midi' => 'audio/midi',
|
'midi' => 'audio/midi',
|
||||||
|
'mp3' => ['audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'],
|
||||||
'mpga' => 'audio/mpeg',
|
'mpga' => 'audio/mpeg',
|
||||||
'mp2' => 'audio/mpeg',
|
'mp2' => 'audio/mpeg',
|
||||||
'mp3' => ['audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'],
|
|
||||||
'aif' => ['audio/x-aiff', 'audio/aiff'],
|
'aif' => ['audio/x-aiff', 'audio/aiff'],
|
||||||
'aiff' => ['audio/x-aiff', 'audio/aiff'],
|
'aiff' => ['audio/x-aiff', 'audio/aiff'],
|
||||||
'aifc' => 'audio/x-aiff',
|
'aifc' => 'audio/x-aiff',
|
||||||
@ -306,10 +306,10 @@ class Mimes
|
|||||||
/**
|
/**
|
||||||
* Attempts to determine the best file extension for a given mime type.
|
* Attempts to determine the best file extension for a given mime type.
|
||||||
*
|
*
|
||||||
* @param string|null $proposedExtension - default extension (in case there is more than one with the same mime type)
|
* @param string $proposedExtension - default extension (in case there is more than one with the same mime type)
|
||||||
* @return string|null The extension determined, or null if unable to match.
|
* @return string|null The extension determined, or null if unable to match.
|
||||||
*/
|
*/
|
||||||
public static function guessExtensionFromType(string $type, string $proposedExtension = null): ?string
|
public static function guessExtensionFromType(string $type, string $proposedExtension = ''): ?string
|
||||||
{
|
{
|
||||||
$type = trim(strtolower($type), '. ');
|
$type = trim(strtolower($type), '. ');
|
||||||
|
|
||||||
|
@ -305,11 +305,10 @@ class PodcastImportController extends BaseController
|
|||||||
);
|
);
|
||||||
$nsContent = $item->children('http://purl.org/rss/1.0/modules/content/');
|
$nsContent = $item->children('http://purl.org/rss/1.0/modules/content/');
|
||||||
|
|
||||||
$slug = slugify(
|
$textToSlugify = $this->request->getPost('slug_field') === 'title'
|
||||||
$this->request->getPost('slug_field') === 'title'
|
|
||||||
? (string) $item->title
|
? (string) $item->title
|
||||||
: basename((string) $item->link),
|
: basename((string) $item->link);
|
||||||
);
|
$slug = slugify($textToSlugify, 185);
|
||||||
if (in_array($slug, $slugs, true)) {
|
if (in_array($slug, $slugs, true)) {
|
||||||
$slugNumber = 2;
|
$slugNumber = 2;
|
||||||
while (in_array($slug . '-' . $slugNumber, $slugs, true)) {
|
while (in_array($slug . '-' . $slugNumber, $slugs, true)) {
|
||||||
@ -348,7 +347,10 @@ class PodcastImportController extends BaseController
|
|||||||
'title' => $item->title,
|
'title' => $item->title,
|
||||||
'slug' => $slug,
|
'slug' => $slug,
|
||||||
'guid' => $item->guid ?? null,
|
'guid' => $item->guid ?? null,
|
||||||
'audio_file' => download_file((string) $item->enclosure->attributes()['url']),
|
'audio_file' => download_file(
|
||||||
|
(string) $item->enclosure->attributes()['url'],
|
||||||
|
(string) $item->enclosure->attributes()['type']
|
||||||
|
),
|
||||||
'description_markdown' => $converter->convert($itemDescriptionHtml),
|
'description_markdown' => $converter->convert($itemDescriptionHtml),
|
||||||
'description_html' => $itemDescriptionHtml,
|
'description_html' => $itemDescriptionHtml,
|
||||||
'image' => $episodeImage,
|
'image' => $episodeImage,
|
||||||
|
@ -11,6 +11,7 @@ declare(strict_types=1);
|
|||||||
use CodeIgniter\Files\File;
|
use CodeIgniter\Files\File;
|
||||||
use CodeIgniter\HTTP\Files\UploadedFile;
|
use CodeIgniter\HTTP\Files\UploadedFile;
|
||||||
use CodeIgniter\HTTP\ResponseInterface;
|
use CodeIgniter\HTTP\ResponseInterface;
|
||||||
|
use Config\Mimes;
|
||||||
use Config\Services;
|
use Config\Services;
|
||||||
|
|
||||||
if (! function_exists('save_media')) {
|
if (! function_exists('save_media')) {
|
||||||
@ -41,7 +42,7 @@ if (! function_exists('save_media')) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (! function_exists('download_file')) {
|
if (! function_exists('download_file')) {
|
||||||
function download_file(string $fileUrl): File
|
function download_file(string $fileUrl, string $mimetype = ''): File
|
||||||
{
|
{
|
||||||
$client = Services::curlrequest();
|
$client = Services::curlrequest();
|
||||||
|
|
||||||
@ -75,12 +76,15 @@ if (! function_exists('download_file')) {
|
|||||||
'http_errors' => false,
|
'http_errors' => false,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$fileExtension = pathinfo(parse_url($newFileUrl, PHP_URL_PATH), PATHINFO_EXTENSION);
|
||||||
|
$extension = $fileExtension === '' ? Mimes::guessExtensionFromType($mimetype) : $fileExtension;
|
||||||
$tmpFilename =
|
$tmpFilename =
|
||||||
time() .
|
time() .
|
||||||
'_' .
|
'_' .
|
||||||
bin2hex(random_bytes(10)) .
|
bin2hex(random_bytes(10)) .
|
||||||
'.' .
|
'.' .
|
||||||
pathinfo(parse_url($newFileUrl, PHP_URL_PATH), PATHINFO_EXTENSION);
|
$extension;
|
||||||
$tmpFilePath = WRITEPATH . 'uploads/' . $tmpFilename;
|
$tmpFilePath = WRITEPATH . 'uploads/' . $tmpFilename;
|
||||||
file_put_contents($tmpFilePath, $response->getBody());
|
file_put_contents($tmpFilePath, $response->getBody());
|
||||||
|
|
||||||
|
@ -23,8 +23,13 @@ if (! function_exists('get_browser_language')) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (! function_exists('slugify')) {
|
if (! function_exists('slugify')) {
|
||||||
function slugify(string $text): string
|
function slugify(string $text, int $maxLength = 191): string
|
||||||
{
|
{
|
||||||
|
// trim text to the nearest whole word if too long
|
||||||
|
if (strlen($text) > $maxLength) {
|
||||||
|
$text = substr($text, 0, strrpos(substr($text, 0, $maxLength), ' '));
|
||||||
|
}
|
||||||
|
|
||||||
// replace non letter or digits by -
|
// replace non letter or digits by -
|
||||||
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
|
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
|
||||||
|
|
||||||
|
@ -89,18 +89,6 @@
|
|||||||
|
|
||||||
<?= form_fieldset('', ['class' => 'flex flex-col mb-4']) ?>
|
<?= form_fieldset('', ['class' => 'flex flex-col mb-4']) ?>
|
||||||
<legend><?= lang('PodcastImport.slug_field.label') ?></legend>
|
<legend><?= lang('PodcastImport.slug_field.label') ?></legend>
|
||||||
<label for="link" class="inline-flex items-center">
|
|
||||||
<?= form_radio(
|
|
||||||
[
|
|
||||||
'id' => 'link',
|
|
||||||
'name' => 'slug_field',
|
|
||||||
'class' => 'form-radio text-pine-700',
|
|
||||||
],
|
|
||||||
'link',
|
|
||||||
old('slug_field') ? old('slug_field') == 'link' : true,
|
|
||||||
) ?>
|
|
||||||
<span class="ml-2"><?= lang('PodcastImport.slug_field.link') ?></span>
|
|
||||||
</label>
|
|
||||||
<label for="title" class="inline-flex items-center">
|
<label for="title" class="inline-flex items-center">
|
||||||
<?= form_radio(
|
<?= form_radio(
|
||||||
[
|
[
|
||||||
@ -109,10 +97,22 @@
|
|||||||
'class' => 'form-radio text-pine-700',
|
'class' => 'form-radio text-pine-700',
|
||||||
],
|
],
|
||||||
'title',
|
'title',
|
||||||
old('slug_field') && old('slug_field') == 'title',
|
old('slug_field') ? old('slug_field') === 'title' : true,
|
||||||
) ?>
|
) ?>
|
||||||
<span class="ml-2"><?= lang('PodcastImport.slug_field.title') ?></span>
|
<span class="ml-2"><?= lang('PodcastImport.slug_field.title') ?></span>
|
||||||
</label>
|
</label>
|
||||||
|
<label for="link" class="inline-flex items-center">
|
||||||
|
<?= form_radio(
|
||||||
|
[
|
||||||
|
'id' => 'link',
|
||||||
|
'name' => 'slug_field',
|
||||||
|
'class' => 'form-radio text-pine-700',
|
||||||
|
],
|
||||||
|
'link',
|
||||||
|
old('slug_field') && old('slug_field') === 'link',
|
||||||
|
) ?>
|
||||||
|
<span class="ml-2"><?= lang('PodcastImport.slug_field.link') ?></span>
|
||||||
|
</label>
|
||||||
<?= form_fieldset_close() ?>
|
<?= form_fieldset_close() ?>
|
||||||
|
|
||||||
<?= form_fieldset('', ['class' => 'flex flex-col mb-4']) ?>
|
<?= form_fieldset('', ['class' => 'flex flex-col mb-4']) ?>
|
||||||
|
20
composer.lock
generated
20
composer.lock
generated
@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "371c6aac9ca489338bf3b3fa06ffdb21",
|
"content-hash": "49719de3dd6af8c394ea0553e2180b5a",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "brick/math",
|
"name": "brick/math",
|
||||||
@ -494,24 +494,24 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "james-heinrich/getid3",
|
"name": "james-heinrich/getid3",
|
||||||
"version": "v2.0.0-beta3",
|
"version": "v2.0.0-beta4",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/JamesHeinrich/getID3.git",
|
"url": "https://github.com/JamesHeinrich/getID3.git",
|
||||||
"reference": "5515a2d24667c3c0ff49fdcbdadc405c0880c7a2"
|
"reference": "5ad79104e937e7d9c8a9141a97e1f063dd1123f8"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/JamesHeinrich/getID3/zipball/5515a2d24667c3c0ff49fdcbdadc405c0880c7a2",
|
"url": "https://api.github.com/repos/JamesHeinrich/getID3/zipball/5ad79104e937e7d9c8a9141a97e1f063dd1123f8",
|
||||||
"reference": "5515a2d24667c3c0ff49fdcbdadc405c0880c7a2",
|
"reference": "5ad79104e937e7d9c8a9141a97e1f063dd1123f8",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.4.0"
|
"php": ">=5.4.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"jakub-onderka/php-parallel-lint": "^0.9 || ^1.0",
|
"php-parallel-lint/php-parallel-lint": "^1.0",
|
||||||
"phpunit/phpunit": "^4.8|^5.0"
|
"phpunit/phpunit": "^4.8 || ^5.0 || ^6.1 || ^7.5 || ^8.5"
|
||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"ext-SimpleXML": "SimpleXML extension is required to analyze RIFF/WAV/BWF audio files (also requires `ext-libxml`).",
|
"ext-SimpleXML": "SimpleXML extension is required to analyze RIFF/WAV/BWF audio files (also requires `ext-libxml`).",
|
||||||
@ -562,9 +562,9 @@
|
|||||||
"keywords": ["audio", "codecs", "id3", "metadata", "tags", "video"],
|
"keywords": ["audio", "codecs", "id3", "metadata", "tags", "video"],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/JamesHeinrich/getID3/issues",
|
"issues": "https://github.com/JamesHeinrich/getID3/issues",
|
||||||
"source": "https://github.com/JamesHeinrich/getID3/tree/2.0"
|
"source": "https://github.com/JamesHeinrich/getID3/tree/v2.0.0-beta4"
|
||||||
},
|
},
|
||||||
"time": "2020-07-21T08:15:44+00:00"
|
"time": "2021-10-06T16:23:45+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "kint-php/kint",
|
"name": "kint-php/kint",
|
||||||
@ -7465,5 +7465,5 @@
|
|||||||
"php": "^8.0"
|
"php": "^8.0"
|
||||||
},
|
},
|
||||||
"platform-dev": [],
|
"platform-dev": [],
|
||||||
"plugin-api-version": "2.0.0"
|
"plugin-api-version": "2.1.0"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user