Пример #1
0
 /**
  * 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;
 }