Пример #1
0
 /**
  * 打包下载
  * @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");
 }
Пример #2
0
 /**
  *
  * 修改用户权限的路径
  *
  * @since 1.0.7
  */
 public function updatedAllFilePath($file_path, $new_file_path)
 {
     $privileges = UserPrivilege::model()->findAll('file_path like :file_path', array(':file_path' => $file_path . '%'));
     if (empty($privileges)) {
         return false;
     }
     //替换所有的path
     foreach ($privileges as $privilege) {
         $privilege->file_path = CUtils::str_replace_once($file_path, $new_file_path, $privilege->file_path);
         $privilege->save();
     }
     return true;
 }