修复分片上传在Windows下的兼容问题

优化分片上传合并失败时清除分片文件
pull/192/MERGE
Karson 2020-08-22 23:26:55 +08:00
parent 260198e1cb
commit dd83a6255e
1 changed files with 32 additions and 7 deletions

View File

@ -28,6 +28,8 @@ class Upload
*/ */
protected static $maxCheckNums = 10; protected static $maxCheckNums = 10;
protected $merging = false;
protected $chunkDir = null; protected $chunkDir = null;
protected $config = []; protected $config = [];
@ -171,7 +173,11 @@ class Upload
{ {
$iterator = new \GlobIterator($this->chunkDir . DS . $chunkid . '-*', FilesystemIterator::KEY_AS_FILENAME); $iterator = new \GlobIterator($this->chunkDir . DS . $chunkid . '-*', FilesystemIterator::KEY_AS_FILENAME);
$array = iterator_to_array($iterator); $array = iterator_to_array($iterator);
var_dump($array); foreach ($array as $index => &$item) {
$sourceFile = $item->getRealPath() ?: $item->getPathname();
$item = null;
@unlink($sourceFile);
}
} }
/** /**
@ -195,6 +201,7 @@ class Upload
} }
} }
if (!$completed) { if (!$completed) {
$this->clean($chunkid);
throw new UploadException(__('Chunk file info error')); throw new UploadException(__('Chunk file info error'));
} }
@ -202,6 +209,7 @@ class Upload
$uploadPath = $filePath; $uploadPath = $filePath;
if (!$destFile = @fopen($uploadPath, "wb")) { if (!$destFile = @fopen($uploadPath, "wb")) {
$this->clean($chunkid);
throw new UploadException(__('Chunk file merge error')); throw new UploadException(__('Chunk file merge error'));
} }
if (flock($destFile, LOCK_EX)) { // 进行排他型锁定 if (flock($destFile, LOCK_EX)) { // 进行排他型锁定
@ -229,12 +237,15 @@ class Upload
'error' => 0, 'error' => 0,
'size' => $file->getSize() 'size' => $file->getSize()
]; ];
$file->setUploadInfo($info); $file->setSaveName($filename)->setUploadInfo($info);
$file->isTest(true); $file->isTest(true);
//重新设置文件 //重新设置文件
$this->setFile($file); $this->setFile($file);
unset($file);
$this->merging = true;
//允许大文件 //允许大文件
$this->config['maxsize'] = "1024G"; $this->config['maxsize'] = "1024G";
@ -287,14 +298,28 @@ class Upload
$uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1); $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
$fileName = substr($savekey, strripos($savekey, '/') + 1); $fileName = substr($savekey, strripos($savekey, '/') + 1);
$destDir = ROOT_PATH . 'public' . $uploadDir; $destDir = ROOT_PATH . 'public' . str_replace('/', DS, $uploadDir);
$sha1 = $this->file->hash(); $sha1 = $this->file->hash();
$file = $this->file->move($destDir, $fileName); //如果是合并文件
if (!$file) { if ($this->merging) {
// 上传失败获取错误信息 if (!$this->file->check()) {
throw new UploadException($this->file->getError()); throw new UploadException($this->file->getError());
}
$destFile = $destDir . $fileName;
$sourceFile = $this->file->getRealPath() ?: $this->file->getPathname();
$info = $this->file->getInfo();
$this->file = null;
rename($sourceFile, $destFile);
$file = new File($destFile);
$file->setSaveName($fileName)->setUploadInfo($info);
} else {
$file = $this->file->move($destDir, $fileName);
if (!$file) {
// 上传失败获取错误信息
throw new UploadException($this->file->getError());
}
} }
$this->file = $file; $this->file = $file;
$params = array( $params = array(