161 lines
5.2 KiB
PHP
161 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace plugin\admin\app\controller;
|
|
|
|
use support\Request;
|
|
use support\Response;
|
|
use support\think\Db;
|
|
use taoser\facade\Validate;
|
|
use Shopwwi\WebmanFilesystem\Facade\Storage;
|
|
|
|
/**
|
|
* 附件管理
|
|
*
|
|
* @icon fa fa-circle-o
|
|
*/
|
|
class FilesController extends Crud
|
|
{
|
|
|
|
/**
|
|
* @var \app\model\Files
|
|
*/
|
|
protected $model = null;
|
|
/**
|
|
* 构造函数
|
|
* @return void
|
|
*/
|
|
function __construct()
|
|
{
|
|
$this->model = new \app\model\Files();
|
|
}
|
|
function list(Request $request)
|
|
{
|
|
return view('', [
|
|
|
|
]);
|
|
}
|
|
function feupload(Request $request): Response
|
|
{
|
|
$user = ['id'=>admin_id()];
|
|
$savePath = $request->post('savePath','files');
|
|
$validate = Validate::rule('savePath', 'alphaNum');
|
|
$data = ['savePath' => $savePath];
|
|
if (!$validate->check($data)) {
|
|
return $this->fail( '参数错误:'.$validate->getError());
|
|
}
|
|
$savePath = trim($savePath,'/');
|
|
$savePath = 'upload/'.$savePath.'/'.$user['id'];
|
|
$maxSize = 1024*1024*100; //100M
|
|
//多文件上传
|
|
$files = $request->file();
|
|
try {
|
|
$result = Storage::adapter('public')
|
|
->path($savePath)
|
|
->size(1024*1024*10)
|
|
->extYes(['image/jpeg','image/png'])
|
|
->uploads($files,0,$maxSize,false);
|
|
$save_datas = [];
|
|
foreach($result as $k=>$fileinfo){
|
|
$save_datas[] = [
|
|
'user_id' => $user['id'],
|
|
'title' => $fileinfo->origin_name,
|
|
'path' => $fileinfo->file_name,
|
|
'size' => $fileinfo->size,
|
|
'mime_type' => $fileinfo->mime_type,
|
|
'extension' => $fileinfo->extension,
|
|
'height' => $fileinfo->file_height,
|
|
'width' => $fileinfo->file_width,
|
|
'sha1' => sha1_file(public_path($fileinfo->file_name)),
|
|
'use_count' => 0,
|
|
];
|
|
}
|
|
\app\model\Files::saveAll($save_datas);
|
|
return $this->success(__('successful'),[
|
|
'link' => $result[0]['path'],
|
|
]);
|
|
}catch (\Exception $e){
|
|
return $this->fail($e->getMessage());
|
|
}
|
|
}
|
|
function upload(Request $request): Response
|
|
{
|
|
cp('0');
|
|
$savePath = $request->post('savePath','files');
|
|
$validate = Validate::rule('savePath', 'alphaNum');
|
|
$data = ['savePath' => $savePath];
|
|
cp($data);
|
|
if (!$validate->check($data)) {
|
|
cp($validate->getError());
|
|
return $this->fail( '参数错误:'.$validate->getError());
|
|
}
|
|
cp('1');
|
|
$file = current($request->file());
|
|
if (!$file || !$file->isValid()) {
|
|
return $this->fail('未找到文件');
|
|
}
|
|
cp('2');
|
|
$data = $this->base($request, $savePath);
|
|
cp('3');
|
|
//cp($data);
|
|
return $this->success( '上传成功', [
|
|
'url' => $data['realpath'],
|
|
'name' => $data['name'],
|
|
'fullurl' => $data['url'],
|
|
'size' => $data['size'],
|
|
'url1' => $data['url'],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取上传数据
|
|
* @param Request $request
|
|
* @param $savePath 保存位置
|
|
* @return array
|
|
* @throws \Exception
|
|
*/
|
|
protected function base(Request $request, $savePath): array
|
|
{
|
|
$user = ['id'=>admin_id()];
|
|
// 适配器 local默认是存储在runtime目录下 public默认是存储在public目录下
|
|
// 可访问的静态文件建议public
|
|
// 默认适配器是local
|
|
//Storage::adapter('public');
|
|
$savePath = trim($savePath,'/');
|
|
$savePath = 'upload/'.$savePath;
|
|
$file = current($request->file());
|
|
$mimetype = explode(',',Config('site.upload_mimetype'));
|
|
$maxsize = Config('site.upload_maxsize')*1024*1024;
|
|
$result = Storage::adapter('public')
|
|
->path($savePath)
|
|
->size($maxsize)
|
|
->extYes($mimetype)
|
|
->upload($file);
|
|
|
|
$save_datas = [
|
|
'user_id' => $user['id'],
|
|
'category' => 'default',
|
|
'title' => $result->origin_name,
|
|
'path' => $result->file_name,
|
|
'size' => $result->size,
|
|
'mime_type' => $result->mime_type,
|
|
'extension' => $result->extension,
|
|
'height' => $result->file_height,
|
|
'width' => $result->file_width,
|
|
'sha1' => sha1_file(public_path($result->file_name)),
|
|
'use_count' => 0,
|
|
];
|
|
(new \app\model\Files)->save($save_datas);
|
|
return [
|
|
'code' => 0,
|
|
'url' => $result->file_url,
|
|
'name' => $result->origin_name,
|
|
'realpath' => '/'.$result->file_name,
|
|
'size' => $result->size,
|
|
'mime_type' => $result->mime_type,
|
|
'image_with'=> $result->file_width,
|
|
'image_height' => $result->file_height,
|
|
'ext' => $result->extension,
|
|
];
|
|
}
|
|
}
|