diff --git a/CHANGELOG.md b/CHANGELOG.md index 623f136..c573b5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,13 @@ +**2019-07-16 v2.0.7** +添加宽松模式支持 +优化代码 + **2019-06-03 v2.0.6** 添加额外Mime-Type自定义配置 添加解析和生成资源保存地址方法 添加文件完整性校验 更换可能被弃用的ajax同步方式 -优化请求参数验证方式 +优化请求参数验证方式 **2019-04-10 v2.0.5** 添加自定义路由支持 diff --git a/README.md b/README.md index 7e5e61b..9af4f2b 100644 --- a/README.md +++ b/README.md @@ -169,12 +169,9 @@ AetherUpload在上传前使用白名单+黑名单的形式进行文件后缀名 虽然做了诸多安全工作,但恶意文件上传是防不胜防的,建议正确设置上传目录权限,确保相关程序对资源文件没有执行权限。 # 更新日志 -**2019-06-03 v2.0.6** -添加额外Mime-Type自定义配置 -添加解析和生成资源保存地址方法 -添加文件完整性校验 -更换可能被弃用的ajax同步方式 -优化请求参数验证方式 +**2019-07-16 v2.0.7** +添加宽松模式支持 +优化代码 详见[CHANGELOG.md](https://github.com/peinhu/AetherUpload-Laravel/blob/master/CHANGELOG.md) diff --git a/assets/aetherupload.js b/assets/aetherupload.js index a373c17..37ffef2 100644 --- a/assets/aetherupload.js +++ b/assets/aetherupload.js @@ -70,10 +70,14 @@ var AetherUpload = { this.outputDom.text(this.messages.status_upload_begin); - if (!('FileReader' in window) || !('File' in window) || typeof SparkMD5 === 'undefined' ) { + if (!('FileReader' in window) || !('File' in window) || typeof SparkMD5 === 'undefined') { this.preprocess(); //浏览器不支持读取本地文件,跳过计算hash + } else if (this.laxMode === true) { + + this.preprocess(); //宽松模式,跳过计算hash + } else { this.calculateHash(); @@ -86,9 +90,9 @@ var AetherUpload = { var _this = this, - chunkSize = 2000000, + clientChunkSize = 4000000, - chunks = Math.ceil(_this.resource.size / chunkSize), + chunks = Math.ceil(_this.resource.size / clientChunkSize), currentChunk = 0, @@ -125,9 +129,9 @@ var AetherUpload = { function loadNext() { - var start = currentChunk * chunkSize, + var start = currentChunk * clientChunkSize, - end = start + chunkSize >= _this.resource.size ? _this.resource.size : start + chunkSize; + end = start + clientChunkSize >= _this.resource.size ? _this.resource.size : start + clientChunkSize; fileReader.readAsArrayBuffer(_this.blobSlice.call(_this.resource, start, end)); @@ -307,7 +311,7 @@ var AetherUpload = { typeof(_this.callback) !== 'undefined' ? _this.callback() : null; - }else{ + } else { ++_this.i; @@ -386,6 +390,13 @@ var AetherUpload = { return this; }, + setLaxMode: function (isLax) { + + this.laxMode = isLax; + + return this; + }, + getLocalizedMessages: function () { var lang = navigator.language ? navigator.language : navigator.browserLanguage; @@ -461,6 +472,8 @@ function aetherupload(resource) { newInstance.uploadingRoute = '/aetherupload/uploading'; //上传路由的默认值 + newInstance.laxMode = false; //宽松模式 + return newInstance; } diff --git a/config/aetherupload.php b/config/aetherupload.php index c7e5709..26041a6 100644 --- a/config/aetherupload.php +++ b/config/aetherupload.php @@ -148,6 +148,19 @@ 'route_display' => '/aetherupload/display', # 文件展示的路由 'route_download' => '/aetherupload/download', # 文件下载的路由 + /* + |-------------------------------------------------------------------------- + | 宽松模式 + |-------------------------------------------------------------------------- + | + | 【一般设置】在某些特殊场景,通过上传前跳过计算hash,可缩短总耗时。此选项开启后,无法进行秒传和完整性校验。默认不启用。 + | + | 注意:开启后需在前端同时调用setLaxMode(true)方法。 + | + */ + + 'lax_mode' => false, + /* |-------------------------------------------------------------------------- | 资源分组 diff --git a/src/ConfigMapper.php b/src/ConfigMapper.php index 7cd75d4..33b8220 100644 --- a/src/ConfigMapper.php +++ b/src/ConfigMapper.php @@ -30,6 +30,7 @@ class ConfigMapper private $route_uploading; private $route_display; private $route_download; + private $lax_mode; private function __construct() { @@ -67,6 +68,7 @@ private function applyCommonConfig() $this->route_uploading = $config->get('aetherupload.route_uploading'); $this->route_display = $config->get('aetherupload.route_display'); $this->route_download = $config->get('aetherupload.route_download'); + $this->lax_mode = $config->get('aetherupload.lax_mode'); return $this; } diff --git a/src/UploadController.php b/src/UploadController.php index ec7f578..cfd9a9f 100644 --- a/src/UploadController.php +++ b/src/UploadController.php @@ -26,8 +26,8 @@ public function preprocess() $this->validate(request(), [ 'resource_name' => 'required', 'resource_size' => 'required', - 'resource_hash' => 'required', 'group' => 'required', + 'resource_hash' => 'present', ]); $resourceName = Request::input('resource_name'); @@ -65,7 +65,7 @@ public function preprocess() $partialResource->filterByExtension($resourceExt); // determine if this upload meets the condition of instant completion - if ( empty($resourceHash) === false && ConfigMapper::get('instant_completion') === true && RedisSavedPath::exists($savedPathKey = RedisSavedPath::getKey($group, $resourceHash)) === true ) { + if ( ConfigMapper::get('instant_completion') === true && !empty($resourceHash) && RedisSavedPath::exists($savedPathKey = RedisSavedPath::getKey($group, $resourceHash)) === true ) { $result['savedPath'] = RedisSavedPath::get($savedPathKey); return Responser::returnResult($result); @@ -97,8 +97,8 @@ public function saveChunk() 'resource_ext' => 'required', 'resource_chunk' => 'required', 'group_subdir' => 'required', - 'resource_hash' => 'required', 'group' => 'required', + 'resource_hash' => 'present', ]); $chunkTotalCount = Request::input('chunk_total'); @@ -129,7 +129,7 @@ public function saveChunk() } // determine if this upload meets the condition of instant completion - if ( empty($resourceHash) === false && ConfigMapper::get('instant_completion') === true && RedisSavedPath::exists($savedPathKey) === true ) { + if ( ConfigMapper::get('instant_completion') === true && !empty($resourceHash) && RedisSavedPath::exists($savedPathKey) === true ) { $partialResource->delete(); unset($partialResource->chunkIndex); $result['savedPath'] = RedisSavedPath::get($savedPathKey); @@ -162,13 +162,13 @@ public function saveChunk() $partialResource->checkMimeType(); // trigger the event before an upload completes - if ( empty($beforeUploadCompleteEvent = ConfigMapper::get('event_before_upload_complete')) === false ) { + if ( !empty($beforeUploadCompleteEvent = ConfigMapper::get('event_before_upload_complete'))) { event(new $beforeUploadCompleteEvent($partialResource)); } $resourceRealHash = $partialResource->calculateHash(); - if ( $resourceHash !== $resourceRealHash ) { + if ( ConfigMapper::get('lax_mode') === false && $resourceHash !== $resourceRealHash ) { throw new \Exception(trans('aetherupload::messages.upload_error')); } @@ -183,7 +183,7 @@ public function saveChunk() unset($partialResource->chunkIndex); // trigger the event when an upload completes - if ( empty($uploadCompleteEvent = ConfigMapper::get('event_upload_complete')) === false ) { + if ( !empty($uploadCompleteEvent = ConfigMapper::get('event_upload_complete'))) { event(new $uploadCompleteEvent(new Resource($group, ConfigMapper::get('group_dir'), $groupSubDir, $completeName))); } diff --git a/views/example.blade.php b/views/example.blade.php index bc8f51e..1103e59 100644 --- a/views/example.blade.php +++ b/views/example.blade.php @@ -21,8 +21,8 @@
- - + +