コード例 #1
0
 /**
  * 控制器执行主逻辑函数, 处理删除文件或者文件夹
  */
 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);
 }
コード例 #2
0
ファイル: Move.php プロジェクト: youngsun45/miniyun
 /**
  * move 执行入口
  * @param bool $isPath - Use path to move if true ,or use id. 
  */
 public function invoke($isPath = true)
 {
     // 初始化入口
     $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"];
     $this->master = $this->_userId;
     //
     // 空间检查
     //
     $this->handleSpace();
     if ($isPath) {
         $this->fromPath = CUtils::convertStandardPath($this->fromPath);
         $this->toPath = CUtils::convertStandardPath($this->toPath);
         $this->initByPath();
     } else {
         $this->initById();
     }
     //
     // 判断是否是共享
     //
     $from_share_filter = MSharesFilter::init();
     $from_share_filter->handlerCheckByFile($this->_userId, $this->from);
     $this->rename = false;
     if ($from_share_filter->_is_shared_path && $this->toParent['id'] == 0) {
         $this->rename = true;
     } elseif ($from_share_filter->is_shared) {
         $this->master = $from_share_filter->master;
         $this->fromPath = '/' . $this->master . $from_share_filter->_path;
         $this->from = UserFile::model()->findByAttributes(array('is_deleted' => 0, 'file_path' => $this->fromPath));
         if (!$this->from) {
             throw new ApiException("Not found");
         }
     }
     //
     // 检查移动原路径与目标路径是否一致,一致则返回成功
     //
     if ($this->fromPath === $this->toPath) {
         $this->handleResult(false, 0, "已存在同名的文件");
         return;
     }
     //
     // 检查是否移动到其子目录下
     //
     if (strpos($this->toPath, $this->fromPath . "/") === 0) {
         $this->result["msg"] = "不能移动到子目录";
         return;
     }
     if ($this->toPath == "/{$this->_userId}" || $this->toPath == "/{$this->_userId}/") {
         $this->result["msg"] = "目标目录不存在";
         return;
     }
     //
     // 命名检查
     //
     if (CUtils::checkNameInvalid($this->toPath) != 0 || CUtils::checkNameInvalid($this->toPath) != 0) {
         $this->result["msg"] = "命名不能包含下列字符串: ^|?*\\<\":>";
         return;
     }
     //
     // 存在同名的则,拒绝
     //
     $target = UserFile::model()->findByAttributes(array("user_id" => $this->_userId, "file_path" => $this->toPath, "is_deleted" => 0));
     if ($target) {
         $this->handleResult(false, 0, "已存在同名的文件");
         return;
     }
     $index = strlen("/{$this->from['user_id']}");
     $fpath = substr_replace($this->fromPath, "", 0, $index);
     $index = strlen("/{$this->toParent['user_id']}");
     $tpath = substr_replace($this->toPath, "", 0, $index);
     //
     // 检查移动方式
     //
     if ($isPath == false && $this->rename == false && ($from_share_filter->handlerCheckMove($from_share_filter->master, $this->to_share_filter->master, $fpath, $tpath) || $this->to_share_filter->is_shared)) {
         //
         // 先copy再删除,如果是移动共享文件夹则只copy,再执行shareManager取消共享
         //
         $copy = new Copy();
         $copy->_userId = $this->_userId;
         $copy->toId = $this->toParent['id'];
         $copy->fromId = $this->from['id'];
         try {
             $copy->invoke(false);
         } catch (Exception $e) {
             $this->result["msg"] = "操作失败";
             return;
         }
         if ($this->from['file_type'] == 2 && $this->from['user_id'] != $this->to_share_filter->operator) {
             $file_meta = FileMeta::model()->findByAttributes(array('meta_key' => 'shared_folders', 'file_path' => $this->from['file_path']));
             if (!$file_meta) {
                 $this->result["msg"] = "操作失败";
                 return;
             }
             $meta_value = unserialize($file_meta['meta_value']);
             $slaves = $meta_value['slaves'];
             $this->from = UserFile::model()->findByAttributes(array('file_path' => $slaves[$this->to_share_filter->operator], 'is_deleted' => 0));
             if (!$this->from) {
                 $this->result["msg"] = "操作失败";
                 return;
             }
         }
         $del = new Delete();
         $del->_userId = $this->from['user_id'];
         try {
             $del->invoke($this->from['id']);
             $trash = new Trash();
             $trash->_userId = $this->master;
             $trash->fromIds = $this->from['id'];
             $trash->invoke(Trash::DELETE);
         } catch (Exception $e) {
             $this->result["msg"] = "操作失败";
             return;
         }
         if ($copy->result['state'] && $del->result['state']) {
             $this->handleResult(true, 0, "操作成功");
             $this->result["data"][$this->fromId]["state"] = true;
         }
         return;
     }
     // 文件直接进行移动
     if ($this->from["file_type"] == 0) {
         $this->handleMoveFile($this->fromPath, $this->toPath);
     } else {
         // 文件夹涉及子对象
         $this->handleMoveFolder($this->fromPath, $this->toPath);
     }
     $this->handleResult(true, 0, "操作成功");
     $this->result["data"][$this->fromId]["state"] = true;
 }
コード例 #3
0
ファイル: MFilesCommon.php プロジェクト: youngsun45/miniyun
 /**
  * 创建文件
  * @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);
     }
 }