Add library for file uploading

This commit is contained in:
Ivan Diaz 2018-10-05 22:23:19 -03:00
parent 28839575a2
commit becb41d3ce
4 changed files with 82 additions and 17 deletions

View File

@ -19,7 +19,7 @@ class FileUploader extends React.Component {
render() { render() {
return ( return (
<label className="file-uploader"> <label className="file-uploader">
<input className="file-uploader__input" type="file" multiple={false} onChange={this.onChange.bind(this)}/> <input className="file-uploader__input" type="file" multiple={false} accept={this.getMimeTypes()} onChange={this.onChange.bind(this)}/>
<span className="file-uploader__custom" tabIndex="0"> <span className="file-uploader__custom" tabIndex="0">
<Icon className="file-uploader__icon" name="upload" /> {this.props.text} <Icon className="file-uploader__icon" name="upload" /> {this.props.text}
</span> </span>
@ -28,6 +28,29 @@ class FileUploader extends React.Component {
); );
} }
getMimeTypes() {
return `
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
`;
}
onChange(event) { onChange(event) {
if(this.props.onChange) { if(this.props.onChange) {
this.props.onChange({ this.props.onChange({

View File

@ -6,7 +6,8 @@
"google/recaptcha": "~1.1", "google/recaptcha": "~1.1",
"gabordemooij/redbean": "^4.3", "gabordemooij/redbean": "^4.3",
"ifsnop/mysqldump-php": "2.*", "ifsnop/mysqldump-php": "2.*",
"ezyang/htmlpurifier": "^4.8" "ezyang/htmlpurifier": "^4.8",
"codeguy/upload": "^1.3"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^5.7" "phpunit/phpunit": "^5.7"

View File

@ -122,7 +122,7 @@ abstract class Controller {
$url = Setting::getSetting('url')->getValue(); $url = Setting::getSetting('url')->getValue();
for($i=0;$i<$totalImages;$i++) { for($i=0;$i<$totalImages;$i++) {
$fileUploader->setGeneratorValues($fileGap, $fileFirst, $fileQuantity->getValue()); $fileUploader->setGeneratorValues($fileGap, $fileFirst, $fileQuantity->getValue());
$fileUploader->upload($_FILES["image_$i"]); $fileUploader->upload("image_$i");
$imagePaths[] = $url . '/api/system/download?file=' . $fileUploader->getFileName(); $imagePaths[] = $url . '/api/system/download?file=' . $fileUploader->getFileName();
$fileQuantity->value++; $fileQuantity->value++;
} }
@ -146,7 +146,7 @@ abstract class Controller {
$fileUploader->setMaxSize($maxSize); $fileUploader->setMaxSize($maxSize);
$fileUploader->setGeneratorValues($fileGap, $fileFirst, $fileQuantity->getValue()); $fileUploader->setGeneratorValues($fileGap, $fileFirst, $fileQuantity->getValue());
if($fileUploader->upload($_FILES['file'])) { if($fileUploader->upload('file')) {
$fileQuantity->value++; $fileQuantity->value++;
$fileQuantity->store(); $fileQuantity->store();

View File

@ -6,6 +6,7 @@ class FileUploader extends FileManager {
private $linearCongruentialGeneratorOffset; private $linearCongruentialGeneratorOffset;
private $fileName; private $fileName;
private $permission; private $permission;
private $storage;
private static $instance = null; private static $instance = null;
@ -17,35 +18,71 @@ class FileUploader extends FileManager {
return self::$instance; return self::$instance;
} }
private function __construct() {} private function __construct() {
$this->storage = new \Upload\Storage\FileSystem($this->getLocalPath());
}
public function isSizeValid($file) { public function isSizeValid($file) {
return $file['size'] <= (1048576 * $this->maxSize); return $file['size'] <= (1048576 * $this->maxSize);
} }
public function upload($file) { public function upload($fileKey) {
$this->setNewName($file['name']); $file = new \Upload\File($fileKey, $this->storage);
$file->setName($this->generateFileName($_FILES[$fileKey]['name']));
if(!$this->isSizeValid($file)) { $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; return false;
} }
move_uploaded_file($file['tmp_name'], $this->getLocalPath() . $this->getFileName());
return true;
} }
private function setNewName($fileName) { private function generateFileName($fileName) {
$newName = $fileName; $newName = $this->removeFileExtension($fileName);
$newName = strtolower($newName); $newName = strtolower($newName);
$newName = preg_replace('/[^a-zA-Z0-9\d\.\-]/', '_', $newName); $newName = preg_replace('/[^a-zA-Z0-9\d\.\-]/', '_', $newName);
$result = "";
if ($this->linearCongruentialGenerator instanceof LinearCongruentialGenerator) { if ($this->linearCongruentialGenerator instanceof LinearCongruentialGenerator) {
if($this->permission) $this->fileName = $this->permission . '_'; if($this->permission) $result = $this->permission . '_';
else $this->fileName = ''; else $result = '';
$this->fileName .= $this->linearCongruentialGenerator->generate($this->linearCongruentialGeneratorOffset) . '_' . $newName; $result .= $this->linearCongruentialGenerator->generate($this->linearCongruentialGeneratorOffset) . '_' . $newName;
} }
return $result;
}
public function removeFileExtension($fileName) {
return substr($fileName, 0, strrpos($fileName, "."));
} }
public function setPermission($type = '', $extra = '') { public function setPermission($type = '', $extra = '') {
@ -67,6 +104,10 @@ class FileUploader extends FileManager {
$this->maxSize = $maxSize; $this->maxSize = $maxSize;
} }
public function setFileName($fileName) {
$this->fileName = $fileName;
}
public function getFileName() { public function getFileName() {
return $this->fileName; return $this->fileName;
} }