modules/acm_sku/src/Entity/SKU.php
protected function downloadMediaImage(array $data) {
    // Preparing args for all info/error messages.
    $args = ['@file' => $data['file'], '@sku_id' => $this->id()];

    // Download the file contents.
    try {
      $file_data = \Drupal::httpClient()->get($data['file'])->getBody();
    }
    catch (RequestException $e) {
      watchdog_exception('acq_commerce', $e);
    }
    // Check to ensure empty file is not saved in SKU.
    if (empty($file_data)) {
      throw new \Exception(new FormattableMarkup('Failed to download file "@file" for SKU id @sku_id.', $args));
    }

    // Get the path part in the url, remove hostname.
    $path = parse_url($data['file'], PHP_URL_PATH);

    // Remove slashes from start and end.
    $path = trim($path, '/');

    // Get the file name.
    $file_name = basename($path);

    // Prepare the directory path.
    $directory = 'public://media/' . str_replace('/' . $file_name, '', $path);

    // Prepare the directory.
    file_prepare_directory($directory, FILE_CREATE_DIRECTORY);

    // Save the file as file entity.
    /** @var \Drupal\file\Entity\File $file */
    if ($file = file_save_data($file_data, $directory . '/' . $file_name, FILE_EXISTS_REPLACE)) {
      \Drupal::logger('acm_sku')
        ->debug('File @url downloaded to @file for SKU @sku',
          [
            '@url' => $data['file'],
            '@file' => $file->id(),
            '@sku' => $this->id(),
          ]);
      return $file;
    }
    else {
      throw new \Exception(new FormattableMarkup('Failed to save file "@file" for SKU id @sku_id.', $args));
    }
  }
protected function downloadMediaImage(array $data) {
    // Preparing args for all info/error messages.
    $args = ['@file' => $data['file'], '@sku_id' => $this->id()];

    // Download the file contents.
    try {
      $file_data = \Drupal::httpClient()->get($data['file'])->getBody();
    }
    catch (RequestException $e) {
      watchdog_exception('acq_commerce', $e);
    }
    // Check to ensure empty file is not saved in SKU.
    if (empty($file_data)) {
      throw new \Exception(new FormattableMarkup('Failed to download file "@file" for SKU id @sku_id.', $args));
    }

    // Get the path part in the url, remove hostname.
    $path = parse_url($data['file'], PHP_URL_PATH);

    // Remove slashes from start and end.
    $path = trim($path, '/');

    // Get the file name.
    $file_name = basename($path);

    // Prepare the directory path.
    $directory = 'public://media/' . str_replace('/' . $file_name, '', $path);

    // Prepare the directory.
    file_prepare_directory($directory, FILE_CREATE_DIRECTORY);

    // Save the file as file entity.
    /** @var \Drupal\file\Entity\File $file */
    if ($file = file_save_data($file_data, $directory . '/' . $file_name, FILE_EXISTS_REPLACE)) {
      \Drupal::logger('acm_sku')
        ->debug('File @url downloaded to @file for SKU @sku',
          [
            '@url' => $data['file'],
            '@file' => $file->id(),
            '@sku' => $this->id(),
          ]);
      return $file;
    }
    else {
      throw new \Exception(new FormattableMarkup('Failed to save file "@file" for SKU id @sku_id.', $args));
    }
  }
protected function downloadMediaImage ( array $ data ) { // Preparing args for all info/error messages. $ args = [ '@file' => $ data [ 'file' ] , '@sku_id' => $ this -> id ( ) ] ; // Download the file contents. try { $ file_data = \ Drupal :: httpClient ( ) -> get ( $ data [ 'file' ] ) -> getBody ( ) ; } catch ( RequestException $ e ) { watchdog_exception ( 'acq_commerce' , $ e ) ; } // Check to ensure empty file is not saved in SKU. if ( empty ( $ file_data ) ) { throw new \ Exception ( new FormattableMarkup ( 'Failed to download file "@file" for SKU id @sku_id.' , $ args ) ) ; } // Get the path part in the url, remove hostname. $ path = parse_url ( $ data [ 'file' ] , PHP_URL_PATH ) ; // Remove slashes from start and end. $ path = trim ( $ path , '/' ) ; // Get the file name. $ file_name = basename ( $ path ) ; // Prepare the directory path. $ directory = 'public://media/' . str_replace ( '/' . $ file_name , '' , $ path ) ; // Prepare the directory. file_prepare_directory ( $ directory , FILE_CREATE_DIRECTORY ) ; // Save the file as file entity. /** @var \Drupal\file\Entity\File $file */ if ( $ file = file_save_data ( $ file_data , $ directory . '/' . $ file_name , FILE_EXISTS_REPLACE ) ) { \ Drupal :: logger ( 'acm_sku' ) -> debug ( 'File @url downloaded to @file for SKU @sku' , [ '@url' => $ data [ 'file' ] , '@file' => $ file -> id ( ) , '@sku' => $ this -> id ( ) , ] ) ; return $ file ; } else { throw new \ Exception ( new FormattableMarkup ( 'Failed to save file "@file" for SKU id @sku_id.' , $ args ) ) ; } }
Function to save image file into public dir.

@param array $data
File data.

@return \Drupal\file\Entity\File
File object.

@throws \Exception
If media fails to be downloaded.
Function to save image file into public dir .