Youtube videos with emjois in title cause SQL-errors if multi-byte UTF-8 support is not enabled

Created on 20 June 2017, over 7 years ago
Updated 12 May 2023, over 1 year ago

While testing a recent media upgrade we noted this error when randomly uploading cat videos. Any that happen to have emojis in the title throw an sql-error.

https://www.youtube.com/watch?v=P2RkTCHTLbY
https://www.youtube.com/watch?v=-x7GjsfGvLE

The following error appears:

SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xF0\x9F\x98\xB9 F...' for column 'filename' at row 1
Notice: Undefined index: upload in file_entity_add_upload_submit() (line 349 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.pages.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 175 of /home/rcaoj/www/includes/entity.inc).
Notice: Trying to get property of non-object in file_entity_file_type() (line 45 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.file.inc).
Notice: Trying to get property of non-object in file_entity_file_type() (line 45 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.file.inc).
Notice: Trying to get property of non-object in file_entity_file_type() (line 45 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.file.inc).
Notice: Trying to get property of non-object in file_entity_file_type() (line 45 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.file.inc).
Notice: Undefined index: upload in file_entity_add_upload_submit() (line 349 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.pages.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 175 of /home/rcaoj/www/includes/entity.inc).
Notice: Trying to get property of non-object in file_entity_file_is_writeable() (line 2232 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.module).
Notice: Trying to get property of non-object in file_entity_add_upload_submit() (line 379 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.pages.inc).
Notice: Undefined index: upload in file_entity_add_upload_submit() (line 405 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.pages.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 175 of /home/rcaoj/www/includes/entity.inc).
Notice: Undefined index: upload in file_entity_add_upload_submit() (line 430 of /home/rcaoj/www/sites/all/modules/file_entity/file_entity.pages.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 175 of /home/rcaoj/www/includes/entity.inc).
An error occurred and no file was uploaded.

I have verified this error with a clean install of media_youtube 7.x-3.4 w/ Drupal 7.55 on simplytest.me

Contrib Dependencies installed:

  • File Entity 7.x-2.2
  • Media 7.x-2.8
💬 Support request
Status

Fixed

Version

3.0

Component

Code

Created by

🇨🇦Canada afoster

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇷🇴Romania reszli Târgu Mureș

    In my case, it was not possible to achive Multi-byte UTF-8 support on the server, so I needed to work around it
    My solution was to replace the original YouTube provider with a custom one that extends the original one and strips non-ASCII characters form the filename on preSave() method.

    add these hook implementations to mymodule.module file

    /**
     * Implements hook_media_internet_providers_alter().
     */
    function mymodule_media_media_internet_providers_alter(&$providers) {
      unset($providers['MediaInternetYouTubeHandler']);
    }
    
    /**
     * Implements hook_media_internet_providers().
     */
    function mymodule_media_media_internet_providers() {
      return [
        'MymoduleMediaInternetYouTubeHandler' => [
          'title' => t('YouTube'),
          'module' => 'mymodule',
          'weight' => 3,
        ],
      ];
    }
    

    create a new file under mymodule/includes/MymoduleMediaInternetYouTubeHandler.inc with the following content:

    
    /**
     * @file
     * Extends the MediaInternetYouTubeHandler class to handle YouTube videos.
     */
    
    /**
     * Implementation of MediaInternetBaseHandler.
     *
     * @see hook_media_internet_providers().
     */
    class MymoduleMediaInternetYouTubeHandler extends MediaInternetYouTubeHandler {
    
      /**
       * Before the file has been saved, implementors may do additional operations.
       *
       * @param object $file_obj
       */
      public function preSave(&$file_obj) {
        parent::preSave($file_obj);
        // Remove non-ASCII characters from the filename.
        $file_obj->filename = trim(preg_replace('/[^\x20-\x7E]/','', $file_obj->filename));
      }
    
    }
    
  • Status changed to Fixed over 1 year ago
  • 🇳🇴Norway steinmb

    That should work :) - I'll going to close this. Only one post in 6 years :)

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024