AFile.php in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2
File
src/Flmngr/FileUploaderServer/lib/file/AFile.php
View source
<?php
namespace Drupal\n1ed\Flmngr\FileUploaderServer\lib\file;
use Drupal\n1ed\Flmngr\FileUploaderServer\lib\action\resp\FileData;
use Drupal\n1ed\Flmngr\FileUploaderServer\lib\action\resp\Message;
use Drupal\n1ed\Flmngr\FileUploaderServer\lib\MessageException;
use Exception;
abstract class AFile {
protected $config;
private $name = NULL;
private $dir = NULL;
protected $commonErrors = [];
public function __construct($config, $dir, $name) {
$this->config = $config;
$this->dir = $dir;
$this->name = $name;
}
public function getData() {
$data = new FileData();
$data->isCommited = $this
->isCommited();
$data->name = $this
->getName();
$data->dir = $this
->getDir();
$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();
if ($data->isCommited) {
if ($this->mainFile === NULL) {
$modifications = $this
->getModifications();
for ($i = 0; $i < count($modifications); $i++) {
$data->sizes[$modifications[$i]
->getModificationName()] = $modifications[$i]
->getData();
}
}
}
}
return $data;
}
public function getModifications() {
return [];
}
public function getModificationName() {
return NULL;
}
public function getSize() {
$path = $this
->getFullPath();
if (file_exists($path)) {
return filesize($path);
}
return 0;
}
public function getErrors() {
return $this->commonErrors;
}
public function checkForErrors($checkForExist) {
$this->commonErrors = [];
if (!Utils::isFileNameSyntaxOk($this
->getName())) {
$this->commonErrors[] = Message::createMessage(Message::FILE_ERROR_SYNTAX, $this
->getName());
return FALSE;
}
if ($checkForExist && !$this
->exists()) {
$this->commonErrors[] = Message::createMessage(Message::FILE_ERROR_DOES_NOT_EXIST);
}
return TRUE;
}
public function setName($name) {
$this->name = $name;
}
public function setDir($dir) {
$this->dir = $dir;
}
public abstract function isCommited();
public abstract function getBaseDir();
public function getName() {
return $this->name;
}
public function getDir() {
if (strlen($this->dir) != 0 && substr($this->dir, strlen($this->dir) - 1) !== DIRECTORY_SEPARATOR) {
return $this->dir . DIRECTORY_SEPARATOR;
}
return $this->dir;
}
public function getPath() {
return $this
->getDir() . $this
->getName();
}
public function getFullPath() {
return $this
->getBaseDir() . DIRECTORY_SEPARATOR . $this
->getPath();
}
public function getExt() {
return Utils::getExt($this->name);
}
public function getNameWithoutExt() {
$ext = $this
->getExt();
if ($ext === NULL) {
return $this->name;
}
return substr($this->name, 0, strlen($this->name) - strlen($ext) - 1);
}
public function exists() {
return file_exists($this
->getFullPath());
}
public function delete() {
if (!unlink($this
->getFullPath())) {
throw new MessageException(Message::createMessage(Message::UNABLE_TO_DELETE_FILE, $this
->getName()));
}
}
public function isImage() {
return Utils::isImage($this
->getName());
}
public function getImageWidth() {
if ($size = @getimagesize($this
->getFullPath())) {
return $size === NULL ? -1 : $size[0];
}
else {
throw new MessageException(Message::createMessage(Message::IMAGE_PROCESS_ERROR));
}
}
public function getImageHeight() {
if ($size = @getimagesize($this
->getFullPath())) {
return $size === NULL ? -1 : $size[1];
}
else {
throw new MessageException(Message::createMessage(Message::IMAGE_PROCESS_ERROR));
}
}
public function getImage() {
$path = $this
->getFullPath();
$image = NULL;
switch (strtolower($this
->getExt())) {
case 'gif':
$image = @imagecreatefromgif($path);
break;
case 'jpeg':
case 'jpg':
$image = @imagecreatefromjpeg($path);
break;
case 'png':
$image = @imagecreatefrompng($path);
break;
case 'bmp':
$image = @imagecreatefromwbmp($path);
break;
}
if (!$image) {
$image = imagecreatefromstring(file_get_contents($path));
}
if (!$image) {
throw new MessageException(Message::createMessage(Message::IMAGE_PROCESS_ERROR));
}
imagesavealpha($image, TRUE);
return $image;
}
protected function setFreeFileName() {
$name = Utils::getFreeFileName($this
->getBaseDir() . $this
->getDir(), $this
->getName(), FALSE);
$this
->setName($name);
}
public function copyTo($dstFile) {
try {
UtilsPHP::copyFile($this
->getFullPath(), $dstFile
->getFullPath());
} catch (Exception $e) {
error_log($e);
throw new MessageException(Message::createMessage(Message::UNABLE_TO_COPY_FILE, $this
->getName(), $dstFile
->getName()));
}
}
}
Classes
Name |
Description |
AFile |
Abstract file item both for just uploaded and fully commited files.
Contains some handy method for accessing files info programmatically. |