Example #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);
 }
Example #2
0
 /**
  * 处理返回值
  */
 public function buildResult($query_db_file, $to_path = null)
 {
     // 处理不同端,不同返回值
     if (MUserManager::getInstance()->isWeb() === true) {
         $this->buildWebResponse();
         return;
     }
     if ($this->isEcho === false) {
         return;
     }
     $is_dir = true;
     $size = $query_db_file["file_size"];
     $response = array();
     if ($query_db_file["file_type"] == MConst::OBJECT_TYPE_FILE) {
         // 根据文件名后缀判断mime type
         $mime_type = MiniUtil::getMimeType($query_db_file["file_name"]);
         $is_dir = false;
         $response["mime_type"] = $mime_type;
         $response["thumb_exists"] = MUtils::isExistThumbnail($mime_type, (int) $query_db_file["file_size"]);
     }
     // 去除/{user_id}
     $path = CUtils::removeUserFromPath($query_db_file["file_path"]);
     $response["size"] = MUtils::getSizeByLocale($this->_locale, $size);
     $response["is_deleted"] = false;
     $response["bytes"] = intval($size);
     $response["modified"] = MUtils::formatIntTime($query_db_file["file_update_time"]);
     if ($to_path) {
         $path = $to_path;
     } else {
         $path = $query_db_file["file_path"];
     }
     $path_info = MUtils::pathinfo_utf($path);
     $path_info_out = MUtils::pathinfo_utf($this->to_share_filter->src_path);
     $path = MUtils::convertStandardPath($path_info_out['dirname'] . "/" . $path_info['basename']);
     $response["path"] = $path;
     $response["root"] = $this->_root;
     $response["is_dir"] = $is_dir;
     $response["rev"] = strval($query_db_file["version_id"]);
     $response["revision"] = intval($query_db_file["version_id"]);
     // 增加操作返回事件编码
     $response["event_uuid"] = $query_db_file["event_uuid"];
     echo json_encode($response);
 }
 /**
  * 控制器执行主逻辑函数
  *
  * @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);
 }
Example #4
0
 /**
  * 处理返回值组装
  * @param string $root
  * @param string $path
  * @param integer $version_id
  * @param string $modified
  * @param boolean $is_dir
  */
 public function buildResult($root, $path, $version_id, $modified, $is_dir)
 {
     // 处理不同端,不同返回值
     if (MUserManager::getInstance()->isWeb() === true) {
         $this->buildWebResponse();
         return;
     }
     $response = array();
     $response["size"] = "0";
     $response["is_deleted"] = true;
     $response["bytes"] = 0;
     $response["thumb_exists"] = false;
     $response["path"] = $path;
     $response["root"] = $root;
     $response["is_dir"] = $is_dir;
     $response["rev"] = strval($version_id);
     $response["revision"] = intval($version_id);
     $response["modified"] = MUtils::formatIntTime($modified);
     if ($is_dir === false) {
         $response["mime_type"] = "text/plain";
     }
     if (!$this->isOutput) {
         return $response;
     }
     echo json_encode($response);
 }
 /**
  * 获取文件以前版本的mtadata
  * @param null $uri
  * @throws MException
  * @throws MFilesException
  */
 public function invoke($uri = null)
 {
     parent::init();
     // 解析url地址,获取root和path,path必须指向一个文件
     $urlManager = new MUrlManager();
     $path = $urlManager->parsePathFromUrl($uri);
     $root = $urlManager->parseRootFromUrl($uri);
     if ($path == false) {
         throw new MException(Yii::t('api', MConst::PATH_ERROR), MConst::HTTP_CODE_411);
     }
     $path = "/" . $path;
     // Default is 10. Max is 1,000.
     $revLimit = 10;
     if (isset($_REQUEST["rev_limit"]) != false) {
         $revLimit = $_REQUEST["rev_limit"];
     }
     $revLimit = $revLimit <= 1000 ? $revLimit : 1000;
     // 文件大小格式化参数
     $locale = "bytes";
     if (isset($_REQUEST["locale"])) {
         $locale = $_REQUEST["locale"];
     }
     // callback -
     // TODO 实现callback
     $callback = NULL;
     // 获取用户数据,如user_id
     $user = MUserManager::getInstance()->getCurrentUser();
     $device = MUserManager::getInstance()->getCurrentDevice();
     $userId = $user["user_id"];
     $userNick = $user["user_name"];
     $userDeviceId = $device["device_id"];
     //
     // 查询文件
     //
     $fileDetail = MFiles::queryAllFilesByPath("/" . $userId . $path);
     if ($fileDetail === false || count($fileDetail) == 0) {
         throw new MException(Yii::t('api', MConst::NOT_FOUND), MConst::HTTP_CODE_404);
     }
     // 判断文件类型,如不是文件则返回错误
     if ($fileDetail[0]["file_type"] != 0) {
         throw new MException(Yii::t('api', "Not Acceptable"), MConst::HTTP_CODE_406);
     }
     $fileMeta = MFileMetas::queryFileMeta("/" . $userId . $path, MConst::VERSION);
     if ($fileMeta == false || empty($fileMeta)) {
         throw new MFilesException(Yii::t("api", MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500);
     }
     //
     // 文件版本历史
     //
     $versions = unserialize($fileMeta[0]["meta_value"]);
     $count = 1;
     // 计数器
     //
     // 轮询
     //
     $response = array();
     foreach ($versions as $k => $v) {
         $var = array();
         $var["rev"] = strval($v["version_id"]);
         $var["revision"] = (int) $v["version_id"];
         $var["bytes"] = 0;
         $var["size"] = "0 bytes";
         $var["thumb_exists"] = false;
         $var["modified"] = MUtils::formatIntTime(microtime(true) * 10000);
         $var["mime_type"] = MConst::DEFAULT_FILE_MIME_TYPE;
         $var["path"] = $path;
         $var["is_dir"] = false;
         $var["root"] = $root;
         if ($v["type"] == MConst::DELETE) {
             $var["is_deleted"] = true;
         }
         //
         // 文件版本信息
         //
         $fileVersion = MiniVersion::getInstance()->getVersion($v["version_id"]);
         if ($fileVersion == null) {
             $var["is_deleted"] = true;
         } else {
             $var['hash'] = $fileVersion["file_signature"];
             $var["bytes"] = (int) $fileVersion["file_size"];
             $var["size"] = MUtils::getSizeByLocale($locale, $fileVersion["file_size"]);
             $var["thumb_exists"] = $this->isExistThumbnail($fileVersion["file_size"], $fileVersion["mime_type"]);
             $var["modified"] = MUtils::formatIntTime($fileVersion["updated_at"]);
             $var["mime_type"] = is_null($fileVersion["mime_type"]) ? $var["mime_type"] : $fileVersion["mime_type"];
         }
         array_push($response, $var);
         if ($count >= $revLimit) {
             break;
         }
         $count += 1;
     }
     echo json_encode($response);
 }
Example #6
0
 /**
  * 处理组装请求元数据
  *
  */
 private function assembleResponse($response, $file, $mimeType)
 {
     $filePath = $file["file_path"];
     $response["size"] = MUtils::getSizeByLocale($this->_locale, $file["file_size"]);
     $response["bytes"] = (int) $file["file_size"];
     $response["path"] = $filePath;
     $response["modified"] = MUtils::formatIntTime($file["file_update_time"]);
     $response["create_time"] = $file["file_create_time"];
     $response["update_time"] = $file["file_update_time"];
     $response["revision"] = intval($file["version_id"]);
     $response["rev"] = strval($file["version_id"]);
     $response["root"] = $this->_root;
     $response["hash"] = !empty($file["signature"]) ? $file["signature"] : "";
     $response["event"] = $file["event_uuid"];
     $response["sort"] = (int) $file["sort"];
     //外链Key
     $response["share_key"] = $file["share_key"];
     $response['is_dir'] = false;
     $permission = UserPermissionBiz::getInstance()->getPermission($filePath, $this->_user_id);
     if (!empty($permission)) {
         if (isset($permission['children_shared'])) {
             $response['children_shared'] = true;
         } else {
             $response['share'] = $permission;
         }
         if (empty($permission['permission'])) {
             //如果没有$permission['permission']权限 则直接返回null
             return null;
         }
     }
     if ($file["file_type"] == MConst::OBJECT_TYPE_FILE) {
         $response["hash"] = $file["signature"];
         $mimeType = MiniUtil::getMimeType($file['file_path']);
         $response["thumb_exists"] = MUtils::isExistThumbnail($mimeType, (int) $file["file_size"]);
     } else {
         $response['is_dir'] = true;
     }
     if ($file["file_type"] > MConst::OBJECT_TYPE_FILE) {
         $response["type"] = (int) $file["file_type"];
     }
     if (!empty($mimeType)) {
         $response["mime_type"] = $mimeType;
     }
     if ($file["is_deleted"] == true) {
         $response["is_deleted"] = true;
     }
     return $response;
 }
 /**
  * 处理组装请求元数据
  * @param $response
  * @param $file
  * @param $mimeType
  * @return mixed
  */
 private function assembleResponse($response, $file, $mimeType)
 {
     $filePath = $file["file_path"];
     $result = LockBiz::getInstance()->status($filePath);
     $response['lock'] = $result["success"];
     $response["size"] = MUtils::getSizeByLocale($this->locale, $file["file_size"]);
     $response["bytes"] = (int) $file["file_size"];
     $response["path"] = $filePath;
     $response["file_name_pinyin"] = $file['file_name_pinyin'];
     $response["modified"] = MUtils::formatIntTime($file["file_update_time"]);
     $response["create_time"] = $file["file_create_time"];
     $response["update_time"] = $file["file_update_time"];
     $response["revision"] = intval($file["version_id"]);
     $response["rev"] = strval($file["version_id"]);
     $response["root"] = $this->root;
     $response["hash"] = !empty($file["signature"]) ? $file["signature"] : "";
     $response["event"] = $file["event_uuid"];
     $response["sort"] = (int) $file["sort"];
     //外链Key
     $link = MiniLink::getInstance()->getByFileId($file['id']);
     if (empty($link['share_key'])) {
         $response["share_key"] = '';
     } else {
         $response["share_key"] = $link['share_key'];
     }
     $response['is_dir'] = false;
     $permission = UserPermissionBiz::getInstance()->getPermission($filePath, $this->userId);
     if (!empty($permission)) {
         if (isset($permission['children_shared'])) {
             $response['children_shared'] = true;
         } else {
             $permission["share_model"] = $this->getShareModel($filePath);
             $response['share'] = $permission;
         }
         $filePermission = new MiniPermission($permission['permission']);
         $response['canDelete'] = $filePermission->canDeleteFile();
         if (empty($permission['permission'])) {
             return null;
         }
     }
     if ($file["file_type"] == MConst::OBJECT_TYPE_FILE) {
         $mimeType = MiniUtil::getMimeType($file['file_path']);
         $response["thumb_exists"] = MUtils::isExistThumbnail($mimeType, (int) $file["file_size"]);
     } else {
         $response['is_dir'] = true;
     }
     if ($file["file_type"] > MConst::OBJECT_TYPE_FILE) {
         $response["type"] = (int) $file["file_type"];
     }
     if (!empty($mimeType)) {
         $response["mime_type"] = $mimeType;
     }
     if ($file["is_deleted"] == true) {
         $response["is_deleted"] = true;
     }
     return $response;
 }
Example #8
0
 /**
  * 组装返回值,并输出
  */
 public function buildResult()
 {
     if (!isset($this->success) || $this->success != true) {
         throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500);
     }
     $pathInfo = MUtils::pathinfo_utf($this->share_filter->src_path);
     $response = array();
     $response["size"] = MUtils::getSizeByLocale($this->locale, $this->size);
     $response["rev"] = $this->version_id;
     $response["thumb_exists"] = $this->isExistThumbnail();
     $response["bytes"] = intval($this->size);
     $response["modified"] = MUtils::formatIntTime($this->file_update_time);
     $response["path"] = MUtils::convertStandardPath($pathInfo['dirname'] . "/" . $this->file_name);
     $response["is_dir"] = false;
     $response["icon"] = "file";
     $response["root"] = $this->root;
     $response["mime_type"] = $this->type;
     $response["revision"] = intval($this->version_id);
     // 版本
     $response["hash"] = $this->file_hash;
     // 版本
     //当且仅当文件在upload_block下,version没有记录,将在这里处理相关的逻辑
     $response["success"] = true;
     //
     // dataserver 增加需要的返回值
     // by Kindac
     // since 2013/06/25
     //
     $response["temp_file_path"] = $this->file_path;
     //
     // 20120720 增加返回event_uuid用于客户端判断不同类型的事件编码 nava
     //
     if (isset($this->event_uuid)) {
         $response["event_uuid"] = $this->event_uuid;
         // 事件编码
     }
     echo json_encode($response);
     exit;
 }
 /**
  * 控制器执行主逻辑函数
  *
  */
 public function invoke($uri = null)
 {
     $this->setAction(MConst::CREATE_DIRECTORY);
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $params = $_REQUEST;
     // 检查参数
     if (isset($params) === false || $params == null) {
         throw new MFileopsException(Yii::t('api', 'Bad Request'), MConst::HTTP_CODE_400);
     }
     // 获取用户数据,如user_id
     $user = MUserManager::getInstance()->getCurrentUser();
     $device = MUserManager::getInstance()->getCurrentDevice();
     $this->_user_id = $user["user_id"];
     $this->_user_device_id = $device["device_id"];
     // 文件大小格式化参数
     $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'), MConst::HTTP_CODE_400);
     }
     $root = $params["root"];
     // dataserver 增加创建返回path,用于导航
     // by Kindac
     // since 2013/06/25
     $path = $params["path"];
     $fileName = MUtils::get_basename($path);
     if ($fileName === false) {
         throw new MFileopsException(Yii::t('api', 'The folder name is invalid'), MConst::HTTP_CODE_400);
     }
     // 检查文件名是否有效
     $isInvalid = MUtils::checkNameInvalid($fileName);
     if ($isInvalid) {
         throw new MFileopsException(Yii::t('api', 'The folder name is invalid'), MConst::HTTP_CODE_400);
     }
     // 转换路径分隔符,便于以后跨平台,如:将 "\"=>"/"
     $path = MUtils::convertStandardPath($path);
     if ($path == false) {
         throw new MFileopsException(Yii::t('api', 'The folder name is invalid'), MConst::HTTP_CODE_400);
     }
     // 检查是否在共享目录
     $this->share_filter = MSharesFilter::init();
     if ($this->share_filter->handlerCheck($this->_user_id, $path, MConst::CREATE_DIRECTORY)) {
         $this->_user_id = $this->share_filter->master;
         $path = $this->share_filter->_path;
     }
     if ($params['is_root'] == "/") {
         $path = "/" . $this->_user_id . $path;
     }
     $item = explode("/", $path);
     if (!preg_match("/^[0-9]+\$/", $item[1])) {
         $path = "/" . $user["user_id"] . $path;
     }
     $parentPath = dirname($path);
     $isSharedPath = false;
     //主要用于判断是否为被共享文件
     if (dirname(MiniUtil::getRelativePath($path)) == "/" . $this->_user_id) {
         $permission = MConst::SUPREME_PERMISSION;
     } else {
         $pathArr = explode('/', $path);
         $masterId = $pathArr[1];
         if ($masterId != $this->_user_id) {
             $isSharedPath = true;
         } else {
             $model = new GeneralFolderPermissionBiz($parentPath);
             if ($model->isParentShared($parentPath)) {
                 //如果是父目录被共享
                 $isSharedPath = true;
             }
         }
         if ($isSharedPath) {
             $permissionArr = UserPermissionBiz::getInstance()->getPermission($parentPath, $this->_user_id);
             if (!isset($permissionArr)) {
                 $permission = MConst::SUPREME_PERMISSION;
             } else {
                 $permission = $permissionArr['permission'];
                 $privilegeModel = new PrivilegeBiz();
                 $this->share_filter->slaves = $privilegeModel->getSlaveIdsByPath($permissionArr['share_root_path']);
                 $this->share_filter->is_shared = true;
             }
         } else {
             $permission = MConst::SUPREME_PERMISSION;
         }
     }
     $miniPermission = new MiniPermission($permission);
     $canCreateFolder = $miniPermission->canCreateFolder();
     if (!$canCreateFolder) {
         throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409);
     }
     // 查询其是否存在 信息
     $file = MiniFile::getInstance()->getByPath($path);
     // 是否存在相同文件路径, 且被删除的记录
     $hadFileDelete = false;
     if (isset($file)) {
         if ($file["is_deleted"] == false) {
             $code = $file["file_type"] == MConst::OBJECT_TYPE_FILE ? MConst::HTTP_CODE_402 : MConst::HTTP_CODE_403;
             if (MUserManager::getInstance()->isWeb() === true) {
                 throw new MFileopsException(Yii::t('api', 'There is already a item at the given destination'), $code);
             }
             $uuid = $file["event_uuid"];
             // 已经存在,403 error
             throw new MFileopsException($code);
         }
         $hadFileDelete = true;
     }
     $this->_parentFilePath = "/{$this->_user_id}";
     // 检查父目录
     $parentFileId = $this->handlerParentFolder($parentPath);
     $fileDetail = $this->createFile($path, $parentFileId, $hadFileDelete);
     // 处理不同端,不同返回值
     if (MUserManager::getInstance()->isWeb() === true) {
         if ($this->isOutput) {
             $this->buildWebResponse($fileName, $path);
         }
         return;
     }
     $response = array();
     $response["size"] = "0";
     $response["thumb_exists"] = false;
     $response["bytes"] = 0;
     $response["modified"] = MUtils::formatIntTime($fileDetail["file_update_time"]);
     $path = CUtils::removeUserFromPath("{$this->_parentFilePath}/{$fileName}");
     if ($this->share_filter->is_shared) {
         $path = $this->share_filter->src_path;
         $path_info = MUtils::pathinfo_utf($path);
         $path = MUtils::convertStandardPath($path_info['dirname'] . "/" . $fileName);
     }
     $response["path"] = $this->_parentFilePath . "/" . $fileName;
     $response["is_dir"] = true;
     $response["icon"] = "folder";
     $response["root"] = $root;
     $response["revision"] = 0;
     // 版本
     // 增加返回事件uuid,便于客户端进行事件对比逻辑
     $response["event_uuid"] = $fileDetail["event_uuid"];
     echo json_encode($response);
 }