123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- /**
- * @filename Upload.php
- * @createdAt 2020/1/25
- * @project https://github.com/yanwenwu/catch-admin
- * @document http://doc.catchadmin.com
- * @author JaguarJack <njphper@gmail.com>
- * @copyright By CatchAdmin
- * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
- */
- namespace catchAdmin\system\controller;
- use catchAdmin\system\model\Attachments;
- use catcher\base\CatchController;
- use catcher\base\CatchRequest;
- use catcher\CatchResponse;
- use catcher\CatchUpload;
- use catcher\exceptions\FailedException;
- use think\facade\Db;
- use catcher\facade\FileSystem;
- use web2oss\WebUploadToOss;
- use catchAdmin\system\model\Config;
- class Upload extends CatchController
- {
- protected $attachment;
- public function __construct(Attachments $attachment)
- {
- $this->attachment = $attachment;
- }
- /**
- * image upload
- *
- * @time 2020年01月25日
- * @param CatchRequest $request
- * @param CatchUpload $upload
- * @return \think\response\Json
- */
- public function image(CatchRequest $request, CatchUpload $upload): \think\response\Json
- {
- $images = $request->file();
- // var_dump($images);
- if (!$images) {
- throw new FailedException('请选择图片上传');
- }
- return CatchResponse::success($upload->checkImages($images)->multiUpload($images['image']));
- }
- /**
- * file upload
- *
- * @time 2020年01月25日
- * @param CatchRequest $request
- * @param CatchUpload $upload
- * @return \think\response\Json
- */
- public function file(CatchRequest $request, CatchUpload $upload): \think\response\Json
- {
- $files = $request->file();
- return CatchResponse::success($upload->checkFiles($files)->multiUpload($files['file']));
- }
- /**
- * file upload 固件升级文件上传特殊处理
- *
- * @time 2020年01月25日
- * @param CatchRequest $request
- * @param RlCatchUpload $upload
- * @return \think\response\Json
- */
- public function file_v2(CatchRequest $request, CatchUpload $upload): \think\response\Json
- {
- $files = $request->file();
- // $content=file_get_contents($files['file']->getPathname());
- $fh = fopen($files['file']->getPathname(), "rb");
- $content = bin2hex(fread($fh, 16));
- fclose($fh);
-
- if(!$content){
- return CatchResponse::fail('文件读取失败');
- }
- $str=str_replace(' ','',$content);
- $ver=substr($str , 8 , 4);
- $uid=substr($str , 16 , 8);
- $rom_ver=substr($str , 28 , 4);
-
- if(!$ver || !$uid || !$rom_ver){
- return CatchResponse::fail('文件内容格式不正确');
- }
- $firm_ver=$rom_ver.$ver;
- $basename=$files['file']->getOriginalName();
- // $path='firmware' . DIRECTORY_SEPARATOR .$basename;
- // if($upload->checkFilesExists($path)){
- // return CatchResponse::fail('文件已存在');
- // }
- if(Db::table("firmware_upgrade")->where('file_name', $basename)->where('deleted_at', 0)->count()){
- return CatchResponse::fail('文件已存在');
- }
- $path_parts = pathinfo($basename);
- $filename=$path_parts['filename'];
- $res=$upload->checkFiles($files)->multiUpload($files['file'],function()use ($filename){
- return 'firmware'. DIRECTORY_SEPARATOR .$filename;
- });
- if(!$res){
- return CatchResponse::fail('文件上传失败');
- }
-
- $data=array('file_url'=>$res,'ver'=>$firm_ver,'uid'=>$uid);
- return CatchResponse::success($data);
- }
- /**
- * file upload 固件升级文件上传特殊处理
- *
- * @time 2020年01月25日
- * @param CatchRequest $request
- * @param RlCatchUpload $upload
- * @return \think\response\Json
- */
- public function file_excel(CatchRequest $request, CatchUpload $upload): \think\response\Json
- {
- $files = $request->file();
- $basename=$files['file']->getOriginalName();
- $path_parts = pathinfo($basename);
- $filename=$path_parts['filename'];
- $res=$upload->checkFiles($files)->multiUpload($files['file'],function()use ($filename){
- return 'student_excel'. DIRECTORY_SEPARATOR .$filename;
- });
- if(!$res){
- return CatchResponse::fail('文件上传失败');
- }
-
- $data=array('excel_url'=>$res,'msg'=>'上传成功');
- return CatchResponse::success($data);
- }
- /**
- * file upload
- *
- * @time 2020年01月25日
- * @param CatchRequest $request
- * @param CatchUpload $upload
- * @return \think\response\Json
- */
- public function unlinkFile(CatchRequest $request): \think\response\Json
- {
- $params = $request->post();
- $file_name=basename($params['file_url']);
- //if ($attachment->driver == 'local') {
- $localPath = config('filesystem.disks.local.root') . DIRECTORY_SEPARATOR;
- $path = $localPath . 'firmware/'.$file_name;
- if (FileSystem::exists($path)) {
- Filesystem::delete($path);
- }
- // } else {
- // Filesystem::delete($attachment->path);
- // }
-
- return CatchResponse::success();
- }
-
- /**
- * 获取阿里云OSS策略
- */
- public function getAliOssPolicy()
- {
- $get = request()->get();
- $max_size = isset($get['maxSize']) ? intval($get['maxSize']) : 10;
- // 从配置表中获取配置信息
- try {
- $configs = (new Config())->getConfig('upload');
- } catch(\Exception $e) {
- return CatchResponse::fail('获取配置数据失败');
- }
- if (!isset($configs['oss'])) {
- return CatchResponse::fail('获取OSS配置数据失败');
- }
- $oss = $configs['oss'];
- $bucket = isset($oss['bucket']) ? $oss['bucket'] : '';
- $end_point = isset($oss['end_point']) ? $oss['end_point'] : '';
- $oss_host = 'https://'. $bucket .'.'.$end_point; // 格式为 bucketname.endpoint
- $config = array(
- 'id' => isset($oss['access_key']) ? $oss['access_key'] : '',
- 'key' => isset($oss['secret_key']) ? $oss['secret_key'] : '',
- 'host' => $oss_host,
- 'callbackUrl' => env('APP_DOMAIN') . '/upload/osscallback', // $_SERVER['HTTP_HOST'].'/osscallback',// 上传完成回调
- 'fileSize' => $max_size * 1024 * 1024, // bit
- 'dir' => 'wxt/'.date('Y-m-d').'/',
- 'expire' => 60, // 设置该policy超时时间(单位:秒). 即这个policy过了这个有效时间,将不能访问。
- );
- $response = WebUploadToOss::getPolicy($config);
- if (!$response['success']) {
- return CatchResponse::fail($response['message']);
- }
- return CatchResponse::success($response['data']);
- }
- /**
- * 阿里云OSS回调
- */
- public function aliOssCallback()
- {
- $res = WebUploadToOss::callback();
- if (!$res['success']) {
- return CatchResponse::fail($res['message']);
- }
- return CatchResponse::success($res['data']);
- }
- }
|