MatisseComponents/Handlers/FileFieldHandler.php
private function newUpload (BaseModel $model, $fieldName, UploadedFileInterface $file)
  {
    // Check is there are already files on this field.
    $filesRelation = $model->files ();
    /** @var Builder $filesQuery */
    $filesQuery = $filesRelation->ofField ($fieldName);
    /** @var File[] $files */
    $files = $filesQuery->get ();

    // Save new file
    $data = File::getFileData ($file->getClientFilename (), FileUtil::getUploadedFilePath ($file), $fieldName);
    /** @var File $fileModel */
    $fileModel = $filesRelation->create ($data);
    $this->repository->saveUploadedFile ($fileModel->path, $file);

    // Delete the previous files of this field, if any exists.
    // This is only performed AFTER the new file is successfully uploaded.
    foreach ($files as $file)
      $file->delete ();

    // If everything went ok, save the models.
    if (!in_array ($fieldName, $model::GALLERY_FIELDS))
      $model[$fieldName] = $fileModel->path;
    $model->save ();
  }
private function newUpload (BaseModel $model, $fieldName, UploadedFileInterface $file)
  {
    // Check is there are already files on this field.
    $filesRelation = $model->files ();
    /** @var Builder $filesQuery */
    $filesQuery = $filesRelation->ofField ($fieldName);
    /** @var File[] $files */
    $files = $filesQuery->get ();

    // Save new file
    $data = File::getFileData ($file->getClientFilename (), FileUtil::getUploadedFilePath ($file), $fieldName);
    /** @var File $fileModel */
    $fileModel = $filesRelation->create ($data);
    $this->repository->saveUploadedFile ($fileModel->path, $file);

    // Delete the previous files of this field, if any exists.
    // This is only performed AFTER the new file is successfully uploaded.
    foreach ($files as $file)
      $file->delete ();

    // If everything went ok, save the models.
    if (!in_array ($fieldName, $model::GALLERY_FIELDS))
      $model[$fieldName] = $fileModel->path;
    $model->save ();
  }
private function newUpload ( BaseModel $ model , $ fieldName , UploadedFileInterface $ file ) { // Check is there are already files on this field. $ filesRelation = $ model -> files ( ) ; /** @var Builder $filesQuery */ $ filesQuery = $ filesRelation -> ofField ( $ fieldName ) ; /** @var File[] $files */ $ files = $ filesQuery -> get ( ) ; // Save new file $ data = File :: getFileData ( $ file -> getClientFilename ( ) , FileUtil :: getUploadedFilePath ( $ file ) , $ fieldName ) ; /** @var File $fileModel */ $ fileModel = $ filesRelation -> create ( $ data ) ; $ this -> repository -> saveUploadedFile ( $ fileModel -> path , $ file ) ; // Delete the previous files of this field, if any exists. // This is only performed AFTER the new file is successfully uploaded. foreach ( $ files as $ file ) $ file -> delete ( ) ; // If everything went ok, save the models. if ( ! in_array ( $ fieldName , $ model :: GALLERY_FIELDS ) ) $ model [ $ fieldName ] = $ fileModel -> path ; $ model -> save ( ) ; }
Handle the case where a file has been uploaded for a field, possibly replacing another already set on the field.

@param BaseModel|FilesModelTrait $model
@param string                    $fieldName
@param UploadedFileInterface     $file
@throws \Exception
@throws \League\Flysystem\FileExistsException
@throws \League\Flysystem\InvalidArgumentException
Handle the case where a file has been uploaded for a field possibly replacing another already set on the field .