FileUploaded.php in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2
File
src/Flmngr/FileUploaderServer/lib/file/FileUploaded.php
View source
<?php
namespace Drupal\n1ed\Flmngr\FileUploaderServer\lib\file;
use Drupal\n1ed\Flmngr\FileUploaderServer\lib\action\resp\Message;
use Drupal\n1ed\Flmngr\FileUploaderServer\lib\MessageException;
class FileUploaded extends AFile {
protected $newName;
protected $confilictsErrors = [];
protected $customErrors = [];
public function __construct($config, $dir, $name, $newName) {
parent::__construct($config, $dir, $name);
$this->newName = $newName;
}
public function getBaseDir() {
return $this->config
->getTmpDir();
}
public function getNewName() {
return $this->newName;
}
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;
}
public function addCustomError($message) {
$this->customErrors[] = $message;
}
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;
}
public function getCommitedFile($dir) {
return new FileCommited($this->config, $dir, $this->newName);
}
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());
}
}
}
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));
}
}
public function rehost($url) {
$dUrl = URLDownloader::download($url, $this
->getBaseDir() . DIRECTORY_SEPARATOR . $this
->getDir());
$this
->setName($dUrl->fileName);
}
public function commit($dir, $autoRename) {
$file = $this
->getCommitedFile($dir);
if ($autoRename) {
$file
->setFreeFileName();
}
$this
->copyTo($file);
return $file;
}
public function isCommited() {
return FALSE;
}
}
Classes
Name |
Description |
FileUploaded |
Uploaded but not commited yet file.
Data stucture about file is to be converted to commited file when
all validations are passed. |