<commit_before><?php

namespace Electro\Plugins\MatisseComponents\Handlers;

use Electro\ContentRepository\Config\ContentRepositorySettings;
use Electro\ContentRepository\Lib\FileUtil;
use Electro\Exceptions\FlashMessageException;
use Electro\Exceptions\FlashType;
use Electro\Interfaces\ContentRepositoryInterface;
use Electro\Interfaces\ModelControllerExtensionInterface;
use Electro\Interfaces\ModelControllerInterface;
use Electro\Plugins\IlluminateDatabase\BaseModel;
use Electro\Plugins\MatisseComponents\Models\File;
use Electro\Plugins\MatisseComponents\Traits\FilesModelTrait;
use Illuminate\Database\Query\Builder;
use Psr\Http\Message\UploadedFileInterface;

/**
 * Handles file uploads for the FileField and ImageField components.
 *
 * Warning: files on sub-models are NOT supported!
 */
class FileFieldHandler implements ModelControllerExtensionInterface
{
  const FILE_FIELD_SUFFIX = '_FileField';

  /** @var string */
  private $fileArchivePath;
  /** @var ContentRepositoryInterface */
  private $repository;

  public function __construct (ContentRepositorySettings $settings, ContentRepositoryInterface $repository)
  {
    $this->fileArchivePath = $settings->fileArchivePath;
    $this->repository      = $repository;

    // Watch for file model deletions and remove the corresponding physical files.
    File::deleting (function (File $model) use ($repository) {
      if (exists ($model->path))
        $repository->deleteFile ($model->path);
    });
  }

  /*
   * Detect if the request has fields that were generated by this component; if so, register an handler for saving
   * them.
   */
  function modelControllerExtension (ModelControllerInterface $modelController)
  {
    $request = $modelController->getRequest ();
    $files   = $request->getUploadedFiles ();

    $uploads = [];

    $this->processDropzoneFiles($request, $modelController);

    // Check if extension is applicable to the current request.
    foreach ($files as $fieldName => $file)
      if (str_endsWith ($fieldName, self::FILE_FIELD_SUFFIX)) {
        // Note: slashes are converted to dots, which delimit path segments to nested fields. See the Field component.
        $fieldName           = str_replace ('/', '.', str_segmentsStripLast ($fieldName, self::FILE_FIELD_SUFFIX));
        $uploads[$fieldName] = $file;
      }

      if ($uploads)
        $modelController->onSave (-1, function () use ($uploads, $modelController) {
          /** @var UploadedFileInterface $file */
          foreach ($uploads as $fieldName => $file) {
            list ($targetModel, $prop) = $modelController->getTarget ($fieldName);
            $err = $file->getError ();
            if ($err == UPLOAD_ERR_OK)
              static::newUpload ($targetModel, $prop, $file);
            else if ($err == UPLOAD_ERR_NO_FILE)
              static::noUpload ($targetModel, $prop);
            else throw new FlashMessageException ("Error $err", FlashType::ERROR, "Error uploading file");
          }
        });
  }

  private function processDropzoneFiles($request,ModelControllerInterface $modelController)
  {
    $oParsedBody = $request->getParsedBody();
    if (!$oParsedBody)
      return;
    foreach ($oParsedBody as $fieldName => $value)
    {
      $model = $modelController->getModel();
      $fieldName = str_replace('model/','',$fieldName);
      try
      {
        if (in_array($fieldName, $model::GALLERY_FIELDS))
        {<commit_msg>Adicionado else if para erro de imagem demasiado grande<commit_after><?php

namespace Electro\Plugins\MatisseComponents\Handlers;

use Electro\ContentRepository\Config\ContentRepositorySettings;
use Electro\ContentRepository\Lib\FileUtil;
use Electro\Exceptions\FlashMessageException;
use Electro\Exceptions\FlashType;
use Electro\Interfaces\ContentRepositoryInterface;
use Electro\Interfaces\ModelControllerExtensionInterface;
use Electro\Interfaces\ModelControllerInterface;
use Electro\Plugins\IlluminateDatabase\BaseModel;
use Electro\Plugins\MatisseComponents\Models\File;
use Electro\Plugins\MatisseComponents\Traits\FilesModelTrait;
use Illuminate\Database\Query\Builder;
use Psr\Http\Message\UploadedFileInterface;

/**
 * Handles file uploads for the FileField and ImageField components.
 *
 * Warning: files on sub-models are NOT supported!
 */
class FileFieldHandler implements ModelControllerExtensionInterface
{
  const FILE_FIELD_SUFFIX = '_FileField';

  /** @var string */
  private $fileArchivePath;
  /** @var ContentRepositoryInterface */
  private $repository;

  public function __construct (ContentRepositorySettings $settings, ContentRepositoryInterface $repository)
  {
    $this->fileArchivePath = $settings->fileArchivePath;
    $this->repository      = $repository;

    // Watch for file model deletions and remove the corresponding physical files.
    File::deleting (function (File $model) use ($repository) {
      if (exists ($model->path))
        $repository->deleteFile ($model->path);
    });
  }

  /*
   * Detect if the request has fields that were generated by this component; if so, register an handler for saving
   * them.
   */
  function modelControllerExtension (ModelControllerInterface $modelController)
  {
    $request = $modelController->getRequest ();
    $files   = $request->getUploadedFiles ();

    $uploads = [];

    $this->processDropzoneFiles($request, $modelController);

    // Check if extension is applicable to the current request.
    foreach ($files as $fieldName => $file)
      if (str_endsWith ($fieldName, self::FILE_FIELD_SUFFIX)) {
        // Note: slashes are converted to dots, which delimit path segments to nested fields. See the Field component.
        $fieldName           = str_replace ('/', '.', str_segmentsStripLast ($fieldName, self::FILE_FIELD_SUFFIX));
        $uploads[$fieldName] = $file;
      }

      if ($uploads)
        $modelController->onSave (-1, function () use ($uploads, $modelController) {
          /** @var UploadedFileInterface $file */
          foreach ($uploads as $fieldName => $file) {
            list ($targetModel, $prop) = $modelController->getTarget ($fieldName);
            $err = $file->getError ();
            if ($err == UPLOAD_ERR_OK)
              static::newUpload ($targetModel, $prop, $file);
			else if ($err = UPLOAD_ERR_INI_SIZE)
             throw new FlashMessageException ("Error Imagem demasiado grande", FlashType::ERROR, "Error uploading file");
            else if ($err == UPLOAD_ERR_NO_FILE)
              static::noUpload ($targetModel, $prop);
            else throw new FlashMessageException ("Error $err", FlashType::ERROR, "Error uploading file");
          }
        });
  }

  private function processDropzoneFiles($request,ModelControllerInterface $modelController)
  {
    $oParsedBody = $request->getParsedBody();
    if (!$oParsedBody)
      return;
    foreach ($oParsedBody as $fieldName => $value)
    {
      $model = $modelController->getModel();
      $fieldName = str_replace('model/','',$fieldName);
      try
      {
        if (in_array($fieldName, $model::GALLERY_FIELDS))
        {
{'fraction_non_alphanumeric': 0.11288499576151455, 'fraction_numerical': 0.00028256569652444194, 'mean_word_length': 3.684976836532098, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4433081', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1905, 'n_words': 489}