You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.6 KiB
100 lines
2.6 KiB
<?php
|
|
|
|
namespace App\Admin\Common;
|
|
|
|
use Dcat\Admin\Traits\HasUploadedFile;
|
|
|
|
class CustomFileController
|
|
{
|
|
/**
|
|
* 自定义上传
|
|
*/
|
|
use HasUploadedFile;
|
|
|
|
public function handle()
|
|
{
|
|
$disk = $this->disk('oss');
|
|
|
|
// 判断是否是删除文件请求
|
|
if ($this->isDeleteRequest()) {
|
|
// 删除文件并响应
|
|
return $this->deleteFileAndResponse($disk);
|
|
}
|
|
|
|
// 获取上传的文件
|
|
$file = $this->file();
|
|
|
|
// 获取上传的字段名称
|
|
// $column = $this->uploader()->upload_column;
|
|
|
|
// 图片拓展名
|
|
// $fileOriginalExtension = '.png';
|
|
|
|
$fileName = md5(time().rand(0000,9999));
|
|
|
|
$dir = 'mp_images';
|
|
$newName = date('Y-m-d-').$fileName.'.'.$file->getClientOriginalExtension();
|
|
|
|
$result = $disk->putFileAs($dir, $file, $newName);
|
|
|
|
$path = "{$dir}/$newName";
|
|
|
|
return $result
|
|
? $this->responseUploaded($path, $disk->url($path))
|
|
: $this->responseErrorMessage('文件上传失败');
|
|
}
|
|
|
|
/**
|
|
* 复制文件
|
|
* @param $imageDir 存储文件夹
|
|
* @param $filePath 文件相对路径
|
|
*/
|
|
public function autoCopyFile($imageDir, $file)
|
|
{
|
|
$disk = $this->disk('oss');
|
|
|
|
$result = false;
|
|
if($disk->exists($file)){
|
|
|
|
// 图片拓展名
|
|
$fileOriginalExtension = pathinfo($file)['extension'];
|
|
|
|
$fileName = md5(time().rand(0000,9999));
|
|
|
|
$dir = $imageDir;
|
|
$newName = 'copy_'.$fileName.'.'.$fileOriginalExtension;
|
|
$path = "{$dir}/$newName";
|
|
|
|
$result = $disk->copy($file, $path);
|
|
};
|
|
|
|
return $result
|
|
? ['status' => true , 'msg' => '文件复制成功', 'path' => $path, 'url' => $disk->url($path)]
|
|
: ['status' => false , 'msg' => '文件复制失败'];
|
|
}
|
|
|
|
/**
|
|
* 上传文件
|
|
* @param $imageDir 存储文件夹
|
|
* @param $filePath 文件相对路径
|
|
*/
|
|
public function autoUploadFile($imageDir, $file)
|
|
{
|
|
$disk = $this->disk('oss');
|
|
|
|
// 图片拓展名
|
|
$fileOriginalExtension = pathinfo($file)['extension'];
|
|
|
|
$fileName = md5(time().rand(0000,9999));
|
|
|
|
$dir = $imageDir;
|
|
$newName = 'upload_'.$fileName.'.'.$fileOriginalExtension;
|
|
$path = "{$dir}/$newName";
|
|
|
|
$result = $disk->putFileAs($dir, $file, $newName);
|
|
|
|
return $result
|
|
? ['status' => true , 'msg' => '文件上传成功', 'path' => $path, 'url' => $disk->url($path)]
|
|
: ['status' => false , 'msg' => '文件上传失败'];
|
|
}
|
|
}
|