Attachment.php 9.6 KB

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