1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace App\Handlers;
- use Qiniu\Auth as Auths;
- use Qiniu\Storage\UploadManager;
- use Qiniu\Storage\BucketManager;
- use Illuminate\Support\Facades\Log;
- class UploadFilesHandler
- {
- protected $allowed_ext = array(
- 'video'=>["mp4", "avi", "wmv",'mpeg'],
- 'audio'=>["mp3", "wma", "wav",'flac'],
- 'image'=>["png", "jpg", "gif", 'jpeg']
- );
-
- public function save($file, $folder, $file_prefix, $type, $max_width = false)
- {
-
- $config = config('filesystems.disks.qiniu');
- Log::info($config);
-
- $auth = new Auths($config['access_key'], $config['secret_key']);
-
- $token = $auth->uploadToken($config['bucket']);
-
- $filePath = $file->getRealPath();
-
- $extension = $file->getClientOriginalExtension();
-
- Log::info($this->allowed_ext[$type]);
-
- if (empty($extension) || !in_array(strtolower($extension), $this->allowed_ext[$type])) {
- return false;
- }
-
-
- if($file_prefix){
- $filename = $file_prefix . '_' . time() . '_' . str_random(10) . '.' . $extension;
- }else{
- $filename = time() . '_' . str_random(10) . '.' . $extension;
- }
-
- $uploadMgr = new UploadManager();
-
- $pathname= $config['dirname'].'/'.$folder.'/'.$filename;
- Log::info([$token, $pathname, $filePath]);
- try{
- $uploadMgr->putFile($token, $pathname, $filePath);
- }catch(\Exception $e){
- Log::info($e->getMessage());
- return false;
- }
-
- Log::info($config['domain'].'/'.$pathname);
- return [
- 'path' => $config['domain'].'/'.$pathname
- ];
- }
- }
|