You are here

class FileUploaded in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2

Uploaded but not commited yet file. Data stucture about file is to be converted to commited file when all validations are passed.

Hierarchy

  • class \Drupal\n1ed\Flmngr\FileUploaderServer\lib\file\AFile
    • class \Drupal\n1ed\Flmngr\FileUploaderServer\lib\file\FileUploaded

Expanded class hierarchy of FileUploaded

3 files declare their use of FileUploaded
ActionUploadAddFile.php in src/Flmngr/FileUploaderServer/lib/action/ActionUploadAddFile.php
ActionUploadCommit.php in src/Flmngr/FileUploaderServer/lib/action/ActionUploadCommit.php
ActionUploadRemoveFile.php in src/Flmngr/FileUploaderServer/lib/action/ActionUploadRemoveFile.php

File

src/Flmngr/FileUploaderServer/lib/file/FileUploaded.php, line 13

Namespace

Drupal\n1ed\Flmngr\FileUploaderServer\lib\file
View source
class FileUploaded extends AFile {
  protected $newName;
  protected $confilictsErrors = [];
  protected $customErrors = [];

  /**
   * {@inheritdoc}
   */
  public function __construct($config, $dir, $name, $newName) {
    parent::__construct($config, $dir, $name);
    $this->newName = $newName;
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseDir() {
    return $this->config
      ->getTmpDir();
  }

  /**
   * {@inheritdoc}
   */
  public function getNewName() {
    return $this->newName;
  }

  /**
   * {@inheritdoc}
   */
  public function checkForErrors($checkForExist) {
    if (!parent::checkForErrors($checkForExist)) {
      return FALSE;
    }
    if ($this->newName !== $this
      ->getName() && !Utils::isFileNameSyntaxOk($this->newName)) {
      $this->commonErrors[] = Message::createMessage(Message::FILE_ERROR_SYNTAX, $this->newName);
    }
    if (Utils::isImage($this
      ->getName())) {
      $ext = $this
        ->getExt();
      $newExt = Utils::getExt($this->newName);
      if ($ext !== $newExt) {
        if (!($ext === "jpg" && $newExt === "jpeg") && !($ext === "jpeg" && $newExt === "jpg")) {
          $this->commonErrors[] = Message::createMessage(Message::FILE_ERROR_INCORRECT_IMAGE_EXT_CHANGE, $ext, $newExt);
        }
      }
    }
    return TRUE;
  }

  /**
   * Adds custom error.
   */
  public function addCustomError($message) {
    $this->customErrors[] = $message;
  }

  /**
   * {@inheritdoc}
   */
  public function getErrors() {
    $errors = (array) parent::getErrors();
    for ($i = 0; $i < count($this->confilictsErrors); $i++) {
      $errors[] = $this->confilictsErrors[$i];
    }
    for ($i = 0; $i < count($this->customErrors); $i++) {
      $errors[] = $this->customErrors[$i];
    }
    return $errors;
  }

  /**
   * Gets commited file.
   */
  public function getCommitedFile($dir) {
    return new FileCommited($this->config, $dir, $this->newName);
  }

  /**
   * Checks for conflicts.
   */
  public function checkForConflicts($dir) {
    $this->confilictsErrors = [];
    $file = $this
      ->getCommitedFile($dir);
    if ($file
      ->exists()) {
      $this->confilictsErrors[] = Message::createMessage(Message::FILE_ALREADY_EXISTS, $file
        ->getName());
    }
    if ($file
      ->isImage()) {
      $fileOriginal = $file
        ->getFileOriginal();
      if ($fileOriginal
        ->exists()) {
        $this->confilictsErrors[] = Message::createMessage(Message::FILE_ALREADY_EXISTS, $fileOriginal
          ->getName());
      }
      $filePreview = $file
        ->getFilePreview();
      if ($filePreview
        ->exists()) {
        $this->confilictsErrors[] = Message::createMessage(Message::FILE_ALREADY_EXISTS, $filePreview
          ->getName());
      }
    }
  }

  /**
   * Uploads and commits file.
   */
  public function uploadAndCommit($file) {
    $initName = $this
      ->getName();
    $this
      ->setFreeFileName();
    if (!move_uploaded_file($file['tmp_name'], $this
      ->getFullPath())) {
      throw new MessageException(Message::createMessage(Message::WRITING_FILE_ERROR, $initName));
    }
  }

  /**
   * Rehosts a file (downloads from some URL).
   */
  public function rehost($url) {
    $dUrl = URLDownloader::download($url, $this
      ->getBaseDir() . DIRECTORY_SEPARATOR . $this
      ->getDir());
    $this
      ->setName($dUrl->fileName);
  }

  /**
   * Commits a file.
   */
  public function commit($dir, $autoRename) {
    $file = $this
      ->getCommitedFile($dir);
    if ($autoRename) {
      $file
        ->setFreeFileName();
    }
    $this
      ->copyTo($file);
    return $file;
  }

  /**
   * Is file commited.
   */
  public function isCommited() {
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AFile::$commonErrors protected property
AFile::$config protected property
AFile::$dir private property 1
AFile::$name private property 1
AFile::copyTo public function Copies file to another place.
AFile::delete public function Deletes file from storage.
AFile::exists public function Does file exist.
AFile::getData public function Gets a data for response format file representation. 1
AFile::getDir public function Gets directory.
AFile::getExt public function Gets extension of file.
AFile::getFullPath public function Gets full path. 1
AFile::getImage public function Gets as image object.
AFile::getImageHeight public function Gets height of image.
AFile::getImageWidth public function Gets width of image.
AFile::getModificationName public function Gets modification name. 1
AFile::getModifications public function Gets all file modifications (original, preview, etc.). 1
AFile::getName public function Gets name.
AFile::getNameWithoutExt public function Gets a name without extension.
AFile::getPath public function Gets path.
AFile::getSize public function Gets a size of file.
AFile::isImage public function Checks file is image or not.
AFile::setDir public function Sets a directory of file.
AFile::setFreeFileName protected function Generates and sets free file name for this file.
AFile::setName public function Sets a name of file.
FileUploaded::$confilictsErrors protected property
FileUploaded::$customErrors protected property
FileUploaded::$newName protected property
FileUploaded::addCustomError public function Adds custom error.
FileUploaded::checkForConflicts public function Checks for conflicts.
FileUploaded::checkForErrors public function Returns do we need to continue check or not. Overrides AFile::checkForErrors
FileUploaded::commit public function Commits a file.
FileUploaded::getBaseDir public function Gets base directory. Overrides AFile::getBaseDir
FileUploaded::getCommitedFile public function Gets commited file.
FileUploaded::getErrors public function Gets errors accumulated for file. Overrides AFile::getErrors
FileUploaded::getNewName public function
FileUploaded::isCommited public function Is file commited. Overrides AFile::isCommited
FileUploaded::rehost public function Rehosts a file (downloads from some URL).
FileUploaded::uploadAndCommit public function Uploads and commits file.
FileUploaded::__construct public function Creates a File instance. Overrides AFile::__construct