AttachmentHelper.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Helper;
  3. use App\Services\OSS;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\UploadedFile;
  6. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  7. use App\Services\Base\ErrorCode;
  8. use App\Models\BaseAttachmentModel;
  9. trait AttachmentHelper
  10. {
  11. /**
  12. * 上传附件
  13. *
  14. * @param Request $request laravel's http request
  15. * @param string|array $key 文件key
  16. * @param string $tag 文件tag
  17. * @param int $size 文件size限制,默认2M
  18. * @param array $mimeType 文件mime类型限制,默认不限
  19. * @return array|string|int 返回:md5字串|ErrorCode或[md5字串|ErrorCode]
  20. */
  21. public function uploadAttachment(Request $request, $key, $tag = 'files', $size = 10 * 1024 * 1024, array $mimeType = []) {
  22. if ($request->hasFile($key)) {
  23. $rel_path = '/upload/' . $tag . '/' . date('Ymd');
  24. $path = public_path() . $rel_path;
  25. if (!file_exists($path)) {
  26. if (!@mkdir($path, 0755, true)) {
  27. return ErrorCode::ATTACHMENT_MKDIR_FAILED;
  28. }
  29. }
  30. $files = $request->file($key);
  31. if ($files === null) {
  32. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  33. }
  34. if ($files instanceof UploadedFile) {
  35. $files = [$files];
  36. }
  37. $result = [];
  38. foreach ($files as $idx => $file) {
  39. if (!$file->isValid()) {
  40. $result[$idx] = ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  41. continue;
  42. }
  43. $fileSize = $file->getSize();
  44. if ($fileSize > $size) {
  45. $result[$idx] = ErrorCode::ATTACHMENT_SIZE_EXCEEDED;
  46. continue;
  47. }
  48. $fileMimeType = $file->getMimeType();
  49. \Log::info("fileMimeType:".$fileMimeType);
  50. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  51. $result[$idx] = ErrorCode::ATTACHMENT_MIME_NOT_ALLOWED;
  52. continue;
  53. }
  54. $clientName = $file->getClientOriginalName();
  55. $md5 = md5($clientName . time());
  56. $md5_filename = $md5 . '.' . $file->getClientOriginalExtension();
  57. try {
  58. $file->move($path, $md5_filename);
  59. $real_path = $path . '/' . $md5_filename;
  60. $url_path = $rel_path . '/' . $md5_filename;
  61. $attachment = new BaseAttachmentModel();
  62. $attachment->name = $clientName;
  63. $attachment->md5 = $md5;
  64. $attachment->path = $real_path;
  65. $attachment->url = $url_path;
  66. $attachment->size = $fileSize;
  67. $attachment->file_type = $fileMimeType;
  68. if ($attachment->save()) {
  69. $result['md5'] = $md5;
  70. $result['url'] = env('APP_URL').$url_path;
  71. } else {
  72. @unlink($real_path);
  73. $result[$idx] = ErrorCode::ATTACHMENT_SAVE_FAILED;
  74. }
  75. } catch (FileException $e) {
  76. $result[$idx] = ErrorCode::ATTACHMENT_MOVE_FAILED;
  77. }
  78. }
  79. if (count($result) == 1) {
  80. return array_shift($result);
  81. }
  82. return $result;
  83. } else {
  84. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  85. }
  86. }
  87. public function aliUpload(Request $request, $key, $tag = 'files', $size = 10 * 1024 * 1024, array $mimeType = []){
  88. if ($request->hasFile($key)) {
  89. $rel_path = '/upload/' . $tag . '/' . date('Ymd');
  90. $path = public_path() . $rel_path;
  91. if (!file_exists($path)) {
  92. if (!@mkdir($path, 0755, true)) {
  93. return ErrorCode::ATTACHMENT_MKDIR_FAILED;
  94. }
  95. }
  96. $files = $request->file($key);
  97. if ($files === null) {
  98. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  99. }
  100. if ($files instanceof UploadedFile) {
  101. $files = [$files];
  102. }
  103. $result = [];
  104. foreach ($files as $idx => $file) {
  105. if (!$file->isValid()) {
  106. $result[$idx] = ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  107. continue;
  108. }
  109. $fileSize = $file->getSize();
  110. if ($fileSize > $size) {
  111. $result[$idx] = ErrorCode::ATTACHMENT_SIZE_EXCEEDED;
  112. continue;
  113. }
  114. $fileMimeType = $file->getMimeType();
  115. \Log::info("fileMimeType:".$fileMimeType);
  116. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  117. $result[$idx] = ErrorCode::ATTACHMENT_MIME_NOT_ALLOWED;
  118. continue;
  119. }
  120. $clientName = $file->getClientOriginalName();
  121. $md5 = md5($clientName . time());
  122. $md5_filename = $md5 . '.' . $file->getClientOriginalExtension();
  123. try {
  124. $file_Path = $file->getRealPath();
  125. OSS::publicUpload(config('alioss.BucketName'),$md5_filename, $file_Path);
  126. $attachment = new BaseAttachmentModel();
  127. $attachment->name = $clientName;
  128. $attachment->md5 = $md5;
  129. $attachment->path = config('alioss.FileUrl').$md5_filename;
  130. $attachment->url = config('alioss.FileUrl').$md5_filename;
  131. $attachment->size = $fileSize;
  132. $attachment->file_type = $fileMimeType;
  133. if ($attachment->save()) {
  134. $result['md5'] = $md5;
  135. $result['url'] = config('alioss.FileUrl').$md5_filename;
  136. } else {
  137. OSS::publicDeleteObject(config('alioss.BucketName'),$md5_filename);
  138. $result[$idx] = ErrorCode::ATTACHMENT_SAVE_FAILED;
  139. }
  140. } catch (FileException $e) {
  141. $result[$idx] = ErrorCode::ATTACHMENT_MOVE_FAILED;
  142. }
  143. }
  144. if (count($result) == 1) {
  145. return array_shift($result);
  146. }
  147. return $result;
  148. } else {
  149. return ErrorCode::ATTACHMENT_UPLOAD_INVALID;
  150. }
  151. }
  152. /**
  153. * 删除附件
  154. *
  155. * @param $md5 string 删除文件的md5码
  156. * @return int 错误码or 0(成功)
  157. */
  158. public function deleteAttachment($md5) {
  159. $attachment = Attachment::where(['md5' => $md5])->first();
  160. if (!$attachment) {
  161. return ErrorCode::ATTACHMENT_NOT_EXIST;
  162. }
  163. if (file_exists($attachment->path)) {
  164. if($attachment->path == $attachment->url){
  165. $key = explode('/',$attachment->path);
  166. OSS::publicDeleteObject(config('alioss.BucketName'),end($key));
  167. return 0;
  168. }else{
  169. if (@unlink($attachment->path)) {
  170. if ($attachment->delete()) {
  171. return 0;
  172. } else {
  173. return ErrorCode::ATTACHMENT_RECORD_DELETE_FAILED;
  174. }
  175. } else {
  176. return ErrorCode::ATTACHMENT_DELETE_FAILED;
  177. }
  178. }
  179. } else {
  180. return ErrorCode::ATTACHMENT_NOT_EXIST;
  181. }
  182. }
  183. }