/** * 创建元数据对象 */ public static function CreateFileDetail($file_detail, $user_id) { $sql = "insert into " . DB_PREFIX . "_files(user_id,file_type,parent_file_id,"; $sql .= "file_create_time,file_update_time,file_name,version_id,"; $sql .= "file_size,file_path,event_uuid,mime_type,created_at,updated_at,file_name_pinyin) values "; // // 新建对象,需要生成对应的长度信息 // $file_detail->event_uuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); if (isset($file_detail->version_id) === false) { $file_detail->version_id = 0; } $sql .= "({$user_id},{$file_detail->file_type},"; $sql .= "{$file_detail->parent_file_id},"; $sql .= "{$file_detail->file_create_time},"; $sql .= "{$file_detail->file_update_time},"; $sql .= "\"{$file_detail->file_name}\","; $sql .= "{$file_detail->version_id},"; $sql .= "{$file_detail->file_size},"; $sql .= "\"{$file_detail->file_path}\","; $sql .= "\"{$file_detail->event_uuid}\","; if ($file_detail->mime_type === NULL) { $sql .= "NULL,"; } else { $sql .= "\"{$file_detail->mime_type}\","; } $sql .= "now(),now(),'" . MiniUtil::getPinYinByName($file_detail->file_name) . "')"; $db_manager = MDbManager::getInstance(); $result = $db_manager->insertDb($sql); if ($result === false) { return false; } // // 修复sort值为id值,确保唯一 // $sql = "update " . DB_PREFIX . "_files set sort=id where sort=0"; $db_manager->insertDb($sql); return $file_detail; }
/** * 控制器执行主逻辑函数, 处理移动文件或者文件夹 * * @return mixed $value 返回最终需要执行完的结果 */ public function invoke($uri = null) { // 调用父类初始化函数,注册自定义的异常和错误处理逻辑 parent::init(); $this->setAction(MConst::MOVE); $params = $_REQUEST; // 检查参数 if (isset($params) === false) { throw new MException(Yii::t('api', 'Bad Request 7')); } // // 获取用户数据,如user_id $user = MUserManager::getInstance()->getCurrentUser(); $device = MUserManager::getInstance()->getCurrentDevice(); $this->_userId = $user["user_id"]; $this->master = $user["user_id"]; $user_nick = $user["user_name"]; $user_device_id = $device["device_id"]; $this->_user_device_name = $device["user_device_name"]; // 文件大小格式化参数 $this->_locale = "bytes"; if (isset($params["locale"])) { $this->_locale = $params["locale"]; } if (isset($params["root"]) === false || isset($params["from_path"]) === false || isset($params["to_path"]) === false) { throw new MFileopsException(Yii::t('api', 'Bad Request'), MConst::HTTP_CODE_400); } $this->_root = $params["root"]; $from_path = $params["from_path"]; $to_path = $params["to_path"]; $arr = explode('/', $from_path); $isRoot = false; $isMine = false; if (count($arr) == 3) { $isRoot = true; } $fileOwnerId = $arr[1]; $currentUserId = $this->_userId; if ($fileOwnerId == $currentUserId) { $isMine = true; } if ($isRoot && !$isMine) { //如果是在根目录下且不是自己的目录 则后台控制不准取消共享 throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_409); } $to_parts = explode('/', $to_path); $from_parts = explode('/', $from_path); if (count($to_parts) == 2) { $to_path = '/' . $this->_userId . $to_path; } $to_parts = explode('/', $to_path); $file = MiniFile::getInstance()->getByPath($from_path); $isSelfFile = false; if (!empty($file) && $file['user_id'] == $this->_userId) { $isSelfFile = true; } // 转换路径分隔符,便于以后跨平台,如:将 "\"=>"/" $from_path = MUtils::convertStandardPath($from_path); $to_path = MUtils::convertStandardPath($to_path); if ($to_path[strlen($to_path) - 1] == "/") { // 目标文件无效,403 error throw new MFileopsException(Yii::t('api', 'The file or folder name is invalid'), MConst::HTTP_CODE_403); } // 检查共享 $this->from_share_filter = MSharesFilter::init(); $this->to_share_filter = MSharesFilter::init(); $isSharedPath = true; $this->rename = false; // 检查移动方式 if ($this->rename == true) { // 先copy再删除,如果是移动共享文件夹则只copy,再执行shareManager取消共享 $copy_handler = new MCopyController(); $copy_handler->isOutput = false; $response = $copy_handler->invoke(); $_REQUEST['path'] = $params["from_path"]; $delete_handler = new MDeleteController(); $delete_handler->isOutput = false; $delete_handler->completely_remove = true; $delete_handler->invoke(); if (MUserManager::getInstance()->isWeb() === true) { $this->buildWebResponse(); exit; return; } echo json_encode($response); return; } $file_name = MUtils::get_basename($to_path); // 检查文件名是否有效 $is_invalid = MUtils::checkNameInvalid($file_name); if ($is_invalid) { throw new MFileopsException(Yii::t('api', 'The file or folder name is invalid'), MConst::HTTP_CODE_400); } // 检查是否移动到其子目录下 if (strpos($to_path, $from_path . "/") === 0) { throw new MFileopsException(Yii::t('api', 'Can not be moved to the subdirectory'), MConst::HTTP_CODE_403); } if ($to_path == "/{$this->_userId}" || $to_path == "/{$this->_userId}/") { throw new MFileopsException(Yii::t('api', 'Can not be moved to the error directory'), MConst::HTTP_CODE_403); } $from_parent = CUtils::pathinfo_utf($from_path); $to_parent = CUtils::pathinfo_utf($to_path); $privilegeModel = new PrivilegeBiz(); if (!(count($to_parts) == 3)) { $isSharedPath = false; $toPathArr = explode('/', $to_path); $masterId = $toPathArr[1]; if ($masterId != $this->_userId) { $isSharedPath = true; } else { $model = new GeneralFolderPermissionBiz($to_parent['dirname']); if ($model->isParentShared($to_parent['dirname'])) { //如果是父目录被共享 $isSharedPath = true; } } if ($isSharedPath) { $toPrivilege = UserPermissionBiz::getInstance()->getPermission($to_parent['dirname'], $this->_userId); if (empty($toPrivilege)) { $toPrivilege['permission'] = MConst::SUPREME_PERMISSION; } else { $this->to_share_filter->slaves = $privilegeModel->getSlaveIdsByPath($toPrivilege['share_root_path']); $this->to_share_filter->is_shared = true; } } else { $toPrivilege['permission'] = MConst::SUPREME_PERMISSION; } $toFilter = new MiniPermission($toPrivilege['permission']); } else { if ($to_parent['dirname'] == $from_parent['dirname']) { $isSharedPath = false; $fromPathArr = explode('/', $from_path); $masterId = $fromPathArr[1]; if ($masterId != $this->_userId) { $isSharedPath = true; } else { $model = new GeneralFolderPermissionBiz($from_path); if ($model->isParentShared($from_path)) { //如果是父目录被共享 $isSharedPath = true; } } if ($isSharedPath) { $toPrivilege = UserPermissionBiz::getInstance()->getPermission($from_path, $this->_userId); if (!empty($toPrivilege)) { $this->to_share_filter->slaves = $privilegeModel->getSlaveIdsByPath($toPrivilege['share_root_path']); $this->to_share_filter->is_shared = true; } else { $toPrivilege['permission'] = MConst::SUPREME_PERMISSION; } } else { $toPrivilege['permission'] = MConst::SUPREME_PERMISSION; } } $toFilter = new MiniPermission(MConst::SUPREME_PERMISSION); } $isSharedPath = false; $fromPathArr = explode('/', $from_path); $masterId = $fromPathArr[1]; if ($masterId != $this->_userId) { $isSharedPath = true; } else { $model = new GeneralFolderPermissionBiz($from_path); if ($model->isParentShared($from_path)) { //如果是父目录被共享 $isSharedPath = true; } } if ($isSharedPath) { $fromPrivilege = UserPermissionBiz::getInstance()->getPermission($from_path, $this->_userId); if (empty($fromPrivilege)) { $fromPrivilege['permission'] = MConst::SUPREME_PERMISSION; } else { $this->from_share_filter->slaves = $privilegeModel->getSlaveIdsByPath($fromPrivilege['share_root_path']); $this->from_share_filter->is_shared = true; } } else { $fromPrivilege['permission'] = MConst::SUPREME_PERMISSION; } $fromFilter = new MiniPermission($fromPrivilege['permission']); if ($to_parent['dirname'] == $from_parent['dirname']) { $this->setAction(MConst::RENAME); $this->isRename = true; $canRenameFile = $fromFilter->canModifyFileName(); $canRenameFolder = $fromFilter->canModifyFolderName(); $canRenameFile2 = $toFilter->canModifyFileName(); $canRenameFolder2 = $toFilter->canModifyFolderName(); if ($file['file_type'] > 0 && !$canRenameFolder && !$isSelfFile) { //如果目标是目录,则当其不为己文件,且无更名权限时 exception throw new MFileopsException(Yii::t('api', 'have no permission to rename folder'), MConst::HTTP_CODE_409); } if ($file['file_type'] == 0 && !$canRenameFile && !$isSelfFile) { //如果目标是文件,则当其不为己文件,且无更名权限时 exception throw new MFileopsException(Yii::t('api', 'have no permission to rename file'), MConst::HTTP_CODE_409); } if ($file['file_type'] > 0 && !$canRenameFolder2 && !$isSelfFile) { //如果目标是目录,则当其不为己文件,且无更名权限时 exception throw new MFileopsException(Yii::t('api', 'have no permission to rename folder'), MConst::HTTP_CODE_409); } if ($file['file_type'] == 0 && !$canRenameFile2 && !$isSelfFile) { //如果目标是文件,则当其不为己文件,且无更名权限时 exception throw new MFileopsException(Yii::t('api', 'have no permission to rename file'), MConst::HTTP_CODE_409); } } else { $canDeleteFile = $fromFilter->canDeleteFile(); $canDeleteFile2 = $toFilter->canDeleteFile(); if (!$canDeleteFile || !$canDeleteFile2) { throw new MFileopsException(Yii::t('api', 'have no permission to move file'), MConst::HTTP_CODE_409); } } // 先检查源目录是否存在,如果不存在抛出404错误 // $query_db_file = MFiles::queryFilesByPath($from_path); if ($query_db_file === false || empty($query_db_file)) { throw new MFileopsException(Yii::t('api', 'The source file was not found at the specified path'), MConst::HTTP_CODE_404); } // // 检查目标是否存在(包括已被删除的状态) // $deleted = null; $query_db_goal_file = MFiles::queryAllFilesByPath($to_path); if ($query_db_goal_file) { if ($from_path !== $to_path && $query_db_goal_file[0]["is_deleted"] == false) { throw new MFileopsException(Yii::t('api', 'There is already a item at the given destination'), MConst::HTTP_CODE_403); } // 已删除文件的处理 if ($query_db_goal_file[0]["is_deleted"] == 1) { MFiles::deleteById($query_db_goal_file[0]["id"]); if ($query_db_goal_file[0]["file_type"] != 0) { // 文件则直接删除 $deleted = $query_db_goal_file[0]["id"]; } } } $query_db_file = MFiles::queryFilesByPath($from_path); if ($query_db_file === false || empty($query_db_file)) { throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404); } // // 检查移动原路径与目标路径是否一致,一致则则返回其文件信息 // if ($from_path === $to_path) { $this->buildResult($query_db_file[0]); return; } // // 查询目标路径父目录信息 // $pathInfo = MUtils::pathinfo_utf($to_path); $parent_path = $pathInfo["dirname"]; $create_folder = new MCreateFolderController(); $create_folder->_user_device_id = $user_device_id; $create_folder->_user_id = $this->_userId; if (count(explode('/', $parent_path)) == 2) { $parent_file_id = 0; } else { $parent_file_id = $create_folder->handlerParentFolder($parent_path); } $fromUserId = $from_parts[1]; $toUserId = $to_parts[1]; // // 组装对象信息 // $file_detail = new MFiles(); $file_detail->file_name = $file_name; $file_detail->file_path = $to_path; $file_detail->file_type = $query_db_file[0]["file_type"]; $file_detail->id = $query_db_file[0]["id"]; $file_detail->from_path = $from_path; $file_detail->parent_file_id = $parent_file_id; $file_detail->user_id = $toUserId; $file_detail->mime_type = NULL; $create_array = array(); // // 判断操作的是文件夹,还是文件 // if ($file_detail->file_type > MConst::OBJECT_TYPE_FILE) { if ($file['user_id'] != $this->_userId) { $updateUserId = $query_db_file[0]["user_id"]; } else { $updateUserId = $this->master; } // // 文件夹,将会对其子文件做进一步处理 // $ret_value = MFiles::updateMoveChildrenFileDetail($updateUserId, $file_detail); if ($ret_value === false) { throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404); } // // 针对文件夹下的文件,组装需要添加版本信息的文件 // $create_array = $this->handleChildrenVersions($create_array, $this->_userId, $user_nick, $from_path, $to_path, $query_db_file[0]["id"], $this->_user_device_name, $query_db_file[0]["file_size"]); } else { $file_detail->mime_type = MiniUtil::getMimeType($file_name); $file_meta = new MFileMetas(); $file_meta->version_id = $query_db_file[0]["version_id"]; // // 查询之前是否包含其版本 // $file_version = MFileMetas::queryFileMeta($to_path, MConst::VERSION); if ($file_version) { $meta_value = MUtils::getFileVersions($this->_user_device_name, $query_db_file[0]['file_size'], $file_meta->version_id, MConst::CREATE_FILE, $this->_userId, $user_nick, $file_version[0]["meta_value"]); $file_meta->is_add = false; } else { $meta_value = MUtils::getFileVersions($this->_user_device_name, $query_db_file[0]['file_size'], $file_meta->version_id, MConst::CREATE_FILE, $this->_userId, $user_nick); $file_meta->is_add = true; } $file_meta->meta_value = $meta_value; $file_meta->file_path = $to_path; $create_array[$to_path] = $file_meta; // // 添加到需要更新的版本ref // array_push($this->versions, $file_meta->version_id); } if ($file['file_type'] == 2 || $file['file_type'] == 4) { MiniUserPrivilege::getInstance()->updateByPath($from_path, $to_path); MiniGroupPrivilege::getInstance()->updateByPath($from_path, $to_path); if ($to_parent['dirname'] != $from_parent['dirname']) { MiniUserPrivilege::getInstance()->deleteByFilePath($to_path); MiniGroupPrivilege::getInstance()->deleteByFilePath($to_path); } } // // 创建版本信息 // $ret = MFileMetas::batchCreateFileMetas($create_array, MConst::VERSION); // if ($ret === false) // { // throw new MFileopsException( // Yii::t('api','Internal Server Error'), // MConst::HTTP_CODE_500); // } // // 更新版本 // foreach ($create_array as $file_meta) { if ($file_meta->is_add === true) { // 不存在记录,不需要更新 continue; } MFileMetas::updateFileMeta($file_meta->file_path, MConst::VERSION, $file_meta->meta_value); } // // 更新版本引用次数 // foreach ($this->versions as $vid) { MiniVersion::getInstance()->updateRefCount($vid); } // // 更新该对象元数据 // $file_detail->event_uuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); $ret_value = MFiles::updateMoveFileDetail($file_detail); // 移动目录 or 文件 if ($ret_value === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } $updates = array(); if ($file['file_type'] == 2 || $file['file_type'] == 4) { if ($to_parent['dirname'] != $from_parent['dirname']) { $updates['file_type'] = 1; $updates['user_id'] = $this->_userId; } } if ($fromUserId != $toUserId) { $updates['user_id'] = $toUserId; } MiniFile::getInstance()->updateByPath($to_path, $updates); //被共享者更改文件名更新file_meta记录 MiniFileMeta::getInstance()->modifyFilePath($from_path, 'create_id', $to_path, $file['file_type']); // // 保存移动事件 // by Kindac; // $event_action = $this->getGroupMove($file_detail->from_path, $file_detail->file_path); $ret_value = MiniEvent::getInstance()->createEvent($this->_userId, $user_device_id, $event_action, $file_detail->from_path, $file_detail->file_path, $file_detail->event_uuid, $this->to_share_filter->type); if ($ret_value === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } if ($file['type'] != 0) { $eventAction = MConst::CREATE_DIRECTORY; } else { $eventAction = MConst::CREATE_FILE; } if ($to_parent['dirname'] == $from_parent['dirname']) { $this->to_share_filter->handlerAction($event_action, $user_device_id, $file_detail->from_path, $file_detail->file_path); } else { if ($this->to_share_filter->is_shared) { $this->to_share_filter->handlerAction($eventAction, $user_device_id, $file_detail->file_path, $file_detail->file_path); } if ($this->from_share_filter->is_shared) { $this->from_share_filter->handlerAction(MConst::DELETE, $user_device_id, $file_detail->from_path, $file_detail->from_path); } } $query_db_file[0]["file_path"] = $file_detail->file_path; $query_db_file[0]["event_uuid"] = $file_detail->event_uuid; if (!empty($deleted)) { MFiles::updateParentId($deleted, $file_detail->id); } //进行扩展操作 $this->extend($from_path, $to_path); //执行完删除操作后执行的额外事物 $after = new MMoveAfter(); $after->action = self::$scene; // $after->from_share_filter = $from_share_filter; $after->to_share_filter = $this->to_share_filter; $after->from_path = $from_path; $after->to_path = $to_path; $after->file_detail = $file_detail; $after->execute(); $this->buildResult($query_db_file[0], $to_path); }
/** * 创建事件 */ public function createEvent($userId, $userDeviceId, $action, $path, $context) { $eventUuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); MiniEvent::getInstance()->createEvent($userId, $userDeviceId, $action, $path, $context, $eventUuid, $extends = NULL); }
/** * * 执行文件夹删除 * @param UserFile $file */ public function delete($parenFile) { $share_filter = MSharesFilter::init(); $share_filter->handlerCheckByFile($this->_userId, $parenFile); // // 取消共享 // if ($parenFile['file_type'] == 2 && $parenFile['user_id'] != $this->_userId || $parenFile['file_type'] == 3) { $parenFile = UserFile::model()->findByAttributes(array('file_path' => $share_filter->slaves[$this->_userId], 'is_deleted' => 0)); if (!$parenFile) { throw new ApiException("Not found"); } $handler = new ShareManager(); $handler->_userId = $share_filter->operator; $handler->_id = $parenFile["id"]; try { $handler->invoke(ShareManager::CANCEL_SHARED); } catch (Exception $e) { throw new ApiException("Not found"); } return; } // 更新每个元素以及子元素 $parentPath = $parenFile["file_path"]; $handler = new UserFile(); $files = $handler->getFilesByPath($parentPath); // 轮询删除 foreach ($files as $file) { if ($file["is_deleted"]) { continue; } // 已经删除的文件不做操作 $file["event_uuid"] = MiniUtil::getEventRandomString(46); if ($file["file_type"] == 0) { if ($file["is_deleted"]) { continue; } // 已经删除的文件不做操作 $file["is_deleted"] = 1; $file->save(); // 创建版本信息 $this->handleFileMeta($file["file_path"], $file["version_id"], $file['user_id'], $this->_userNick, $this->_deviceName, $file['file_size']); // 创建事件 MiniEvent::getInstance()->createEvent($file['user_id'], $this->_deviceId, $this->_action, $file["file_path"], $file["file_path"], $file["event_uuid"]); $share_filter->handlerAction($this->_action, $this->_deviceId, $file["file_path"], $file["file_path"]); continue; } $this->delete($file); $file["is_deleted"] = 1; $file->save(); // 创建事件 MiniEvent::getInstance()->createEvent($file['user_id'], $this->_deviceId, $this->_action, $file["file_path"], $file["file_path"], $file["event_uuid"]); $share_filter->handlerAction($this->_action, $this->_deviceId, $file["file_path"], $file["file_path"]); } $parenFile["event_uuid"] = MiniUtil::getEventRandomString(46); $parenFile["is_deleted"] = 1; $parenFile->save(); // 创建事件 MiniEvent::getInstance()->createEvent($parenFile['user_id'], $this->_deviceId, $this->_action, $parenFile["file_path"], $parenFile["file_path"], $parenFile["event_uuid"]); $share_filter->handlerAction($this->_action, $this->_deviceId, $parenFile["file_path"], $parenFile["file_path"]); // // 删除共享目录 // if ($share_filter->_is_shared_path && $share_filter->operator == $share_filter->master) { $id = $parenFile["id"]; $handler = new ShareManager(); $handler->_userId = $share_filter->operator; $handler->_id = $id; try { $handler->invoke(ShareManager::CANCEL_SHARED); } catch (Exception $e) { throw new ApiException('Internal Server Error'); } } }
/** * @return string 获得随机的字符串 */ private function getAppKey() { $key = MiniUtil::getEventRandomString(self::LEN_KEY); $appKey = substr($key, 0, self::LEN_KEY); return $appKey; }
/** * 创建文件 * @param array $fileDetail * @throws * @return array */ private function createFile($fileDetail) { // // 如果创建文件标志为false,则不执行创建 // if ($this->create_file == false) { return; } // // 是否有标记为删除的对象,可能存在多个 // $conflictFile = MFiles::queryFilesByPath($this->file_path, TRUE); if ($conflictFile != false && empty($conflictFile) == false) { foreach ($conflictFile as $file) { // // 如果非文件类型,删除 // if ($file["file_type"] == MConst::OBJECT_TYPE_FILE) { $fileDetail = MFiles::exchange2Object($file, TRUE); $this->creatFileDeleted($fileDetail); return; } else { // 彻底删除之后再进行创建 $trash = new Trash(); $trash->_userId = $this->user_id; $trash->fromIds = $file['id']; try { $trash->invoke(Trash::DELETE); } catch (Exception $e) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } } } } $this->spaceFilter($this->size); // 过滤器,空间大小计算 $fileDetail->file_create_time = $this->file_create_time; $fileDetail->file_update_time = $this->file_update_time; $fileDetail->file_size = $this->size; $fileDetail->file_type = MConst::OBJECT_TYPE_FILE; $fileDetail->parent_file_id = $this->parent_file_id; $fileDetail->version_id = $this->version_id; $fileDetail->file_path = $this->path; $fileDetail->file_name = $this->file_name; $fileDetail->event_uuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); $fileDetail->mime_type = $this->type; // // 创建文件时,如果存在老的版本 需要兼容 不能覆盖 // $metaValue = null; $fileMeta = MFileMetas::queryFileMeta($fileDetail->file_path, MConst::VERSION); if ($fileMeta) { $metaValue = $fileMeta[0]['meta_value']; } // // 文件meta属性,版本信息 // $version = MUtils::getFileVersions($this->user_device_name, $fileDetail->file_size, $this->version_id, $this->action, $this->user_id, $this->user_nick, $metaValue); // // 保存文件元数据 // $retVal = MFiles::CreateFileDetail($fileDetail, $this->user_id, $this->user_nick); if ($retVal === 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, $this->file_path, serialize($this->context), $fileDetail->event_uuid, $this->share_filter->type); // 为每个共享用户创建事件 $this->share_filter->handlerAction($this->action, $this->user_device_id, $this->file_path, $this->context); if (isset($fileDetail->event_uuid)) { $this->event_uuid = $fileDetail->event_uuid; } if ($retVal === false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } // 文件版本引用次数更新 if (MiniVersion::getInstance()->updateRefCount($this->version_id) == false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } if ($fileMeta) { $retVal = MFileMetas::updateFileMeta($this->file_path, MConst::VERSION, $version); } else { $retVal = MFileMetas::createFileMeta($this->file_path, MConst::VERSION, $version); $pathArr = explode('/', $this->file_path); $user = Yii::app()->session["user"]; if ((int) $pathArr[1] !== (int) $user['user_id']) { //只有当被共享者在共享目录下创建文件时,才会记录create_id MFileMetas::createFileMeta($this->file_path, 'create_id', $user['user_id']); } } if ($retVal === false) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } }
/** * 处理创建文件信息及事件 * @param string $folderPath * @param int $parentFileId * @param boolean $hadFileDelete * @param int $userId * @return array */ private function createFileMeta($folderPath, $parentFileId, $hadFileDelete, $userId) { $fileName = MUtils::get_basename($folderPath); // 组装对象信息 $fileDetail = array(); $fileDetail["file_create_time"] = time(); $fileDetail["file_update_time"] = time(); $fileDetail["file_name"] = $fileName; $fileDetail["file_path"] = $folderPath; $fileDetail["file_size"] = 0; $fileDetail["file_type"] = MConst::OBJECT_TYPE_DIRECTORY; $fileDetail["parent_file_id"] = $parentFileId; $fileDetail["event_uuid"] = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); $fileDetail["mime_type"] = NULL; // 保存文件元数据 if ($hadFileDelete) { $updates = array(); $updates["file_update_time"] = time(); $updates["is_deleted"] = intval(false); $updates["file_type"] = MConst::OBJECT_TYPE_DIRECTORY; $updates["event_uuid"] = $fileDetail["event_uuid"]; // 存在已被删除的数据,只需更新 $this->updateByPath($folderPath, $updates); } else { // 不存在数据,添加 $file = $this->getByPath($folderPath); if (empty($file)) { if (!empty($fileDetail["file_name"])) { $this->create($fileDetail, $userId); $device = MUserManager::getInstance()->getCurrentDevice(); $event_action = MConst::CREATE_DIRECTORY; MiniEvent::getInstance()->createEvent($userId, $device["device_id"], $event_action, $fileDetail["file_path"], $fileDetail["file_path"], $fileDetail["event_uuid"], 0); } } } return $fileDetail; }
/** * 全部还原 */ public function revertFileAll() { $models = $this->findAllByAttributes(array('is_deleted' => 1)); $device_id = Yii::app()->session["deviceId"]; foreach ($models as $model) { $shareFilter = MSharesFilter::init(); $shareFilter->handlerCheckByFile($model['user_id'], $model); $user_id = $model['user_id']; $path = $model['file_path']; $context = $path; $event_uuid = MiniUtil::getEventRandomString(46); $action = 0; // 修改文件 if ($model['file_type'] == 0) { // // 如果是文件,需要创建版本 // $user = Yii::app()->session['user']; $device = UserDevice::model()->findByUserIdAndType($user['id'], CConst::DEVICE_WEB); $deviceName = $device["user_device_name"]; $this->_saveFileMeta($path, $model['version_id'], $user['id'], $user['name'], CConst::WEB_RESTORE, $deviceName, $model['file_size']); $action = CConst::CREATE_FILE; $version = FileVersion::model()->findByPk($model["version_id"]); $context = array("hash" => $version['file_signature'], "rev" => (int) $model["version_id"], "bytes" => (int) $model["file_size"]); $context = serialize($context); } $model['event_uuid'] = $event_uuid; $model["is_deleted"] = 0; $model->save(); MiniEvent::getInstance()->createEvent($user_id, $device_id, $action, $path, $context, $event_uuid, $shareFilter->type); } return true; }
/** * * * @param int $action * @param int $device_id * @param string $path * @param mixed context - */ public function handlerAction($action, $device_id, $path, $context = '') { // // 如果不是在共享文件夹中做操作,则不做处理 // if ($this->is_shared == false) { return; } if (!$path) { return; } $index = strlen("{$this->_shared_path}"); $path = substr_replace($path, "", 0, $index); // 根据不同的事件处理 switch ($action) { case 3: case 4: $context = serialize($context); break; default: $context = substr_replace($context, "", 0, $index); break; } $text = $context; // 为每个成员添加事件 foreach ($this->slaves as $slaves_id => $v) { if ($action != 3 && $action != 4) { $text = $v . $context; } MiniEvent::getInstance()->createEvent($slaves_id, $device_id, $action, $v . $path, $text, MiniUtil::getEventRandomString(46)); } }
/** * 控制器执行主逻辑函数 * * @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); }
/** * create file * @param $userId * @param $file * @param $deviceId * @return array */ public function createFile($userId, $file, $deviceId) { $event = new Event(); $event->user_id = $userId; $event->user_device_id = $deviceId; if ((int) $file["file_type"] === MiniFile::$TYPE_FILE) { $event->action = MiniEvent::$CREATE_FILE; } else { //TODO include public folder/share folder/ $event->action = MiniEvent::$CREATE_FOLDER; } $event->file_path = $file["file_path"]; $context = $file["file_path"]; if ((int) $file["file_type"] === MiniFile::$TYPE_FILE) { //get version $version = MiniVersion::getInstance()->getVersion($file["version_id"]); $versionInfo = array("hash" => $version["file_signature"], "rev" => (int) $version["id"], "bytes" => (int) $file["file_size"], "update_time" => (int) $file["file_update_time"], "create_time" => (int) $file["file_create_time"]); $context = serialize($versionInfo); } $event->context = $context; $event->event_uuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); $event->type = MiniEvent::$EVENT_COMMON_TYPE; $event->save(); //update file event_uuid MiniFile::getInstance()->update($file["id"], array("event_uuid" => $event->event_uuid)); }
/** * 方法描述:随机生成文件明 */ public static function getRandomName($length = 40) { return MiniUtil::getEventRandomString($length); }
/** * * 将删除的文件(夹)还原 * * @since 1.0.7 */ private function handleRevert() { // 获取用户信息 $device = new UserDevice(); $device = $device->findByUserIdAndType($this->_userId, CConst::DEVICE_WEB); $this->_deviceId = $device["id"]; $this->_deviceName = $device["user_device_name"]; $user = User::model()->findByPk($this->_userId); $this->_userNick = $user["user_name"]; $filter = MSharesFilter::init(); $files = UserFile::model()->getFilesByIds($this->fromIds, 1); foreach ($files as $file) { $file_path = CUtils::removeUserFromPath($file["file_path"]); if ($filter->handlerCheck($this->_userId, $file_path)) { $this->_userId = $filter->master; $path = $filter->_path; } if ($filter->is_shared && $filter->master != $filter->operator) { //如果没有读权限则不进行删除 $permission = Yii::app()->privilege->checkPrivilege('/' . $filter->master . $path); if (!$permission[MPrivilege::RESOURCE_READ]) { continue; } //没有删除权限则不能进行还原 if ($file["file_type"] == 0 && !$permission[MPrivilege::FILE_DELETE]) { continue; } elseif (!$permission[MPrivilege::FOLDER_DELETE]) { continue; } } $path = $file["file_path"]; $file["is_deleted"] = 0; $file["event_uuid"] = MiniUtil::getEventRandomString(46); $file['file_update_time'] = time(); $file->save(); // 创建事件 $share_filter = MSharesFilter::init(); $share_filter->handlerCheckByFile($this->_userId, $file); if ($file["file_type"] == 0) { $version = FileVersion::model()->findByPk($file["version_id"]); // 创建meta $this->handleFileMeta($path, $file["version_id"], $filter->operator, $this->_userNick, CConst::CREATE_FILE, $this->_deviceName, $file['file_size']); $hash = $version["file_signature"]; $context = array("hash" => $hash, "rev" => (int) $file["version_id"], "bytes" => (int) $file["file_size"], "update_time" => (int) $file['file_update_time'], "create_time" => (int) $file['file_create_time']); MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, 3, $path, serialize($context), $file["event_uuid"], $share_filter->type); $share_filter->handlerAction(3, $this->_deviceId, $path, $context); continue; } // 文件夹创建事件 $event->CreateEvent($this->_userId, $this->_deviceId, 0, $path, $path, $file["event_uuid"], $share_filter->type); $share_filter->handlerAction(0, $this->_deviceId, $path, $path); // 处理子文件 $children = UserFile::model()->getFilesByPath($path, 1); foreach ($children as $child) { $child["is_deleted"] = 0; $child["event_uuid"] = MiniUtil::getEventRandomString(46); $child['file_update_time'] = time(); $child->save(); $path = $childcontext = $child["file_path"]; // 如果是文件则创建meta if ($child["file_type"] == 0) { $version = FileVersion::model()->findByPk($child["version_id"]); // 创建meta $this->handleFileMeta($path, $child["version_id"], $filter->operator, $this->_userNick, CConst::CREATE_FILE, $this->_deviceName, $child['file_size']); $hash = $version["file_signature"]; $childcontext = array("hash" => $hash, "rev" => (int) $child["version_id"], "bytes" => (int) $child["file_size"], "update_time" => (int) $child['file_update_time'], "create_time" => (int) $child['file_create_time']); // 文件夹创建事件 MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, 3, $path, serialize($childcontext), $child["event_uuid"], $share_filter->type); $share_filter->handlerAction(3, $this->_deviceId, $path, $childcontext); continue; } // 文件夹创建事件 $event->CreateEvent($this->_userId, $this->_deviceId, 0, $path, $childcontext, $child["event_uuid"]); $share_filter->handlerAction(0, $this->_deviceId, $path, $childcontext); } } $this->handleResult(TRUE, 0, Yii::t('api_message', 'action_success')); }
/** * * 复制文件夹 * @param string $fromPath * @param string $toPath */ public function handleCopyFolder($fromPath, $toPath) { $createFolder = new CreateFolder(); $createFolder->_deviceId = $this->_deviceId; $createFolder->_userId = $this->_userId; $createFolder->share_filter = $this->to_share_filter; $pathInfo = CUtils::pathinfo_utf($toPath); $parentId = $createFolder->handleCreateByPath($pathInfo["dirname"]); // 重命名 $name = $this->handleRename($parentId, $pathInfo["basename"]); $this->toPath = $pathInfo["dirname"] . "/" . $name; // // 如果是存在同名的已经删除的记录,则还原之 // $target = UserFile::model()->findByAttributes(array("user_id" => $this->_userId, "file_path" => $toPath, "is_deleted" => 1)); if (!$target) { $target = new UserFile(); $target["user_id"] = $this->_userId; $target["parent_file_id"] = $parentId; $target["file_create_time"] = time(); } $target["file_type"] = 1; $target["is_deleted"] = 0; $target["version_id"] = $this->from["version_id"]; $target["file_size"] = $this->from["file_size"]; $target["file_path"] = $toPath; $target["file_name"] = $name; $target["file_update_time"] = time(); $target["event_uuid"] = MiniUtil::getEventRandomString(46); $target->save(); // 创建事件 MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, 0, $toPath, $toPath, $target["event_uuid"]); $this->to_share_filter->handlerAction(0, $this->_deviceId, $toPath, $toPath); // copy 所有的文件,创建文件的时候会将父目录创建 $handler = new UserFile(); $children = $handler->getChildrenFileByPath($fromPath); foreach ($children as $child) { $this->from = $child; $childFromPath = $child["file_path"]; $index = strlen($this->fromPath); $childtoPath = substr_replace($childFromPath, $this->toPath, 0, $index); $this->handleCopyFile($childFromPath, $childtoPath); } // copy 所有的文件夹,补偿没有子文件的 路径 $folders = $handler->getChildrenFileByPath($fromPath, 1); $createFolder = new CreateFolder(); $createFolder->_deviceId = $this->_deviceId; $createFolder->_userId = $this->_userId; $createFolder->share_filter = $this->to_share_filter; foreach ($folders as $folder) { $childFromPath = $folder["file_path"]; $index = strlen($this->fromPath); $childtoPath = substr_replace($childFromPath, $this->toPath, 0, $index); $parentId = $createFolder->handleCreateByPath($childtoPath); } }
/** * * 创建权限 * * @since 1.0.7 */ public function createEvent($userId, $action, $file_path, $content) { $device = UserDevice::model()->findByUserIdAndType($userId, 1); $deviceId = $device["id"]; $event_uuid = MiniUtil::getEventRandomString(46); MiniEvent::getInstance()->createEvent($userId, $deviceId, $action, $file_path, $content, $event_uuid); }
/** * * 取消共享文件夹 * * @since 1.0.7 */ private function handlerCancelSharedFolder() { $this->_file = UserFile::model()->findByPk($this->_id); if (empty($this->_file)) { throw new ApiException("NOT FOUND"); } if ($this->_file['file_type'] != 2 && $this->_file['file_type'] != 3 && $this->_file['file_type'] != 4) { throw new ApiException("NOT FOUND"); } $file_meta = FileMeta::model()->findByAttributes(array('file_path' => $this->_file['file_path'], 'meta_key' => self::SHARED_META_FLAG)); if (empty($file_meta)) { $this->_file['file_type'] = 1; $this->_file->save(); return; } $meta_value = unserialize($file_meta['meta_value']); $this->_master = $meta_value['master']; $this->_slaves = $meta_value['slaves']; $path = $meta_value['path']; // // 表示共享者删除共享,所有全部都删除,否则只删除自己的 // if ($this->_master == $this->_userId) { $this->_file['file_type'] = 1; // // 删除共享信息 // foreach ($this->_slaves as $k => $v) { //添加需要删除的共享用户到列表中 $this->deleteShareUsers[] = $k; $event_uuid = MiniUtil::getEventRandomString(46); UserFile::model()->deleteAllByAttributes(array('file_path' => $v, 'file_type' => 3)); FileMeta::model()->deleteAllByAttributes(array('file_path' => $v, 'meta_key' => self::SHARED_META_FLAG)); MiniEvent::getInstance()->createEvent($k, $this->_deviceId, 1, $v, $v, $event_uuid); $this->handlerDeleteMyFavorate($k, $path); } FileMeta::model()->deleteAllByAttributes(array('file_path' => $meta_value['path'], 'meta_key' => self::SHARED_META_FLAG)); $event_uuid = MiniUtil::getEventRandomString(46); MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, 6, $this->_file['file_path'], $this->_file['file_path'], $event_uuid); $this->_file['event_uuid'] = $event_uuid; $this->_file->save(); } else { if (isset($this->_slaves[$this->_userId])) { UserFile::model()->deleteAllByAttributes(array('file_path' => $this->_slaves[$this->_userId], 'file_type' => 3)); // // 只读共享 // 如果 type发生改变 // by kindac // UserFile::model()->deleteAllByAttributes(array('file_path' => $this->_slaves[$this->_userId], 'file_type' => 4)); FileMeta::model()->deleteAllByAttributes(array('file_path' => $this->_slaves[$this->_userId], 'meta_key' => self::SHARED_META_FLAG)); } $event_uuid = MiniUtil::getEventRandomString(46); MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, 1, $this->_slaves[$this->_userId], $this->_slaves[$this->_userId], $event_uuid); // // 删除 // unset($this->_slaves[$this->_userId]); $meta_value['slaves'] = $this->_slaves; $meta_value['send_msg'] = $this->_send_msg; $this->_slaves[$meta_value['master']] = $meta_value['path']; $meta_value = serialize($meta_value); foreach ($this->_slaves as $k => $v) { FileMeta::model()->updateAll(array('meta_value' => $meta_value), 'meta_key=:meta_key and file_path=:file_path', array(':meta_key' => self::SHARED_META_FLAG, ':file_path' => $v)); } $this->handlerDeleteMyFavorate($this->_userId, $path); // // 取消没有被共享者的共享 // if (count($this->_slaves) <= 1) { FileMeta::model()->deleteAllByAttributes(array('file_path' => $path, 'meta_key' => self::SHARED_META_FLAG)); $event_uuid = MiniUtil::getEventRandomString(46); MiniEvent::getInstance()->createEvent($this->_master, $this->_deviceId, 6, $path, $path, $event_uuid); UserFile::model()->updateAll(array('file_type' => 1, 'event_uuid' => $event_uuid), 'file_path=:file_path', array(':file_path' => $path)); } } }
/** * 处理删除文件,数据组装 * @param $file_detail 文件对象 * @param $query_db_file 数据库查询对象 * @return mixed $value 返回最终需要执行完的结果 */ private function assembleFileDetail($file_detail, $query_db_file) { $file_detail->event_action = MConst::DELETE; $file_detail->file_name = $query_db_file["file_name"]; $file_detail->from_path = $query_db_file["file_path"]; $file_detail->context = $query_db_file["file_path"]; $file_detail->event_uuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); }
/** * * 移动文件 * @param string $fromPath * @param string $toPath */ private function handleMoveFile($fromPath, $toPath) { $createFolder = new CreateFolder(); $createFolder->_deviceId = $this->to_share_filter->master; $createFolder->_userId = $this->_userId; $pathInfo = CUtils::pathinfo_utf($this->toPath); $parentId = $createFolder->handleCreateByPath($pathInfo["dirname"]); // 重命名 $name = $this->handleRename($parentId, $pathInfo["basename"]); $this->toPath = $pathInfo["dirname"] . "/" . $name; // // 如果是存在同名的已经删除的记录,则删除之 // $deleted = UserFile::model()->findByAttributes(array("user_id" => $this->_userId, "file_path" => $this->toPath, "is_deleted" => 1)); if ($deleted) { $deleted->delete(); } // 修改属性后 保存 $this->from["file_path"] = $this->toPath; $this->from["file_name"] = $name; $this->from["event_uuid"] = MiniUtil::getEventRandomString(46); $this->from["parent_file_id"] = $parentId; $this->from["user_id"] = $this->to_share_filter->master; $this->from->save(); // 更新meta $this->handleFileMeta($this->toPath, $this->from["version_id"], $this->_userId, $this->_userNick, CConst::CREATE_FILE, $this->_deviceName, $this->from['file_size']); // 创建事件 MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, $this->_action, $this->fromPath, $this->toPath, $this->from["event_uuid"]); $this->to_share_filter->handlerAction($this->_action, $this->_deviceId, $this->fromPath, $this->toPath); }
/** * 取消共享,删除权限 */ public function delete($filePath) { $arr = explode('/', $filePath); $isRoot = false; $isMine = false; if (count($arr) == 3) { $isRoot = true; } $fileOwnerId = $arr[1]; $currentUser = $this->user; $currentUserId = $currentUser['user_id']; if ($fileOwnerId == $currentUserId) { $isMine = true; } if ($isRoot && !$isMine) { //如果是在根目录下且不是自己的目录 则后台控制不准取消共享 throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_409); } $this->share_filter = MSharesFilter::init(); $device = MUserManager::getInstance()->getCurrentDevice(); $userDeviceId = $device["device_id"]; $this->share_filter->slaves = $this->getSlaveIdsByPath($filePath); MiniUserPrivilege::getInstance()->deleteByFilePath($filePath); MiniGroupPrivilege::getInstance()->deleteByFilePath($filePath); MiniFile::getInstance()->cancelPublic($filePath); $eventAction = MConst::CANCEL_SHARED; MiniEvent::getInstance()->createEvent($this->user['id'], $userDeviceId, $eventAction, $filePath, $filePath, MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID), $this->share_filter->type); $this->share_filter->is_shared = true; //把共享目录下的共享目录设置记录删除 MiniFileMeta::getInstance()->deleteFileMetaByPath($filePath, "share_model"); // 为每个共享用户创建事件 $this->share_filter->handlerAction($eventAction, $userDeviceId, $filePath, $filePath); return true; }
/** * 处理组装复制文件需要的对象 * @param $fileName * @param $parentFileId * @param $fileDetail * @param $queryDbFile */ public function assembleFileDetail($fileName, $parentFileId, $fileDetail, $queryDbFile) { $filePath = $queryDbFile["file_path"]; $fileDetail->file_type = $queryDbFile["file_type"]; $eventAction = MConst::CREATE_DIRECTORY; $fileDetail->context = $filePath; $fileDetail->mime_type = NULL; if ($fileDetail->file_type == MConst::OBJECT_TYPE_FILE) { $fileDetail->mime_type = MiniUtil::getMimeType($fileName); $eventAction = MConst::CREATE_FILE; $versionId = $queryDbFile["version_id"]; $version = MiniVersion::getInstance()->getVersion($versionId); if ($version != null) { $context = array("hash" => $version["file_signature"], "rev" => (int) $versionId, "bytes" => (int) $queryDbFile["file_size"], "update_time" => (int) $queryDbFile["file_update_time"], "create_time" => (int) $queryDbFile["file_create_time"]); $fileDetail->context = serialize($context); } } elseif ($fileDetail->file_type > MConst::OBJECT_TYPE_DIRECTORY) { $fileDetail->file_type = 1; } $fileDetail->from_id = $queryDbFile['id']; $fileDetail->parent_file_id = $parentFileId; $fileDetail->event_action = $eventAction; $fileDetail->file_name = $fileName; $fileDetail->version_id = $queryDbFile["version_id"]; $fileDetail->file_size = $queryDbFile["file_size"]; $fileDetail->event_uuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); $index = strlen($this->_from_path); $filePath = substr_replace($filePath, $this->_to_path, 0, $index); $fileDetail->from_path = $filePath; $fileDetail->file_path = $filePath; $fileDetail->file_create_time = time(); $fileDetail->file_update_time = time(); }
/** * * 根据路径创建目录 */ public function handleCreateByPath($path) { if ($path == "/{$this->_userId}" || $path == "\\" || $path == "/{$this->_userId}/") { return 0; } $event_uuid = MiniUtil::getEventRandomString(46); $file = UserFile::model()->find(array('condition' => 'file_path=:file_path', 'params' => array(':file_path' => $path))); if (empty($file) || is_null($file)) { $pathInfo = CUtils::pathinfo_utf($path); $parenPath = $pathInfo["dirname"]; $fileName = $pathInfo["basename"]; $parentId = $this->handleCreateByPath($parenPath); $file = new UserFile(); $file['user_id'] = $this->_userId; $file['file_type'] = 1; $file['parent_file_id'] = $parentId; $file['file_create_time'] = time(); $file['file_update_time'] = time(); $file['file_name'] = $fileName; $file['file_path'] = $path; $file['is_deleted'] = 0; $file['version_id'] = 0; $file['file_size'] = 0; $file["event_uuid"] = $event_uuid; $file->save(); $file['sort'] = $file['id']; $file->save(); // 创建事件 MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, $this->_action, $path, $path, $event_uuid, $this->share_filter->type); $this->share_filter->handlerAction($this->_action, $this->_deviceId, $path, $path); } else { // // 回收站插件: -1保留值 0正常 1删除 // 这里由is_deleted==1 特别修改为 is_deleted!=0 // By Kindac 2012/11/5 // if ($file["is_deleted"] != 0) { $file["is_deleted"] = 0; $file["file_update_time"] = time(); $file["event_uuid"] = $event_uuid; // // 递归创建父目录 // $pathInfo = CUtils::pathinfo_utf($path); $parenPath = $pathInfo["dirname"]; $this->handleCreateByPath($parenPath); $file->save(); MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, $this->_action, $path, $path, $event_uuid, $this->share_filter->type); $this->share_filter->handlerAction($this->_action, $this->_deviceId, $path, $path); } } return $file["id"]; }
/** * 处理创建文件信息及事件 */ private function createFile($path, $parentFileId, $hadFileDelete) { $fileName = MUtils::get_basename($path); // 组装对象信息 $fileDetail = array(); $fileDetail["file_create_time"] = time(); $fileDetail["file_update_time"] = time(); $fileDetail["file_name"] = $fileName; $fileDetail["file_path"] = $path; $fileDetail["file_size"] = 0; $fileDetail["file_type"] = MConst::OBJECT_TYPE_DIRECTORY; $fileDetail["parent_file_id"] = $parentFileId; $fileDetail["event_uuid"] = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID); $fileDetail["mime_type"] = NULL; // 保存文件元数据 if ($hadFileDelete) { $updates = array(); $updates["file_update_time"] = time(); $updates["is_deleted"] = intval(false); $updates["file_type"] = MConst::OBJECT_TYPE_DIRECTORY; $updates["event_uuid"] = $fileDetail["event_uuid"]; // 存在已被删除的数据,只需更新 $retValue = MiniFile::getInstance()->updateByPath($path, $updates); } else { $pathArr = explode("/", $path); // if($this->_user_id!=$pathArr[1]){ // $this->_user_id = $pathArr[1]; // } // 不存在数据,添加 $retValue = MiniFile::getInstance()->create($fileDetail, $pathArr[1]); $user = Yii::app()->session["user"]; if ((int) $pathArr[1] !== (int) $user['user_id']) { //只有当被共享者在共享目录下创建目录时,才会记录create_id MFileMetas::createFileMeta($path, 'create_id', $user['user_id']); } } if ($retValue === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } // 保存事件 $event_action = MConst::CREATE_DIRECTORY; $retValue = MiniEvent::getInstance()->createEvent($this->_user_id, $this->_user_device_id, $event_action, $fileDetail["file_path"], $fileDetail["file_path"], $fileDetail["event_uuid"], $this->share_filter->type); if ($retValue === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } // 为每个共享用户创建事件 if (isset($this->share_filter)) { $this->share_filter->handlerAction($event_action, $this->_user_device_id, $fileDetail["file_path"], $fileDetail["file_path"]); } return $fileDetail; }