Ejemplo n.º 1
0
 /**
  * 控制器执行主逻辑函数, 复制文件或者文件夹
  */
 public function invoke($uri = null)
 {
     $this->setAction(MConst::COPY);
     $this->beforeInvoke();
     $this->beforecheck();
     $user = MUserManager::getInstance()->getCurrentUser();
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $params = $_REQUEST;
     // 检查参数
     if (isset($params) === false) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 11'), MConst::HTTP_CODE_400);
     }
     // 文件大小格式化参数
     $locale = "bytes";
     if (isset($params["root"]) === false || isset($params["from_path"]) === false || isset($params["to_path"]) === false) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 12'), MConst::HTTP_CODE_400);
     }
     if (isset($params["locale"])) {
         $locale = $params["locale"];
     }
     $root = $params["root"];
     $this->_from_path = $params["from_path"];
     $this->_to_path = $params["to_path"];
     if ($params['is_root']) {
         $this->_to_path = '/' . $user['id'] . $this->_to_path;
     }
     //
     // 检查文件名是否有效
     //
     $isInvalid = MUtils::checkNameInvalid(MUtils::get_basename($this->_to_path));
     if ($isInvalid) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 13'), MConst::HTTP_CODE_400);
     }
     //
     // 转换路径分隔符,便于以后跨平台,如:将 "\"=>"/"
     //
     $this->_from_path = MUtils::convertStandardPath($this->_from_path);
     $this->_to_path = MUtils::convertStandardPath($this->_to_path);
     if ($this->_from_path == "/" || $this->_to_path == "/" || $this->_from_path === false || $this->_to_path === false) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 14'), MConst::HTTP_CODE_400);
     }
     if ($this->_to_path[strlen($this->_to_path) - 1] == "/") {
         // 目标文件无效,403 error
         throw new Exception(Yii::t('api', 'The file or folder name is invalid'), MConst::HTTP_CODE_403);
     }
     //
     // 检查共享
     //
     $from_share_filter = MSharesFilter::init();
     $this->to_share_filter = MSharesFilter::init();
     // 当从共享目录拷贝到其他目录时,源目录用户id设置为共享用户id
     //        if ($from_share_filter->handlerCheck($this->owner, $this->_from_path)) {
     //            $this->master = $from_share_filter->master;
     //            $this->_from_path = $from_share_filter->_path;
     //        }
     //
     //        // 当拷贝到共享目录的时候,目标目录的用户id设置为共享用户id
     //        if ($this->to_share_filter->handlerCheck($this->_user_id, $this->_to_path)) {
     //            $this->_user_id = $this->to_share_filter->master;
     //            $this->user_nick      = $this->to_share_filter->master_nick;
     //            $this->_to_path = $this->to_share_filter->_path;
     //        }
     //        if($this->_from_shared_path){
     //            $this->_from_path =  $this->_from_shared_path;
     //        }else{
     //            $this->_from_path = "/".$this->master.$this->_from_path;
     //        }
     //        if($this->_to_shared_path){
     //            $this->_to_path =  $this->_to_shared_path;
     //        }else{
     //            $this->_to_path   = "/".$this->_user_id.$this->_to_path;
     //        }
     //
     // 检查目标路径是否在复制目录下
     //
     if (strpos($this->_to_path, $this->_from_path . "/") === 0) {
         throw new MFileopsException(Yii::t('api', 'Can not be copied to the subdirectory'), MConst::HTTP_CODE_403);
     }
     $check = CUtils::removeUserFromPath($this->_to_path);
     if (empty($check) || $check == '/') {
         throw new MFileopsException(Yii::t('api', 'Can not be copied to the error directory'), MConst::HTTP_CODE_403);
     }
     //
     // 检查目标路径文件是否存在
     //
     $queryToPathDbFile = MFiles::queryAllFilesByPath($this->_to_path);
     $isUpdate = false;
     if ($queryToPathDbFile) {
         if ($queryToPathDbFile[0]["is_deleted"] == false) {
             // 已经存在,403 error
             throw new MFileopsException(Yii::t('api', 'There is already a item at the given destination'), MConst::HTTP_CODE_403);
         }
         $isUpdate = true;
     }
     //
     // 查询其信息
     //
     $fileName = MUtils::get_basename($this->_to_path);
     $queryFromPathDbFile = MFiles::queryFilesByPath($this->_from_path);
     $queryToPathDbFile = MFiles::queryFilesByPath(dirname($this->_to_path));
     if ($queryFromPathDbFile === false || empty($queryFromPathDbFile)) {
         throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404);
     }
     $fromArr = explode('/', $this->_from_path);
     $fromId = $fromArr[1];
     if ($params['root']) {
         $toArr = explode('/', $this->_to_path);
         $toId = $toArr[1];
     } else {
         $toId = $user['id'];
     }
     //权限判断
     //当属于共享目录时才进行权限控制(源路径)
     $fromFile = MiniFile::getInstance()->getByFilePath($this->_from_path);
     if ($fromId != $user['id']) {
         //判断文件重命名是否有权限操作
         $permissionArr = UserPermissionBiz::getInstance()->getPermission($this->_from_path, $user['id']);
         if (!isset($permissionArr)) {
             $permission = MConst::SUPREME_PERMISSION;
         } else {
             $permission = $permissionArr['permission'];
         }
         $miniPermission = new MiniPermission($permission);
         $canCopy = $miniPermission->canCopy($fromFile['file_type']);
         if (!$canCopy) {
             throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409);
         }
     }
     $isSharedPath = false;
     //主要用于判断是否为被共享文件
     //目标路径
     if ($toId != $user['id']) {
         $isSharedPath = true;
         //拷贝到 (目标路径的创建权限)  的判断
         //            if ($query_from_path_db_file[0]["file_type"] == 0){  //文件
         //                $this->to_share_filter->hasPermissionExecute($this->_to_path, MPrivilege::FILE_CREATE);
         //            } else {                                           //文件夹
         //                $this->to_share_filter->hasPermissionExecute($this->_to_path, MPrivilege::FOLDER_CREATE);
         //            }
     } else {
         $model = new GeneralFolderPermissionBiz($this->_to_path);
         if ($model->isParentShared($this->_to_path)) {
             //如果是父目录被共享
             $isSharedPath = true;
         }
     }
     if ($isSharedPath) {
         $permissionArr = UserPermissionBiz::getInstance()->getPermission(dirname($this->_to_path), $user['id']);
         if (!isset($permissionArr)) {
             $permission = MConst::SUPREME_PERMISSION;
         } else {
             $permission = $permissionArr['permission'];
             $privilegeModel = new PrivilegeBiz();
             $this->to_share_filter->slaves = $privilegeModel->getSlaveIdsByPath($permissionArr['share_root_path']);
             $this->to_share_filter->is_shared = true;
         }
         $miniPermission = new MiniPermission($permission);
         $toFile = MiniFile::getInstance()->getByFilePath(dirname($this->_to_path));
         $canCopy = $miniPermission->canCopy($toFile['file_type']);
         if (!$canCopy) {
             throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409);
         }
     }
     //
     // 查询目标路径父目录信息
     //
     $parentPath = dirname($this->_to_path);
     $createFolder = new MCreateFolderController();
     $createFolder->_user_device_id = $this->_user_device_id;
     $createFolder->_user_id = $this->_user_id;
     $createFolder->share_filter = $this->to_share_filter;
     $parentFileId = $createFolder->handlerParentFolder($parentPath);
     //
     // 组装对象信息
     //
     $fileDetail = new MFiles();
     $fileDetail->file_name = $fileName;
     $fileDetail->file_path = $this->_to_path;
     $this->assembleFileDetail($fileName, $parentFileId, $fileDetail, $queryFromPathDbFile[0]);
     //
     // 首先处理复制根目录操作
     //
     if ($isUpdate) {
         $fileDetail->event_uuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID);
         $updates = array();
         $updates["file_update_time"] = time();
         $updates["is_deleted"] = intval(false);
         $updates["event_uuid"] = $fileDetail->event_uuid;
         $updates["file_type"] = $fileDetail->file_type;
         $retValue = MFiles::updateFileDetailByPath($this->_to_path, $updates);
     } else {
         $retValue = MFiles::CreateFileDetail($fileDetail, $this->_user_id);
     }
     if ($retValue === false) {
         throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
     }
     //
     // 更新版本信息
     //
     $this->updateVerRef(array($fileDetail));
     $retValue = MiniEvent::getInstance()->createEvent($this->_user_id, $this->_user_device_id, $fileDetail->event_action, $fileDetail->file_path, $fileDetail->context, $fileDetail->event_uuid, $this->to_share_filter->type);
     if ($retValue === false) {
         throw new MFileopsException(Yii::t('api', 'There is already a item at the given destination'), MConst::HTTP_CODE_500);
     }
     $context = $fileDetail->context;
     if ($fileDetail->file_type == 0) {
         $context = unserialize($context);
     }
     $this->to_share_filter->handlerAction($fileDetail->event_action, $this->_user_device_id, $fileDetail->file_path, $context);
     //
     // 判断操作的是文件夹,还是文件
     //
     $createArray = array();
     $queryDbFile = MFiles::queryFilesByPath($this->_to_path);
     //
     // 查询其复制目录路径id
     //
     if ($queryDbFile === false || empty($queryDbFile)) {
         throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404);
     }
     if ($fileDetail->file_type != MConst::OBJECT_TYPE_FILE) {
         $fileDetail->id = $queryDbFile[0]["id"];
         $fileDetail->file_size = $queryDbFile[0]["file_size"];
         $this->handlerChildrenFile($fileDetail);
         //
         // 处理版本信息
         //
         $moveController = new MMoveController();
         $moveController->versions = array();
         $createArray = $moveController->handleChildrenVersions($createArray, $this->_user_id, $this->user_nick, $this->_from_path, $this->_to_path, $queryToPathDbFile[0]["id"], $this->_user_device_name, $queryFromPathDbFile[0]["file_size"]);
         $this->versions = $moveController->versions;
     } else {
         $fileMeta = new MFileMetas();
         $fileMeta->version_id = $queryFromPathDbFile[0]["version_id"];
         //
         // 查询其版本
         //
         $fileVersion = MFileMetas::queryFileMeta($this->_to_path, MConst::VERSION);
         $fileMeta->is_add = false;
         if ($fileVersion) {
             $metaValue = MUtils::getFileVersions($this->_user_device_name, $fileDetail->file_size, $fileMeta->version_id, MConst::CREATE_FILE, $this->_user_id, $this->user_nick, $fileVersion[0]["meta_value"]);
         } else {
             $metaValue = MUtils::getFileVersions($this->_user_device_name, $fileDetail->file_size, $fileMeta->version_id, MConst::CREATE_FILE, $this->_user_id, $this->user_nick);
             $fileMeta->is_add = true;
             // 不存在记录,需要添加
         }
         $fileMeta->meta_value = $metaValue;
         $fileMeta->file_path = $this->_to_path;
         $createArray[$queryFromPathDbFile[0]["file_path"]] = $fileMeta;
         //
         // 添加到需要更新的版本ref
         //
         array_push($this->versions, $fileMeta->version_id);
     }
     //
     // 创建版本信息
     //
     MFileMetas::batchCreateFileMetas($createArray, MConst::VERSION);
     //        if ($ret === false)
     //        {
     //            throw new MFileopsException(
     //                                        Yii::t('api','Internal Server Error'),
     //                                        MConst::HTTP_CODE_500);
     //        }
     //
     // 更新版本
     //
     foreach ($createArray as $key => $fileMeta) {
         if ($fileMeta->is_add === true) {
             // 不存在记录,不需要更新
             continue;
         }
         MFileMetas::updateFileMeta($fileMeta->file_path, MConst::VERSION, $fileMeta->meta_value);
     }
     //
     // 处理不同端,不同返回值
     //
     if (MUserManager::getInstance()->isWeb() === true) {
         $this->buildWebResponse();
         return;
     }
     $response = array();
     $isDir = true;
     if ($queryDbFile[0]["file_type"] == MConst::OBJECT_TYPE_FILE) {
         // TODO
         $mimeType = "text/plain";
         $response["mime_type"] = $mimeType;
         $isDir = false;
         $response["thumb_exists"] = MUtils::isExistThumbnail($mimeType, (int) $queryDbFile[0]["file_size"]);
     }
     $size = $queryDbFile[0]["file_size"];
     $response["size"] = MUtils::getSizeByLocale($locale, $size);
     $response["bytes"] = intval($size);
     $pathInfo = MUtils::pathinfo_utf($this->_to_path);
     $pathInfoOut = MUtils::pathinfo_utf($this->to_share_filter->src_path);
     $path = MUtils::convertStandardPath($pathInfoOut['dirname'] . "/" . $pathInfo['basename']);
     $response["path"] = $path;
     $response["root"] = $root;
     $response["is_dir"] = $isDir;
     $response["rev"] = strval($queryDbFile[0]["version_id"]);
     $response["revision"] = intval($queryDbFile[0]["version_id"]);
     $response["modified"] = MUtils::formatIntTime($queryDbFile[0]["file_update_time"]);
     //
     // 如果标记为不输出结果的话,直接返回$response
     //
     if (!$this->isOutput) {
         return $response;
     }
     echo json_encode($response);
 }
