/** * 历史版本恢复 * @param int $deviceId * @param string $filePath * @param string $signature * @return bool */ public function recover($deviceId, $filePath, $signature) { $version = MiniVersion::getInstance()->getBySignature($signature); $file = $this->getModelByPath($filePath); if ($version["id"] == $file['version_id']) { return true; } $device = MiniUserDevice::getInstance()->getById($deviceId); $userId = $device["user_id"]; $user = MiniUser::getInstance()->getUser($userId); $userNick = $user["nick"]; // events表 相关操作 $userDeviceName = $device["user_device_name"]; $userDeviceId = $device["id"]; $signature = $version['file_signature']; $action = CConst::MODIFY_FILE; $file->file_update_time = time(); $context = array('hash' => $signature, 'rev' => (int) $version["id"], 'bytes' => (int) $version['file_size'], 'update_time' => (int) $file->file_update_time, 'create_time' => (int) $file['file_create_time']); $filePath = $file['file_path']; $eventUuid = MiniUtil::getEventRandomString(CConst::LEN_EVENT_UUID); MiniEvent::getInstance()->createEvent($file['user_id'], $userDeviceId, $action, $filePath, serialize($context), $eventUuid); //create event // files表相关操作 $file->version_id = $version["id"]; $file->event_uuid = $eventUuid; $file->file_size = $version['file_size']; $file->save(); // meta表相关操作 $fileMeta = FileMeta::model()->find('file_path = ?', array($filePath)); $versions = CUtils::getFileVersions($userDeviceName, $version['file_size'], $version["id"], CConst::WEB_RESTORE, $userId, $userNick, $fileMeta['meta_value']); $fileMeta->meta_value = $versions; $fileMeta->save(); //更新版本引用数 MiniVersion::getInstance()->updateRefCountByIds(array($version["id"]), TRUE); return true; }
/** * 为更名文件创建meta版本 */ private function _saveFileMeta($filePath, $versionId, $userId, $userNick, $action, $deviceName, $fileSize) { // // 获取文件旧版本 // $meta = FileMeta::model()->findByAttributes(array('file_path' => $filePath, 'meta_key' => 'version')); if (!$meta) { $meta = new FileMeta(); $value = CUtils::getFileVersions($deviceName, $fileSize, $versionId, $action, $userId, $userNick); $meta["file_path"] = $filePath; $meta["meta_key"] = "version"; $meta["meta_value"] = serialize(array()); } $value = CUtils::getFileVersions($deviceName, $fileSize, $versionId, $action, $userId, $userNick, $meta["meta_value"]); $meta["meta_value"] = $value; $meta->save(); if ($action == MConst::CREATE_FILE || $action == MConst::MODIFY_FILE || $action == CConst::WEB_RESTORE) { FileVersion::model()->updateRefCountByIds(array($versionId), TRUE); } }
/** * 清理file meta中多余的值 * @param $limit */ private function handleCleanFileMeta($limit) { $condition = 'meta_key="version" and file_path not in ('; $condition .= 'select file_path from '; $condition .= UserFile::model()->tableName(); $condition .= ' where file_type = 0 '; $condition .= ') limit ' . $limit; $fileMetas = FileMeta::model()->findAll($condition); foreach ($fileMetas as $fileMeta) { $metas = unserialize($fileMeta['meta_value']); $this->handleMeta($metas); $fileMeta->delete(); } }
/** * * 发生重命名的时候,更新meta信息 */ private function handlerRenameShared() { $meta = FileMeta::model()->findByAttributes(array('file_path' => $this->_from, 'meta_key' => self::SHARED_META_FLAG)); if (!$meta) { return false; } $meta_value = unserialize($meta['meta_value']); $updates = $meta_value['slaves']; //添加需要修改的权限用户的列表 $this->modifyShareUsers = array_keys($updates); if ($meta_value['master'] == $this->_userId) { $meta_value['path'] = $this->_to; $updates[$this->_userId] = $this->_to; } else { $meta_value['slaves'][$this->_userId] = $this->_to; $updates[$meta_value['master']] = $meta_value['path']; } $meta_value['send_msg'] = $this->_send_msg; $meta_value = serialize($meta_value); FileMeta::model()->updateAll(array('meta_value' => $meta_value, 'file_path' => $this->_to), 'meta_key=:meta_key and file_path=:file_path', array(':meta_key' => self::SHARED_META_FLAG, ':file_path' => $this->_from)); foreach ($updates as $v) { FileMeta::model()->updateAll(array('meta_value' => $meta_value), 'meta_key=:meta_key and file_path=:file_path', array(':meta_key' => self::SHARED_META_FLAG, ':file_path' => $v)); } return true; }
/** * 根据from_path和meta_key=‘create_id’改file_path * @param $fromPath * @param $key * @param $toPath * @param $fileType * @return bool */ public function modifyFilePath($fromPath, $key, $toPath, $fileType) { if ($fileType == 0) { //文件时候meta信息如下处理 $criteria = new CDbCriteria(); $criteria->condition = "meta_key=:meta_key and file_path=:file_path"; $criteria->params = array("meta_key" => $key, "file_path" => $fromPath); $item = FileMeta::model()->find($criteria); if (!empty($item)) { $item->file_path = $toPath; $item->save(); } return true; } else { //目录时meta信息如下处理 $fromPathArr = explode('/', $fromPath); $toPathArr = explode('/', $toPath); $fromPathArrCount = count($fromPathArr); $toPathArrCount = count($toPathArr); $toFolderName = $toPathArr[$toPathArrCount - 1]; $criteria = new CDbCriteria(); $criteria->condition = "meta_key=:meta_key and file_path like :file_path"; $criteria->params = array("meta_key" => $key, "file_path" => $fromPath . '%'); $items = FileMeta::model()->findAll($criteria); foreach ($items as $item) { $itemPath = $item->file_path; $itemPathArr = explode('/', $itemPath); $itemPathArr[$fromPathArrCount - 1] = $toFolderName; $itemPathArrCount = count($itemPathArr); $newPath = ""; for ($i = 1; $i < $itemPathArrCount; $i++) { $newPath .= '/' . $itemPathArr[$i]; } $item->file_path = $newPath; $item->save(); } return true; } }
/** * * 获得文件自身默认的权限 * * @since 1.0.7 */ public function getFilePrivilege($file_path) { $meta = false; $count = substr_count($file_path, "/"); while ($count > 1) { $meta = FileMeta::model()->find('file_path=:file_path and meta_key=:permission', array(':file_path' => $file_path, ':permission' => 'permission')); if (!empty($meta)) { break; } else { $fileInfo = pathinfo($file_path); $file_path = $fileInfo["dirname"]; $count = substr_count($file_path, "/"); } } if (!$meta) { return false; } return unserialize($meta->meta_value); }
/** * 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; }
/** * 根据path找到共享 */ public function handlerFindSlave($user_id, $path) { $meta = FileMeta::model()->findByAttributes(array('file_path' => $path, 'meta_key' => ShareManager::SHARED_META_FLAG)); if (!$meta) { return false; } $meta_value = unserialize($meta['meta_value']); $slaves = $meta_value['slaves']; $slaves[$meta_value['master']] = $meta_value['path']; if (isset($slaves[$user_id])) { $file = UserFile::model()->findByAttributes(array('file_path' => $meta_value['path'])); if ($file) { return $file['id']; } } return false; }