opensupports/server/libs/FileUploader.php

115 lines
3.5 KiB
PHP
Raw Normal View History

<?php
class FileUploader extends FileManager {
2018-09-20 20:52:27 +02:00
private $maxSize = 1;
private $linearCongruentialGenerator;
private $linearCongruentialGeneratorOffset;
private $fileName;
private $permission;
2018-10-06 03:23:19 +02:00
private $storage;
private static $instance = null;
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new FileUploader();
}
return self::$instance;
}
2018-10-06 03:23:19 +02:00
private function __construct() {
$this->storage = new \Upload\Storage\FileSystem($this->getLocalPath());
}
2018-09-20 20:52:27 +02:00
public function isSizeValid($file) {
return $file['size'] <= (1048576 * $this->maxSize);
}
2018-10-06 03:23:19 +02:00
public function upload($fileKey) {
$file = new \Upload\File($fileKey, $this->storage);
$file->setName($this->generateFileName($_FILES[$fileKey]['name']));
$file->addValidations(array(
new \Upload\Validation\Mimetype([
'image/png',
'image/jpeg',
'image/bmp',
'image/tiff',
'application/gzip',
'application/x-gzip',
'application/zip',
'application/x-rar-compressed',
'application/x-7z-compressed',
'application/x-tar',
'application/x-bzip',
'application/x-bzip2',
'text/csv',
'text/rtf',
'application/msword',
'application/vnd.ms-excel',
'text/plain',
'application/pdf'
]),
new \Upload\Validation\Size($this->maxSize.'M')
));
try {
$file->upload();
$this->setFileName($file->getNameWithExtension());
return true;
} catch (\Exception $e) {
return false;
}
}
2018-10-06 03:23:19 +02:00
private function generateFileName($fileName) {
$newName = $this->removeFileExtension($fileName);
$newName = strtolower($newName);
2017-06-20 21:47:27 +02:00
$newName = preg_replace('/[^a-zA-Z0-9\d\.\-]/', '_', $newName);
2018-10-06 03:23:19 +02:00
$result = "";
if ($this->linearCongruentialGenerator instanceof LinearCongruentialGenerator) {
2018-10-06 03:23:19 +02:00
if($this->permission) $result = $this->permission . '_';
else $result = '';
2018-10-06 03:23:19 +02:00
$result .= $this->linearCongruentialGenerator->generate($this->linearCongruentialGeneratorOffset) . '_' . $newName;
}
2018-10-06 03:23:19 +02:00
return $result;
}
public function removeFileExtension($fileName) {
return substr($fileName, 0, strrpos($fileName, "."));
}
public function setPermission($type = '', $extra = '') {
if($type === FileManager::PERMISSION_ARTICLE) $this->permission = 'a';
else if($type === FileManager::PERMISSION_TICKET) $this->permission = 't' . $extra;
else if($type === FileManager::PERMISSION_PROFILE) $this->permission = 'p';
else $this->permission = '';
}
public function setGeneratorValues($gap, $first, $offset) {
$this->linearCongruentialGenerator = new LinearCongruentialGenerator();
$this->linearCongruentialGeneratorOffset = $offset;
$this->linearCongruentialGenerator->setGap($gap);
$this->linearCongruentialGenerator->setFirst($first);
}
public function setMaxSize($maxSize) {
$this->maxSize = $maxSize;
}
2018-10-06 03:23:19 +02:00
public function setFileName($fileName) {
$this->fileName = $fileName;
}
public function getFileName() {
return $this->fileName;
}
}