Ejemplo n.º 2
0
 /**
  * 控制器执行主逻辑函数, 处理移动文件或者文件夹
  *
  * @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);
 }
Ejemplo n.º 3
0
 /**
  * 创建文件详情
  * @since 1.0.7
  */
 public function saveFileMeta()
 {
     $this->share_filter = MSharesFilter::init();
     $parentPath = $this->parent_path;
     $currentUserId = $this->user_id;
     $this->currentUserId = $currentUserId;
     //echo("5555");exit;
     // 检查文件名是否有效
     if (MUtils::checkNameInvalid($this->file_name)) {
         throw new MFileopsException(Yii::t('api', 'bad request' . "6"), MConst::HTTP_CODE_400);
     }
     // 获取父目录信息
     $parentCheckHandler = new MCreateFolderController();
     $parentCheckHandler->_user_id = $this->user_id;
     $parentCheckHandler->_user_device_id = $this->user_device_id;
     //        $parent_check_handler->share_filter    = $this->share_filter;
     if (empty($parentPath) || $parentPath == "/" || $parentPath == "/" . $currentUserId) {
         $this->parent_file_id = 0;
     } else {
         $this->parent_file_id = $parentCheckHandler->handlerParentFolder($this->parent_path);
     }
     $isSharedPath = false;
     //主要用于判断是否为被共享文件
     if (empty($parentPath) || $parentPath == "/") {
         //说明此时在根目录下创建文件,有创建权限
         $canCreateFile = true;
         $this->path = "/" . $currentUserId . $this->path;
         $parentPath = "/";
     } else {
         //非根目录情况
         $canCreateFile = false;
         $arr = explode('/', $parentPath);
         $masterId = $arr[1];
         if ($masterId == $currentUserId) {
             //自己目录下皆有创建权限
             $model = new GeneralFolderPermissionBiz($parentPath);
             if ($model->isParentShared($parentPath)) {
                 //如果是父目录被共享
                 $isSharedPath = true;
             }
         } else {
             //别人共享目录下判断有无创建权限
             $this->user_id = $masterId;
             $isSharedPath = true;
         }
         if ($isSharedPath) {
             $permissionArr = UserPermissionBiz::getInstance()->getPermission($parentPath, $currentUserId);
             if (!empty($permissionArr)) {
                 $privilegeModel = new PrivilegeBiz();
                 $this->share_filter->slaves = $privilegeModel->getSlaveIdsByPath($permissionArr['share_root_path']);
                 $this->share_filter->is_shared = true;
                 $permission = $permissionArr['permission'];
                 $create_file_num = substr($permission, 4, 1);
                 if ($create_file_num == 1) {
                     $canCreateFile = true;
                 }
             } else {
                 $canCreateFile = true;
             }
         } else {
             $canCreateFile = true;
         }
     }
     // 保存到数据库中的地址
     $this->file_path = $this->path;
     // 从数据库中获取路径对应的文件,未删除的
     //
     $fileDetail = MFiles::queryFilesByPath($this->file_path);
     $this->create_file = false;
     if ($fileDetail == false || count($fileDetail) == 0) {
         // 创建文件
         $this->create_file = true;
         $fileDetail = new MFiles();
     } else {
         // 文件存在判断为修改文件(如果按照正常逻辑)
         $fileDetail = MFiles::exchange2Object($fileDetail);
         //
         // 判断指向的是未删除的非文件,否则返回错误
         //
         if ($fileDetail->file_type != MConst::OBJECT_TYPE_FILE) {
             throw new MFilesException(Yii::t('api', "There is already a folder at the given destination"), MConst::HTTP_CODE_403);
         }
     }
     $this->modifyFile($fileDetail);
     if (isset($fileDetail->is_deleted)) {
         $this->spaceFilter($this->size - $fileDetail->file_size);
         // 过滤器,空间大小计算
     }
     if (isset($fileDetail->event_uuid)) {
         $this->event_uuid = $fileDetail->event_uuid;
     }
     if ($canCreateFile == false) {
         throw new MFilesException(Yii::t('api', "No Permission"), MConst::HTTP_CODE_409);
     }
     $this->conflictFile();
     $this->renameFile();
     $this->createFile($fileDetail);
     $this->success = true;
     //
     // 创建成功为每个用户
     //
     if ($this->create_event) {
         $path = MUtils::convertStandardPath($this->parent_path . '/' . $this->file_name);
         $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);
         //            $this->share_filter->handlerAction($this->action, $this->user_device_id, $path, $this->context);
     }
     // 异步文档转换
     do_action('file_upload_after', array("signature" => $this->file_hash, "file_name" => $this->file_name));
 }