Browse Source

add Api controller

master
filesite 2 years ago
parent
commit
091a22b405
  1. 2
      conf/app.php
  2. 78
      controller/ApiController.php
  3. 5
      controller/Controller.php

2
conf/app.php

@ -26,7 +26,7 @@ return array(
'error_layout' => 'error', //exception layout, show error title and content 'error_layout' => 'error', //exception layout, show error title and content
//for debug, log directory: ../runtime/logs/ //for debug, log directory: ../runtime/logs/
'debug' => false, 'debug' => true,
//for themes //for themes
/* /*

78
controller/ApiController.php

@ -0,0 +1,78 @@
<?php
/**
* Api Controller
*/
require_once __DIR__ . '/../lib/DirScanner.php';
Class ApiController extends Controller {
protected $version = '1.0';
//show api list
public function actionIndex() {
$params = array(
'version' => $this->version,
'list' => array(
'列表' => '/api/ls/',
//文件操作
'重命名' => '/api/rename/',
'删除文件' => '/api/delete/',
'移动' => '/api/move/',
//目录操作
'创建目录' => '/api/mkdir/',
'删除目录' => '/api/rmdir/',
),
);
return $this->renderJson($params);
}
//目录、文件列表
public function actionLs() {
$code = 0;
$msg = $err = '';
$data = array();
$scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']);
$target = __DIR__ . '//../www/' . FSC::$app['config']['content_directory'];
$maxLevels = 2;
$dirTree = $scanner->scan($target, $maxLevels);
$scanResults = $scanner->getScanResults();
//获取目录
$menus = $scanner->getMenus();
if (empty($menus)) {
$err = '没有任何目录/文件';
return $this->renderJson(compact('code', 'msg', 'err', 'data'));
}
//排序
$menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序
$readmeFile = $scanner->getDefaultReadme();
if (!empty($readmeFile)) {
if (!empty($readmeFile['sort'])) {
$menus_sorted = explode("\n", $readmeFile['sort']);
}
}
$sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree);
if (!empty($sortedTree)) {
$menus = $sortedTree['menus'];
$dirTree = $sortedTree['dirTree'];
}
$cateId = $this->get('id', $menus[0]['id']);
$data['menus'] = $menus;
$data['dirTree'] = $scanResults[$cateId];
$code = 1;
$msg = '';
$err = '';
return $this->renderJson(compact('code', 'msg', 'err', 'data'));
}
}

5
controller/Controller.php

@ -80,6 +80,11 @@ Class Controller {
//render json data //render json data
protected function renderJson($data) { protected function renderJson($data) {
if (!empty(FSC::$app['config']['debug'])) {
$end_time = microtime(true);
$data['page_time_cost'] = ceil( ($end_time - FSC::$app['start_time']) * 1000 ); //ms
}
header("Content-Type: application/json; charset=utf-8"); header("Content-Type: application/json; charset=utf-8");
echo json_encode($data); echo json_encode($data);
exit; exit;

Loading…
Cancel
Save