/** * 控制器执行主逻辑函数 * */ public function invoke($uri = null) { // 调用父类初始化函数,注册自定义的异常和错误处理逻辑 parent::init(); // 解析文件路径,若返回false,则错误处理 $urlManager = new MUrlManager(); $path = $urlManager->parsePathFromUrl($uri); $root = $urlManager->parseRootFromUrl($uri); if ($path == false || $root == false) { throw new MFilesException(Yii::t('api', MConst::PATH_ERROR), MConst::HTTP_CODE_411); } // 解析路径 $path = MUtils::convertStandardPath($path); MiniFile::getInstance()->download($path); }
/** * 控制器执行主逻辑函数 * @param null $uri * @param null $absolutePath * @throws MAuthorizationException * @throws MFileopsException */ public function invoke($uri = null, $absolutePath = null) { // 调用父类初始化函数,注册自定义的异常和错误处理逻辑 parent::init(); $params = $_REQUEST; // 检查参数 if (isset($params) === false || $params == null) { throw new MAuthorizationException(Yii::t('api', 'Invalid parameters')); } // 获取用户数据,如user_id $user = MUserManager::getInstance()->getCurrentUser(); $this->userId = $user["user_id"]; $includeDeleted = false; // 处理删除的同样也需要返回 if (isset($params["include_deleted"])) { $includeDeleted = $params["include_deleted"]; } $this->locale = "bytes"; if (isset($params["locale"])) { $this->locale = $params["locale"]; } $includeDeleted = MUtils::convertToBool($includeDeleted); $urlManager = new MUrlManager(); $path = MUtils::convertStandardPath($urlManager->parsePathFromUrl($uri)); $this->root = $urlManager->parseRootFromUrl($path); if ($path === false) { $path = "/"; } $pathPart = explode('/', $path); // 根目录 if (count($pathPart) <= 2) { $response = $this->handleRootPath($includeDeleted); } else { $response = $this->handleNotRootPath($path, $includeDeleted); } echo json_encode($response); }
/** * 控制器执行主逻辑函数 */ public function invoke($uri = null) { $this->setAction(MConst::CREATE_FILE); // 调用父类初始化函数,注册自定义的异常和错误处理逻辑 parent::init(); $urlManager = new MUrlManager(); $root = $urlManager->parseRootFromUrl($uri); if ($root == false) { //支持参数模式传递上传路径 $path = MiniHttp::getParam("path", ""); if (empty($path)) { throw new MFilesException(Yii::t('api', MConst::PATH_ERROR), MConst::HTTP_CODE_411); } } // 初始化创建文件公共类句柄 $createFileHandler = MFilesCommon::initMFilesCommon(); if (count($_FILES) == 0) { throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "5"), MConst::HTTP_CODE_400); } $keys = array_keys($_FILES); if (count($keys) != 1) { throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "6"), MConst::HTTP_CODE_400); } $key = $keys[0]; // 检查请求参数$_FILES if (isset($_FILES[$key]) === false) { throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "7"), MConst::HTTP_CODE_400); } // 检查文件上传过程是否有错 if ($_FILES[$key]["error"] != 0) { throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "8"), MConst::HTTP_CODE_400); } $fileName = $_FILES[$key]["name"]; $type = MiniUtil::getMimeType($fileName); $size = $_FILES[$key]["size"]; $tmpName = $_FILES[$key]["tmp_name"]; // 验证文件是否已经上传成功 if (file_exists($tmpName) === false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } // 检查文件上传错误 if (filesize($tmpName) != $size) { throw new MFilesException(Yii::t('api', "The file upload error!"), MConst::HTTP_CODE_400); } //断点文件上传 if ($this->isBreakpointUpload()) { $filesController = new MFilePutController(); $filesController->invoke($uri); } else { //完整文件上传 $signature = MiniUtil::getFileHash($tmpName); // 解析路径 $path = MiniHttp::getParam("path", ""); $path = MiniUtil::specialWordReplace($path); $parentPath = dirname($path); $user = MUserManager::getInstance()->getCurrentUser(); $parentFile = MiniFile::getInstance()->getByPath($parentPath); //如果目录存在,且该目录is_delete=1,则把目录状态删除状态修改为0 if (!empty($parentFile) && $parentFile['is_deleted'] == 1) { $values = array(); $values['is_deleted'] = false; MiniFile::getInstance()->updateByPath($parentPath, $values); } else { //如果是根目录,则不用新建目录 //否则会创建文件名名称的文件夹出来,而且目标文件位于该文件夹的下面 if (!MiniUtil::isRootPath($parentPath, $user["id"])) { MiniFile::getInstance()->createFolder($parentPath, $user['id']); } } $createFileHandler->size = $size; $createFileHandler->parent_path = MUtils::convertStandardPath($parentPath); $createFileHandler->file_name = MiniUtil::specialWordReplace($fileName); $createFileHandler->root = $root; $createFileHandler->path = MUtils::convertStandardPath($path); $createFileHandler->type = $type; // 文件不存在,保存文件 $createFileHandler->saveFile($tmpName, $signature, $size); // 保存文件meta $createFileHandler->saveFileMeta(); // 处理不同端,不同返回值 if (MUserManager::getInstance()->isWeb() === true) { $createFileHandler->buildWebResponse(); return; } $createFileHandler->buildResult(); } }
/** * 控制器执行主逻辑函数 * * @return mixed $value 返回最终需要执行完的结果 */ public function invoke($uri = null) { // 调用父类初始化函数,注册自定义的异常和错误处理逻辑 parent::init(); $params = $_REQUEST; // 检查参数 if (isset($params) === false || $params == null) { throw new Exception(Yii::t('api', 'Invalid parameters')); } // 文件大小格式化参数 $locale = "bytes"; if (isset($params["locale"])) { $locale = $params["locale"]; } $url = $uri; $url_manager = new MUrlManager(); $this->_root = $url_manager->parseRootFromUrl($uri); $path = $url_manager->parsePathFromUrl($uri); $path = MUtils::convertStandardPath($path); $originalPath = $path; // 检查共享 $share_filter = MSharesFilter::init(); // $share_filter // // 获取用户数据,如user_id $user = MUserManager::getInstance()->getCurrentUser(); $device = MUserManager::getInstance()->getCurrentDevice(); $this->_user_id = $user["user_id"]; $user_nick = $user["user_name"]; $user_device_id = $device["device_id"]; $this->_user_device_name = $device["user_device_name"]; $rev = $params["rev"]; $rev = intval($rev); $path = "/{$this->_user_id}{$path}"; // // 该文件是否具有此版本 // $file_meta = MFileMetas::queryFileMeta($path, MConst::VERSION); if ($file_meta == false || empty($file_meta)) { throw new MFileopsException(Yii::t('api', ' Unable to find the revision at that path'), MConst::HTTP_CODE_404); } if (MUtils::isExistReversion($rev, $file_meta[0]["meta_value"]) == false) { throw new MFileopsException(Yii::t('api', ' Unable to find the revision at that path'), MConst::HTTP_CODE_404); } // // 查询版本信息 // $version = MiniVersion::getInstance()->getVersion($rev); if ($version == null) { throw new MFileopsException(Yii::t('api', ' Unable to find the revision at that path'), MConst::HTTP_CODE_404); } $size = $version["file_size"]; $file_hash = $version["file_signature"]; // // 查询文件信息 // $query_db_file = MFiles::queryFilesByPath($path); if ($query_db_file === false || empty($query_db_file)) { throw new MFileopsException(Yii::t('api', 'not existed'), MConst::HTTP_CODE_404); } if ($query_db_file[0]["file_type"] == MConst::OBJECT_TYPE_DIRECTORY) { // 文件夹不需要版本 throw new MFileopsException(Yii::t('api', 'folder not existed version'), MConst::HTTP_CODE_403); } if ($rev !== $query_db_file[0]["version_id"]) { // // 更新文件版本 // $updates = array(); $updates["version_id"] = $rev; $updates["file_update_time"] = time(); $updates["file_size"] = $size; $updates["event_uuid"] = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); $ret = MFiles::updateFileDetailById($query_db_file[0]["id"], $updates); if ($ret === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } $file_detail = new MFiles(); $file_detail->file_name = $query_db_file[0]["file_name"]; // // 保存事件 // $context = array("hash" => $file_hash, "rev" => (int) $rev, "bytes" => (int) $size); // // 增加修改事件 // $ret = MiniEvent::getInstance()->createEvent($this->_user_id, $user_device_id, MConst::MODIFY_FILE, $path, serialize($context), $updates["event_uuid"]); if ($ret === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } $this->handleFileMeta($path, $rev, $user_nick, $this->_user_device_name, $query_db_file[0]["file_size"]); } // TODO $mime_type = $version["mime_type"]; $response = array(); $response["size"] = MUtils::getSizeByLocale($locale, $size); $response["is_deleted"] = true; $response["bytes"] = intval($size); $response["thumb_exists"] = MUtils::isExistThumbnail($mime_type, $size); $response["path"] = $originalPath; $response["root"] = $this->_root; $response["is_dir"] = false; $response["mime_type"] = $mime_type; $response["modified"] = MUtils::formatIntTime(time()); $response["rev"] = strval($rev); $response["revision"] = $rev; echo json_encode($response); }
/** * 获取文件以前版本的mtadata * @param null $uri * @throws MException * @throws MFilesException */ public function invoke($uri = null) { parent::init(); // 解析url地址,获取root和path,path必须指向一个文件 $urlManager = new MUrlManager(); $path = $urlManager->parsePathFromUrl($uri); $root = $urlManager->parseRootFromUrl($uri); if ($path == false) { throw new MException(Yii::t('api', MConst::PATH_ERROR), MConst::HTTP_CODE_411); } $path = "/" . $path; // Default is 10. Max is 1,000. $revLimit = 10; if (isset($_REQUEST["rev_limit"]) != false) { $revLimit = $_REQUEST["rev_limit"]; } $revLimit = $revLimit <= 1000 ? $revLimit : 1000; // 文件大小格式化参数 $locale = "bytes"; if (isset($_REQUEST["locale"])) { $locale = $_REQUEST["locale"]; } // callback - // TODO 实现callback $callback = NULL; // 获取用户数据,如user_id $user = MUserManager::getInstance()->getCurrentUser(); $device = MUserManager::getInstance()->getCurrentDevice(); $userId = $user["user_id"]; $userNick = $user["user_name"]; $userDeviceId = $device["device_id"]; // // 查询文件 // $fileDetail = MFiles::queryAllFilesByPath("/" . $userId . $path); if ($fileDetail === false || count($fileDetail) == 0) { throw new MException(Yii::t('api', MConst::NOT_FOUND), MConst::HTTP_CODE_404); } // 判断文件类型,如不是文件则返回错误 if ($fileDetail[0]["file_type"] != 0) { throw new MException(Yii::t('api', "Not Acceptable"), MConst::HTTP_CODE_406); } $fileMeta = MFileMetas::queryFileMeta("/" . $userId . $path, MConst::VERSION); if ($fileMeta == false || empty($fileMeta)) { throw new MFilesException(Yii::t("api", MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } // // 文件版本历史 // $versions = unserialize($fileMeta[0]["meta_value"]); $count = 1; // 计数器 // // 轮询 // $response = array(); foreach ($versions as $k => $v) { $var = array(); $var["rev"] = strval($v["version_id"]); $var["revision"] = (int) $v["version_id"]; $var["bytes"] = 0; $var["size"] = "0 bytes"; $var["thumb_exists"] = false; $var["modified"] = MUtils::formatIntTime(microtime(true) * 10000); $var["mime_type"] = MConst::DEFAULT_FILE_MIME_TYPE; $var["path"] = $path; $var["is_dir"] = false; $var["root"] = $root; if ($v["type"] == MConst::DELETE) { $var["is_deleted"] = true; } // // 文件版本信息 // $fileVersion = MiniVersion::getInstance()->getVersion($v["version_id"]); if ($fileVersion == null) { $var["is_deleted"] = true; } else { $var['hash'] = $fileVersion["file_signature"]; $var["bytes"] = (int) $fileVersion["file_size"]; $var["size"] = MUtils::getSizeByLocale($locale, $fileVersion["file_size"]); $var["thumb_exists"] = $this->isExistThumbnail($fileVersion["file_size"], $fileVersion["mime_type"]); $var["modified"] = MUtils::formatIntTime($fileVersion["updated_at"]); $var["mime_type"] = is_null($fileVersion["mime_type"]) ? $var["mime_type"] : $fileVersion["mime_type"]; } array_push($response, $var); if ($count >= $revLimit) { break; } $count += 1; } echo json_encode($response); }
/** * 控制器执行主逻辑函数 */ public function invoke($uri = null) { // 调用父类初始化函数,注册自定义的异常和错误处理逻辑 parent::init(); $params = $_REQUEST; // 检查参数 if (isset($params) === false || $params == null) { throw new MAuthorizationException(Yii::t('api', 'Invalid parameters')); } $url_manager = new MUrlManager(); $path = MUtils::convertStandardPath($url_manager->parsePathFromUrl($uri)); $root = $url_manager->parseRootFromUrl($uri); // 去掉问号后面参数 if ($pos = strpos($this->_root, '?')) { $this->_root = substr($this->_root, 0, $pos); } if ($pos = strpos($path, '?')) { $path = substr($path, 0, $pos); } // 获取用户数据,如user_id $user = MUserManager::getInstance()->getCurrentUser(); $this->_user_id = $user["user_id"]; $query = ""; if (isset($params["query"])) { $query = $params["query"]; } if ($query === "") { throw new MFileopsException(Yii::t('api', 'bad request 15'), MConst::HTTP_CODE_400); } $file_limit = 10000; if (isset($params["file_limit"])) { $file_limit = $params["file_limit"]; } $include_deleted = false; // 处理删除的同样也需要返回 // if(isset($params["include_deleted"])) { // $include_deleted = $params["include_deleted"]; // if(is_string($include_deleted) === true) { // if(strtolower($include_deleted) === "false") { // $include_deleted = false; // } elseif(strtolower($include_deleted) === "true") { // $include_deleted = true; // } // } // } $this->_locale = "bytes"; if (isset($params["locale"])) { $this->_locale = $params["locale"]; } $callback = null; if (isset($params["callback"])) { $callback = $params["callback"]; } if (empty($path) || "/" . $this->_user_id == $path) { $path = '/' . $this->_user_id; $userFiles = array(); $query_db_file = MFiles::searchFilesByPath($path, $query, $this->_user_id, $include_deleted); foreach ($query_db_file as $db_file) { // if(($db_file['parent_file_id'] == 0) and ($db_file['file_type'] != 4) and ($db_file['file_type'] != 2)){ $userFiles[] = $db_file; // } } $retval = $this->handleSearchRoot($path, $query); $files = array_merge($retval, $userFiles); } else { // $files = array(); $includeDeleted = false; $currentFile = MiniFile::getInstance()->getByPath($path); // if (empty($currentFile)){ // throw new MFileopsException(Yii::t('api','not existed'),MConst::HTTP_CODE_404); // } // //查询文件类型 // $version = MiniVersion::getInstance()->getVersion($currentFile["version_id"]); // $mimeType = null; // if ($version != NULL) // { // $currentFile["signature"] = $version["file_signature"]; // $mimeType = $version["mime_type"]; // } // // 组装子文件数据 $childrenFiles = MiniFile::getInstance()->getChildrenByFileID($parentFileId = $currentFile['id'], $includeDeleted); $currentFileParts = explode('/', $path); $currentFileUserId = $currentFileParts[1]; $files = $childrenFiles; // $query = str_replace("%", "\\%", $query); // $sql = ' file_name like "%' . $query . '%"'; // $sql = ''; // $files = array(); // foreach($childrenFiles as $childrenFile){ // $condition = $sql . 'file_path="' . $childrenFile['file_path'] . '" '; // $file = MFiles::findAll($condition); // $files = array_merge($files,$file); // } // $contents = array(); // if(!empty($childrenFiles)){ // foreach($childrenFiles as $childrenFile){ // $content = array(); // $version = MiniVersion::getInstance()->getVersion($childrenFile["version_id"]); // $mimeType = null; // if ($version != NULL){ // $mimeType = $version["mime_type"]; // $childrenFile["signature"] = $version["file_signature"]; // } // $content = $this->assembleResponse($content, $childrenFile, $mimeType); // if(!empty($content) && $childrenFile['is_deleted'] == 0){ // array_push($contents, $content); // } // } // } // $response['contents'] = $contents; } $result = array(); $query = str_replace("%", "\\%", $query); $sql = ' file_name like "%' . $query . '%"'; foreach ($files as $file) { $condition = $sql . 'and file_path like"' . $file['file_path'] . '%" '; $file = MFiles::findAll($condition); if (empty($file)) { continue; } $result = array_merge($result, $file); } // $path = "/{$this->_user_id}{$path}"; // $path = MUtils::convertStandardPath($path) . "/"; // 查询其 信息 // $operator = $this->_user_id; // // $sharefilter = MSharesFilter::init(); // $sharefilter->handlerCheck($this->_user_id, CUtils::removeUserFromPath($path)); // if($sharefilter->is_shared) { // $operator = $sharefilter->master; // $qpath = '/' . $sharefilter->master . $sharefilter->_path; // $query_db_file = MFiles::searchFilesByPath($qpath, $query, $sharefilter->master, $include_deleted); // // // 判断搜索出来的文件是否有权限访问 // foreach($query_db_file as $index => $file) { // // 列表权限,如果没有列表权限,则不进行显示 // try { // $sharefilter->hasPermissionExecute($file['file_path'], MPrivilege::RESOURCE_READ); // }catch(Exception $e) { // unset($query_db_file[$index]); // continue; // } // } // } else { // $query_db_file = MFiles::searchFilesByPath($path, $query, $this->_user_id, $include_deleted); // } // // 查询根目录 // // $retval = $this->handleSearchRoot($path, $query); // $query_db_file = array_merge($query_db_file, $retval); // if (count($query_db_file) > $file_limit) // { // throw new MFileopsException( // Yii::t('api','Too many file entries to return'), // MConst::HTTP_CODE_406); // } // $keys = array(); $response = array(); $filePaths = array(); if (!empty($result)) { foreach ($result as $file) { if (in_array($file['file_path'], $filePaths)) { continue; } array_push($filePaths, $file['file_path']); $item = array(); $version = MiniVersion::getInstance()->getVersion($file["version_id"]); $mimeType = null; $signature = null; if ($version != NULL) { $mimeType = $version["mime_type"]; $signature = $version["file_signature"]; $file["signature"] = $signature; } $item = $this->assembleResponse($item, $file, $mimeType); if (!empty($item)) { array_push($response, $item); } } } // foreach($query_db_file as $key => $db_file) { // if ($key >= $file_limit) // break; // $file_array = array(); // $mime_type = null; // if($db_file["file_type"] == MConst::OBJECT_TYPE_FILE) { // $version = MiniVersion::getInstance()->getVersion($db_file["version_id"]); // if($version) { // $mime_type = $version["mime_type"]; // } // } // $file_array = $this->assembleResponse($file_array, $db_file, $mime_type); // #这里对数据进行了二次过滤,如果路径是一致的,则过滤掉 // #TODO 这里的冗余数据初步分析是权限导致的,二次重构要去掉这个代码 // $path = $file_array["path"]; // if(!array_key_exists($path, $keys)){ // $keys[$path] = $file_array; // array_push($response, $file_array); // } // } echo json_encode($response); }
/** * (non-PHPdoc) * @see MIController::invoke() */ public function invoke($uri = NULL) { $this->setAction(MConst::CREATE_FILE); // 调用父类初始化函数,注册自定义的异常和错误处理逻辑 parent::init(); $hash = @$_REQUEST['hash']; if (empty($hash)) { $hash = MiniHttp::getParam("signature", ""); } // 接收参数 if (empty($hash)) { throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "Missing parameter 'hash'."), MConst::HTTP_CODE_400); } // 解析文件路径,若返回false,则错误处理 $urlManager = new MUrlManager(); $path = $urlManager->parsePathFromUrl($uri); $root = $urlManager->parseRootFromUrl($uri); if ($path == false || $root == false) { //支持参数模式传递上传路径 $path = MiniHttp::getParam("path", ""); $root = "miniyun"; $this->isNewVersion = true; if (empty($path)) { throw new MFilesException(Yii::t('api', MConst::PATH_ERROR), MConst::HTTP_CODE_411); } } $path = MiniUtil::specialWordReplace($path); $pathInfo = MUtils::pathinfo_utf($path); $fileName = $pathInfo["basename"]; $parentPath = $pathInfo["dirname"]; // 检查是否在共享文件夹内, 如果在共享文件夹内,则进行权限检查 $user = MUserManager::getInstance()->getCurrentUser(); $userId = $user["user_id"]; $shareFilter = MSharesFilter::init(); if ($shareFilter->handlerCheck($userId, $path)) { $userId = $shareFilter->master; $path = $shareFilter->_path; $filePath = "/" . $userId . $path; $shareFilter->hasPermissionExecute($filePath, MPrivilege::FILE_CREATE); } // 检查版本是否存在 if ($this->handleCheckFileVersion($hash, $fileName) == FALSE) { return; } $this->handler = MFilesCommon::initMFilesCommon(); $this->handler->parent_path = MUtils::convertStandardPath($parentPath); $this->handler->file_name = $fileName; $this->handler->root = $root; $this->handler->path = MUtils::convertStandardPath($path); $this->handler->type = MiniUtil::getMimeType($fileName); $this->handler->size = $this->size; $this->handler->file_hash = $hash; $this->handler->version_id = $this->version_id; // 保存文件meta $this->handler->saveFileMeta(); if (MUserManager::getInstance()->isWeb() === true) { $this->handler->buildWebResponse(); return; } $this->handler->buildResult(); }