示例#1
0
文件: Api.php 项目: rgwybb/tars
 public function treeReadOnly($product, $name, $version, $route)
 {
     $query = Flight::request()->query;
     $pkg = new Pkg();
     $base = $query->home;
     // $base = '/' . $product . '/' . $name;
     $path = $this->getSplat($route);
     $files = $pkg->listDirectory($path, $base, 'check');
     if (is_array($files)) {
         $tree = array();
         foreach ($files as $row) {
             // 过滤目录 `/admin`
             if (!$path && $row['name'] === 'admin') {
                 continue;
             }
             // 统一文件大小输出
             if ($row['type'] === 'dir') {
                 $row['size'] = '';
             } else {
                 $pos = strpos($row['size'], ' ');
                 if ($pos !== false) {
                     $row['size'] = substr($row['size'], 0, $pos);
                 }
             }
             $row['mode'] = preg_replace('/^0([0-7]{3}$)/', '$1', $row['mode']);
             $tree[] = $row;
         }
         usort($tree, function ($a, $b) {
             $c = $a['type'] === 'dir';
             $d = $b['type'] === 'dir';
             $r = $d - $c;
             if ($r) {
                 return $r;
             } else {
                 return strcmp($a['name'], $b['name']);
             }
         });
         Flight::json($tree);
     } else {
         Flight::json(null, 500);
     }
 }