fix(rss-import): add Castopod user-agent, handle redirects for downloaded files, add Content namespace

This commit is contained in:
Benjamin Bellamy 2021-02-11 17:41:20 +00:00
parent 3eecb82de8
commit 214243b3fe
5 changed files with 83 additions and 33 deletions

View File

@ -76,6 +76,7 @@ class PodcastImport extends BaseController
->with('errors', $this->validator->getErrors()); ->with('errors', $this->validator->getErrors());
} }
try { try {
ini_set('user_agent', 'Castopod/' . CP_VERSION);
$feed = simplexml_load_file( $feed = simplexml_load_file(
$this->request->getPost('imported_feed_url') $this->request->getPost('imported_feed_url')
); );
@ -98,6 +99,9 @@ class PodcastImport extends BaseController
$nsPodcast = $feed->channel[0]->children( $nsPodcast = $feed->channel[0]->children(
'https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md' 'https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md'
); );
$nsContent = $feed->channel[0]->children(
'http://purl.org/rss/1.0/modules/content/'
);
if ((string) $nsPodcast->locked === 'yes') { if ((string) $nsPodcast->locked === 'yes') {
return redirect() return redirect()
@ -313,6 +317,9 @@ class PodcastImport extends BaseController
$nsPodcast = $item->children( $nsPodcast = $item->children(
'https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md' 'https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md'
); );
$nsContent = $item->children(
'http://purl.org/rss/1.0/modules/content/'
);
$slug = slugify( $slug = slugify(
$this->request->getPost('slug_field') === 'title' $this->request->getPost('slug_field') === 'title'
@ -328,13 +335,21 @@ class PodcastImport extends BaseController
} }
$slugs[] = $slug; $slugs[] = $slug;
$itemDescriptionHtml = $itemDescriptionHtml = null;
$this->request->getPost('description_field') === 'summary' switch ($this->request->getPost('description_field')) {
? $nsItunes->summary case 'content':
: ($this->request->getPost('description_field') === $itemDescriptionHtml = $nsContent->encoded;
'subtitle_summary' break;
? $nsItunes->subtitle . '<br/>' . $nsItunes->summary case 'summary':
: $item->description); $itemDescriptionHtml = $nsItunes->summary;
break;
case 'subtitle_summary':
$itemDescriptionHtml =
$nsItunes->subtitle . '<br/>' . $nsItunes->summary;
break;
default:
$itemDescriptionHtml = $item->description;
}
$newEpisode = new \App\Entities\Episode([ $newEpisode = new \App\Entities\Episode([
'podcast_id' => $newPodcastId, 'podcast_id' => $newPodcastId,

View File

@ -6,6 +6,8 @@
* @link https://castopod.org/ * @link https://castopod.org/
*/ */
use CodeIgniter\HTTP\ResponseInterface;
/** /**
* Saves a file to the corresponding podcast folder in `public/media` * Saves a file to the corresponding podcast folder in `public/media`
* *
@ -34,14 +36,50 @@ function save_podcast_media($file, $podcast_name, $media_name)
function download_file($fileUrl) function download_file($fileUrl)
{ {
$client = \Config\Services::curlrequest();
$uri = new \CodeIgniter\HTTP\URI($fileUrl);
$response = $client->get($uri, [
'headers' => [
'User-Agent' => 'Castopod/' . CP_VERSION,
],
]);
// redirect to new file location
$newFileUrl = $fileUrl;
while (
in_array(
$response->getStatusCode(),
[
ResponseInterface::HTTP_MOVED_PERMANENTLY,
ResponseInterface::HTTP_FOUND,
ResponseInterface::HTTP_SEE_OTHER,
ResponseInterface::HTTP_NOT_MODIFIED,
ResponseInterface::HTTP_TEMPORARY_REDIRECT,
ResponseInterface::HTTP_PERMANENT_REDIRECT,
],
true
)
) {
$newFileUrl = (string) trim(
$response->getHeader('location')->getValue()
);
$newLocation = new \CodeIgniter\HTTP\URI($newFileUrl);
$response = $client->get($newLocation, [
'headers' => [
'User-Agent' => 'Castopod/' . CP_VERSION,
],
'http_errors' => false,
]);
}
$tmpFilename = $tmpFilename =
time() . time() .
'_' . '_' .
bin2hex(random_bytes(10)) . bin2hex(random_bytes(10)) .
'.' . '.' .
pathinfo($fileUrl, PATHINFO_EXTENSION); pathinfo($newFileUrl, PATHINFO_EXTENSION);
$tmpFilePath = WRITEPATH . 'uploads/' . $tmpFilename; $tmpFilePath = WRITEPATH . 'uploads/' . $tmpFilename;
file_put_contents($tmpFilePath, file_get_contents($fileUrl)); file_put_contents($tmpFilePath, $response->getBody());
return new \CodeIgniter\Files\File($tmpFilePath); return new \CodeIgniter\Files\File($tmpFilePath);
} }

View File

@ -25,13 +25,8 @@ return [
'link' => '&lt;link&gt;', 'link' => '&lt;link&gt;',
'title' => '&lt;title&gt;', 'title' => '&lt;title&gt;',
], ],
'description_field' => [ 'description_field' =>
'label' => 'Source field used for episode description / show notes', 'Source field used for episode description / show notes',
'description' => '&lt;description&gt;',
'summary' => '&lt;itunes:summary&gt;',
'subtitle_summary' =>
'&lt;itunes:subtitle&gt; + &lt;itunes:summary&gt;',
],
'force_renumber' => 'Force episodes renumbering', 'force_renumber' => 'Force episodes renumbering',
'force_renumber_hint' => 'force_renumber_hint' =>
'Use this if your podcast does not have episode numbers but wish to set them during import.', 'Use this if your podcast does not have episode numbers but wish to set them during import.',

View File

@ -26,13 +26,7 @@ return [
'link' => '&lt;link&gt; (adresse)', 'link' => '&lt;link&gt; (adresse)',
'title' => '&lt;title&gt; (titre)', 'title' => '&lt;title&gt; (titre)',
], ],
'description_field' => [ 'description_field' => 'Champs pour la description des épisodes',
'label' => 'Champs pour la description des épisodes',
'description' => '&lt;description&gt;',
'summary' => '&lt;itunes:summary&gt;',
'subtitle_summary' =>
'&lt;itunes:subtitle&gt; + &lt;itunes:summary&gt;',
],
'force_renumber' => 'Forcer la re-numérotation des épisodes', 'force_renumber' => 'Forcer la re-numérotation des épisodes',
'force_renumber_hint' => 'force_renumber_hint' =>
'Utilisez ceci si le podcast à importer ne contient pas de numéros dépisodes mais que vous souhaitez en ajouter pendant limport.', 'Utilisez ceci si le podcast à importer ne contient pas de numéros dépisodes mais que vous souhaitez en ajouter pendant limport.',

View File

@ -111,7 +111,7 @@
<?= form_fieldset_close() ?> <?= form_fieldset_close() ?>
<?= form_fieldset('', ['class' => 'flex flex-col mb-4']) ?> <?= form_fieldset('', ['class' => 'flex flex-col mb-4']) ?>
<legend><?= lang('PodcastImport.description_field.label') ?></legend> <legend><?= lang('PodcastImport.description_field') ?></legend>
<label for="description" class="inline-flex items-center"> <label for="description" class="inline-flex items-center">
<?= form_radio( <?= form_radio(
[ [
@ -124,9 +124,7 @@
? old('description_field') == 'description' ? old('description_field') == 'description'
: true : true
) ?> ) ?>
<span class="ml-2"><?= lang( <span class="ml-2">&lt;description&gt;</span>
'PodcastImport.description_field.description'
) ?></span>
</label> </label>
<label for="summary" class="inline-flex items-center"> <label for="summary" class="inline-flex items-center">
<?= form_radio( <?= form_radio(
@ -140,9 +138,7 @@
? old('description_field') == 'summary' ? old('description_field') == 'summary'
: false : false
) ?> ) ?>
<span class="ml-2"><?= lang( <span class="ml-2">&lt;itunes:summary&gt;</span>
'PodcastImport.description_field.summary'
) ?></span>
</label> </label>
<label for="subtitle_summary" class="inline-flex items-center"> <label for="subtitle_summary" class="inline-flex items-center">
<?= form_radio( <?= form_radio(
@ -156,9 +152,21 @@
? old('description_field') == 'subtitle_summary' ? old('description_field') == 'subtitle_summary'
: false : false
) ?> ) ?>
<span class="ml-2"><?= lang( <span class="ml-2">&lt;itunes:subtitle&gt; + &lt;itunes:summary&gt;</span>
'PodcastImport.description_field.subtitle_summary' </label>
) ?></span> <label for="content" class="inline-flex items-center">
<?= form_radio(
[
'id' => 'content',
'name' => 'description_field',
'class' => 'form-radio text-green-500',
],
'content',
old('description_field')
? old('description_field') == 'content'
: false
) ?>
<span class="ml-2">&lt;content:encoded&gt;</span>
</label> </label>
<?= form_fieldset_close() ?> <?= form_fieldset_close() ?>