You are here

plupload.module in Plupload integration 8

Implementation of plupload.module.

File

plupload.module
View source
<?php

/**
 * @file
 * Implementation of plupload.module.
 */
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Environment;

/**
 * Implements hook_library_info_alter().
 *
 * Adds dynamic parts of library definition.
 */
function plupload_library_info_alter(&$library_definition, $extension) {
  if ($extension == 'plupload') {
    $library_definition['plupload']['settings']['plupload']['_default']['max_file_size'] = Environment::getUploadMaxSize() . 'b';
    $library_definition['plupload']['settings']['plupload']['_default']['chunk_size'] = Bytes::toInt(ini_get('post_max_size')) . 'b';
    if (\Drupal::service('module_handler')
      ->moduleExists('locale')) {
      $library_definition['plupload']['js']['js/i18n.js'] = [
        'scope' => 'footer',
      ];
    }
  }
}

/**
 * Fix the temporary filename provided by the plupload library.
 *
 * Newer versions of the plupload JavaScript library upload temporary files
 * with names that contain the intended final prefix of the uploaded file
 * (e.g., ".jpg" or ".png"). Older versions of the plupload library always use
 * ".tmp" as the temporary file extension.
 *
 * We prefer the latter behavior, since although the plupload temporary
 * directory where these files live is always expected to be private (and we
 * protect it via .htaccess; see plupload_handle_uploads()), in case it ever
 * isn't we don't want people to be able to upload files with an arbitrary
 * extension into that directory.
 *
 * This function therefore fixes the plupload temporary filenames so that they
 * will always use a ".tmp" extension.
 *
 * @param string $filename
 *   The original temporary filename provided by the plupload library.
 *
 * @return string
 *   The corrected temporary filename, with a ".tmp" extension replacing the
 *   original one.
 */
function _plupload_fix_temporary_filename($filename) {
  $pos = strpos($filename, '.');
  if ($pos !== FALSE) {
    $filename = substr_replace($filename, '.tmp', $pos);
  }
  return $filename;
}

/**
 * Helper function to add defaults to $element['#upload_validators'].
 */
function _plupload_default_upload_validators() {
  return [
    // See file_save_upload() for details.
    'file_validate_extensions' => [
      'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp',
    ],
  ];
}

Functions

Namesort descending Description
plupload_library_info_alter Implements hook_library_info_alter().
_plupload_default_upload_validators Helper function to add defaults to $element['#upload_validators'].
_plupload_fix_temporary_filename Fix the temporary filename provided by the plupload library.