generated from liutk/owl-admin-base
调整分片上传处理
parent
6b8caeedf5
commit
ab4f0e3683
|
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
namespace App\Traits;
|
namespace App\Traits;
|
||||||
|
|
||||||
use Illuminate\Support\Arr;
|
|
||||||
use Illuminate\Support\Facades\Storage;
|
|
||||||
use Illuminate\Support\Str;
|
|
||||||
use Slowlyo\OwlAdmin\Admin;
|
use Slowlyo\OwlAdmin\Admin;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
trait UploadTrait
|
trait UploadTrait
|
||||||
{
|
{
|
||||||
|
|
@ -69,7 +69,7 @@ trait UploadTrait
|
||||||
return $this->response()->success(['value' => $path]);
|
return $this->response()->success(['value' => $path]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function saveImage($field, $path)
|
public function saveImage($field = 'image', $path)
|
||||||
{
|
{
|
||||||
return $this->saveFile($field, $path);
|
return $this->saveFile($field, $path);
|
||||||
}
|
}
|
||||||
|
|
@ -77,7 +77,7 @@ trait UploadTrait
|
||||||
/**
|
/**
|
||||||
* 表单提交时,转存实际目录,并保留上传时文件名称;文件保存全路径
|
* 表单提交时,转存实际目录,并保留上传时文件名称;文件保存全路径
|
||||||
*/
|
*/
|
||||||
public function saveFile($field, $path)
|
public function saveFile($field = 'file', $path)
|
||||||
{
|
{
|
||||||
$file = request()->file($field);
|
$file = request()->file($field);
|
||||||
|
|
||||||
|
|
@ -100,8 +100,12 @@ trait UploadTrait
|
||||||
case 'init':
|
case 'init':
|
||||||
if(strpos($file['value'], 'temporary') !== false){
|
if(strpos($file['value'], 'temporary') !== false){
|
||||||
$filePath = $path.'/'.$file['name'];
|
$filePath = $path.'/'.$file['name'];
|
||||||
Storage::disk(Admin::config('admin.upload.disk'))->move($file['value'], $filePath);
|
$res = Storage::disk(Admin::config('admin.upload.disk'))->move($file['value'], $filePath);
|
||||||
|
if($res){
|
||||||
$fileArr[] = Storage::disk(Admin::config('admin.upload.disk'))->url($filePath);
|
$fileArr[] = Storage::disk(Admin::config('admin.upload.disk'))->url($filePath);
|
||||||
|
}else{
|
||||||
|
return response()->fail(__('admin.upload_file_error'));
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
$fileArr[] = $file['value'];
|
$fileArr[] = $file['value'];
|
||||||
}
|
}
|
||||||
|
|
@ -111,12 +115,17 @@ trait UploadTrait
|
||||||
$filePath = $path.'/'.$file['name'];
|
$filePath = $path.'/'.$file['name'];
|
||||||
if(Str::startsWith($file['value'], ['http://', 'https://'])){
|
if(Str::startsWith($file['value'], ['http://', 'https://'])){
|
||||||
$fileUrl = parse_url($file['value']);
|
$fileUrl = parse_url($file['value']);
|
||||||
$fileValue = ltrim($fileUrl['path'], '/');
|
$fileValue = ltrim($fileUrl['path'], '/storage');
|
||||||
}else{
|
}else{
|
||||||
$fileValue = $file['value'];
|
$fileValue = $file['value'];
|
||||||
}
|
}
|
||||||
Storage::disk(Admin::config('admin.upload.disk'))->move($file['value'], $filePath);
|
$res = Storage::disk(Admin::config('admin.upload.disk'))->move($fileValue, $filePath);
|
||||||
|
|
||||||
|
if($res){
|
||||||
$fileArr[] = Storage::disk(Admin::config('admin.upload.disk'))->url($filePath);
|
$fileArr[] = Storage::disk(Admin::config('admin.upload.disk'))->url($filePath);
|
||||||
|
}else{
|
||||||
|
return $this->response()->fail(__('admin.upload_file_error'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +134,6 @@ trait UploadTrait
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $fileArr;
|
return $fileArr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -159,8 +167,8 @@ trait UploadTrait
|
||||||
$uploadId = request()->get('uploadId');
|
$uploadId = request()->get('uploadId');
|
||||||
$partNumber = request()->get('partNumber');
|
$partNumber = request()->get('partNumber');
|
||||||
$partSize = request()->get('partSize');
|
$partSize = request()->get('partSize');
|
||||||
// dd($file);
|
|
||||||
$fileName = $file->getClientOriginalName();
|
$fileName = $file->getClientOriginalName();
|
||||||
|
$fileName = str_replace(' ', '', $fileName);//去掉空格
|
||||||
|
|
||||||
//判断该分片是否已存在,
|
//判断该分片是否已存在,
|
||||||
$dirPath = Admin::config('admin.upload.tem_directory.' . $type).'/'.$uploadId;
|
$dirPath = Admin::config('admin.upload.tem_directory.' . $type).'/'.$uploadId;
|
||||||
|
|
@ -171,7 +179,6 @@ trait UploadTrait
|
||||||
$path = $file->storeAs($dirPath, $fileName.'_'.$partNumber, Admin::config('admin.upload.disk'));
|
$path = $file->storeAs($dirPath, $fileName.'_'.$partNumber, Admin::config('admin.upload.disk'));
|
||||||
$realPath = Storage::disk(Admin::config('admin.upload.disk'))->url($path);
|
$realPath = Storage::disk(Admin::config('admin.upload.disk'))->url($path);
|
||||||
$eTag = md5_file($realPath);
|
$eTag = md5_file($realPath);
|
||||||
|
|
||||||
return $this->response()->success(['eTag' => $eTag]);
|
return $this->response()->success(['eTag' => $eTag]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -184,6 +191,7 @@ trait UploadTrait
|
||||||
$type = 'file';
|
$type = 'file';
|
||||||
//合并文件
|
//合并文件
|
||||||
$fileName = request()->get('filename', '');
|
$fileName = request()->get('filename', '');
|
||||||
|
$fileName = str_replace(' ', '', $fileName);
|
||||||
$uploadId = request()->get('uploadId', '');
|
$uploadId = request()->get('uploadId', '');
|
||||||
$partList = request()->get('partList', []);
|
$partList = request()->get('partList', []);
|
||||||
|
|
||||||
|
|
@ -194,13 +202,13 @@ trait UploadTrait
|
||||||
$partNumberList = Arr::pluck($partList, 'partNumber');
|
$partNumberList = Arr::pluck($partList, 'partNumber');
|
||||||
|
|
||||||
if(max($partNumberList) === count($partNumberList)){
|
if(max($partNumberList) === count($partNumberList)){
|
||||||
//判断是否已存在同名文件,进行删除
|
//判断是否已存在同名文件,进行重命名
|
||||||
if(Storage::disk(Admin::config('admin.upload.disk'))->exists($realPath)){
|
if(Storage::disk(Admin::config('admin.upload.disk'))->exists($realPath)){
|
||||||
$realPath = 'chunk/(1)'.$fileName;
|
$realPath = 'chunk/(1)'.$fileName;
|
||||||
}
|
}
|
||||||
for($i = 1; $i<= count($partNumberList); $i++){
|
for($i = 1; $i<= count($partNumberList); $i++){
|
||||||
$_file = Storage::disk(Admin::config('admin.upload.disk'))->get($basePath.'/'.$fileName.'_'.$i);
|
$_file = Storage::disk(Admin::config('admin.upload.disk'))->get($basePath.'/'.$fileName.'_'.$i);
|
||||||
Storage::disk(Admin::config('admin.upload.disk'))->append($realPath, $_file, null);
|
$res = Storage::disk(Admin::config('admin.upload.disk'))->append($realPath, $_file, null);
|
||||||
}
|
}
|
||||||
//删除分片文件夹
|
//删除分片文件夹
|
||||||
Storage::disk(Admin::config('admin.upload.disk'))->deleteDirectory($basePath);
|
Storage::disk(Admin::config('admin.upload.disk'))->deleteDirectory($basePath);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue