Files
webman-buildadmin/app/admin/controller/docs/Doc36ZiHuaMobileApi.php

78 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
}