fdc51b2c25034e56464db70a9f97ccd616c44577.svn-base 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * 七牛上传方法
  4. *
  5. * @author widuu <admin@widuu.com>
  6. * @document https://github.com/widuu/qiniu_ueditor_1.4.3
  7. */
  8. class Qiniu extends Base{
  9. private $qiniu;
  10. /**
  11. * 继承Base并且实列化QiniuDirver
  12. *
  13. * @author widuu <admin@widuu.com>
  14. */
  15. public function __construct($config){
  16. parent::__construct($config);
  17. $this->qiniu = new QiniuDriver($this->config);
  18. }
  19. /**
  20. * 直接七牛URL直传的时候获取token的方法
  21. *
  22. * @return array
  23. * @author widuu <admin@widuu.com>
  24. */
  25. public function getToken(){
  26. return $this->qiniu->getToken();
  27. }
  28. /**
  29. * 上传文件的方法
  30. *
  31. * @param string $method 根据method来配置传输参数
  32. * @return array
  33. * @author widuu <admin@widuu.com>
  34. */
  35. public function upload($method){
  36. if($method == 'uploadimage'){
  37. //上传图片走本地保存再上传到七牛云方案
  38. $ue_config = $this->getUeConfig();
  39. $upload_config = $this->setUploadConfig($method);
  40. $fieldName = $upload_config['fieldName'];
  41. $base64 = $upload_config['base64'];
  42. unset($upload_config['fieldName']);
  43. unset($upload_config['base64']);
  44. $File = new LocalDriver($fieldName, $upload_config, $base64);
  45. $fileinfo = $File->getFileInfo();
  46. if ($fileinfo['url']) {
  47. $upload_info['name'] = 'file';
  48. $upload_info['file_name'] = substr($fileinfo['url'], 1);
  49. $upload_info['file_body'] = file_get_contents('../../../'.$fileinfo['url']);;
  50. $res = $this->qiniu->uploadFile($upload_info, $ue_config);
  51. $fileinfo = $res;
  52. }
  53. return $fileinfo;
  54. }else{
  55. $upload_config = $this->setUploadConfig($method);
  56. $ue_config = $this->getUeConfig();
  57. return $this->qiniu->getFileInfo( $upload_config ,$ue_config );
  58. }
  59. }
  60. /**
  61. * 删除文件的方法,七牛远程删除单个文件的方法
  62. *
  63. * @return array
  64. * @author widuu <admin@widuu.com>
  65. */
  66. public function remove(){
  67. $file_key = trim($_POST['key']);
  68. $result = $this->qiniu->removeFile($file_key);
  69. if( $result['code'] == 0 ){
  70. $return_info = array('state'=>'SUCCESS');
  71. }else{
  72. $return_info = array('state'=>$result['error']);
  73. }
  74. return $return_info;
  75. }
  76. /**
  77. * Ueditor的七牛在线列表
  78. *
  79. * @param string $method 列出文件的类型
  80. * @return array
  81. * @author widuu <admin@widuu.com>
  82. */
  83. public function listFile( $method ){
  84. $ue_config = $this->getUeConfig();
  85. if( $method == 'listimage'){
  86. $config_prefix = 'image';
  87. }else{
  88. $config_prefix = 'file';
  89. }
  90. $config = array(
  91. "allowFiles" => $ue_config[$config_prefix.'ManagerAllowFiles'],
  92. "listSize" => $ue_config[$config_prefix.'ManagerListSize'],
  93. 'prefix' => $ue_config['qiniuUploadPath']
  94. );
  95. $allow_files = substr(str_replace(".", "|", join("", $config['allowFiles'])), 1);
  96. $size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $config['listSize'];
  97. $start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
  98. $marker = isset($_GET['marker']) ? trim($_GET['marker']) : '';
  99. $end = $start + $size;
  100. $files = $this->qiniu->getList($config['prefix'] , $marker, $size );
  101. // 通过marker来判断文件是否还有文件
  102. if ( empty($_GET['marker']) && $start != 0 ) {
  103. return array(
  104. "state" => "no match file",
  105. "list" => array(),
  106. "start" => $start,
  107. "total" => $start
  108. );
  109. }
  110. /* 获取指定范围的列表 */
  111. $len = count($files['items']);
  112. for ($i = 0; $i<=$len; $i++ ){
  113. if ( preg_match( "/\.($allow_files)$/i" , $files['items'][$i]['key'] ) ) {
  114. $list[] = array(
  115. "url" => $this->qiniu->host."/".$files['items'][$i]['key'],
  116. "key" => $files['items'][$i]['key']
  117. );
  118. }
  119. }
  120. $marker = '';
  121. if( $files['marker'] != null ){
  122. $marker = $files['marker'];
  123. }
  124. /* 返回数据 */
  125. $result = array(
  126. "state" => "SUCCESS",
  127. "list" => $list,
  128. "start" => $start,
  129. "marker" => $marker,
  130. "total" => $start + $config['listSize']*2 // 保持最大数保证文件能够完全列出
  131. );
  132. return $result;
  133. }
  134. /**
  135. * 分片上传合成文件的方法
  136. *
  137. * @author widuu <admin@widuu.com>
  138. */
  139. public function makeFile(){
  140. $ue_config = $this->getUeConfig();
  141. return $this->qiniu->Synthesis($_POST,$ue_config);
  142. }
  143. /**
  144. * 远程图片抓取 [采用原有ueditor方法]
  145. *
  146. * @author widuu <admin@widuu.com>
  147. */
  148. public function catchimage(){
  149. // Ueditor 配置文件
  150. $ue_config = $this->getUeConfig();
  151. $field_name = $ue_config['catcherFieldName'];
  152. /* 抓取远程图片 */
  153. $list = array();
  154. if (isset($_POST[$field_name])) {
  155. $source = $_POST[$field_name];
  156. } else {
  157. $source = $_GET[$field_name];
  158. }
  159. foreach ( $source as $img_url ) {
  160. $info = $this->qiniu->fetchFile($img_url ,$ue_config );
  161. array_push($list, array(
  162. "state" => $info["state"],
  163. "url" => $info["url"],
  164. "size" => $info["size"],
  165. "title" => htmlspecialchars($info["title"]),
  166. "original" => htmlspecialchars($info["original"]),
  167. "source" => htmlspecialchars($img_url)
  168. ));
  169. }
  170. return array(
  171. 'state'=> count($list) ? 'SUCCESS':'ERROR',
  172. 'list'=> $list
  173. );
  174. }
  175. }