You are here

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

Hierarchy

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

Expanded class hierarchy of FileUploadedQuick

1 file declares its use of FileUploadedQuick
ActionQuickUpload.php in src/Flmngr/FileUploaderServer/lib/action/ActionQuickUpload.php

File

src/Flmngr/FileUploaderServer/lib/file/FileUploadedQuick.php, line 16

Namespace

Drupal\n1ed\Flmngr\FileUploaderServer\lib\file
View source
class FileUploadedQuick extends AFile {
  protected $m_newName;
  protected $m_confilictsErrors = [];
  protected $m_customErrors = [];
  protected $dir;
  protected $name;
  protected $relativePath;
  public function __construct($config, $dir, $name, $newName, $relativePath) {
    parent::__construct($config, $dir, $name);
    $this->dir = $dir;
    $this->name = $name;
    $this->m_newName = $newName;
    $this->name = $this
      ->checkFileNameExistence();
    $this->relativePath = $relativePath;
  }
  private function checkFileNameExistence() {
    function file_newname($path, $filename) {
      if ($pos = strrpos($filename, '.')) {
        $name = substr($filename, 0, $pos);
        $ext = substr($filename, $pos);
      }
      else {
        $name = $filename;
      }
      $newpath = $path . '/' . $filename;
      $newname = $filename;
      $counter = 0;
      while (file_exists($newpath)) {
        $newname = $name . '_' . $counter . $ext;
        $newpath = $path . '/' . $newname;
        $counter++;
      }
      return $newname;
    }
    return file_newname($this->dir, $this->name);
  }
  public function getBaseDir() {
    return $this->config
      ->getTmpDir();
  }
  public function getNewName() {
    return $this->m_newName;
  }
  public function checkForErrors($checkForExist) {
    if (!parent::checkForErrors($checkForExist)) {
      return false;
    }
    if ($this->m_newName !== $this
      ->getName() && !Utils::isFileNameSyntaxOk($this->m_newName)) {
      $this->m_commonErrors[] = Message::createMessage(Message::FILE_ERROR_SYNTAX, $this->m_newName);
    }
    if (Utils::isImage($this
      ->getName())) {
      $ext = $this
        ->getExt();
      $newExt = Utils::getExt($this->m_newName);
      if ($ext !== $newExt) {
        if (!($ext === 'jpg' && $newExt === 'jpeg') && !($ext === 'jpeg' && $newExt === 'jpg')) {
          $this->m_commonErrors[] = Message::createMessage(Message::FILE_ERROR_INCORRECT_IMAGE_EXT_CHANGE, $ext, $newExt);
        }
      }
    }
    return true;
  }
  public function addCustomError($message) {
    $this->m_customErrors[] = $message;
  }
  public function getErrors() {
    $errors = (array) parent::getErrors();
    for ($i = 0; $i < count($this->m_confilictsErrors); $i++) {
      $errors[] = $this->m_confilictsErrors[$i];
    }
    for ($i = 0; $i < count($this->m_customErrors); $i++) {
      $errors[] = $this->m_customErrors[$i];
    }
    return $errors;
  }
  public function getCommitedFile($dir) {
    return new FileCommited($this->config, $dir, $this->m_newName);
  }
  public function checkForConflicts($dir) {
    $this->m_confilictsErrors = [];
    $file = $this
      ->getCommitedFile($dir);
    if ($file
      ->exists()) {
      $this->m_confilictsErrors[] = Message::createMessage(Message::FILE_ALREADY_EXISTS, $file
        ->getName());
    }
    if ($file
      ->isImage()) {
      $fileOriginal = $file
        ->getFileOriginal();
      if ($fileOriginal
        ->exists()) {
        $this->m_confilictsErrors[] = Message::createMessage(Message::FILE_ALREADY_EXISTS, $fileOriginal
          ->getName());
      }
      $filePreview = $file
        ->getFilePreview();
      if ($filePreview
        ->exists()) {
        $this->m_confilictsErrors[] = Message::createMessage(Message::FILE_ALREADY_EXISTS, $filePreview
          ->getName());
      }
    }
  }
  public function upload($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));
    }
  }
  public function isCommited() {
    return true;
  }
  public function getFullPath() {
    return $this->dir . '/' . $this->name;
  }
  public function getData() {
    $data = new FileData();
    $data->name = $this->name;
    $data->dir = $this->relativePath;
    $data->bytes = $this
      ->getSize();
    $errors = $this
      ->getErrors();
    $data->errors = [];
    for ($i = 0; $i < count($errors); $i++) {
      $data->errors[] = (array) $errors[$i];
    }
    $data->isImage = $this
      ->isImage();
    $data->sizes = [];
    if ($data->isImage) {
      $data->width = $this
        ->getImageWidth();
      $data->height = $this
        ->getImageHeight();
    }
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AFile::$commonErrors protected property
AFile::$config protected property
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::getDir public function Gets directory.
AFile::getExt public function Gets extension of file.
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.
FileUploadedQuick::$dir protected property Overrides AFile::$dir
FileUploadedQuick::$m_confilictsErrors protected property
FileUploadedQuick::$m_customErrors protected property
FileUploadedQuick::$m_newName protected property
FileUploadedQuick::$name protected property Overrides AFile::$name
FileUploadedQuick::$relativePath protected property
FileUploadedQuick::addCustomError public function
FileUploadedQuick::checkFileNameExistence private function
FileUploadedQuick::checkForConflicts public function
FileUploadedQuick::checkForErrors public function Returns do we need to continue check or not. Overrides AFile::checkForErrors
FileUploadedQuick::getBaseDir public function Gets base directory. Overrides AFile::getBaseDir
FileUploadedQuick::getCommitedFile public function
FileUploadedQuick::getData public function Gets a data for response format file representation. Overrides AFile::getData
FileUploadedQuick::getErrors public function Gets errors accumulated for file. Overrides AFile::getErrors
FileUploadedQuick::getFullPath public function Gets full path. Overrides AFile::getFullPath
FileUploadedQuick::getNewName public function
FileUploadedQuick::isCommited public function Is file commited. Overrides AFile::isCommited
FileUploadedQuick::upload public function
FileUploadedQuick::__construct public function Creates a File instance. Overrides AFile::__construct