OldUploadController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: PhpStorm
  5. * Date: 2019/3/18
  6. * Time: 14:01
  7. */
  8. namespace app\platform\modules\Application\controllers;
  9. use app\platform\controllers\BaseController;
  10. use app\platform\modules\system\models\SystemSetting;
  11. use app\platform\modules\application\models\CoreAttach;
  12. use app\common\services\qcloud\Api;
  13. use app\common\services\aliyunoss\OssClient;
  14. use app\common\services\aliyunoss\OSS\Core\OssException;
  15. use app\common\services\ImageZip;
  16. class OldUploadController extends BaseController
  17. {
  18. protected $proto;
  19. protected $path;
  20. protected $pattern;
  21. public function __construct()
  22. {
  23. //本地存放路径协议
  24. $this->proto = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://';
  25. $this->path = config('filesystems.disks.syst')['url'].'/'; //本地图片实际存放路径
  26. $this->pattern = '_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS';
  27. }
  28. public function upload()
  29. {
  30. $file = request()->file('file');
  31. \Log::info('upload_get_file', $file);
  32. if (count($file) > 6) {
  33. \Log::info('upload_file_num_out');
  34. return $this->errorJson('文件数量过多, 请选择低于6个文件');
  35. }
  36. if (count($file) > 1 && count($file) < 7) {
  37. //多文件上传
  38. \Log::info('upload_more_files');
  39. foreach ($file as $k => $v) {
  40. if ($v) {
  41. $url = $this->doUpload($v);
  42. $success[] = $url;
  43. //检验返回的是否是 正确合法链接
  44. if (!preg_match($pattern, $url)) {
  45. \Log::info('more_upload_fail');
  46. $fail[] = $url;
  47. unset($success[$k]);
  48. }
  49. }
  50. }
  51. } else {
  52. $success = $this->doUpload($file);
  53. \Log::info('upload_one_files');
  54. //检验参数是否为正确的url
  55. if (!preg_match($this->pattern, $success)) {
  56. \Log::info('upload_file_fail');
  57. $fail = $success;
  58. unset($success);
  59. }
  60. }
  61. return $this->successJson('ok', ['success'=>$success, 'fail'=>$fail]);
  62. }
  63. //视频类型建议使用第三方存储, 本地暂不支持
  64. public function doUpload($file)
  65. {
  66. if (!$file->isValid()) {
  67. \Log::info('no_upload_file');
  68. return false;
  69. }
  70. $setting = SystemSetting::settingLoad('global', 'system_global');
  71. $remote = SystemSetting::settingLoad('remote', 'system_remote');
  72. \Log::info('system_setting', [$setting, $remote]);
  73. //文件大小是否大于所设置的文件最大值
  74. //文件上传最大执行时间
  75. $ext = $file->getClientOriginalExtension();
  76. if ($file->getClientSize() > 30*1024) {
  77. //文件过大时执行本地上传
  78. \Log::info('file_size_out');
  79. }
  80. //默认支持的文件格式类型
  81. $defaultImgType = [
  82. 'jpg', 'bmp', 'eps', 'gif', 'mif', 'miff', 'png', 'tif',
  83. 'tiff', 'svg', 'wmf', 'jpe', 'jpeg', 'dib', 'ico', 'tga', 'cut', 'pic'
  84. ];
  85. $defaultAudioType = ['avi', 'asf', 'wmv', 'avs', 'flv', 'mkv', 'mov', '3gp', 'mp4',
  86. 'mpg', 'mpeg', 'dat', 'ogm', 'vob', 'rm', 'rmvb', 'ts', 'tp', 'ifo', 'nsv'
  87. ];
  88. $defaultVideoType = [
  89. 'mp3', 'aac', 'wav', 'wma', 'cda', 'flac', 'm4a', 'mid', 'mka', 'mp2',
  90. 'mpa', 'mpc', 'ape', 'ofr', 'ogg', 'ra', 'wv', 'tta', 'ac3', 'dts'
  91. ];
  92. $merge_ext = array_merge($defaultImgType, $defaultAudioType, $defaultVideoType);
  93. if (!in_array($ext, $merge_ext)) {
  94. return '非规定类型的文件格式';
  95. }
  96. if (in_array($ext, $defaultImgType)) {
  97. $file_type = 'syst';
  98. } elseif (in_array($ext, $defaultAudioType)) {
  99. $file_type = 'audio';
  100. } elseif (in_array($ext, $defaultVideoType)) {
  101. $file_type = 'video';
  102. }
  103. if ($setting['type'] == 0) {
  104. //判断是否属于设置的类型
  105. \Log::info('upload_local_file');
  106. if (in_array($ext, $defaultImgType)) {
  107. if ($sett['image_extentions'] && !in_array($ext, $img_type) ) {
  108. \Log::info('local_file_type_is_not_set_type');
  109. return '非规定类型的文件格式';
  110. }
  111. $defaultImgSize = $setting['img_size'] ? $setting['img_size'] * 1024 : 1024*1024*5; //默认大小为5M
  112. if ($file->getClientSize() > $defaultImgSize) {
  113. \Log::info('local_file_size_is_not_set_size');
  114. return '文件大小超出规定值';
  115. }
  116. }
  117. if (in_array($ext, $defaultAudioType) || in_array($ext, $defaultVideoType)) {
  118. if ($setting['audio_extentions'] && !in_array($ext, $img_type) ) {
  119. \Log::info('local_audio_video_file_type_is_not_set_type');
  120. return '非规定类型的文件格式';
  121. }
  122. $defaultAudioSize = $setting['audio_limit'] ? $setting['audio_limit'] * 1024 : 1024*1024*30; //音视频最大 30M
  123. if ($file->getClientSize() > $defaultAudioSize) {
  124. \Log::info('local_audio_video_file_size_is_not_set_size');
  125. return '文件大小超出规定值';
  126. }
  127. }
  128. //执行本地上传
  129. $res = $this->uploadLocal($file, $file_type, $setting['zip_percentage']);
  130. }
  131. if ($remote['type'] == 2) {
  132. //阿里OSS
  133. \Log::info('do_oss_upload');
  134. $res = $this->OssUpload($file, $remote['alioss'], $file_type);
  135. } if ($remote['type'] == 4) {
  136. //腾讯cos
  137. \Log::info('do_cos_upload');
  138. $res = $this->CosUpload($file, $remote['cos'], $file_type);
  139. }
  140. return $res;
  141. }
  142. //本地上传
  143. public function uploadLocal($file, $file_type, $percent)
  144. {
  145. if (!$file) {
  146. \Log::info('file_not_size');
  147. return '请传入正确参数';
  148. }
  149. if ($file->isValid()) {
  150. $originalName = $file->getClientOriginalName(); // 文件原名
  151. $realPath = $file->getRealPath(); //临时文件的绝对路径
  152. $ext = $file->getClientOriginalExtension(); //文件扩展名
  153. $newOriginalName = $this->getNewFileName($originalName, $ext);
  154. \Log::info('get_info', [$newOriginalName, $getRealPath]);
  155. $res = \Storage::disk($file_type)->put($newOriginalName, file_get_contents($realPath));
  156. if ($res) {
  157. $log = $this->getData($originalName, $file_type, \Storage::disk($file_type)->url().$newOriginalName, 0);
  158. if ($log != 1) {
  159. \Log::info('新框架本地上传记录失败', [$originalName, \Storage::disk($file_type)->url().$newOriginalName]);
  160. }
  161. }
  162. if ($percent) {
  163. //执行图片压缩
  164. $imagezip = new ImageZip();
  165. $zipOrNot = $imagezip->makeThumb(
  166. \Storage::disk($file_type)->url().$newOriginalName,
  167. \Storage::disk($file_type)->url().$newOriginalName,
  168. $percent
  169. );
  170. }
  171. return $this->proto.$_SERVER['HTTP_HOST'].$this->path.'/'.$newOriginalName;
  172. }
  173. }
  174. //获取本地已上传图片的列表 需优化搜索
  175. public function getLocalList()
  176. {
  177. $core = new CoreAttach();
  178. if (request()->year != '不限') {
  179. $search['year'] = request()->year;
  180. }
  181. if(request()->month != '不限') {
  182. $search['month'] = request()->month;
  183. }
  184. if ($search) {
  185. $core = $core->search($search);
  186. }
  187. $list = $core->paginate()->toArray();
  188. foreach ($list['data'] as $k => $v) {
  189. if ($v['attachment'] && $v['id']) {
  190. $data['data'][$k]['id'] = $v['id'];
  191. $data['data'][$k]['url'] = $this->proto.$_SERVER['HTTP_HOST'].$this->path.$v['attachment'];
  192. }
  193. }
  194. $data['total'] = $list['total'];
  195. $data['per_page'] = $list['per_page'];
  196. $data['last_page'] = $list['last_page'];
  197. $data['prev_page_url'] = $list['prev_page_url'];
  198. $data['next_page_url'] = $list['next_page_url'];
  199. $data['current_page'] = $list['current_page'];
  200. $data['from'] = $list['from'];
  201. $data['to'] = $list['to'];
  202. if (!$data['data']) {
  203. $data['data'] = [];
  204. }
  205. return $this->successJson('获取成功', $data);
  206. }
  207. public function delLocalImg()
  208. {
  209. $id = request()->id;
  210. $core = CoreAttach::find($id);
  211. if (!$core) {
  212. return $this->errorJson('请重新选择');
  213. }
  214. $setting = SystemSetting::settingLoad('remote', 'system_remote');
  215. if ($core['upload_type']== 2) { //oss
  216. $oss = new OssClient($setting['alioss']['key'], $setting['alioss']['secret'], $setting['alioss']['ossurl']);
  217. $res = $oss->deleteObject($setting['alioss']['bucket'], $core['attachment']); //info['url']
  218. if (!$res['info']['url']) {
  219. \Log::info('删除阿里云图片失败', [$core['id'], $res]);
  220. return $res;
  221. }
  222. } elseif ($core['upload_type'] == 4) { //cos
  223. $cos = new Api([
  224. 'app_id' => $setting['cos']['appid'],
  225. 'secret_id' => $setting['cos']['secretid'],
  226. 'secret_key' => $setting['cos']['secretkey'],
  227. 'region' => $setting['cos']['url']
  228. ]);
  229. $res = $cos->delFile($setting['cos']['bucket'], $core['attachment']); //[code =0 'message'='SUCCESS']
  230. if ($res['code'] != 0 || $res['message'] != 'SUCCESS') {
  231. //删除失败
  232. \Log::info('删除腾讯云图片失败', [$core['id'], $res]);
  233. return $res;
  234. }
  235. } else {
  236. //删除文件
  237. $res = \app\common\services\Storage::remove($core['attachment']);
  238. if (!$res) {
  239. \Log::info('本地图片删除失败', $core['attachment']);
  240. }
  241. }
  242. if ($core->delete()) {
  243. return $this->successJson('删除成功');
  244. }
  245. return $this->errorJson('删除失败');
  246. }
  247. //上传记录表
  248. public function getData($originalName, $file_type, $newOriginalName, $save_type)
  249. {
  250. //存储至数据表中
  251. $core = new CoreAttach;
  252. $d = [
  253. 'uniacid' => \YunShop::app()->uniacid ? : 0,
  254. 'uid' => \Auth::guard('admin')->user()->uid,
  255. 'filename' => $originalName,
  256. 'type' => $file_type == 'syst' ? 1 : 2, //类型1.图片; 2.音乐
  257. 'attachment' => $newOriginalName,
  258. 'upload_type' => $save_type
  259. ];
  260. $core->fill($d);
  261. $validate = $core->validator();
  262. if (!$validate->fails()) {
  263. if ($core->save()) {
  264. return 1;
  265. }
  266. }
  267. return $validate->messages();
  268. }
  269. //腾讯云上传
  270. public function CosUpload($file, $setting, $file_type)
  271. {
  272. if (!$setting) {
  273. return '请配置参数';
  274. }
  275. $config['app_id'] = $setting['appid'];
  276. $config['secret_id'] = $setting['secretid'];
  277. $config['secret_key'] = $setting['secretkey'];
  278. $config['region'] = $setting['url'];
  279. $cos = new Api($config);
  280. \Log::info('cos_config', [$config, $cos]);
  281. $originalName = $file->getClientOriginalName(); // 文件原名
  282. $ext = $file->getClientOriginalExtension(); //文件扩展名
  283. $realPath = $file->getRealPath(); //临时文件的绝对路径
  284. \Log::info('cos_upload_file_content:', ['name'=> $originalName, 'ext'=>$ext, 'path'=>$realPath]);
  285. $truePath = substr($this->getOsPath($file_type), 1); // COS服务器 bucket 路径
  286. $newFileName = $this->getNewFileName($originalName, $ext); //新文件名
  287. \Log::info('cos_upload_path: origin_path', [$truePath.$newFileName, file_get_contents($realPath)]);
  288. $res = file_remote_upload($newFileName, false, $remote);
  289. if (!$res) {
  290. return yz_tomedia($newFileName);
  291. }
  292. return false;
  293. /////////////以下请忽略/////////////////////////////////////////////////////////////////////
  294. // $res = $cos->upload($setting['bucket'], $truePath.$newFileName, file_get_contents($realPath)); //执行上传
  295. $res = $cos->upload($setting['bucket'], file_get_contents($realPath), $truePath.$newFileName); //执行上传
  296. \Log::info('cos_upload_res:', $res);
  297. //检查 object 及服务器路径 权限
  298. if ($res['code'] == 0 && $res['message'] == 'SUCCESS') {
  299. $log = $this->getData($originalName, $file_type, $truePath.$newFileName, 4); //添加上传记录
  300. if ($log != 1) {
  301. \Log::info('新框架腾讯云上传存储失败', [$originalName, $truePath.$newFileName]);
  302. }
  303. // 自定义域名拼接文件名
  304. // \Log::info('return_url and cos_upload_url', [$res['Location'], $setting['url'].$truePath.$newFileName]);
  305. // return $setting['url'].$truePath.$newFileName;
  306. return $res['data']['access_url'];
  307. }
  308. return '上传失败,请重试';
  309. }
  310. //阿里云OSS 文件上传 (支持内网上传, 加强参数权限检验, 暂不支持CDN中转自定义域名)
  311. public function OssUpload($file, $setting, $file_type)
  312. {
  313. if (!$setting['key'] || !$setting['secret'] || !$setting['bucket']) {
  314. return '请配置参数';
  315. }
  316. \Log::info('oss_upload_config:', [$setting['key'], $setting['secret'], $setting['bucket'], $setting['ossurl']]);
  317. try{
  318. $oss = new OssClient($setting['key'], $setting['secret'], $setting['ossurl']);
  319. } catch(OssException $e) {
  320. return $e->getMessage();
  321. }
  322. $lists = $oss->listBuckets();
  323. \Log::info('oss_upload_bucket_lists', $lists);
  324. foreach ($lists as $k => $v) {
  325. if ($v['name'] != $setting['bucket'] || $v['location'] != $setting['ossurl']) {
  326. return '数据错误,请检查参数';
  327. }
  328. }
  329. //检查bucket中的域名
  330. $bucketInfo = $oss->getBucketMeta($setting['bucket']);
  331. $bu = explode('.', $bucketInfo['info']['url']);
  332. \Log::info('oss_upload_bucketInfo_and check_ossurl:', [$bucketInfo, $bu]);
  333. if ($setting['ossurl'].'/' !== $bu[1].'.'.$bu[2].'.'.$bu[3]) {
  334. \Log::info('oss_check_bucket_is_error_return');
  335. return 'ossurl 数据错误';
  336. }
  337. unset($bucketInfo, $bu);
  338. if ($setting['internal'] == 1) {
  339. //使用内网上传时
  340. $data = explode('.', $setting['ossurl']); //获取ossurl_internal
  341. $one = $data[0].'-internal';
  342. $domain = $one.'.'.$data[1].'.'.$data[2]; //拼接内网地址
  343. $oss = new OssClient($setting['key'], $setting['secret'], $domain, $setting['ossurl']);
  344. \Log::info('oss_domain_public_or_private', [$domain, $oss_internal]);
  345. }
  346. $originalName = $file->getClientOriginalName(); // 文件原名
  347. $ext = $file->getClientOriginalExtension(); //文件扩展名
  348. $realPath = $file->getRealPath(); //临时文件的绝对路径
  349. \Log::info('oss_upload_file_content:', ['name'=> $originalName, 'ext'=>$ext, 'path'=>$realPath]);
  350. $truePath = substr($this->getOsPath($file_type), 1); // OSS服务器 bucket 路径
  351. $newFileName = $this->getNewFileName($originalName, $ext);
  352. $object = $truePath.$newFileName;
  353. \Log::info('oss_upload_path:', $object);
  354. //如果使用内网上传需要加强校验, 保证 ecs 和 oss 统一取域 暂无API
  355. if ($setting['internal'] != 1) {
  356. //使用外网上传 域名为
  357. $domain = $setting['url'] ? : $setting['ossurl'];
  358. }
  359. $res = $oss->putObject($setting['bucket'], $object, file_get_contents($realPath));
  360. \Log::info('AliOss_res, and Content', [$res, file_get_contents($realPath)]);
  361. if ($res['info']['http_code'] == 200) {
  362. $log = $this->getData($originalName, $file_type, $object, 2);
  363. if ($log != 1) {
  364. \Log::info('新框架阿里云上传存储失败', [$originalName, $truePath]);
  365. }
  366. //检查 object bucket 权限
  367. if ($oss->getBucketAcl($setting['bucket']) == 'private' || $oss->getObjectAcl($setting['bucket'], $object) == 'private') {
  368. //私有时访问加签
  369. $url = $oss->signUrl($setting['bucket'], $object, 3600*24);
  370. \Log::info('getBucketOrObjAcl, true');
  371. } else {
  372. $url = $res['info']['url']; //公有权限时
  373. // $url = 'http://'.$setting['bucket'].'.'.$domain.'/'.$object;
  374. }
  375. return $url;
  376. }
  377. return '上传失败,请重试';
  378. }
  379. /**
  380. * 生成文件存放路径
  381. * @param string $file_type 文件类型:syst图片,audio音频,video视频
  382. * @return string 路径
  383. */
  384. public function getOsPath($file_type)
  385. {
  386. $file_type = $file_type === 'syst' ? 'image' : $file_type ;
  387. $uniacid = \YunShop::app()->uniacid ? : 0 ;
  388. return '/'.$file_type.'s/'.$uniacid.'/'.date('Y').'/'.date('m').'/';
  389. }
  390. /**
  391. * 获取新文件名
  392. * @param string $originalName 原文件名
  393. * @param string $ext 文件扩展名
  394. * @return string 新文件名
  395. */
  396. public function getNewFileName($originalName, $ext)
  397. {
  398. return date('Ymd').md5($originalName . str_random(6)) . '.' . $ext;
  399. }
  400. public function ossTest()
  401. {
  402. $res = \app\common\services\Storage::remove('D:\wamp\www\shop\storage\app\public\2019032013c77983d092519e0fe9cd79f6bec7b9.png'); dd($res);
  403. $oss = new OssClient(config('filesystems.disks.oss.access_id'), config('filesystems.disks.oss.access_key'), config('filesystems.disks.oss.endpoint'));
  404. // $setting = SystemSetting::settingLoad('remote', 'system_remote');
  405. // dd($setting);
  406. $o = explode('.', 'test-yunshop-com.oss-cn-hangzhou.aliyuncs.com');
  407. $setting['endpoint'] = $o[1].'.'.$o[2].'.'.$o[3];
  408. // $internal_oss = new OssClient(config('filesystems.disks.oss.access_id'), config('filesystems.disks.oss.access_key'), config('filesystems.disks.oss.endpoint_internal')); dd($internal_oss); //内网上传时使用
  409. // $authOss = new OssClient(config('filesystems.disks.oss.access_id'), config('filesystems.disks.oss.access_key'), config('filesystems.disks.oss.endpoint'), 'false', $token); //bucket或object权限私有时并设置STS作为验签方法时使用
  410. // $auth = $oss->getObjectAcl(config('filesystems.disks.oss.bucket'), '20190318347e0052aa60ce815f6f58bcd4b15a5e.png'); dd($auth); //获取对象权限
  411. // $acl = $oss->getBucketAcl(config('filesystems.disks.oss.bucket')); dd($acl); //获取bucket 权限 return string
  412. // $res = $oss->PutBucketACL('test-yunshop-com', 'public-read'); dd($res); //修改bucket 权限
  413. // $bucketInfo = $oss->getBucketMeta(config('filesystems.disks.oss.bucket'));
  414. // dd(config('filesystems.disks.oss.bucket'), $bucketInfo);
  415. // $newUrl = $oss->addBucketCname(config('filesystems.disks.oss.bucket'), config('filesystems.disks.oss.url'));
  416. // dd($newUrl);
  417. // $a = explode('.', $bucketInfo['info']['url']);
  418. // dd($a, $a[1].'.'.$a[2].'.'.$a[3]); //获取bucket信息
  419. // $b = $oss->getBucketCname(config('filesystems.disks.oss.bucket')); dd($b);
  420. // $lists = $oss->listBuckets(); dd($lists);
  421. // foreach ($lists as $k => $v) {
  422. // if ($v['name'] != config('filesystems.disks.oss.bucket') && $v['location'] != config('filesystems.disks.oss.endpoint')) {
  423. // dd('1');
  424. // }
  425. // }
  426. // dd('存在');
  427. // $checkObj = $oss->doesObjectExist(config('filesystems.disks.oss.bucket'), 'test/2a2eae2748b5788ea3a190dd8948e137.png'); dd($$checObj); //检查文件是否存在 true 时存在
  428. $image = 'images/0/2019/03/'.md5('2s1sf4s411').'.jpeg';
  429. $res = $oss->putObject(
  430. config('filesystems.disks.oss.bucket'),
  431. $image,
  432. // file_get_contents('E:\iqiyi\young\young-1.qsv'),
  433. file_get_contents('D:\wamp\www\shop\storage\app\public\201903145c8a3fdbda5b23006.jpeg'),
  434. ['content_type'=>'image/jpeg']
  435. );
  436. $res2 = $oss->deleteObject( config('filesystems.disks.oss.bucket'), $image);
  437. $log = $this->getData('201903145c8a3fdbda5b23006.jpeg', 'syst', $image, 2);
  438. dd($res, $res2, $log);
  439. $signUrl = $oss->signUrl(config('filesystems.disks.oss.bucket'), $image, 3600); //dd($signUrl); //加签名 return string
  440. }
  441. public function cosTest()
  442. {
  443. // $config['credentials']['appId'] = config('filesystems.disks.cos.app_id');
  444. // $config['credentials']['secretId'] = config('filesystems.disks.cos.secret_id');
  445. // $config['credentials']['secretKey'] = config('filesystems.disks.cos.secret_key');
  446. // $config['region'] = config('filesystems.disks.cos.region');
  447. // $config['endpoint'] = config('filesystems.disks.cos.endpoint');
  448. // unset($config['region']);
  449. $cos = new Api(config('filesystems.disks.cos'));
  450. $check = '/images/0/2019/03/';
  451. $ext='jpeg';
  452. // $newFileName = date('Ymd').md5('3dj43wd'. str_random(6)) . '.' . $ext;
  453. $newName = $this->getNewFileName('a2212dw3', 'jpeg');
  454. $path = substr($this->getOsPath('syst'), 1);
  455. // dd($newFileName, $path, $newName);
  456. $res = $cos->upload(
  457. config('filesystems.disks.cos.bucket'),
  458. 'D:\wamp\www\shop\storage\app\public\201903203974dc2b7ba9eefbe640b5395a8de517.jpeg',
  459. $path.$newName
  460. // $options = array('ContentType' => 'image/jpeg')
  461. // $path.$newName
  462. );
  463. // $log = $this->getData($originalName, 'image', 'test/'.date('Y').'/'.$newFileName, 4);
  464. $res2 = $cos->delFile(config('filesystems.disks.cos.bucket'),$path.$newName);
  465. dd($res, $log, $res2);
  466. header('Content-Type: image/jpeg');
  467. //初始化
  468. $curl = curl_init();
  469. //设置抓取的url
  470. curl_setopt($curl, CURLOPT_URL, $res['Location']);
  471. //设置头文件的信息作为数据流输出
  472. curl_setopt($curl, CURLOPT_HEADER, 1);
  473. //设置获取的信息以文件流的形式返回,而不是直接输出。
  474. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  475. //执行命令
  476. $data = curl_exec($curl);
  477. //关闭URL请求
  478. curl_close($curl);
  479. //显示获得的数据
  480. print_r($data);
  481. dd('a');
  482. // $cosApi->;
  483. $zip = new ImageZip();
  484. $res = $zip->makeThumb('D:\wamp\www\shop\storage\app\public\201903203974dc2b7ba9eefbe640b5395a8de517.jpeg', 'D:\wamp\www\shop\storage\app\\'.md5('2w43d3').'.jpeg', '36%');
  485. // dd($res);
  486. $im = $this->imageDeal('D:\wamp\www\shop\storage\app\public\201903203974dc2b7ba9eefbe640b5395a8de517.jpeg', '50%', $ext);
  487. dd($im);
  488. $res = $cos->upload(
  489. config('filesystems.disks.cos.bucket'),
  490. 'D:\wamp\www\shop\storage\app\public\201903203974dc2b7ba9eefbe640b5395a8de517.jpeg',
  491. 'test/'.date('Y').'/'.$newFileName
  492. );
  493. $url = config('filesystems.disks.cos.bucket').'-'.config('filesystems.disks.cos.app_id').'.cos'.config('filesystems.disks.cos.region').'.myqcloud.com/'.$check.'/iamges';
  494. dd($res, $url, $res2);
  495. $file_type = $file_type == 'syst' ? 'images' : $file_type.'s';
  496. $uniacid = \YunShop::app()->uniacid ? : 0;
  497. $Syspath = '/images';
  498. //查看该目录下有无此文件夹
  499. $first = $cos->listFolder(config('filesystems.disks.cos.bucket'), $Syspath); dd($first);
  500. if ($first['code'] == 0 && $first['message'] == 'SUCCESS' && $first['data']['infos']) {
  501. }
  502. //查询目录路径
  503. // $stat = $cos->stat(config('filesystems.disks.cos.bucket'), $Syspath);
  504. // dd($stat, $Syspath);
  505. $checkdir = $cos->statFolder(config('filesystems.disks.cos.bucket'), $Syspath); dd($checkdir);
  506. // $dirLists = $cos->listFolder(config('filesystems.disks.cos.bucket'), '/');
  507. // dd($dirLists);
  508. return '/images/'.$uniacid.'/'.date('Y').'/'.date('m');
  509. }
  510. }