/** * 分页获取文件数据 */ public function getFiles($pageSize, $currentPage) { $files = MiniFile::getInstance()->getAllFilesList($pageSize, $currentPage); $data = array(); foreach ($files as $file) { $file = $this->dealFile($file); array_push($data, $file); } $this->list['list'] = $data; $this->list['total'] = UserFile::model()->count(); return $this->list; }
/** * * 调用入口 * @param unknown_type $action * * @since 1.1.0 */ public function invoke($action) { if (!empty($this->file_id)) { $this->file = UserFile::model()->findByAttributes(array('id' => $this->file_id, 'is_deleted' => 0)); if (!empty($this->file)) { $this->master = CUtils::getUserFromPath($this->file["file_path"]); } } switch ($action) { case self::ACTION_SEARCH: $retval = $this->handlerSeach(); break; case self::ACTION_LIST_FRIENDS: $retval = $this->handlerList(); default: break; } return $retval; }
/** * 清理全部的缓存,包括多余的file_meta * @param $limit */ public function cleanCache($limit) { //data源处理对象 $dataObj = Yii::app()->data; // 回收站插件: -1保留值 0正常 1删除 $this->handleCleanFileMeta($limit); // 清理ref_count等于0的文件 $versions = MiniVersion::getInstance()->getCleanFiles(100); foreach ($versions as $version) { $files = UserFile::model()->findAll('version_id=?', array($version['id'])); // 如果$file存在此version_id,不删除 if (!empty($files)) { for ($i = 0; $i < count($files); $i++) { MiniVersion::getInstance()->updateRefCount($version["id"]); } continue; } // 如果不存在的话,删除流文件,删除该条version记录 $signature = $version['file_signature']; $signaturePath = MiniUtil::getPathBySplitStr($signature); // 判断文件是否存在 if ($dataObj->exists($signaturePath) === false) { MiniVersion::getInstance()->deleteById($version["id"]); continue; } // 删除文件 $dataObj->delete($signaturePath); //删除空的文件夹 $parts = CUtils::getFoldersBySplitStr($signature); foreach ($parts as $part) { $dataObj->delete($part); } // 删除version记录 MiniVersion::getInstance()->deleteById($version["id"]); } MiniUtil::deleteDir(BASE . 'temp'); }
/** * 更名 */ public function updateFileName($id, $name, &$message) { if (CUtils::checkNameInvalid($name) || strstr($name, '&') || strstr($name, ';')) { $message = Yii::t("front_common", "name_error"); return false; } $model = $this->findByAttributes(array('id' => $id)); if (empty($model)) { $message = Yii::t("admin_common", "rename_file_not_found"); return false; } $oldPath = $model['file_path']; $model['file_name'] = $name; $model['file_path'] = dirname($model['file_path']) . "/" . $name; // // 判断同名否 // $same = $this->findByAttributes(array('file_path' => $model['file_path'])); if ($same) { $message = Yii::t("admin_common", "rename_file_already_exists"); return false; } $time = (int) time(); $model['file_update_time'] = $time; $path = $model['file_path']; $context = $path; $device_id = Yii::app()->session["deviceId"]; $share_filter = MSharesFilter::init(); $share_filter->handlerCheckByFile($model['user_id'], $model); if ($model['file_type'] == 0) { $model['mime_type'] = MiniUtil::getMimeType($name); // // 如果是文件,需要创建版本 // $user = Yii::app()->session['user']; $device = UserDevice::model()->findByUserIdAndType($user['id'], CConst::DEVICE_WEB); $deviceName = $device["user_device_name"]; $this->_saveFileMeta($path, $model['version_id'], $user['id'], $user['name'], CConst::CREATE_FILE, $deviceName, $model['file_size']); } else { // // 文件夹需要更名所有子目录 // $moreFile = UserFile::model()->findAll("file_path like :type", array('type' => $oldPath . '%')); foreach ($moreFile as $key => $value) { $data = date("Y-m-d H:i:s", time()); // // 查找匹配路径 index, 替换路径 // $index = strlen($oldPath); $value['file_path'] = substr_replace($value['file_path'], $model['file_path'], 0, $index); $s = UserFile::model()->updateAll(array('file_path' => $value['file_path'], 'file_update_time' => $time, 'updated_at' => $data), "id=" . $value['id']); // 文件创建meta if ($value['file_type'] == 0) { // // 如果是文件,需要创建版本 // $user = Yii::app()->session['user']; $device = UserDevice::model()->findByUserIdAndType($user['id'], CConst::DEVICE_WEB); $deviceName = $device["user_device_name"]; $this->_saveFileMeta($value['file_path'], $value['version_id'], $user['id'], $user['name'], CConst::CREATE_FILE, $deviceName, $value['file_size']); } } } $user_id = $model['user_id']; $event_uuid = MiniUtil::getEventRandomString(46); $action = MConst::MOVE; // 重命名 MiniEvent::getInstance()->createEvent($user_id, $device_id, $action, $oldPath, $context, $event_uuid, $share_filter->type); // // 重命名共享目录 // if ($model['file_type'] >= 2) { $share_filter->handlerRenameShared($oldPath, $context); } else { $share_filter->handlerAction($action, $device_id, $oldPath, $context); } $model['event_uuid'] = $event_uuid; $message = Yii::t("admin_common", "rename_file_success"); return $model->save(false); }
public function findByUseridAndFileid($user_id, $file_id) { $data = UserFile::model()->findAllBySql("select * from " . Yii::app()->params['tablePrefix'] . "file_star where user_id=" . $user_id . " and file_id=" . $file_id); return $data; }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return UserFile the loaded model * @throws CHttpException */ public function loadModel($id) { $model = UserFile::model()->findByPk($id); $fileModel = new FileUploadForm(); $fileModel = $model; if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $fileModel; }
/** * * 列举共享信息 */ public function handlerListDetails() { $this->_slaves = array(); $this->_file = UserFile::model()->findByAttributes(array('id' => $this->_id, 'user_id' => $this->_userId, 'is_deleted' => 0)); if (is_null($this->_file)) { throw new ApiException('Not Found'); } if ($this->_file['file_type'] != 2) { throw new ApiException('Not Found'); } $meta = FileMeta::model()->findByAttributes(array('file_path' => $this->_file['file_path'], 'meta_key' => self::SHARED_META_FLAG)); if (!$meta) { throw new ApiException('Not Found'); } $meta_value = unserialize($meta['meta_value']); if ($meta_value['master'] != $this->_userId) { throw new ApiException('Not Found'); } $slaves = $meta_value['slaves']; $send = $meta_value['send_msg']; foreach ($slaves as $key => $value) { $user = User::model()->findByPk($key); if (!$user) { continue; } $mate = UserMeta::model()->findByAttributes(array("user_id" => $user->id, "meta_key" => "nick")); if ($mate) { $value = $mate->meta_value; if (!empty($value) && strlen(trim($value)) > 0) { $nick = $value; } } else { $nick = $user['user_name']; } $shared_file = UserFile::model()->findByAttributes(array('user_id' => $key, 'file_path' => $value, 'is_deleted' => 0)); $path = $this->_file['file_path']; $permission = Yii::app()->privilege->checkPrivilegeUser($key, $path); array_push($this->_slaves, array('user_id' => $key, 'user_name' => $user['user_name'], 'nick' => $nick, 'type' => $shared_file['file_type'], 'permission' => $permission, 'send' => $send)); } }
/** * * 列举共享信息 * * @since 1.1.0 */ public function handlerSetPermission() { $this->_file = UserFile::model()->findByAttributes(array('id' => $this->_id, 'is_deleted' => 0)); if (is_null($this->_file)) { throw new ApiException('Not Found'); } //判断此用户是否有 分配 权限 $file_path = $this->_file["file_path"]; $hasPermissionAllot = Yii::app()->privilege->hasPermission($file_path, MPrivilege::PERMISSION_GRANT); if (!$hasPermissionAllot) { $this->result["msg"] = Yii::t('front_common', 'Can not grant permission'); throw new ApiException('Can not grant permission'); } // // 将逗号分割的id组装成数组 // if (!empty($this->_slaves)) { $this->_slaves = array_slice(explode(',', $this->_slaves), 0); } else { $this->_slaves = array(); } $this->_permissions = array_slice(explode(',', $this->_permissions), 0); $tmpUser = $this->_slaves; //查询出此路径下的直接权限的所有用户 $privileges = MUserPrivilege::model()->findAll('file_path=:file_path', array(':file_path' => $this->_file["file_path"])); $currentUser = MUserManager::getInstance()->getCurrentUser(); foreach ($privileges as $pri) { //分配权限时如果权限表中的权限的所有者是自己, 则不进行处理 if ($pri["user_id"] == $currentUser["id"]) { continue; } //如果此用户不存在则进行添加权限,已经存在则进行修改权限,否则进行删除权限操作 if (in_array($pri["user_id"], $tmpUser)) { $newPermission = $this->getUserPermission($this->_slaves, $pri["user_id"]); //进行是否事件生成判断 $this->updatePrivelegeEvent($pri, $newPermission); //更新权限 Yii::app()->privilege->updatedPrivilege($pri["user_id"], $this->_file["file_path"], $newPermission); $tmpUser = CUtils::arrayRemove($tmpUser, $pri["user_id"]); } else { $pri->delete(); $this->deletePrivelegeEvent($pri["user_id"], $pri["file_path"], unserialize($pri["permission"])); } } //创建权限 foreach ($tmpUser as $index => $userId) { $permission = $this->getUserPermission($this->_slaves, $userId); $this->createPrivelegeEvent($userId, $this->_file["file_path"], $permission); Yii::app()->privilege->createPrivilege($userId, $this->_file["file_path"], $permission); } }
public function getUsedSpaceById($id) { $usedSpace = 0; $used = UserFile::model()->getUsedSpace($id); if (count($used) > 0) { $usedSpace = $used[0]["usedSpace"]; } return $usedSpace; }
/** * * 执行文件夹删除 * @param UserFile $file */ public function delete($parenFile) { $share_filter = MSharesFilter::init(); $share_filter->handlerCheckByFile($this->_userId, $parenFile); // // 取消共享 // if ($parenFile['file_type'] == 2 && $parenFile['user_id'] != $this->_userId || $parenFile['file_type'] == 3) { $parenFile = UserFile::model()->findByAttributes(array('file_path' => $share_filter->slaves[$this->_userId], 'is_deleted' => 0)); if (!$parenFile) { throw new ApiException("Not found"); } $handler = new ShareManager(); $handler->_userId = $share_filter->operator; $handler->_id = $parenFile["id"]; try { $handler->invoke(ShareManager::CANCEL_SHARED); } catch (Exception $e) { throw new ApiException("Not found"); } return; } // 更新每个元素以及子元素 $parentPath = $parenFile["file_path"]; $handler = new UserFile(); $files = $handler->getFilesByPath($parentPath); // 轮询删除 foreach ($files as $file) { if ($file["is_deleted"]) { continue; } // 已经删除的文件不做操作 $file["event_uuid"] = MiniUtil::getEventRandomString(46); if ($file["file_type"] == 0) { if ($file["is_deleted"]) { continue; } // 已经删除的文件不做操作 $file["is_deleted"] = 1; $file->save(); // 创建版本信息 $this->handleFileMeta($file["file_path"], $file["version_id"], $file['user_id'], $this->_userNick, $this->_deviceName, $file['file_size']); // 创建事件 MiniEvent::getInstance()->createEvent($file['user_id'], $this->_deviceId, $this->_action, $file["file_path"], $file["file_path"], $file["event_uuid"]); $share_filter->handlerAction($this->_action, $this->_deviceId, $file["file_path"], $file["file_path"]); continue; } $this->delete($file); $file["is_deleted"] = 1; $file->save(); // 创建事件 MiniEvent::getInstance()->createEvent($file['user_id'], $this->_deviceId, $this->_action, $file["file_path"], $file["file_path"], $file["event_uuid"]); $share_filter->handlerAction($this->_action, $this->_deviceId, $file["file_path"], $file["file_path"]); } $parenFile["event_uuid"] = MiniUtil::getEventRandomString(46); $parenFile["is_deleted"] = 1; $parenFile->save(); // 创建事件 MiniEvent::getInstance()->createEvent($parenFile['user_id'], $this->_deviceId, $this->_action, $parenFile["file_path"], $parenFile["file_path"], $parenFile["event_uuid"]); $share_filter->handlerAction($this->_action, $this->_deviceId, $parenFile["file_path"], $parenFile["file_path"]); // // 删除共享目录 // if ($share_filter->_is_shared_path && $share_filter->operator == $share_filter->master) { $id = $parenFile["id"]; $handler = new ShareManager(); $handler->_userId = $share_filter->operator; $handler->_id = $id; try { $handler->invoke(ShareManager::CANCEL_SHARED); } catch (Exception $e) { throw new ApiException('Internal Server Error'); } } }
/** * * 重命名冲突的文件名 * @param int $parentId * @param string $name */ private function handleRename($parentId, $name) { $children = UserFile::model()->findAllByAttributes(array("parent_file_id" => $parentId, "user_id" => $this->_userId, "is_deleted" => 0)); $names = array(); foreach ($children as $child) { $names[$child["file_name"]] = $child["file_name"]; } // 重命名 $name = CUtils::getConflictName($name, $names); return $name; }
/** * 根据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; }
/** * 从数据库中查询, 判断用户名是否重复 * @param int $userId * @param int $parentId * @param string $fileName * @return string */ public static function getConflictFileName($userId, $parentId, $fileName) { $files = UserFile::model()->getByParentID($userId, $parentId); $names = array(); foreach ($files as $k => $v) { $names[$v["file_name"]] = $v["file_name"]; } $fileName = CUtils::getConflictName($fileName, $names); return $fileName; }
/** * 获取回收站文件 * @return boolean|string */ private function getTrashList() { $condition = "user_id=:user_id and is_deleted = 1"; //and parent_file_id not in (select id from ".Yii::app()->params['tablePrefix']."files where is_deleted = 1 and user_id=:user_id)"; $params = array(':user_id' => $this->_userId); // // 修改查询条件 // $value = array('condition' => $condition, 'params' => $params); $info = UserFile::model()->findAll(array('condition' => $value['condition'], 'params' => $value['params'])); $files = ''; foreach ($info as $item) { $files .= ',' . $item['id']; } if (empty($files)) { return false; } return substr($files, 1); }
/** * 更新时间 */ public function updateTime($filePath) { $criteria = new CDbCriteria(); $criteria->condition = "file_path = :file_path"; $criteria->params = array("file_path" => $filePath); $item = UserFile::model()->find($criteria); if (isset($item)) { $item->save(); } }
/** * * 根据路径创建目录 */ public function handleCreateByPath($path) { if ($path == "/{$this->_userId}" || $path == "\\" || $path == "/{$this->_userId}/") { return 0; } $event_uuid = MiniUtil::getEventRandomString(46); $file = UserFile::model()->find(array('condition' => 'file_path=:file_path', 'params' => array(':file_path' => $path))); if (empty($file) || is_null($file)) { $pathInfo = CUtils::pathinfo_utf($path); $parenPath = $pathInfo["dirname"]; $fileName = $pathInfo["basename"]; $parentId = $this->handleCreateByPath($parenPath); $file = new UserFile(); $file['user_id'] = $this->_userId; $file['file_type'] = 1; $file['parent_file_id'] = $parentId; $file['file_create_time'] = time(); $file['file_update_time'] = time(); $file['file_name'] = $fileName; $file['file_path'] = $path; $file['is_deleted'] = 0; $file['version_id'] = 0; $file['file_size'] = 0; $file["event_uuid"] = $event_uuid; $file->save(); $file['sort'] = $file['id']; $file->save(); // 创建事件 MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, $this->_action, $path, $path, $event_uuid, $this->share_filter->type); $this->share_filter->handlerAction($this->_action, $this->_deviceId, $path, $path); } else { // // 回收站插件: -1保留值 0正常 1删除 // 这里由is_deleted==1 特别修改为 is_deleted!=0 // By Kindac 2012/11/5 // if ($file["is_deleted"] != 0) { $file["is_deleted"] = 0; $file["file_update_time"] = time(); $file["event_uuid"] = $event_uuid; // // 递归创建父目录 // $pathInfo = CUtils::pathinfo_utf($path); $parenPath = $pathInfo["dirname"]; $this->handleCreateByPath($parenPath); $file->save(); MiniEvent::getInstance()->createEvent($this->_userId, $this->_deviceId, $this->_action, $path, $path, $event_uuid, $this->share_filter->type); $this->share_filter->handlerAction($this->_action, $this->_deviceId, $path, $path); } } return $file["id"]; }
/** * web返回值处理 */ public function buildWebResponse() { if (!isset($this->success) || $this->success != true) { throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500); } // 查询数据库,找出对应id $fileDetail = UserFile::model()->find(array('condition' => 'file_path=:file_path', 'params' => array(':file_path' => "/" . $this->user_id . $this->path))); if (empty($fileDetail)) { throw new MFileopsException(Yii::t('api', 'Can not find the folder.'), MConst::HTTP_CODE_404); } $pathInfo = MUtils::pathinfo_utf($this->share_filter->src_path); $user = MiniUser2::getInstance()->getUser2($this->user_id); $total = $user["space"]; $usedSpace = $user["usedSpace"]; $uploadSizeRemain = $total - $usedSpace; $cid = isset($_REQUEST['cid']) ? $_REQUEST['cid'] : 0; $data = array(); $data["rev"] = $this->version_id; $data["is_user"] = 1; $data["user_id"] = $this->user_id; $data["group_id"] = 0; $data["aid"] = 0; $data["user_ip"] = "0.0.0.0"; $data["upload_type"] = 1; // // dataserver 修改需要的返回值 文件path // by Kindac // since 2013/06/25 // $data["temp_file_path"] = $this->file_path; $data["temp_file_name"] = $this->file_name; $data["temp_file_sha1"] = $this->file_hash; $data["is_share"] = 0; $data["file_ext"] = $pathInfo["extension"]; $data["file_name_sort"] = strlen($this->file_name); $data["file_size"] = $this->size; $data["file_description"] = ""; $data["pick_time"] = ""; $data["is_collect"] = 0; $data["file_status"] = 1; $data["file_ptime"] = time(); $data["file_id"] = $fileDetail->id; $data["ico"] = $pathInfo["extension"]; $data["area_id"] = $cid; $data["category_id"] = $cid; $data["upload_size_remain"] = $uploadSizeRemain; $ret = array(); $ret["state"] = true; $ret["data"] = $data; echo json_encode($ret); }