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