Attachment.php 10 KB

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