/** * 获取系统已用空间 */ public function getUsedSpace() { $storeData = MiniUtil::getPluginMiniStoreData(); if (empty($storeData)) { //非迷你存储模式 $remain = $this->getDiskFreeSpace(); //空闲空间 字节 $total = $this->getDiskTotalSpace(); //总空间 字节 $usedSpace = $this->_byteFormat(MiniVersion::getInstance()->getTotalSize()); $totalSpace = $this->_byteFormat($total); $usedPercentage = $this->getUsedPercent(); //已用空间占的百分比 } else { //迷你存储模式下,获得迷你存储所有节点的空间使用情况 $usedSpace = 0; $totalSpace = 0; $usedPercentage = 100; $nodes = PluginMiniStoreNode::getInstance()->getNodeList(); foreach ($nodes as $node) { if ($node["status"] == 1) { $url = $node["host"] . "/api.php?route=store/info"; $content = file_get_contents($url); if ($content != "") { $disks = json_decode($content); foreach ($disks as $disk) { $usedSpace += $disk->{"used"}; $totalSpace += $disk->{"total"}; } } } } if ($totalSpace > 0) { $usedPercentage = round($usedSpace / $totalSpace, 3) * 100; } $usedSpace = round($usedSpace / 1024 / 1024, 2); $totalSpace = round($totalSpace / 1024 / 1024, 2); } //获得缓存空间大小 $tempDirectory = $this->getDirectorySize(BASE . 'temp'); $tempSize = $tempDirectory['size']; $cacheSize = $this->countCache(); $cacheSpace = MiniUtil::formatSize($tempSize + $cacheSize); $data = array(); $data['usedSpace'] = $usedSpace; $data['totalSpace'] = $totalSpace; $data['usedPercentage'] = $usedPercentage; $data['cacheSpace'] = $cacheSpace; return $data; }
/** * 设置站点信息 * @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); }
/** * * 检查文件data和 meta是否存在 * @param string $hash 文件sha1 * @param string $fileName 文件名 * @return bool|void */ protected function handleCheckFileVersion($hash, $fileName) { $version = MiniVersion::getInstance()->getBySignature($hash); if (!$this->isNewVersion) { //data源处理对象 if ($version == null) { return $this->handleAssign(); } // 检查文件是否存在 $dataObj = Yii::app()->data; $storePath = MiniUtil::getPathBySplitStr($hash); if ($dataObj->exists($storePath) == false) { return $this->handleAssign(); } $this->version_id = $version['id']; $this->size = $version['file_size']; return true; } else { //返回断点文件信息 $data = array(); if (empty($version)) { $miniStoreInfo = MiniUtil::getPluginMiniStoreData(); if (empty($miniStoreInfo)) { //普通文件上传 $data['success'] = false; $data['url'] = MiniHttp::getMiniHost() . "api.php"; $storePath = MiniUtil::getPathBySplitStr($hash); $filePath = BASE . "upload_block/cache/" . $storePath; if (file_exists($filePath)) { $data['offset'] = filesize($filePath); //如文件大小相同而且Hash值相同,说明流数据文件已经存在,直接生成元数据即可 $size = MiniHttp::getParam("size", ""); if ($data['offset'] == $size) { //生成version记录,为使用老逻辑代码,这里处理得很羞涩 //理想的逻辑是在这里直接返回相关结果 $mimeType = MiniUtil::getMimeType($fileName); $version = MiniVersion::getInstance()->create($hash, $size, $mimeType); $this->version_id = $version['id']; $this->size = $version['file_size']; return true; } } else { $data['offset'] = 0; } echo json_encode($data); exit; } else { //迷你存储与第3方存储秒传接口 apply_filters("file_sec", array("route" => "module/miniStore/report", "sign" => MiniHttp::getParam("sign", ""), "access_token" => MiniHttp::getParam("access_token", ""), "signature" => $hash, "size" => MiniHttp::getParam("size", ""), "path" => MiniHttp::getParam("path", ""))); } } else { //上传文件到其它目录下,支持秒传 $this->version_id = $version['id']; $this->size = $version['file_size']; return true; } } }