Skip to content

Commit

Permalink
version 2.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
peinhu committed Jul 16, 2019
1 parent 3085326 commit 8aa2781
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**
添加自定义路由支持
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
25 changes: 19 additions & 6 deletions assets/aetherupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,

Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -307,7 +311,7 @@ var AetherUpload = {

typeof(_this.callback) !== 'undefined' ? _this.callback() : null;

}else{
} else {

++_this.i;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -461,6 +472,8 @@ function aetherupload(resource) {

newInstance.uploadingRoute = '/aetherupload/uploading'; //上传路由的默认值

newInstance.laxMode = false; //宽松模式

return newInstance;
}

Expand Down
13 changes: 13 additions & 0 deletions config/aetherupload.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@
'route_display' => '/aetherupload/display', # 文件展示的路由
'route_download' => '/aetherupload/download', # 文件下载的路由

/*
|--------------------------------------------------------------------------
| 宽松模式
|--------------------------------------------------------------------------
|
| 【一般设置】在某些特殊场景,通过上传前跳过计算hash,可缩短总耗时。此选项开启后,无法进行秒传和完整性校验。默认不启用。
|
| 注意:开启后需在前端同时调用setLaxMode(true)方法。
|
*/

'lax_mode' => false,

/*
|--------------------------------------------------------------------------
| 资源分组
Expand Down
2 changes: 2 additions & 0 deletions src/ConfigMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ConfigMapper
private $route_uploading;
private $route_display;
private $route_download;
private $lax_mode;

private function __construct()
{
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions src/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'));
}

Expand All @@ -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)));
}

Expand Down
4 changes: 2 additions & 2 deletions views/example.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<div class="form-group " id="aetherupload-wrapper"><!--组件最外部需要一个名为aetherupload-wrapper的id,用以包装组件-->
<label>文件1(自定义):</label>
<div class="controls">
<input type="file" id="aetherupload-resource" onchange="aetherupload(this).setGroup('file').setSavedPathField('#aetherupload-savedpath').setPreprocessRoute('/aetherupload/preprocess').setUploadingRoute('/aetherupload/uploading').success(someCallback).upload()"/>
<!--需要一个名为aetherupload-resource的id,用以标识上传的文件,setGroup(...)设置分组名,setSavedPathField(...)设置资源存储路径的保存节点,setPreprocessRoute(...)设置预处理路由,setUploadingRoute(...)设置上传分块路由,success(...)可用于声名上传成功后的回调方法名。默认为选择文件后触发上传,也可根据需求手动更改为特定事件触发,如点击提交表单时-->
<input type="file" id="aetherupload-resource" onchange="aetherupload(this).setGroup('file').setSavedPathField('#aetherupload-savedpath').setPreprocessRoute('/aetherupload/preprocess').setUploadingRoute('/aetherupload/uploading').setLaxMode(false).success(someCallback).upload()"/>
<!--需要一个名为aetherupload-resource的id,用以标识上传的文件,setGroup(...)设置分组名,setSavedPathField(...)设置资源存储路径的保存节点,setPreprocessRoute(...)设置预处理路由,setUploadingRoute(...)设置上传分块路由,setLaxMode(...)设置宽松模式,success(...)可用于声名上传成功后的回调方法名。默认为选择文件后触发上传,也可根据需求手动更改为特定事件触发,如点击提交表单时-->
<div class="progress " style="height: 6px;margin-bottom: 2px;margin-top: 10px;width: 200px;">
<div id="aetherupload-progressbar" style="background:blue;height:6px;width:0;"></div><!--需要一个名为aetherupload-progressbar的id,用以标识进度条-->
</div>
Expand Down

0 comments on commit 8aa2781

Please sign in to comment.