Update hpt controller to support DISPOSITION_ATTACHMENT and consider general memory issues

Created on 14 July 2023, over 1 year ago

Problem/Motivation

The hpt controller uses DISPOSITION_INLINE when serving the contents to the hospital price file.

The only way to control whether the file is downloaded vs loaded in the browser is to rely on the download attribute on links. Otherwise we rely on default browser behavior. For instance, in Chrome and xml will load in the browser while a csv will download automatically no matter what. Perhaps there is interest in having a global configuration setting to force download through the controller rather than through the link.

Further, while working on πŸ“Œ Allow upload of zipped hospital price file Fixed , I learned that huge files can result in memory problems when served by the controller. During that issue, the controller was improved by using `fpassthru` rather than `file_get_contents`. However, if the fie is larger than some limit (say 50% or 75% of the memory limit?) would it be wise to use DISPOSITION_ATTACHMENT instead of DISPOSITION_INLINE? Or would it be more important to avoid caching a response like that? Would that be the bigger memory issue? Should this response never be cached? Or should it simply always use DISPOSITION_ATTACHMENT?

There's some code in the color module that is careful about memory usage from which we may be able to copy some logic:

// Make sure enough memory is available.
  if (isset($info['base_image'])) {
    // Fetch source image dimensions.
    $source = \Drupal::service('extension.list.theme')->getPath($theme) . '/' . $info['base_image'];
    [$width, $height] = getimagesize($source);

    // We need at least a copy of the source and a target buffer of the same
    // size (both at 32bpp).
    $required = $width * $height * 8;
    // We intend to prevent color scheme changes if there isn't enough memory
    // available.  memory_get_usage(TRUE) returns a more accurate number than
    // memory_get_usage(), therefore we won't inadvertently reject a color
    // scheme change based on a faulty memory calculation.
    $usage = memory_get_usage(TRUE);
    $memory_limit = ini_get('memory_limit');
    $size = Bytes::toNumber($memory_limit);
    if (!Environment::checkMemoryLimit($usage + $required, $memory_limit)) {
      \Drupal::messenger()->addError(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size more. Check the <a href="http://php.net/manual/ini.core.php#ini.sect.resource-limits">PHP documentation</a> for more information.', ['%size' => format_size($usage + $required - $size)]));
      return;
    }
  }

Proposed resolution

not sure

Remaining tasks

User interface changes

API changes

Data model changes

πŸ“Œ Task
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States danflanagan8 St. Louis, US

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

Comments & Activities

Production build 0.71.5 2024