| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 | 
							- <?php
 
- namespace App\Service;
 
- use Illuminate\Support\Facades\Storage;
 
- class FileUploadService extends Service
 
- {
 
-     //文件类型
 
-     const FILE_TYPE = [
 
-         'txt',
 
-         'jpg',
 
-         'png',
 
-         'gif',
 
-         'jpeg',
 
-         'zip',
 
-         'rar',
 
-         'xlsx',
 
-         'xls',
 
-     ];
 
-     //视频类型
 
-     const VIDEO_FILE_TYPE = [
 
-         'mp4',
 
-     ];
 
-     const tmp_dir = 'upload_occ';
 
-     const string = '/api/uploadFiles/';
 
-     const string2 = 't9|';
 
-     const string3 = 't9/';
 
-     public function uploadFile($file){
 
-         if(empty($file)) return [false, '请上传文件'];
 
-         // 获取文件相关信息
 
-         $ext = $file->getClientOriginalExtension();     // 扩展名
 
-         $realPath = $file->getRealPath();   //临时文件的绝对路径
 
-         $ext = strtolower($ext);
 
-         $file_type = array_merge_recursive(self::FILE_TYPE, self::VIDEO_FILE_TYPE);
 
-         if (! in_array($ext, $file_type)){
 
-             $str = '文件格式支持类型:';
 
-             foreach ($file_type as $value){
 
-                 $str.= $value . ' ' ;
 
-             }
 
-             return [false, $str];
 
-         }
 
-         if (in_array($ext, self::VIDEO_FILE_TYPE)) {
 
-             $fileSize = $file->getSize(); // 获取文件大小(单位:字节)
 
-             $maxVideoSize = 50 * 1024 * 1024; // 50 MB
 
-             if ($fileSize > $maxVideoSize) {
 
-                 return [false, '视频文件大小不能超过 50MB'];
 
-             }
 
-         }
 
-         $date = date("Y-m-d");
 
-         //文件名
 
-         $file_name = date("Ymd").time().rand(1000,9999);
 
-         $filename =  $file_name.'.' . $ext;
 
-         $dir = self::tmp_dir . '/' . $date . '/' . $filename;
 
-         Storage::disk('public')->put($dir, file_get_contents($realPath));
 
-         return [true, self::string . self::string2 . $filename];
 
-     }
 
-     //获取文件的位置oss
 
-     public function getFileShow($file_name,$expired = 3500){
 
-         $path = "";
 
-         if(empty($file_name)) return $path;
 
-         $document = self::string3;
 
-         if(strpos($file_name, FileUploadService::string . FileUploadService::string2) !== false){
 
-             $file_name = str_replace(FileUploadService::string . FileUploadService::string2,'', $file_name);
 
-             $timestamp = substr($file_name, 0, 8); // 截取前八位数字
 
-             $date = \DateTime::createFromFormat('Ymd', $timestamp);
 
-             $date = $date->format('Y-m-d');
 
-             $savePath = $document . $date . '/' . $file_name;
 
-             list($status,$path) = (new OssService())->getTemporaryUrl($savePath,$expired);
 
-         }else{
 
-             if(strpos($file_name, FileUploadService::string) !== false){
 
-                 $file_name = str_replace(FileUploadService::string ,'', $file_name);
 
-                 $savePath = $document . 'old/upload_files/' . $file_name;
 
-                 list($status,$path) = (new OssService())->getTemporaryUrl($savePath,$expired);
 
-             }
 
-         }
 
-         return $path;
 
-     }
 
-     public function uploadFileLocal($file){
 
-         if(empty($file)) return [false, '请上传文件'];
 
-         // 获取文件相关信息
 
-         $ext = $file->getClientOriginalExtension();     // 扩展名
 
-         $realPath = $file->getRealPath();   //临时文件的绝对路径
 
-         $ext = strtolower($ext);
 
-         if (! in_array($ext, self::FILE_TYPE)){
 
-             $str = '文件格式为:';
 
-             foreach (self::FILE_TYPE as $value){
 
-                 $str.= $value . ' ' ;
 
-             }
 
-             return [false,$str];
 
-         }
 
-         // 上传文件
 
-         $file_name = date("Ymd").time().rand(1000,9999);
 
-         $filename =  $file_name.'.' . $ext;
 
-         // 使用我们新建的uploads本地存储空间(目录)
 
-         Storage::disk('public')->put('upload_files/'.$filename, file_get_contents($realPath));
 
-         return [true, self::string . $filename];
 
-     }
 
-     public function createOssUpload($file){
 
-         if(! is_array($file) || empty($file)) return;
 
-         foreach ($file as $filename){
 
-             $filename = str_replace(FileUploadService::string.FileUploadService::string2,'', $filename);
 
-             $timestamp = substr($filename, 0, 8); // 截取前八位数字
 
-             $date = \DateTime::createFromFormat('Ymd', $timestamp);
 
-             $date = $date->format('Y-m-d');
 
-             $dir = FileUploadService::tmp_dir . '/' . $date . '/' . $filename;
 
-             if(Storage::disk('public')->exists($dir)){
 
-                 $realPath = storage_path() . "/app/public/" . $dir;
 
-                 $savePath = self::string3 . $date . '/' . $filename;
 
-                 list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
 
-                 if($status) Storage::disk('public')->delete($dir);
 
-             }
 
-         }
 
-     }
 
-     public function createOssUploadOld($file){
 
-         if(! is_array($file) || empty($file)) return;
 
-         foreach ($file as $filename){
 
-             if(strpos($filename, FileUploadService::string.FileUploadService::string2) !== false){
 
-                 $filename = str_replace(FileUploadService::string.FileUploadService::string2,'',$filename);
 
-                 $timestamp = substr($filename, 0, 8); // 截取前八位数字
 
-                 $date = \DateTime::createFromFormat('Ymd', $timestamp);
 
-                 $date = $date->format('Y-m-d');
 
-                 $delPath = self::string3 . $date . '/' . $filename;
 
-                 list($status,$msg) = (new OssService())->deleteFile($delPath);
 
- //                if(! $status) return [false , $msg];
 
-             }else{
 
-                 if(strpos($filename, FileUploadService::string) !== false){
 
-                     $filename = str_replace(FileUploadService::string,'',$filename);
 
-                     $delPath = self::string3 . 'old/upload_files/' . $filename;
 
-                     list($status,$msg) = (new OssService())->deleteFile($delPath);
 
-                 }
 
-             }
 
-         }
 
-     }
 
-     public function createOssUploadBatch($file){
 
-         if(empty($file['origin']) || empty($file['img_list'])) return;
 
-         $from = $file['origin'];
 
-         $filename = str_replace(FileUploadService::string.FileUploadService::string2,'', $from);
 
-         $timestamp = substr($filename, 0, 8); // 截取前八位数字
 
-         $date = \DateTime::createFromFormat('Ymd', $timestamp);
 
-         $date = $date->format('Y-m-d');
 
-         $dir = FileUploadService::tmp_dir . '/' . $date . '/' . $filename;
 
-         if(! Storage::disk('public')->exists($dir)) return;
 
-         $realPath = storage_path() . "/app/public/" . $dir;
 
-         foreach ($file['img_list'] as $filename){
 
-             $filename_tmp = str_replace(FileUploadService::string.FileUploadService::string2,'', $filename);
 
-             $savePath = self::string3 . $date . '/' . $filename_tmp;
 
-             list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
 
-         }
 
-         Storage::disk('public')->delete($dir);
 
-     }
 
- }
 
 
  |