Example #1
0
 /**
  * 根據文件Id获取文件数据
  */
 public function getLink($fileId, $linkType, $shareKey)
 {
     $data = array();
     $file = MiniFile::getInstance()->getById($fileId);
     $data["name"] = $file["file_name"];
     $data["bytes"] = intval($file["file_size"] + "");
     $fileType = $file["file_type"];
     if ($fileType == 0) {
         $data["icon"] = MiniHttp::getIcon4File($file["file_name"]);
         if (!MiniUtil::isMixCloudVersion()) {
             $data["icon"] = MiniHttp::getMiniHost() . "statics/static/mini-box/images/link/" . $data["icon"];
         } else {
             $data["icon"] = "http://static.miniyun.cn/static/mini-box/images/link/" . $data["icon"];
         }
     }
     $ext = MiniUtil::getFileExtension($file["file_name"]);
     $path = MiniUtil::getRelativePath($file["file_path"]);
     if ($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "gif") {
         $data["thumbnail_link"] = MiniHttp::createAnonymousUrl("linkAccess/thumbnail?key=" . $shareKey . "&size=256x256&path=" . urlencode($path));
     } else {
         $data["thumbnail_link"] = "";
     }
     if ($linkType == MiniLink::$PREVIEW_LINK) {
         $data["link"] = MiniHttp::createUrl("link/access/key/" . $shareKey);
     } else {
         $data["link"] = MiniHttp::createAnonymousUrl("linkAccess/download?key=" . $shareKey . "&path=" . urlencode($path));
     }
     return $data;
 }
Example #2
0
 /**
  * 设置站点信息
  * @param $site
  * @return array
  */
 public function settingSiteInfo($site)
 {
     //迷你存储没有启用,将判断用户存储路径的完整性
     $storeData = MiniUtil::getPluginMiniStoreData();
     if (empty($storeData)) {
         $fileStorePath = $site['fileStorePath'];
         //文件存储路径的合法性检测
         if (is_dir($fileStorePath) == false) {
             return array('success' => false, 'msg' => 'dir_is_not_exist');
         }
         //
         // 判断父目录是否存在
         //
         if (file_exists(dirname($fileStorePath)) == false) {
             return array('success' => false, 'msg' => 'parent_dir_is_not_exist');
         }
         //
         // 文件不存在
         //
         if (file_exists($fileStorePath) == false) {
             mkdir($fileStorePath);
             chmod($fileStorePath, 0755);
         }
         //
         // 文件夹不可写
         //
         if (is_writable($fileStorePath) == false) {
             return array('success' => false, 'msg' => 'dir_is_not_writable');
         }
         //修改文件存储配置
         $this->setStorePath($site['fileStorePath']);
     }
     MiniOption::getInstance()->setOptionValue("miniyun_host", $site['miniyun_host']);
     MiniOption::getInstance()->setOptionValue("site_title", $site['siteTitle']);
     MiniOption::getInstance()->setOptionValue("site_name", $site['siteName']);
     MiniOption::getInstance()->setOptionValue("site_default_space", $site['siteDefaultSpace']);
     MiniOption::getInstance()->setOptionValue("site_company", $site['siteCompany']);
     MiniOption::getInstance()->setOptionValue("user_register_enabled", $site['userRegisterEnabled']);
     //如果是混合云版,则调整迷你存储的访问地址
     if (MiniUtil::isMixCloudVersion()) {
         $plugins = MiniUtil::getActivedPluginsInfo();
         if (!empty($plugins)) {
             foreach ($plugins as $plugin) {
                 if ($plugin["name"] === "miniStore") {
                     //创建默认迷你存储站点
                     PluginMiniStoreNode::getInstance()->createDefault();
                 }
             }
         }
     }
     return array('success' => true);
 }
Example #3
0
 /**
  * 在线浏览文件获得内容
  * @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;
         }
     }
 }