Ivan - Fix file uploader issues, add file upload in ticket creation [skip ci]

This commit is contained in:
ivan 2017-01-12 18:29:50 -03:00
parent 134540fed9
commit f2401dcec7
5 changed files with 38 additions and 9 deletions

View File

@ -38,12 +38,13 @@ class InitSettingsController extends Controller {
'maintenance-mode' => 0,
'layout' => 'boxed',
'allow-attachments' => 0,
'max-size' => 0,
'max-size' => 1024,
'title' => 'Support Center',
'url' => 'http://www.opensupports.com/support',
'ticket-gap' => Hashing::generateRandomPrime(100000, 999999),
'file-gap' => Hashing::generateRandomPrime(100000, 999999),
'file-first-number' => Hashing::generateRandomNumber(100000, 999999),
'file-quantity' => 0
]);
}

View File

@ -60,7 +60,7 @@ class CreateController extends Controller {
'language' => $this->language,
'author' => $author,
'department' => $department,
'file' => '',
'file' => $this->uploadFile(),
'date' => Date::getCurrentDate(),
'unread' => false,
'unreadStaff' => true,
@ -75,4 +75,26 @@ class CreateController extends Controller {
$this->ticketNumber = $ticket->ticketNumber;
}
private function uploadFile() {
if(!isset($_FILES['file'])) return '';
$maxSize = Setting::getSetting('max-size')->getValue();
$fileGap = Setting::getSetting('file-gap')->getValue();
$fileFirst = Setting::getSetting('file-first-number')->getValue();
$fileQuantity = Setting::getSetting('file-quantity');
$fileUploader = FileUploader::getInstance();
$fileUploader->setMaxSize($maxSize);
$fileUploader->setGeneratorValues($fileGap, $fileFirst, $fileQuantity->getValue());
if($fileUploader->upload($_FILES['file'])) {
$fileQuantity->value++;
$fileQuantity->store();
return $fileUploader->getFileName();
} else {
throw new Exception(ERRORS::INVALID_FILE);
}
}
}

View File

@ -4,6 +4,7 @@ class FileUploader extends FileManager {
private $maxSize = 1024;
private $linearCongruentialGenerator;
private $linearCongruentialGeneratorOffset;
private $fileName;
private static $instance = null;
@ -18,18 +19,18 @@ class FileUploader extends FileManager {
private function __construct() {}
public function upload($file) {
$newFileName = $this->generateNewName($file['name']);
$this->setNewName($file['name']);
if($file['size'] > (1024 * $this->maxSize)) {
return false;
}
move_uploaded_file($file['tmp_name'], $this->getLocalPath() . $newFileName);
move_uploaded_file($file['tmp_name'], $this->getLocalPath() . $this->getFileName());
return true;
}
private function generateNewName($fileName) {
private function setNewName($fileName) {
$newName = $fileName;
$newName = strtolower($newName);
$newName = preg_replace('/\s+/', '_', $newName);
@ -38,7 +39,7 @@ class FileUploader extends FileManager {
$newName = $this->linearCongruentialGenerator->generate($this->linearCongruentialGeneratorOffset) . '_' . $newName;
}
return $newName;
$this->fileName = $newName;
}
public function setGeneratorValues($gap, $first, $offset) {
@ -52,5 +53,9 @@ class FileUploader extends FileManager {
public function setMaxSize($maxSize) {
$this->maxSize = $maxSize;
}
public function getFileName() {
return $this->fileName;
}
}

View File

@ -21,7 +21,8 @@ class LinearCongruentialGenerator {
}
public function generate($offset) {
return ($this->first - $this->min + $offset * $this->gap) % ($this->max - $this->min + 1) + $this->min;
if($offset) return ($this->first - $this->min + $offset * $this->gap) % ($this->max - $this->min + 1) + $this->min;
else return $this->generateFirst();
}
public function generateFirst() {

View File

@ -10,7 +10,7 @@ describe'system/edit-settings' do
"time-zone" => -3,
"layout" => 'full-width',
"allow-attachments" => 1,
"max-size" => 2,
"max-size" => 2048,
"language" => 'en',
"no-reply-email" => 'testemail@hotmail.com'
})
@ -27,7 +27,7 @@ describe'system/edit-settings' do
(row['value']).should.equal('full-width')
row = $database.getRow('setting', 'max-size', 'name')
(row['value']).should.equal('2')
(row['value']).should.equal('2048')
row = $database.getRow('setting', 'language', 'name')
(row['value']).should.equal('en')