/** * 当文件全部上传完成后执行 */ private function handleCheck($cache, $hash, $store_path, $des_path) { $signature = MiniUtil::getFileHash($cache); if ($hash != $signature) { unlink($des_path); throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR), MConst::HTTP_CODE_400); } // 存储目录 if (!MUtils::MkDirs(dirname($store_path))) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } //data源处理对象 $dataObj = Yii::app()->data; // 移动文件到存储路径 if ($dataObj->put($cache, $store_path) == FALSE) { throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR), MConst::HTTP_CODE_400); } unlink($cache); unlink($des_path); return true; }
/** * 上传保存文件操作 * @param $root * @param $post * @param $file * @param bool $isNeedCheckSignature * @return bool */ public static function create($root, $post, $file, $isNeedCheckSignature = false) { Yii::trace(Yii::t('api', 'Begin to process {class}::{function}', array('{class}' => "MUtils", '{function}' => __FUNCTION__)), "miniyun.api"); ob_start(); ob_end_clean(); $key = $post["key"]; $fileName = $post["Filename"]; // 检查错误,在此之前,已经通过post请求将参数传入到此 if ($file["file"]["error"] > 0) { Yii::log(Yii::t("api", "Request is Error, file:'{$file["file"]["error"]}'"), CLogger::LEVEL_ERROR, "miniyun.api"); return false; } // 参数检查 if (strlen(trim($key)) <= 0 || strlen(trim($fileName)) <= 0) { Yii::log(Yii::t("api", "Request is Error, file_name:'{$fileName}'"), CLogger::LEVEL_ERROR, "miniyun.api"); return false; } // $key 必须包含 / , ${filename} if (is_bool(strpos(trim($key), "/")) || is_bool(strpos(trim($key), "\${filename}"))) { Yii::log(Yii::t("api", "Request is Error, file_name:'{$fileName}'"), CLogger::LEVEL_ERROR, "miniyun.api"); return false; } // 全路径 $path = $root . str_replace("\${filename}", $fileName, $key); // 确保需要合并的文件不存在 if (file_exists($path)) { return true; } // 目录不存在,就创建 $dir = dirname($path); if (file_exists($dir) == false) { $dirname = dirname($path); MUtils::MkDirs($dirname); } // 移动临时文件 move_uploaded_file($file["file"]["tmp_name"], $path); // // 检查文件signature是否与传入的一致 // if ($isNeedCheckSignature) { if (MUtils::checkSignature($path, PYTHON_PATH, $fileName) == false) { MUtils::RemoveFile($path); Yii::log(Yii::t("api", "signature is not same , file_name:'{$fileName}'"), CLogger::LEVEL_ERROR, "miniyun.api"); return false; } } Yii::trace(Yii::t('api', 'end to process {class}::{function}', array('{class}' => "MUtils", '{function}' => __FUNCTION__)), "miniyun.api"); return true; }
/** * 保存文件版本 * @param $tmpName * @param $signature * @param $size * @param bool $move * @throws MFilesException */ public function saveFile($tmpName, $signature, $size, $move = true) { //data源处理对象 $dataObj = Yii::app()->data; // // 文件内容保存路径 // $storePath = MiniUtil::getPathBySplitStr($signature); if ($dataObj->exists(dirname($storePath)) === false) { MUtils::MkDirs(dirname($storePath)); } $version = MiniVersion::getInstance()->getBySignature($signature); if ($version != null) { // // 文件版本id // $this->version_id = $version["id"]; $this->file_hash = $version["file_signature"]; if ($dataObj->exists($storePath) == false) { if ($dataObj->put($tmpName, $storePath, true) == false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } } if ($move === true) { unlink($tmpName); } return; } // 移动临时文件到保存路径中 if ($move === true) { if ($dataObj->put($tmpName, $storePath, true) == false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } } // // 创建version // $version = MiniVersion::getInstance()->create($signature, $size, $this->type); if ($version == null) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } // // 文件版本id // $this->version_id = $version["id"]; $this->file_hash = $version["file_signature"]; }