FileUploadService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 = 'work_hour|';
  25. const string3 = 'work_hour/';
  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. }
  68. return $path;
  69. }
  70. public function uploadFileLocal($file){
  71. if(empty($file)) return [false, '请上传文件'];
  72. // 获取文件相关信息
  73. $ext = $file->getClientOriginalExtension(); // 扩展名
  74. $realPath = $file->getRealPath(); //临时文件的绝对路径
  75. $ext = strtolower($ext);
  76. if (! in_array($ext, self::FILE_TYPE)){
  77. $str = '文件格式为:';
  78. foreach (self::FILE_TYPE as $value){
  79. $str.= $value . ' ' ;
  80. }
  81. return [false,$str];
  82. }
  83. // 上传文件
  84. $file_name = date("Ymd").time().rand(1000,9999);
  85. $filename = $file_name.'.' . $ext;
  86. // 使用我们新建的uploads本地存储空间(目录)
  87. Storage::disk('public')->put('upload_files/'.$filename, file_get_contents($realPath));
  88. return [true, self::string . $filename];
  89. }
  90. public function createOssUpload($file){
  91. if(! is_array($file) || empty($file)) return;
  92. foreach ($file as $filename){
  93. $filename = str_replace(FileUploadService::string.FileUploadService::string2,'', $filename);
  94. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  95. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  96. $date = $date->format('Y-m-d');
  97. $dir = FileUploadService::tmp_dir . '/' . $date . '/' . $filename;
  98. if(Storage::disk('public')->exists($dir)){
  99. $realPath = storage_path() . "/app/public/" . $dir;
  100. $savePath = self::string3 . $date . '/' . $filename;
  101. list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
  102. if($status) Storage::disk('public')->delete($dir);
  103. }
  104. }
  105. }
  106. public function createOssUploadOld($file){
  107. if(! is_array($file) || empty($file)) return;
  108. foreach ($file as $filename){
  109. if(strpos($filename, FileUploadService::string.FileUploadService::string2) !== false){
  110. $filename = str_replace(FileUploadService::string.FileUploadService::string2,'',$filename);
  111. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  112. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  113. $date = $date->format('Y-m-d');
  114. $delPath = self::string3 . $date . '/' . $filename;
  115. list($status,$msg) = (new OssService())->deleteFile($delPath);
  116. // if(! $status) return [false , $msg];
  117. }else{
  118. if(strpos($filename, FileUploadService::string) !== false){
  119. $filename = str_replace(FileUploadService::string,'',$filename);
  120. $delPath = self::string3 . 'old/upload_files/' . $filename;
  121. list($status,$msg) = (new OssService())->deleteFile($delPath);
  122. }
  123. }
  124. }
  125. }
  126. public function createOssUploadBatch($file){
  127. if(empty($file['origin']) || empty($file['img_list'])) return;
  128. $from = $file['origin'];
  129. $filename = str_replace(FileUploadService::string.FileUploadService::string2,'', $from);
  130. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  131. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  132. $date = $date->format('Y-m-d');
  133. $dir = FileUploadService::tmp_dir . '/' . $date . '/' . $filename;
  134. if(! Storage::disk('public')->exists($dir)) return;
  135. $realPath = storage_path() . "/app/public/" . $dir;
  136. foreach ($file['img_list'] as $filename){
  137. $filename_tmp = str_replace(FileUploadService::string.FileUploadService::string2,'', $filename);
  138. $savePath = self::string3 . $date . '/' . $filename_tmp;
  139. list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
  140. }
  141. Storage::disk('public')->delete($dir);
  142. }
  143. public function createOssUploadSingle($file){
  144. $filename = str_replace(FileUploadService::string.FileUploadService::string2,'', $file);
  145. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  146. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  147. $date = $date->format('Y-m-d');
  148. $dir = FileUploadService::tmp_dir . '/' . $date . '/' . $filename;
  149. if(Storage::disk('public')->exists($dir)){
  150. $realPath = storage_path() . "/app/public/" . $dir;
  151. $savePath = self::string3 . $date . '/' . $filename;
  152. list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
  153. if($status) Storage::disk('public')->delete($dir);
  154. return [$status, $msg];
  155. }else{
  156. return [false, '本地文件不存在'];
  157. }
  158. }
  159. public function createOssUploadOldSingle($file){
  160. $filename = str_replace(FileUploadService::string.FileUploadService::string2,'',$file);
  161. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  162. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  163. $date = $date->format('Y-m-d');
  164. $delPath = self::string3 . $date . '/' . $filename;
  165. list($status,$msg) = (new OssService())->deleteFile($delPath);
  166. return [$status, $msg];
  167. }
  168. }