public function handlerChildrenFile($fileDetail) { $directories = array(); // 记录这层中文件夹的对象 $files = array(); // 记录需要处理的文件对象(包括文件夹) // // 文件夹,查询其子文件这一层数据 // $dbChildrenFiles = MFiles::queryChildrenFilesByParentFileID($fileDetail->from_id); if ($dbChildrenFiles === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } if (empty($dbChildrenFiles)) { $p = $fileDetail->file_path; return; // 没有子文件,返回 } // // 检查文件数量,复制数量限制在10000条内 // if (count($dbChildrenFiles) > MConst::MAX_FILES_COUNT) { throw new MFileopsException(Yii::t('api', 'Too many files or folders need to be copied'), MConst::HTTP_CODE_406); } // // 转换数据 // foreach ($dbChildrenFiles as $dbFile) { $newFileDetail = new MFiles(); // // 排除已被删除的对象 // if ($dbFile["is_deleted"] == true) { continue; } $this->assembleFileDetail($dbFile['file_name'], $fileDetail->id, $newFileDetail, $dbFile); array_push($files, $newFileDetail); if ($dbFile["file_type"] == MConst::OBJECT_TYPE_DIRECTORY) { array_push($directories, $newFileDetail); } } if (empty($files)) { return; } // // 批量处理这批数据 // $ret = MFiles::batchCreateFileDetails($this->_user_id, $files); if ($ret === false || empty($ret)) { throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404); } $this->updateVerRef($files); $ret = MiniEvent::getInstance()->createEvents($this->_user_id, $this->_user_device_id, $files, $this->to_share_filter->type); if ($ret === false || empty($ret)) { throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404); } // // 为共享创建事件 // if ($this->to_share_filter->is_shared) { foreach ($files as $v) { $context = $v->context; if ($v->event_action == MConst::CREATE_FILE || $v->event_action == MConst::MODIFY_FILE) { $context = unserialize($context); } $this->to_share_filter->handlerAction($v->event_action, $this->_user_device_id, $v->from_path, $context); } } // // 处理这层中的文件夹 // foreach ($directories as $file) { // // 查询其复制目录路径id // $queryDbDirectory = MFiles::queryFilesByPath($file->file_path); if ($queryDbDirectory === false || empty($queryDbDirectory)) { throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404); } $file->id = $queryDbDirectory[0]["id"]; $this->handlerChildrenFile($file); } }
/** * 查找该路径下所有子文件:文件存入数组 * @param string $parent_file_id 父目录id * @return mixed $value 返回最终需要执行完的结果 */ public function findAllChildrenFiles($parent_file_id, $user_nick, $from_path, $to_path, $file_array, $device_name, $file_size) { $query_db_files = MFiles::queryChildrenFilesByParentFileID($parent_file_id); if ($query_db_files === false) { return false; } foreach ($query_db_files as $key => $db_file) { if ($db_file["file_type"] != MConst::OBJECT_TYPE_FILE) { $file_array = $this->findAllChildrenFiles($db_file["id"], $user_nick, $from_path, $to_path, $file_array, $device_name, $file_size); continue; } // // 转换路径 // $file_path = $db_file["file_path"]; // // 文件存入数组 // $file = new MFiles(); $file->file_path = $file_path; $file->version_id = $db_file["version_id"]; $meta_value = MUtils::getFileVersions($device_name, $db_file['file_size'], $file->version_id, MConst::CREATE_FILE, $db_file["user_id"], $user_nick); $file->meta_value = $meta_value; $file->is_add = true; // 记录是否需要添加 $file_array[$file_path] = $file; } return $file_array; }
/** * 处理删除子文件 * @param int $user_id * @param string $parent_path 文件路径,传入路径中已经加入用户id:"/".$this->_user_id.$parent_path * @param array $files * @throws MFileopsException * @return mixed $value 返回最终需要执行完的结果 * * @since 1.0.7 */ private function handleChildrenFile($parent_path, $files) { // // 查询所有子文件, 不包含已删除的 // $db_children_files = MFiles::queryChildrenFilesByPath($parent_path, true, false); if ($db_children_files === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } // // 检查文件数量,复制数量限制在10000条内 // if (count($db_children_files) > MConst::MAX_FILES_COUNT) { throw new MFileopsException(Yii::t('api', 'Too many files or folders need to be deleted'), MConst::HTTP_CODE_406); } // // 转换数据 // foreach ($db_children_files as $db_file) { $file_detail = new MFiles(); // // 排除已被删除的对象 // if ($db_file["is_deleted"] == true) { continue; } $this->assembleFileDetail($file_detail, $db_file); array_push($files, $file_detail); // // 处理共享文件夹 // if ($db_file['file_type'] >= MConst::OBJECT_TYPE_SHARED) { $handler = new ShareManager(); $handler->_userId = $db_file['user_id']; $handler->_id = $db_file['id']; try { $handler->invoke(ShareManager::CANCEL_SHARED); } catch (Exception $e) { continue; } } } // // 更新其为删除状态 // $ret_value = MFiles::updateRemoveChildrenFile($parent_path); if ($ret_value === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } return $files; }
/** * 控制器执行主逻辑函数 * * @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 handleSearchRoot($path, $query) { $user = MUserManager::getInstance()->getCurrentUser(); $sharedpaths = array(); $publicFiles = MiniFile::getInstance()->getPublics(); $groupShareFiles = MiniGroupPrivilege::getInstance()->getAllGroups(); $userShareFiles = MiniUserPrivilege::getInstance()->getAllUserPrivilege($user["id"]); $shareFiles = array_merge($publicFiles, $groupShareFiles, $userShareFiles); foreach ($shareFiles as $shareFile) { $sharedpaths[] = $shareFile['file_path']; } $sharedpaths = array_unique($sharedpaths); // // 搜索共享目录,根目录查询 // if ($path != '/' . $this->_user_id) { return array(); } $query = str_replace("%", "\\%", $query); // $sql = ' file_name like "%' . $query . '%"'; $sql = ''; $retval = array(); foreach ($sharedpaths as $sharedpath) { $condition = $sql . 'parent_file_id=0 and file_path="' . $sharedpath . '" '; $files = MFiles::findAll($condition); if (empty($files)) { continue; } $retval = array_merge($retval, $files); } return $retval; }
/** * 当参数overwrite=false时,执行文件重命名,再创建文件 */ private function renameFile() { // overwrite=true,不执行之后操作 if ($this->overwrite == true) { return; } $children = MFiles::queryChildrenByParentId($this->user_id, $this->parent_file_id); if ($children === false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } $names = array(); foreach ($children as $k => $v) { $names[strtolower($v["file_name"])] = $v["file_name"]; } // // 执行重命名文件操作 // $this->file_name = MUtils::getConflictName($this->file_name, $names); $this->path = MUtils::convertStandardPath($this->parent_path . "/" . $this->file_name); $this->file_path = "/" . $this->user_id . $this->path; $this->create_file = true; }