78 lines
2.2 KiB
PHP
78 lines
2.2 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace app\admin\controller\docs;
|
||
|
||
use app\common\controller\Backend;
|
||
use support\Response;
|
||
use Webman\Http\Request;
|
||
|
||
/**
|
||
* 后台只读展示《36字花-移动端接口设计草案》Markdown,并提供下载。
|
||
*/
|
||
class Doc36ZiHuaMobileApi extends Backend
|
||
{
|
||
private const DOC_RELATIVE = 'docs' . DIRECTORY_SEPARATOR . '36字花-移动端接口设计草案.md';
|
||
|
||
public function content(Request $request): Response
|
||
{
|
||
$response = $this->initializeBackend($request);
|
||
if ($response !== null) {
|
||
return $response;
|
||
}
|
||
|
||
$path = $this->docAbsolutePath();
|
||
if (!is_file($path)) {
|
||
return $this->error(__('Document file not found'));
|
||
}
|
||
|
||
$raw = file_get_contents($path);
|
||
if ($raw === false) {
|
||
return $this->error(__('Failed to read document'));
|
||
}
|
||
|
||
return $this->success('', [
|
||
'markdown' => $raw,
|
||
'filename' => '36字花-移动端接口设计草案.md',
|
||
]);
|
||
}
|
||
|
||
public function download(Request $request): Response
|
||
{
|
||
$response = $this->initializeBackend($request);
|
||
if ($response !== null) {
|
||
return $response;
|
||
}
|
||
|
||
$path = $this->docAbsolutePath();
|
||
if (!is_file($path)) {
|
||
return $this->error(__('Document file not found'));
|
||
}
|
||
|
||
$body = file_get_contents($path);
|
||
if ($body === false) {
|
||
return $this->error(__('Failed to read document'));
|
||
}
|
||
|
||
$utf8Name = '36字花-移动端接口设计草案.md';
|
||
$asciiFallback = '36zihua-mobile-api-design-draft.md';
|
||
$disposition = sprintf(
|
||
'attachment; filename="%s"; filename*=UTF-8\'\'%s',
|
||
$asciiFallback,
|
||
rawurlencode($utf8Name)
|
||
);
|
||
|
||
return new Response(200, [
|
||
'Content-Type' => 'text/markdown; charset=UTF-8',
|
||
'Content-Disposition' => $disposition,
|
||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||
], $body);
|
||
}
|
||
|
||
private function docAbsolutePath(): string
|
||
{
|
||
return rtrim(base_path(), DIRECTORY_SEPARATOR . '/') . DIRECTORY_SEPARATOR . self::DOC_RELATIVE;
|
||
}
|
||
}
|