FileUploadService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Storage;
  4. class FileUploadService extends Service
  5. {
  6. //文件类型
  7. const FILE_TYPE = [
  8. 'txt',
  9. 'jpg',
  10. 'png',
  11. 'gif',
  12. 'jpeg',
  13. 'zip',
  14. 'rar',
  15. 'xlsx',
  16. 'xls',
  17. ];
  18. //视频类型
  19. const VIDEO_FILE_TYPE = [
  20. 'mp4',
  21. ];
  22. const tmp_dir = 'upload_occ';
  23. const string = '/api/uploadFiles/';
  24. const string2 = 't9|';
  25. const string3 = 't9/';
  26. public function uploadFile($file){
  27. if(empty($file)) return [false, '请上传文件'];
  28. // 获取文件相关信息
  29. $ext = $file->getClientOriginalExtension(); // 扩展名
  30. $realPath = $file->getRealPath(); //临时文件的绝对路径
  31. $ext = strtolower($ext);
  32. $file_type = array_merge_recursive(self::FILE_TYPE, self::VIDEO_FILE_TYPE);
  33. if (! in_array($ext, $file_type)){
  34. $str = '文件格式支持类型:';
  35. foreach ($file_type as $value){
  36. $str.= $value . ' ' ;
  37. }
  38. return [false, $str];
  39. }
  40. if (in_array($ext, self::VIDEO_FILE_TYPE)) {
  41. $fileSize = $file->getSize(); // 获取文件大小(单位:字节)
  42. $maxVideoSize = 50 * 1024 * 1024; // 50 MB
  43. if ($fileSize > $maxVideoSize) {
  44. return [false, '视频文件大小不能超过 50MB'];
  45. }
  46. }
  47. $date = date("Y-m-d");
  48. //文件名
  49. $file_name = date("Ymd").time().rand(1000,9999);
  50. $filename = $file_name.'.' . $ext;
  51. $dir = self::tmp_dir . '/' . $date . '/' . $filename;
  52. Storage::disk('public')->put($dir, file_get_contents($realPath));
  53. return [true, self::string . self::string2 . $filename];
  54. }
  55. //获取文件的位置oss
  56. public function getFileShow($file_name,$expired = 3500){
  57. $path = "";
  58. if(empty($file_name)) return $path;
  59. $document = self::string3;
  60. if(strpos($file_name, FileUploadService::string . FileUploadService::string2) !== false){
  61. $file_name = str_replace(FileUploadService::string . FileUploadService::string2,'', $file_name);
  62. $timestamp = substr($file_name, 0, 8); // 截取前八位数字
  63. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  64. $date = $date->format('Y-m-d');
  65. $savePath = $document . $date . '/' . $file_name;
  66. list($status,$path) = (new OssService())->getTemporaryUrl($savePath,$expired);
  67. }else{
  68. if(strpos($file_name, FileUploadService::string) !== false){
  69. $file_name = str_replace(FileUploadService::string ,'', $file_name);
  70. $savePath = $document . 'old/upload_files/' . $file_name;
  71. list($status,$path) = (new OssService())->getTemporaryUrl($savePath,$expired);
  72. }
  73. }
  74. return $path;
  75. }
  76. public function uploadFileLocal($file){
  77. if(empty($file)) return [false, '请上传文件'];
  78. // 获取文件相关信息
  79. $ext = $file->getClientOriginalExtension(); // 扩展名
  80. $realPath = $file->getRealPath(); //临时文件的绝对路径
  81. $ext = strtolower($ext);
  82. if (! in_array($ext, self::FILE_TYPE)){
  83. $str = '文件格式为:';
  84. foreach (self::FILE_TYPE as $value){
  85. $str.= $value . ' ' ;
  86. }
  87. return [false,$str];
  88. }
  89. // 上传文件
  90. $file_name = date("Ymd").time().rand(1000,9999);
  91. $filename = $file_name.'.' . $ext;
  92. // 使用我们新建的uploads本地存储空间(目录)
  93. Storage::disk('public')->put('upload_files/'.$filename, file_get_contents($realPath));
  94. return [true, self::string . $filename];
  95. }
  96. public function createOssUpload($file){
  97. if(! is_array($file) || empty($file)) return;
  98. foreach ($file as $filename){
  99. $filename = str_replace(FileUploadService::string.FileUploadService::string2,'', $filename);
  100. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  101. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  102. $date = $date->format('Y-m-d');
  103. $dir = FileUploadService::tmp_dir . '/' . $date . '/' . $filename;
  104. if(Storage::disk('public')->exists($dir)){
  105. $realPath = storage_path() . "/app/public/" . $dir;
  106. $savePath = self::string3 . $date . '/' . $filename;
  107. list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
  108. if($status) Storage::disk('public')->delete($dir);
  109. }
  110. }
  111. }
  112. public function createOssUploadOld($file){
  113. if(! is_array($file) || empty($file)) return;
  114. foreach ($file as $filename){
  115. if(strpos($filename, FileUploadService::string.FileUploadService::string2) !== false){
  116. $filename = str_replace(FileUploadService::string.FileUploadService::string2,'',$filename);
  117. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  118. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  119. $date = $date->format('Y-m-d');
  120. $delPath = self::string3 . $date . '/' . $filename;
  121. list($status,$msg) = (new OssService())->deleteFile($delPath);
  122. // if(! $status) return [false , $msg];
  123. }else{
  124. if(strpos($filename, FileUploadService::string) !== false){
  125. $filename = str_replace(FileUploadService::string,'',$filename);
  126. $delPath = self::string3 . 'old/upload_files/' . $filename;
  127. list($status,$msg) = (new OssService())->deleteFile($delPath);
  128. }
  129. }
  130. }
  131. }
  132. public function createOssUploadBatch($file){
  133. if(empty($file['origin']) || empty($file['img_list'])) return;
  134. $from = $file['origin'];
  135. $filename = str_replace(FileUploadService::string.FileUploadService::string2,'', $from);
  136. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  137. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  138. $date = $date->format('Y-m-d');
  139. $dir = FileUploadService::tmp_dir . '/' . $date . '/' . $filename;
  140. if(! Storage::disk('public')->exists($dir)) return;
  141. $realPath = storage_path() . "/app/public/" . $dir;
  142. foreach ($file['img_list'] as $filename){
  143. $filename_tmp = str_replace(FileUploadService::string.FileUploadService::string2,'', $filename);
  144. $savePath = self::string3 . $date . '/' . $filename_tmp;
  145. list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
  146. }
  147. Storage::disk('public')->delete($dir);
  148. }
  149. }