Skip to content

Commit

Permalink
修正同时两个组件时图片资源混淆
Browse files Browse the repository at this point in the history
  • Loading branch information
emnabs committed Nov 7, 2017
1 parent 0114878 commit fd28119
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 58 deletions.
74 changes: 37 additions & 37 deletions ChunkUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@
*/
class ChunkUploader {

/**
* Processes a chunked file upload.
* @param UploadedFile $uploadedFile
* @param string $path path to write chunks to
* @returns boolean true if file upload is complete, or false if there are more chunks
* @throws Exception
*/
public static function process($uploadedFile, $path) {
if (!$uploadedFile || $uploadedFile->hasError) {
throw new Exception('Failed to upload file');
}
$chunk = (int) Yii::$app->request->getBodyParam('chunk', 0);
$totalChunks = (int) Yii::$app->request->getBodyParam('chunks', 0);
$out = fopen("$path.part", $chunk == 0 ? 'wb' : 'ab');
if (!$out) {
throw new Exception('Failed to open output stream');
}
// Read binary input stream and append it to temporary .part file
$in = fopen($uploadedFile->tempName, 'rb');
if ($in) {
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
} else {
throw new Exception('Failed to open input stream');
}
fclose($in);
fclose($out);
unlink($uploadedFile->tempName);
// Check if all chunks have been processed
if (!$totalChunks || $chunk == $totalChunks - 1) {
// Strip the temp .part suffix off
rename("$path.part", $path);
return true;
}
return false;
}
/**
* Processes a chunked file upload.
* @param UploadedFile $uploadedFile
* @param string $path path to write chunks to
* @returns boolean true if file upload is complete, or false if there are more chunks
* @throws Exception
*/
public static function process($uploadedFile, $path) {
if (!$uploadedFile || $uploadedFile->hasError) {
throw new Exception('Failed to upload file');
}
$chunk = (int) Yii::$app->request->getBodyParam('chunk', 0);
$totalChunks = (int) Yii::$app->request->getBodyParam('chunks', 0);
$out = fopen("$path.part", $chunk == 0 ? 'wb' : 'ab');
if (!$out) {
throw new Exception('Failed to open output stream');
}
// Read binary input stream and append it to temporary .part file
$in = fopen($uploadedFile->tempName, 'rb');
if ($in) {
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
} else {
throw new Exception('Failed to open input stream');
}
fclose($in);
fclose($out);
unlink($uploadedFile->tempName);
// Check if all chunks have been processed
if (!$totalChunks || $chunk == $totalChunks - 1) {
// Strip the temp .part suffix off
rename("$path.part", $path);
return true;
}
return false;
}

}
6 changes: 2 additions & 4 deletions Plupload.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function init() {
if (is_array($value) || $matches[3] === '[]') {
$this->multiSelection = true;
}
if ($this->multiSelection && !is_array($value)) {
if ($value && $this->multiSelection && !is_array($value)) {
$model->$attribute = [$value];
}
$this->responeElement = Html::getInputId($this->model, $this->attribute);
Expand All @@ -129,7 +129,6 @@ public function init() {
Html::addCssClass($this->htmlOptions, 'plupload_many_thumb');
} else {
Html::addCssClass($this->htmlOptions, 'plupload_one');
// Set wrapper of this widget style.
$this->setWrapperStyle();
$this->allow_max_nums = 1;
}
Expand Down Expand Up @@ -241,9 +240,8 @@ public function registerAssets() {
'name' => $this->name,
'id' => $this->responeElement,
]);
$scripts .= "\nPluploadCustom.init({$customOptions});";
}
$scripts .= "\nCustom.init({$customOptions});";

$view->registerJs($scripts);
}

Expand Down
2 changes: 1 addition & 1 deletion PluploadAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PluploadAsset extends AssetBundle {
*/
public $js = [
'js/plupload.full.min.js',
'js/custom.js',
'js/plupload.custom.js',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion PluploadEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected function bindFilesAdded() {
' . self::JQUERY . '("#' . $this->errorContainer . '").hide();
var upfiles = "";
plupload.each(files, function(file) {
upfiles += Custom.tplUploadItem(file);
upfiles += PluploadCustom.tplUploadItem(file,' . $this->multiSelection . ');
});
' . self::JQUERY . '(document).on("click", ".plupload_file_action", function () {
var id = ' . self::JQUERY . '(this).parent().attr("id");
Expand Down
25 changes: 10 additions & 15 deletions assets/js/custom.js → assets/js/plupload.custom.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
/**
Custom module for you to write your own javascript functions
**/
var Custom = function () {
var temp = '';

var PluploadCustom = function () {
var inputName;
var inputId;
var multiInput = false;
var plupload;
return {
tplUploadItem: function (file) {
tplUploadItem: function (file, multi = false) {
var path = file.path;
if (path === undefined) {
path = '';
}
return this.buildItem(file.id, file.name, path, plupload.formatSize(file.size));
return this.buildItem(file.id, file.name, path, plupload.formatSize(file.size), multi);
},
buildItem: function (id, name, path, size) {
temp = '<li class="plupload_file plupload_file_loading" id="' + id + '">';
buildItem: function (id, name, path, size, multi) {
var temp = '<li class="plupload_file plupload_file_loading" id="' + id + '">';

temp += this.buildInput(id, path);
temp += this.buildInput(id, path, multi);

temp += '<div class="plupload_file_thumb"><img src="' + path + '"></div>';

Expand All @@ -39,8 +35,8 @@ var Custom = function () {
'</div>';
return temp += '</li>';
},
buildInput: function (index, value) {
if (multiInput) {
buildInput: function (index, value, multi) {
if (multi) {
return '<input id="' + this.getInputId(index) + '" name="' + this.getInputName(index) + '" value="' + value + '" type="hidden" class="plupload_file_input">';
}
return '';
Expand All @@ -57,10 +53,9 @@ var Custom = function () {
return;
}
plupload = window.plupload;
if (options.length !== 0) {
if (options.length > 0) {
inputName = options.name;
inputId = options.id;
multiInput = true;
}
}
};
Expand Down

0 comments on commit fd28119

Please sign in to comment.