/** * 检查文档节点状态 * @param string $host * @return int */ public function checkNodeStatus($host) { $url = $host . '/api.php'; $data = array('route' => "doc/status", 'callback_url' => PluginMiniDocOption::getInstance()->getMiniyunHost() . "info.htm"); $http = new HttpClient(); $http->post($url, $data); $result = $http->get_body(); $result = @json_decode($result, true); if ($result["status"] == "1") { return 1; } return -1; }
/** * 把文档进行转换 * @param array $version * @param string $mimeType 如mineType为空,则使用version.mime_type */ private function pushFileConvert($version, $mimeType) { if (empty($mimeType)) { $mimeType = $version["mime_type"]; } $miniHost = PluginMiniDocOption::getInstance()->getMiniyunHost(); $siteId = MiniSiteUtils::getSiteID(); $signature = $version["file_signature"]; $node = PluginMiniDocNode::getInstance()->getConvertNode($signature); if (!empty($node)) { $url = $node["host"] . '/api.php?route=file/convert'; $downloadUrl = $miniHost . "api.php?route=module/miniDoc/download&signature=" . $signature; $callbackUrl = $miniHost . "api.php?route=module/miniDoc/report&node_id=" . $node["id"] . "&signature=" . $signature; $data = array('signature' => $signature, 'site_id' => $siteId, 'mime_type' => $mimeType, 'download_url' => $downloadUrl, "callback_url" => $callbackUrl); $http = new HttpClient(); $http->post($url, $data); $result = $http->get_body(); $result = json_decode($result, true); if ($result['status'] == 1) { $this->updateDocConvertStatus($node["id"], $version["file_signature"], 1); } } }
/** * 在线浏览文件获得内容 * @param string $path 文件当前路径 * @param string $type 文件类型,可选择pdf/png * @throws * @return NULL */ public function previewContent($path, $type) { $file = MiniFile::getInstance()->getByPath($path); // 权限处理 if (empty($file)) { return array('success' => false, 'msg' => 'file not existed'); } $fileBiz = new FileBiz(); $canRead = $fileBiz->privilege($path); if (!$canRead) { throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409); } //获得文件当前版本对应的version $version = PluginMiniDocVersion::getInstance()->getVersion($file["version_id"]); $signature = $version["file_signature"]; $localPath = PluginMiniDocOption::getInstance()->getMiniDocCachePath() . $signature . "/" . $signature . "." . $type; if (!file_exists($localPath)) { //文档还在转换中 $node = PluginMiniDocNode::getInstance()->getConvertNode($signature); if (empty($node)) { throw new MFileopsException(Yii::t('api', 'convert error'), MConst::HTTP_CODE_412); } //根据情况判断是否需要向迷你文档拉取内容 $parentPath = dirname($localPath); //如果缓存目录不存在,则需要创建 if (!file_exists($parentPath)) { MUtils::MkDirsLocal($parentPath); } //文件不存在,则需要从迷你文档拉取文件内容 $url = PluginMiniDocNode::getInstance()->getDownloadUrl($node["id"], $version, $type); $http = new HttpClient(); $http->get($url); $status = $http->get_status(); if ($status == "200") { $content = $http->get_body(); //把文件内容存储到本地硬盘 file_put_contents($localPath, $content); Yii::log($signature . " get " . $type . " success", CLogger::LEVEL_INFO, "doc.convert"); } else { if (!($version["doc_convert_status"] == -1)) { //如迷你文档服务器不存在该文档,说明迷你文档服务器发生了变动 //这个时候自动启动负载均衡机制,把文档重新转换 PluginMiniDocVersion::getInstance()->pushConvertSignature($signature, ""); Yii::log($signature . " get " . $type . " error", CLogger::LEVEL_ERROR, "doc.convert"); } } } if (file_exists($localPath)) { if ($type === "png") { $contentType = "image/png"; } if ($type === "pdf") { $contentType = "Content-type: application/pdf"; } //Firefox+混合云模式下直接输出内容 //其它浏览器使用sendfile模式输出内容 $isSendFile = true; if (MiniUtil::isMixCloudVersion()) { $ua = isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : NULL; if (strpos($ua, "Firefox") > 0 || strpos($ua, "Safari") > 0) { $isSendFile = false; } } if ($isSendFile) { header('Location: ' . MiniHttp::getMiniHost() . "assets/minidoc/" . $signature . "/" . $signature . "." . $type); } else { Header("Content-type: " . $contentType); echo file_get_contents($localPath); exit; } } }