/** * 根據文件Id获取文件数据 */ public function getLink($fileId, $linkType, $shareKey) { $data = array(); $file = MiniFile::getInstance()->getById($fileId); $data["name"] = $file["file_name"]; $data["bytes"] = intval($file["file_size"] + ""); $fileType = $file["file_type"]; if ($fileType == 0) { $data["icon"] = MiniHttp::getIcon4File($file["file_name"]); if (!MiniUtil::isMixCloudVersion()) { $data["icon"] = MiniHttp::getMiniHost() . "statics/static/mini-box/images/link/" . $data["icon"]; } else { $data["icon"] = "http://static.miniyun.cn/static/mini-box/images/link/" . $data["icon"]; } } $ext = MiniUtil::getFileExtension($file["file_name"]); $path = MiniUtil::getRelativePath($file["file_path"]); if ($ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "gif") { $data["thumbnail_link"] = MiniHttp::createAnonymousUrl("linkAccess/thumbnail?key=" . $shareKey . "&size=256x256&path=" . urlencode($path)); } else { $data["thumbnail_link"] = ""; } if ($linkType == MiniLink::$PREVIEW_LINK) { $data["link"] = MiniHttp::createUrl("link/access/key/" . $shareKey); } else { $data["link"] = MiniHttp::createAnonymousUrl("linkAccess/download?key=" . $shareKey . "&path=" . urlencode($path)); } return $data; }
/** * 把数据库对像转换为视图对象 * @param $file * @return array */ private function do2vo($file) { $info = array(); $info['file_type'] = $file['file_type']; $info['file_size'] = $file['file_size']; $info['file_path'] = MiniUtil::getRelativePath($file['file_path']); $info['updated_at'] = $file['updated_at']; return $info; }
/** * 获取events数据 */ public function getList($path, $time, $deviceUuid, $pageSize, $currentPage) { $user = $this->user; $userId = $user['id']; $time = $this->getTime($time); if ($path != "") { $path = MiniUtil::joinPath($path); } $total = MiniEvent::getInstance()->getTotal($path, $time, $userId, $deviceUuid); $totalPage = ceil($total / $pageSize); $events = MiniEvent::getInstance()->getByCondition($path, $userId, $time, $deviceUuid, $pageSize, ($currentPage - 1) * $pageSize); $itemList = array(); $data = array(); foreach ($events as $event) { $item = array(); $device = MiniUserDevice::getInstance()->getUserDevice($event['user_device_id']); $item['create_user_id'] = $device['user_id']; $item['file_path'] = MiniUtil::getRelativePath($event['file_path']); $item['action'] = $event['action']; $item['user_name'] = $user['user_name']; $item['user_device_type'] = $device['user_device_type']; if ($device['user_id'] == $userId) { $item['user_self'] = true; } else { $item['user_self'] = false; $user = MiniUser::getInstance()->getById($device['user_id']); $userMetas = MiniUserMeta::getInstance()->getUserMetas($device['user_id']); if (isset($userMetas['nick'])) { $item['user_name'] = $userMetas['nick']; } else { $item['user_name'] = $user['user_name']; } } $item['created_at'] = MiniUtil::formatTime(strtotime($event['created_at'])); $item['user_device_name'] = $device['user_device_name']; $item['context'] = MiniUtil::getRelativePath($event['context']); $item['device_uuid'] = $device['user_device_uuid']; if ($event['action'] == 2) { //判断是否是重命名还是创建 $fromParent = CUtils::pathinfo_utf($event['file_path']); $toParent = CUtils::pathinfo_utf($event['context']); if ($fromParent['dirname'] == $toParent['dirname']) { $item['action'] = MConst::RENAME; } } $itemList[] = $item; } $data['events'] = $itemList; $data['totalPage'] = $totalPage; return $data; }
/** 按照文件名查找假删除文件 * @param $fileName * @return mixed */ public function search($fileName) { $sessionUser = $this->user; $user_id = $sessionUser["id"]; $deleteList = MiniFile::getInstance()->getFileByNameRecycle($user_id, $fileName); $list = array(); $data = array(); foreach ($deleteList as $value) { $data["file_name"] = $value["file_name"]; $data["file_size"] = $value["file_size"]; $data["file_path"] = MiniUtil::getRelativePath($value["file_path"]); $data["create_time"] = $value["file_create_time"]; $data["is_deleted"] = $value["is_deleted"]; $data["type"] = $value["file_type"]; $list[] = $data; } $dataList['list'] = $list; return $dataList; }
/** * 打包下载 * @param $paths * @param $filePath * @throws MFileopsException */ public function downloadToPackage($paths, $filePath) { $userId = $this->user['id']; $parentPath = dirname($filePath); $isSharedPath = false; //主要用于判断是否为被共享文件 if (dirname(MiniUtil::getRelativePath($filePath)) == "/" . $userId) { $permission = MConst::SUPREME_PERMISSION; } else { $pathArr = explode('/', $filePath); $masterId = $pathArr[1]; if ($masterId != $userId) { $isSharedPath = true; } else { $model = new GeneralFolderPermissionBiz($parentPath); if ($model->isParentShared($parentPath)) { //如果是父目录被共享 $isSharedPath = true; } } if ($isSharedPath) { $permissionArr = UserPermissionBiz::getInstance()->getPermission($parentPath, $userId); 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); $canDownload = $miniPermission->canDownload(); if (!$canDownload) { throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409); } $arr = explode('/', $filePath); $isRoot = false; $isMine = false; if (count($arr) == 3) { $isRoot = true; } $fileOwnerId = $arr[1]; $currentUser = $this->user; $currentUserId = $currentUser['user_id']; if ($fileOwnerId == $currentUserId) { $isMine = true; } if ($isRoot && !$isMine) { //如果是在根目录下且不是自己的目录 则后台控制不准取消共享 throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_409); } //打包下载限制 header("Content-type: text/html; charset=utf-8"); $limit = new DownloadPackageLimit(); $limitCount = $limit->getLimitCount(); $limitSize = $limit->getLimitSize(); $code = ''; $fileNames = array(); $user = $this->user; $userId = $user['user_id']; $paths = explode(',', $paths); foreach ($paths as $path) { $file = MiniFile::getInstance()->getByPath($path); if (empty($file)) { echo "批量下载的文件存在不存在的文件"; exit; } $code = $code . ',' . $file['id']; array_push($fileNames, $file['file_name']); } if (count($fileNames) > 1) { $packageName = 'miniyun'; } else { $packageName = $fileNames[0]; } //创建临时文件夹 $fileSystem = new CFileSystem(); MUtils::MkDirsLocal(DOCUMENT_TEMP . $userId); $storePath = DOCUMENT_TEMP . $userId . "/" . $packageName; $array = array(); $ids = explode(",", $code); foreach ($ids as $id) { $file = MiniFile::getInstance()->getById($id); if (empty($file)) { continue; } if ($file["file_type"] == MConst::OBJECT_TYPE_FILE) { //属于自己的文件 $array[] = $file; } else { //不属于自己的文件 //查询共有多少个子目录 $array[] = $file; $files = MiniFile::getInstance()->getChildrenByPath($file["file_path"]); $array = array_merge($array, $files); } } if (count($array) > $limitCount) { echo "批量下载单次最大文件数不能超过:" . $limitCount; exit; } $size = $this->calculateSize($array); if ($size > $limitSize * 1024 * 1024) { echo "批量下载单次最大文件大小不能超过:" . $limitSize . "M"; exit; } $path = CUtils::removeUserFromPath($array[0]["file_path"]); $removeParent = pathinfo($path, PATHINFO_DIRNAME); if (strlen($removeParent) == 1) { $removeParent = ""; } //zip压缩 $zip = new ZipArchive(); $zipFile = $storePath . ".zip"; //删除上次存在的压缩文件 $fileSystem->delete($zipFile); try { $zipFile = mb_convert_encoding($zipFile, "gb2312", "UTF-8"); } catch (Exception $e) { $zipFile = $zipFile; } if ($zip->open($zipFile, ZIPARCHIVE::OVERWRITE) === TRUE) { //执行拷贝操作 foreach ($array as $file) { $fileType = $file["file_type"]; $filePath = $file["file_path"]; //获取存储文件的绝对路径 if (!empty($removeParent)) { $relativePath = CUtils::str_replace_once($removeParent, "", CUtils::removeUserFromPath($filePath)); } else { $relativePath = CUtils::removeUserFromPath($filePath); } //打包加上nick $relativePath = $packageName . $relativePath; //转换文件编码为中文编码 try { $store = mb_convert_encoding($relativePath, "gb2312", "UTF-8"); } catch (Exception $e) { $store = $relativePath; } $hasRead = true; if ($userId == $file["user_id"] && $fileType == MConst::OBJECT_TYPE_FILE) { //属于自己的文件 $this->addToFile($zip, $file, $store, $fileSystem); } elseif ($userId != $file["user_id"] && $fileType == MConst::OBJECT_TYPE_FILE) { //不属于自己的文件 if ($hasRead) { $this->addToFile($zip, $file, $store, $fileSystem); } } elseif ($userId == $file["user_id"] && $fileType == MConst::OBJECT_TYPE_DIRECTORY) { //属于自己的文件夹 $this->addToFolder($zip, $store); } else { //不属于自己的文件夹 if ($hasRead) { $this->addToFolder($zip, $store); } } } $zip->close(); //关闭 } if (!file_exists($zipFile)) { echo Yii::t('i18n', 'no_privilege'); Yii::app()->end(); } //进行下载 CUtils::output($zipFile, "application/octet-stream", $packageName . ".zip"); }
/** 组装数组 * @param $fileList * @param $shareList * @param $key * @return array */ private function do2vo($fileList, $shareList, $key) { if ($key == 1) { $list = $fileList; } else { $list = $shareList; } $listShare = array(); foreach ($list as $value) { if ($key == 1) { $file = $value; $shareList = MiniLink::getInstance()->getByFileId($file["id"]); $share = $shareList; $shareData['file_name'] = null; } else { $share = $value; $fileList = MiniFile::getInstance()->getUndeleteFile($share['file_id']); $file = $fileList; } if (empty($file)) { break; } $shareData['file_name'] = $file['file_name']; $shareData['path'] = MiniUtil::getRelativePath($file['file_path']); $shareData['size'] = $file['file_size']; $shareData['updated_at'] = $share['updated_at']; $shareData['share_key'] = $share['share_key']; $shareData['file_type'] = $file['file_type']; $shareData['link'] = $this->link($share['share_key']); $listShare[] = $shareData; } return $listShare; }
/** * 图片列表分页 * @param $page * @param $pageSize * @return mixed */ public function getPage($page, $pageSize) { $fileList = array(); $list = array(); $sharedList = array(); $limit = ($page - 1) * $pageSize; $user = $this->user; $shares = $this->getAllSharedPath($user['id']); $albumTotal = MiniFile::getInstance()->getImageTotal($user["id"], "image"); //获得当前用户被共享目录里面的每个共享目录的子图片总数 $sharedImgTotal = 0; foreach ($shares as $filePath) { $count = MiniFile::getInstance()->getImageTotalByPath($filePath); $sharedImgTotal = $count + $sharedImgTotal; if ($count > 0) { $item = array(); $item[] = $filePath; $item[] = $count; $sharedList[] = $item; } } $fileList["total"] = $albumTotal + $sharedImgTotal; //共享目录图片取完后,取自己的图片 if ($limit >= $sharedImgTotal) { $limit = $limit - $sharedImgTotal; $albums = MiniFile::getInstance()->getFileListPage($limit, $pageSize, $user["id"], "image"); } else { //共享目录图片下的图片 if ($page * $pageSize < $sharedImgTotal) { $albums = $this->getSharedImgPage($limit, $pageSize, $sharedList); } else { //部分共享目录,部分自己的图片 $albumShared = $this->getSharedImgPage($limit, $sharedImgTotal, $sharedList); $albumList = MiniFile::getInstance()->getFileListPage(0, $pageSize * $page - $sharedImgTotal, $user["id"], "image"); if (count($albumList) > 0) { $albums = array_merge($albumList, $albumShared); } else { $albums = $albumShared; } } } foreach ($albums as $value) { $permission = UserPermissionBiz::getInstance()->getPermission($value['file_path'], $this->user['user_id']); if (!empty($permission)) { $filePermission = new MiniPermission($permission['permission']); $data['canDelete'] = $filePermission->canDeleteFile(); } else { $data['canDelete'] = true; } $data["filename"] = $value['file_name']; $data["fileSize"] = $value['file_size']; $data["path"] = MiniUtil::getRelativePath($value['file_path']); $data["createTime"] = $value['file_create_time']; $list[] = $data; } $fileList['list'] = $list; return $fileList; }
/** * 控制器执行主逻辑函数 * */ 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); }