Add library for file uploading
This commit is contained in:
parent
28839575a2
commit
becb41d3ce
|
@ -19,7 +19,7 @@ class FileUploader extends React.Component {
|
|||
render() {
|
||||
return (
|
||||
<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">
|
||||
<Icon className="file-uploader__icon" name="upload" /> {this.props.text}
|
||||
</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) {
|
||||
if(this.props.onChange) {
|
||||
this.props.onChange({
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
"google/recaptcha": "~1.1",
|
||||
"gabordemooij/redbean": "^4.3",
|
||||
"ifsnop/mysqldump-php": "2.*",
|
||||
"ezyang/htmlpurifier": "^4.8"
|
||||
"ezyang/htmlpurifier": "^4.8",
|
||||
"codeguy/upload": "^1.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^5.7"
|
||||
|
|
|
@ -122,7 +122,7 @@ abstract class Controller {
|
|||
$url = Setting::getSetting('url')->getValue();
|
||||
for($i=0;$i<$totalImages;$i++) {
|
||||
$fileUploader->setGeneratorValues($fileGap, $fileFirst, $fileQuantity->getValue());
|
||||
$fileUploader->upload($_FILES["image_$i"]);
|
||||
$fileUploader->upload("image_$i");
|
||||
$imagePaths[] = $url . '/api/system/download?file=' . $fileUploader->getFileName();
|
||||
$fileQuantity->value++;
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ abstract class Controller {
|
|||
$fileUploader->setMaxSize($maxSize);
|
||||
$fileUploader->setGeneratorValues($fileGap, $fileFirst, $fileQuantity->getValue());
|
||||
|
||||
if($fileUploader->upload($_FILES['file'])) {
|
||||
if($fileUploader->upload('file')) {
|
||||
$fileQuantity->value++;
|
||||
$fileQuantity->store();
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ class FileUploader extends FileManager {
|
|||
private $linearCongruentialGeneratorOffset;
|
||||
private $fileName;
|
||||
private $permission;
|
||||
private $storage;
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
|
@ -17,35 +18,71 @@ class FileUploader extends FileManager {
|
|||
return self::$instance;
|
||||
}
|
||||
|
||||
private function __construct() {}
|
||||
private function __construct() {
|
||||
$this->storage = new \Upload\Storage\FileSystem($this->getLocalPath());
|
||||
}
|
||||
|
||||
public function isSizeValid($file) {
|
||||
return $file['size'] <= (1048576 * $this->maxSize);
|
||||
}
|
||||
|
||||
public function upload($file) {
|
||||
$this->setNewName($file['name']);
|
||||
public function upload($fileKey) {
|
||||
$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;
|
||||
}
|
||||
|
||||
move_uploaded_file($file['tmp_name'], $this->getLocalPath() . $this->getFileName());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function setNewName($fileName) {
|
||||
$newName = $fileName;
|
||||
private function generateFileName($fileName) {
|
||||
$newName = $this->removeFileExtension($fileName);
|
||||
$newName = strtolower($newName);
|
||||
$newName = preg_replace('/[^a-zA-Z0-9\d\.\-]/', '_', $newName);
|
||||
$result = "";
|
||||
|
||||
if ($this->linearCongruentialGenerator instanceof LinearCongruentialGenerator) {
|
||||
if($this->permission) $this->fileName = $this->permission . '_';
|
||||
else $this->fileName = '';
|
||||
if($this->permission) $result = $this->permission . '_';
|
||||
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 = '') {
|
||||
|
@ -67,6 +104,10 @@ class FileUploader extends FileManager {
|
|||
$this->maxSize = $maxSize;
|
||||
}
|
||||
|
||||
public function setFileName($fileName) {
|
||||
$this->fileName = $fileName;
|
||||
}
|
||||
|
||||
public function getFileName() {
|
||||
return $this->fileName;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue