/** * 控制器执行主逻辑函数 * * @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); }
/** * 文件修改逻辑处理,包括冲突处理 * @param array $fileDetail * @throws * @return array */ private function modifyFile($fileDetail) { // 判断是否重写 if ($this->overwrite == false) { $this->create_file = true; return; } // // 如果不为修改文件,则返回 // if ($this->create_file == true) { return; } $fileMeta = MFileMetas::queryFileMeta($fileDetail->file_path, MConst::VERSION); if ($fileMeta == false || empty($fileMeta)) { $this->create_file = true; MFiles::updateFileDetailById($fileDetail->id, array('is_deleted' => 1)); return; } // // 检查parent_rev是否存在 // if (MUtils::isExistReversion($this->parent_rev, $fileMeta[0]["meta_value"]) == false) { $this->parent_rev = 0; } // // 修改的内容一致 // if ($this->version_id == $fileDetail->version_id) { $this->create_event = false; return; } // // 生成冲突文件 // if ($fileDetail->version_id != $this->parent_rev && $this->parent_rev != 0) { $this->conflict = true; $this->create_file = true; return; } // // 空间判断 // $sizeDiff = $this->size - $fileDetail->file_size; $this->spaceFilter($sizeDiff); // // 修改文件 // $this->action = MConst::MODIFY_FILE; //如果在共享目录内进行修改则进行修改权限判断 if ($this->share_filter->is_shared) { $this->share_filter->hasPermissionExecute($fileDetail->file_path, MPrivilege::FILE_MODIFY); } // // 文件meta属性,版本信息 // $version = MUtils::getFileVersions($this->user_device_name, $this->size, $this->version_id, $this->action, $this->user_id, $this->user_nick, $fileMeta[0]["meta_value"]); // // 需要更新的数据字段和值 // $updates = array(); $updates["version_id"] = (int) $this->version_id; $updates["file_size"] = $this->size; $updates["file_update_time"] = $this->file_update_time; $updates["event_uuid"] = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); // // 执行更新操作 // if (MFiles::updateFileDetailById($fileDetail->id, $updates) === false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } // // 保存事件 // $this->context = array("hash" => $this->file_hash, "rev" => (int) $this->version_id, "bytes" => (int) $this->size, "update_time" => (int) $this->file_update_time, "create_time" => (int) $this->file_create_time); $retVal = MiniEvent::getInstance()->createEvent($this->currentUserId, $this->user_device_id, $this->action, $fileDetail->file_path, serialize($this->context), $updates["event_uuid"], $this->share_filter->type); if ($retVal === false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } $this->share_filter->handlerAction($this->action, $this->user_device_id, $fileDetail->file_path, $this->context); // // 只有版本历史中不存在的时候才更新 // if (MUtils::isExistReversion($this->version_id, $fileMeta[0]["meta_value"]) == false) { // 文件版本引用次数更新 if (MiniVersion::getInstance()->updateRefCount($this->version_id) == false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } } // 保存版本历史 if (MFileMetas::updateFileMeta($fileDetail->file_path, MConst::VERSION, $version) == false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } }