Attachment.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. *------------------------------------------------------
  4. * BaseProcess.php
  5. *------------------------------------------------------
  6. *
  7. * @author Mike
  8. * @date 2016/5/26 11:17
  9. * @version V1.0
  10. *
  11. */
  12. namespace App\Services\Base;
  13. use App\Models\BaseAttachmentModel;
  14. class Attachment
  15. {
  16. private $_model;
  17. public function __construct() {
  18. if( !$this->_model ) $this->_model = new BaseAttachmentModel();
  19. }
  20. public function aliUpload(){
  21. }
  22. public function specialUpload(){
  23. }
  24. public function _uploadfiles(){
  25. }
  26. public function _jsonMessage($status,$ret){
  27. $ret['code'] = $status;
  28. return $ret;
  29. }
  30. // /**
  31. // * 上传到本地服务器
  32. // * @param $field
  33. // * @param $request
  34. // */
  35. // public function localUpload($field = '', $request = array())
  36. // {
  37. // $uploadfiles = $this->_uploadfiles($field);
  38. // if(!$uploadfiles){
  39. // return $this->_jsonMessage(FAILURE_CODE, $request['elementid'], ['message' => '没有文件被上传']);
  40. // }
  41. //
  42. // //文件夹路径
  43. // $folder = isset($request['folder']) ? $request['folder'] : 'common';
  44. //
  45. // //上传的文件名是否MD5
  46. // $is_md5 = isset($request['is_md5']) ? $request['is_md5'] : 1;
  47. // if($is_md5){
  48. // $fileurl = $folder . '/' . md5_file($uploadfiles[0]['tmp_name']);
  49. // }else{
  50. // $fileName = basename($uploadfiles[0]['name'], "." . substr(strrchr($uploadfiles[0]['name'], '.'), 1));
  51. // $fileurl = $folder . '/' . $fileName;
  52. // }
  53. //
  54. // $filePath = public_path() . DIRECTORY_SEPARATOR . $fileurl;
  55. // $dirPath = public_path() . DIRECTORY_SEPARATOR .$folder;
  56. //
  57. // //创建目录
  58. // if(!is_dir($dirPath)){
  59. // @mkdir($dirPath, 0755, true)
  60. //// $this->_dirUtil->dirCreate($dirPath);
  61. // }
  62. // /*if(!file_exists($filePath)){
  63. // $this->_dirUtil->dirCreate($filePath);
  64. // }*/
  65. //
  66. // //获取文件后缀名
  67. // $fileext = strtolower(trim(substr(strrchr($uploadfiles[0]['name'], '.'), 1, 10)));
  68. //
  69. // //判断是否需要解包
  70. // $is_extract = isset($request['is_extract']) ? $request['is_extract'] : 0;
  71. // if($is_extract && $fileext == 'zip'){ //ZIP解压
  72. //
  73. //// $message = $this->_zipTool($uploadfiles[0]['tmp_name'], $filePath);
  74. //
  75. // }elseif($is_extract && $fileext == 'rar'){ //RAR解压
  76. //
  77. // if(!get_extension_funcs('rar')){
  78. // return $this->_jsonMessage(500, $request['elementid'], ['message' => '没有发现RAR扩展库']);
  79. // }else{
  80. //// $message = $this->_rarTool($uploadfiles[0]['tmp_name'], $filePath);
  81. // }
  82. //
  83. // }else{
  84. //
  85. // $fileurl = $filePath . '.' . $fileext;
  86. // if(move_uploaded_file($uploadfiles[0]['tmp_name'], $fileurl)){
  87. // $fileurl = trim(str_replace(public_path(), '', $fileurl), '\\');
  88. // return $this->_jsonMessage(SUCESS_CODE, $request['elementid'], ['message' => '文件上传成功', 'fileurl' => $fileurl]);
  89. // }else{
  90. // return $this->_jsonMessage(FAILURE_CODE, $request['elementid'], ['message' => '文件上传成功', 'fileurl' => '']);
  91. // }
  92. //
  93. // }
  94. // $attachment = new BaseAttachmentModel();
  95. // $attachment->name = $clientName;
  96. // $attachment->md5 = $md5;
  97. // $attachment->path = $real_path;
  98. // $attachment->url = $url_path;
  99. // $attachment->size = $fileSize;
  100. // $attachment->file_type = $fileMimeType;
  101. // if ($attachment->save()) {
  102. // $result = 'Foundation/Attachment/download/?md5='.$md5;
  103. // } else {
  104. // @unlink($real_path);
  105. // $result= ErrorCode::ATTACHMENT_SAVE_FAILED;
  106. // }
  107. // if($message['code'] == 200){
  108. // //修改文件夹为只读属性
  109. // //chmod($filePath, 0444);
  110. // }else{
  111. // if(file_exists($filePath)){
  112. // rmdir($filePath);
  113. // }
  114. // }
  115. // return $this->_jsonMessage($message['code'], $request['elementid'], ['message' => $message['message'], 'fileurl' => $fileurl,'name'=>basename($uploadfiles[0]['name'], '.' . $fileext)]);
  116. // }
  117. /**
  118. * 上传附件
  119. *
  120. * @param string|array $field 文件key
  121. * @param Request $request laravel's http request
  122. * @param string $tag 文件tag
  123. * @param int $size 文件size限制,默认2M
  124. * @param array $mimeType 文件mime类型限制,默认不限
  125. * @return array|string|int 返回:md5字串|ErrorCode或[md5字串|ErrorCode]
  126. */
  127. public function localUpload($field, $request, $tag = 'files', $size = 2 * 1024 * 1024, array $mimeType = ['image/jpeg', 'image/png', 'image/gif']) {
  128. $tag = $request['folder'];
  129. $rel_path = $tag . '/' . date('Ymd');
  130. $path = public_path() . $rel_path;
  131. // dd($request);
  132. if (!file_exists($path)) {
  133. if (!@mkdir($path, 0755, true)) {
  134. // return ErrorCode::ATTACHMENT_MKDIR_FAILED;
  135. return $this->_jsonMessage(500, ['message' => '目录创建失败']);
  136. }
  137. }
  138. $file = $request[$field];
  139. if ($file === null) {
  140. return $this->_jsonMessage(500, ['message' => '没有文件被上传']);
  141. }
  142. if (!$file->isValid()) {
  143. return $this->_jsonMessage(500, ['message' => '不允许上传']);
  144. }
  145. $fileSize = $file->getSize();
  146. if ($fileSize > $size) {
  147. // $result[$idx] = ErrorCode::ATTACHMENT_SIZE_EXCEEDED;
  148. return $this->_jsonMessage(500, ['message' => '文件大小超过限制']);
  149. }
  150. $fileMimeType = $file->getMimeType();
  151. if (!empty($mimeType) && !in_array($fileMimeType, $mimeType)) {
  152. return $this->_jsonMessage(500, ['message' => '文件格式不被允许']);
  153. }
  154. $clientName = $file->getClientOriginalName();
  155. $md5 = md5($clientName . time());
  156. $md5_filename = $md5 . '.' . $file->getClientOriginalExtension();
  157. try {
  158. if(!$file->move($path, $md5_filename)){
  159. return $this->_jsonMessage(500, ['message' => '上传失败']);
  160. }
  161. $real_path = $path . '/' . $md5_filename;
  162. $url_path = $rel_path . '/' . $md5_filename;
  163. $attachment = new BaseAttachmentModel();
  164. $attachment->name = $clientName;
  165. $attachment->md5 = $md5;
  166. $attachment->path = $real_path;
  167. $attachment->url = $url_path;
  168. $attachment->size = $fileSize;
  169. $attachment->file_type = $fileMimeType;
  170. if ($attachment->save()) {
  171. return $this->_jsonMessage(200, ['message' => "上传成功", 'fileurl' => $url_path,'name'=>$md5_filename]);
  172. } else {
  173. @unlink($real_path);
  174. return $this->_jsonMessage(500, ['message' => '数据库保存错误']);
  175. // $result= ErrorCode::ATTACHMENT_SAVE_FAILED;
  176. }
  177. } catch (FileException $e) {
  178. return $this->_jsonMessage(500, ['message' => '上传失败']);
  179. // $result = ErrorCode::ATTACHMENT_MOVE_FAILED;
  180. }
  181. }
  182. /**
  183. * 删除附件
  184. *
  185. * @param $md5 string 删除文件的md5码
  186. * @return int 错误码or 0(成功)
  187. */
  188. public function deleteAttachment($md5) {
  189. $attachment = $this->_model->where(['md5' => $md5])->first();
  190. if (!$attachment) {
  191. return ErrorCode::ATTACHMENT_NOT_EXIST;
  192. }
  193. if (file_exists($attachment->path)) {
  194. if (@unlink($attachment->path)) {
  195. if ($attachment->delete()) {
  196. return 0;
  197. } else {
  198. return ErrorCode::ATTACHMENT_RECORD_DELETE_FAILED;
  199. }
  200. } else {
  201. return ErrorCode::ATTACHMENT_DELETE_FAILED;
  202. }
  203. } else {
  204. return ErrorCode::ATTACHMENT_NOT_EXIST;
  205. }
  206. }
  207. }