示例#1
0
 /**
  * 查找该路径下所有子文件:文件存入数组
  * @param string $parent_file_id 父目录id
  * @return mixed $value 返回最终需要执行完的结果
  */
 public function findAllChildrenFiles($parent_file_id, $user_nick, $from_path, $to_path, $file_array, $device_name, $file_size)
 {
     $query_db_files = MFiles::queryChildrenFilesByParentFileID($parent_file_id);
     if ($query_db_files === false) {
         return false;
     }
     foreach ($query_db_files as $key => $db_file) {
         if ($db_file["file_type"] != MConst::OBJECT_TYPE_FILE) {
             $file_array = $this->findAllChildrenFiles($db_file["id"], $user_nick, $from_path, $to_path, $file_array, $device_name, $file_size);
             continue;
         }
         //
         // 转换路径
         //
         $file_path = $db_file["file_path"];
         //
         // 文件存入数组
         //
         $file = new MFiles();
         $file->file_path = $file_path;
         $file->version_id = $db_file["version_id"];
         $meta_value = MUtils::getFileVersions($device_name, $db_file['file_size'], $file->version_id, MConst::CREATE_FILE, $db_file["user_id"], $user_nick);
         $file->meta_value = $meta_value;
         $file->is_add = true;
         // 记录是否需要添加
         $file_array[$file_path] = $file;
     }
     return $file_array;
 }
示例#2
0
 /**
  * 处理添加当前文件记录的版本
  * @param string $file_path
  * @param int $version_id
  * @param string $user_nick
  */
 private function handleFileMeta($file_path, $version_id, $user_id, $user_nick, $deviceName, $fileSize)
 {
     //
     // 查询之前的版本
     //
     $file_meta = MFileMetas::queryFileMeta($file_path, MConst::VERSION);
     if ($file_meta) {
         $meta_value = MUtils::getFileVersions($deviceName, $fileSize, $version_id, MConst::DELETE, $user_id, $user_nick, $file_meta[0]["meta_value"]);
         $ret = MFileMetas::updateFileMeta($file_meta[0]["file_path"], MConst::VERSION, $meta_value);
     } else {
         $meta_value = MUtils::getFileVersions($deviceName, $fileSize, $version_id, MConst::DELETE, $user_id, $user_nick);
         $ret = MFileMetas::createFileMeta($file_path, MConst::VERSION, $meta_value);
     }
     return $ret;
 }
示例#3
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);
 }
示例#4
0
 /**
  * 创建文件
  * @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);
     }
 }