Attachment.php 10 KB

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