Commit c5f18bb6 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

fix(import): add extension when downloading file without + truncate slug if too long

parent d86315ed
Loading
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -131,9 +131,9 @@ class Mimes
        'rar' => ['application/vnd.rar', 'application/x-rar', 'application/rar', 'application/x-rar-compressed'],
        'mid' => 'audio/midi',
        'midi' => 'audio/midi',
        'mp3' => ['audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'],
        'mpga' => 'audio/mpeg',
        'mp2' => 'audio/mpeg',
        'mp3' => ['audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'],
        'aif' => ['audio/x-aiff', 'audio/aiff'],
        'aiff' => ['audio/x-aiff', 'audio/aiff'],
        'aifc' => 'audio/x-aiff',
@@ -306,10 +306,10 @@ class Mimes
    /**
     * 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.
     */
    public static function guessExtensionFromType(string $type, string $proposedExtension = null): ?string
    public static function guessExtensionFromType(string $type, string $proposedExtension = ''): ?string
    {
        $type = trim(strtolower($type), '. ');

+8 −6
Original line number Diff line number Diff line
@@ -305,11 +305,10 @@ class PodcastImportController extends BaseController
            );
            $nsContent = $item->children('http://purl.org/rss/1.0/modules/content/');

            $slug = slugify(
                $this->request->getPost('slug_field') === 'title'
            $textToSlugify = $this->request->getPost('slug_field') === 'title'
            ? (string) $item->title
                    : basename((string) $item->link),
            );
            : basename((string) $item->link);
            $slug = slugify($textToSlugify, 185);
            if (in_array($slug, $slugs, true)) {
                $slugNumber = 2;
                while (in_array($slug . '-' . $slugNumber, $slugs, true)) {
@@ -348,7 +347,10 @@ class PodcastImportController extends BaseController
                'title' => $item->title,
                'slug' => $slug,
                '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_html' => $itemDescriptionHtml,
                'image' => $episodeImage,
+6 −2
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ declare(strict_types=1);
use CodeIgniter\Files\File;
use CodeIgniter\HTTP\Files\UploadedFile;
use CodeIgniter\HTTP\ResponseInterface;
use Config\Mimes;
use Config\Services;

if (! function_exists('save_media')) {
@@ -41,7 +42,7 @@ if (! function_exists('save_media')) {
}

if (! function_exists('download_file')) {
    function download_file(string $fileUrl): File
    function download_file(string $fileUrl, string $mimetype = ''): File
    {
        $client = Services::curlrequest();

@@ -75,12 +76,15 @@ if (! function_exists('download_file')) {
                'http_errors' => false,
            ]);
        }

        $fileExtension = pathinfo(parse_url($newFileUrl, PHP_URL_PATH), PATHINFO_EXTENSION);
        $extension = $fileExtension === '' ? Mimes::guessExtensionFromType($mimetype) : $fileExtension;
        $tmpFilename =
            time() .
            '_' .
            bin2hex(random_bytes(10)) .
            '.' .
            pathinfo(parse_url($newFileUrl, PHP_URL_PATH), PATHINFO_EXTENSION);
            $extension;
        $tmpFilePath = WRITEPATH . 'uploads/' . $tmpFilename;
        file_put_contents($tmpFilePath, $response->getBody());

+6 −1
Original line number Diff line number Diff line
@@ -23,8 +23,13 @@ if (! function_exists('get_browser_language')) {
}

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 -
        $text = preg_replace('~[^\pL\d]+~u', '-', $text);

+10 −10
Original line number Diff line number Diff line
@@ -89,29 +89,29 @@

<?= form_fieldset('', ['class' => 'flex flex-col mb-4']) ?>
    <legend><?= lang('PodcastImport.slug_field.label') ?></legend>
    <label for="link" class="inline-flex items-center">
    <label for="title" class="inline-flex items-center">
        <?= form_radio(
            [
                'id' => 'link',
                'id' => 'title',
                'name' => 'slug_field',
                'class' => 'form-radio text-pine-700',
            ],
            'link',
            old('slug_field') ? old('slug_field') == 'link' : true,
            'title',
            old('slug_field') ? old('slug_field') === 'title' : true,
        ) ?>
        <span class="ml-2"><?= lang('PodcastImport.slug_field.link') ?></span>
        <span class="ml-2"><?= lang('PodcastImport.slug_field.title') ?></span>
    </label>
    <label for="title" class="inline-flex items-center">
    <label for="link" class="inline-flex items-center">
        <?= form_radio(
            [
                'id' => 'title',
                'id' => 'link',
                'name' => 'slug_field',
                'class' => 'form-radio text-pine-700',
            ],
            'title',
            old('slug_field') && old('slug_field') == 'title',
            'link',
            old('slug_field') && old('slug_field') === 'link',
        ) ?>
        <span class="ml-2"><?= lang('PodcastImport.slug_field.title') ?></span>
        <span class="ml-2"><?= lang('PodcastImport.slug_field.link') ?></span>
    </label>
<?= form_fieldset_close() ?>

Loading