/** * 整个会话期内只读一次用户最大事件ID * @param $userId */ public function getMaxIdByUser2($userId) { if ($this->hasCache2 === true && $this->userMaxEventId !== NULL) { Yii::trace("cache 2 get userMaxEventId userId:" . $userId, "miniyun.cache2"); return $this->userMaxEventId; } $this->userMaxEventId = MiniEvent::getInstance()->getMaxIdByUser($userId); return $this->userMaxEventId; }
/** * 获取events数据 */ public function getList($path, $time, $deviceUuid, $pageSize, $currentPage) { $user = $this->user; $userId = $user['id']; $time = $this->getTime($time); if ($path != "") { $path = MiniUtil::joinPath($path); } $total = MiniEvent::getInstance()->getTotal($path, $time, $userId, $deviceUuid); $totalPage = ceil($total / $pageSize); $events = MiniEvent::getInstance()->getByCondition($path, $userId, $time, $deviceUuid, $pageSize, ($currentPage - 1) * $pageSize); $itemList = array(); $data = array(); foreach ($events as $event) { $item = array(); $device = MiniUserDevice::getInstance()->getUserDevice($event['user_device_id']); $item['create_user_id'] = $device['user_id']; $item['file_path'] = MiniUtil::getRelativePath($event['file_path']); $item['action'] = $event['action']; $item['user_name'] = $user['user_name']; $item['user_device_type'] = $device['user_device_type']; if ($device['user_id'] == $userId) { $item['user_self'] = true; } else { $item['user_self'] = false; $user = MiniUser::getInstance()->getById($device['user_id']); $userMetas = MiniUserMeta::getInstance()->getUserMetas($device['user_id']); if (isset($userMetas['nick'])) { $item['user_name'] = $userMetas['nick']; } else { $item['user_name'] = $user['user_name']; } } $item['created_at'] = MiniUtil::formatTime(strtotime($event['created_at'])); $item['user_device_name'] = $device['user_device_name']; $item['context'] = MiniUtil::getRelativePath($event['context']); $item['device_uuid'] = $device['user_device_uuid']; if ($event['action'] == 2) { //判断是否是重命名还是创建 $fromParent = CUtils::pathinfo_utf($event['file_path']); $toParent = CUtils::pathinfo_utf($event['context']); if ($fromParent['dirname'] == $toParent['dirname']) { $item['action'] = MConst::RENAME; } } $itemList[] = $item; } $data['events'] = $itemList; $data['totalPage'] = $totalPage; return $data; }
/** * 清除过期事件 */ public function handleCleanEvents($limit) { $sql_str = "select user_id, min(meta_value) as min from "; $sql_str .= UserDeviceMeta::model()->tableName() . " where meta_name = 'event_id' group by user_id,meta_name "; $sql = Yii::app()->db->createCommand($sql_str); $user_devices_list = $sql->queryAll(); foreach ($user_devices_list as $val) { $e_user_id = $val['user_id']; //用户id $event_id = $val['min']; //事件id $events = MiniEvent::getInstance()->queryAllbyCondition($e_user_id, $event_id, $limit); foreach ($events as $event) { $event->delete(); } } }
/** * * 执行文件夹删除 * @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'); } } }
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 int $userId */ public function deleteUser($userId) { $user = User::model()->findByPk($userId); if (isset($user)) { //管理员账户不能被删除 if ($user->user_name == 'admin' || $userId == 1) { return; } //清理Cache $this->cleanCache($userId); //删除用户元数据信息 MiniUserMeta::getInstance()->deleteMeta($userId); //删除用户权限数据 MiniUserPrivilege::getInstance()->deletePrivilege($userId); //删除用户设备信息 MiniUserDevice::getInstance()->deleteDeviceByUser($userId); //把用户所有的事件信息删除 MiniEvent::getInstance()->deleteByUserId($userId); //把userId资源暂存到Options表中 $this->temporary2Option($userId); //删除自己,这里不能修改为sql模式,因为用户ID在删除的时候,自动将ID记录到了Options表中 $user->delete(); } }
/** * 创建文件 * @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; }
/** * * 取消共享文件夹 * * @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)); } } }
/** * 控制器执行主逻辑函数 * * @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); }
/** * * 将删除的文件(夹)还原 * * @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); }
/** * 创建事件 */ 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 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(); $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 invoke($uri = null) { $this->setAction(MConst::DELETE); // 调用父类初始化函数,注册自定义的异常和错误处理逻辑 parent::init(); $params = $_REQUEST; // 检查参数 if (isset($params) === false) { throw new MFileopsException(Yii::t('api', 'Bad Request 8'), MConst::HTTP_CODE_400); } // // 获取用户数据,如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"]; // 文件大小格式化参数 $locale = "bytes"; if (isset($params["locale"])) { $locale = $params["locale"]; } if (isset($params["root"]) === false || isset($params["path"]) === false) { throw new MFileopsException(Yii::t('api', 'Bad Request 9'), MConst::HTTP_CODE_400); } $root = $params["root"]; $path = $params["path"]; $isDir = $params["is_dir"]; $pathArr = explode('/', $path); if ($path == '/' || empty($pathArr[2]) || empty($pathArr[1])) { return; } if ($isDir) { //避免人为添加删除目录 $arr = explode('/', $path); $isRoot = false; $isMine = false; if (count($arr) == 3) { $isRoot = true; } $fileOwnerId = $arr[1]; $currentUserId = $this->_user_id; if ($fileOwnerId == $currentUserId) { $isMine = true; } if ($isRoot && !$isMine) { //如果是在根目录下且不是自己的目录 则后台控制不准取消共享 throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_409); } } // // 转换路径分隔符,便于以后跨平台,如:将 "\"=>"/" // $path = MUtils::convertStandardPath($path); if ($path == "" || $path == "/" || $path === false) { throw new MFileopsException(Yii::t('api', 'Bad request 10'), MConst::HTTP_CODE_400); } // 检查是否是共享目录 $share_filter = MSharesFilter::init(); if ($share_filter->handlerCheck($this->_user_id, $path)) { $this->_user_id = $share_filter->master; $path = $share_filter->_path; } // // 如果删除的是共享目录,则转到ShareManager处理 // if ($share_filter->_is_shared_path && $share_filter->operator != $share_filter->master) { $file = MFiles::queryFilesByPath("/" . $share_filter->operator . $share_filter->src_path); if (!$file) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } $id = $file[0]["id"]; $handler = new ShareManager(); $handler->_userId = $share_filter->operator; $handler->_id = $id; try { $handler->invoke(ShareManager::CANCEL_SHARED); } catch (Exception $e) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } // 输出返回值 $path = MUtils::convertStandardPath($share_filter->src_path); $this->buildResult($root, $path, $handler->_file["version_id"], $handler->_file["file_update_time"], true); return; } if ($share_filter->_is_shared_path && $share_filter->operator != $share_filter->master) { throw new MException(Yii::t('api', 'You do not have permission to perform the delete operation.'), MConst::HTTP_CODE_409); } // // 组装对象信息 // $file_name = MUtils::get_basename($path); $file_detail = new MFiles(); $file_detail->file_name = $file_name; $file_detail->file_path = $path; // // 查询其目录信息,是否存在 // $query_db_file = MFiles::queryFilesByPath($file_detail->file_path); //数据已不存在 if (count($query_db_file) <= 0) { throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404); } $data = array("obj" => $this, "share_filter" => $share_filter, "query_db_file" => $query_db_file[0]); //在共享文件夹中进行删除权限判断 if ($share_filter->is_shared && $query_db_file[0]["file_type"] != MConst::OBJECT_TYPE_BESHARED) { if ($query_db_file[0]["file_type"] == 0) { //文件删除 $share_filter->hasPermissionExecute($query_db_file[0]["file_path"], MPrivilege::FILE_DELETE); } else { //文件夹删除 $share_filter->hasPermissionExecute($query_db_file[0]["file_path"], MPrivilege::FOLDER_DELETE); } } // // 可以删除包含子文件的目录 // 检查其是否为文件夹 // $files = array(); $file_detail->is_dir = false; $file_detail->id = $query_db_file[0]["id"]; $file_detail->file_size = $query_db_file[0]["file_size"]; $file_detail->file_type = $query_db_file[0]["file_type"]; if ($query_db_file[0]["file_type"] > MConst::OBJECT_TYPE_FILE) { $file_detail->is_dir = true; $files = $this->handleChildrenFile($file_detail->file_path, $files); } else { // 处理加入版本历史 $this->handleFileMeta($file_detail->file_path, $query_db_file[0]["version_id"], $this->_user_id, $user_nick, $this->_user_device_name, $file_detail->file_size); } $isSharedPath = false; $pathArr = explode('/', $file_detail->file_path); $masterId = $pathArr[1]; if ($masterId != $this->_user_id) { $isSharedPath = true; } else { $model = new GeneralFolderPermissionBiz($file_detail->file_path); if ($model->isParentShared($file_detail->file_path)) { //如果是父目录被共享 $isSharedPath = true; } } if ($isSharedPath) { $permissionArr = UserPermissionBiz::getInstance()->getPermission($file_detail->file_path, $user["user_id"]); $permission = $permissionArr['permission']; if (!empty($permission)) { $privilegeModel = new PrivilegeBiz(); $share_filter->slaves = $privilegeModel->getSlaveIdsByPath($permissionArr['share_root_path']); $share_filter->is_shared = true; if ($file_detail->file_type == 0) { //删除文件 $can_file_delete = substr($permission, 7, 1); if ($can_file_delete == 0) { throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409); } } if ($file_detail->file_type == 1 || $file_detail->file_type == 2 || $file_detail->file_type == 4) { $can_folder_delete = substr($permission, 3, 1); if ($can_folder_delete == 0) { throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409); } } } } // // 更新文件元数据的为删除数据 // $this->assembleFileDetail($file_detail, $query_db_file[0]); $ret_value = MFiles::updateRemoveFileDetail($file_detail); if ($ret_value === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } // // 将删除目录加入数组 // array_push($files, $file_detail); // // 保存事件 // $ret_value = MiniEvent::getInstance()->createEvents($this->_user_id, $user_device_id, $files, $share_filter->type); if ($ret_value === false) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } // // // if ($share_filter->is_shared) { foreach ($files as $file) { $share_filter->handlerAction($file->event_action, $user_device_id, $file->from_path, $file->context); } } // // 删除共享目录(删除共享目录,对应的权限也一起删除) // //首先判断用户有无删除权限 $userPrivilegeList = MiniUserPrivilege::getInstance()->getPrivilegeList($file_detail->file_path); $groupPrivilegeList = MiniGroupPrivilege::getInstance()->getPrivilegeList($file_detail->file_path); if (!empty($userPrivilegeList)) { MiniUserPrivilege::getInstance()->deleteByFilePath($file_detail->file_path); } if (!empty($groupPrivilegeList)) { MiniGroupPrivilege::getInstance()->deleteByFilePath($file_detail->file_path); } //并且将file_type改为1 if ($file_detail->file_type == 0) { MiniFile::getInstance()->togetherShareFile($file_detail->file_path, Mconst::OBJECT_TYPE_FILE); } else { MiniFile::getInstance()->togetherShareFile($file_detail->file_path, Mconst::OBJECT_TYPE_DIRECTORY); } if ($filter !== true && $share_filter->_is_shared_path && $share_filter->operator == $share_filter->master) { $file = MFiles::queryFilesByPath("/" . $share_filter->operator . $path, true); if (!$file) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } $id = $file[0]["id"]; $handler = new ShareManager(); $handler->_userId = $share_filter->operator; $handler->_id = $id; try { $handler->invoke(ShareManager::CANCEL_SHARED); } catch (Exception $e) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } } // 如果彻底删除,则调用回收站 if ($this->completely_remove) { $trash = new Trash(); $trash->_userId = $this->_user_id; $trash->fromIds = $file_detail->id; try { $trash->invoke(Trash::DELETE); } catch (Exception $e) { throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500); } //执行的额外操作 $this->extend($share_filter, $query_db_file, $file_detail); return; } $path = CUtils::removeUserFromPath($query_db_file[0]["file_path"]); $path_info = MUtils::pathinfo_utf($path); $path_info_out = MUtils::pathinfo_utf($share_filter->src_path); $path = MUtils::convertStandardPath($path_info_out['dirname'] . "/" . $path_info['basename']); //执行的额外操作 $this->extend($share_filter, $query_db_file, $file_detail); $this->buildResult($root, $path, $query_db_file[0]["version_id"], $query_db_file[0]["file_update_time"], $file_detail->is_dir); }
/** * 全部还原 */ 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 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; }
/** * * 删除用户相关信息 * @userIds 用户列表{1,2,3,4,5}这样的格式 */ public function deleteUsers($userIds) { if ($userIds != '' && strlen($userIds) > 0) { $ids = explode(',', $userIds); $userFile = new UserFile(); foreach ($ids as $id) { // 删除用户共享文件 $userFile->deleteSharedFolders($id); //删除所有标签信息 Tag::model()->deleteUserAllTag($id); //删除我的最爱文件 FileStar::model()->deleteUserAllStar($id); } //删除用户的文件信息 $userFile->deleteUserFile($userIds); //删除用户的群组部门关系 MiniUserGroupRelation::getInstance()->deleteUserRelation($userIds); //删除用户的事件信息 MiniEvent::getInstance()->deleteByIds($userIds); //删除用户Meta以及用户自己 foreach ($ids as $id) { //删除用户自身 MiniUser::getInstance()->deleteUser($id); } } }
/** * * 根据路径创建目录 */ 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; }