You are here

public function CommerceSmartImporerService::createFile in Commerce Smart Importer 8

Creates image value based on field settings.

1 call to CommerceSmartImporerService::createFile()
CommerceSmartImporerService::formatField in src/Plugin/CommerceSmartImporerService.php
Formats one field value based on field settings.

File

src/Plugin/CommerceSmartImporerService.php, line 415
Main Commerce Smart Importer Service.

Class

CommerceSmartImporerService
This is main Commerce Smart Importer Service.

Namespace

Drupal\commerce_smart_importer\Plugin

Code

public function createFile($data, $field_settings, $folders, $create_mode, $file_type = 'file') {
  if (empty($data)) {
    return NULL;
  }
  $config = $this
    ->getConfig();
  if ($create_mode) {
    if (filter_var(trim($data), FILTER_VALIDATE_URL)) {
      $data = basename($data);
    }
  }
  $saveFolder = $this->token
    ->replace($field_settings['file_directory']);
  if (strpos($saveFolder, 'temporary://') === FALSE) {
    $saveFolder = 'public://' . $saveFolder;
  }
  foreach ($folders as $key => $folder) {
    if (!is_array($folder) && strpos($folder, 'temporary://') === FALSE && strpos($folder, 'public://') === FALSE) {
      $folders[$key] = 'public://' . $folder;
    }
  }
  if (!is_dir($saveFolder)) {
    mkdir($saveFolder, 0777, TRUE);
  }
  if (filter_var(trim($data), FILTER_VALIDATE_URL)) {
    if ($file_type !== 'image' || @getimagesize(trim($data))) {
      $imageData = file_get_contents($data);
      $imagesPath = $saveFolder;
      $fileSave = file_save_data($imageData, $imagesPath . '/' . basename($data), FileSystemInterface::EXISTS_REPLACE);
      if ($config['flush_image_cache'] === '1' && $file_type === 'image') {
        $this
          ->flushImageStyleUri($fileSave
          ->getFileUri());
      }
      if (is_bool($fileSave)) {
        throw new Exception('Remote Image ' . $data . ' was not found!');
      }
      else {
        return [
          'target_id' => $fileSave
            ->id(),
        ];
      }
    }
    else {
      throw new Exception('Image ' . $data . ' was not found!');
    }
  }
  else {
    if (is_iterable($folders)) {
      foreach ($folders as $key => $images) {
        if ($key === 'fids') {
          foreach ($images as $image) {
            $file = $this
              ->entityTypeManager()
              ->getStorage('file')
              ->load($image);
            $uri = $file
              ->getFileUri();
            if (strtolower($data) == strtolower(basename($uri))) {
              $imageData = file_get_contents($uri);
              $imagesPath = $saveFolder;
              $fileSave = file_save_data($imageData, $imagesPath . '/' . basename($uri), FileSystemInterface::EXISTS_REPLACE);
              if ($config['flush_image_cache'] === '1' && $file_type === 'image') {
                $this
                  ->flushImageStyleUri($fileSave
                  ->getFileUri());
              }
              return [
                'target_id' => $fileSave
                  ->id(),
              ];
            }
          }
          foreach ($images as $image) {
            $file = $this
              ->entityTypeManager()
              ->getStorage('file')
              ->load($image);
            $uri = $file
              ->getFileUri();
            if ($data == $this
              ->replaceDuplicateInNames(basename($uri))) {
              $imageData = file_get_contents($uri);
              $imagesPath = $saveFolder;
              $fileSave = file_save_data($imageData, $imagesPath . '/' . $this
                ->replaceDuplicateInNames(basename($uri)), FileSystemInterface::EXISTS_REPLACE);
              if ($config['flush_image_cache'] === '1' && $file_type === 'image') {
                $this
                  ->flushImageStyleUri($fileSave
                  ->getFileUri());
              }
              return [
                'target_id' => $fileSave
                  ->id(),
              ];
            }
          }
        }
        else {
          $dir = scandir($images);
          foreach ($dir as $file) {
            if (is_file($images . '/' . $file)) {
              if (strtolower($data) == strtolower($file)) {
                $imageData = file_get_contents($images . '/' . $file);
                $imagesPath = $saveFolder;
                $fileSave = file_save_data($imageData, $imagesPath . '/' . $data, FileSystemInterface::EXISTS_REPLACE);
                if ($config['flush_image_cache'] === '1' && $file_type === 'image') {
                  $this
                    ->flushImageStyleUri($fileSave
                    ->getFileUri());
                }
                return [
                  'target_id' => $fileSave
                    ->id(),
                ];
              }
            }
          }
        }
      }
    }
  }
  throw new Exception('Image ' . $data . ' was not found!');
}