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; } }