Spaces:
Running
Running
souliya_pps commited on
Commit ·
27aeaab
1
Parent(s): 57087bc
feat: add REST API endpoints for book listing, details, filtering, and page navigation
Browse filesadd apiList, apiDetails, apiFilter, and apiPageNav methods to PdfBookController with JSON responses. include cover URL resolution, book metadata, text preview, pagination, and query-based filtering. register new API routes for /list, /details, and /filter endpoints.
- app/Controllers/PdfBookController.php +213 -0
- routes/web.php +5 -1
app/Controllers/PdfBookController.php
CHANGED
|
@@ -240,6 +240,219 @@ class PdfBookController {
|
|
| 240 |
]);
|
| 241 |
}
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
private function json($data) {
|
| 244 |
header('Content-Type: application/json; charset=utf-8');
|
| 245 |
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
|
|
| 240 |
]);
|
| 241 |
}
|
| 242 |
|
| 243 |
+
public function apiList() {
|
| 244 |
+
$books = PdfBook::getBooks();
|
| 245 |
+
if (empty($books)) {
|
| 246 |
+
$this->json(['books' => []]);
|
| 247 |
+
return;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
$result = [];
|
| 251 |
+
foreach ($books as $book) {
|
| 252 |
+
$slug = $book['slug'];
|
| 253 |
+
$coverDir = __DIR__ . '/../../public/assets/' . $slug;
|
| 254 |
+
$coverUrl = null;
|
| 255 |
+
foreach (['png', 'jpg', 'jpeg'] as $ext) {
|
| 256 |
+
if (file_exists($coverDir . '/cover.' . $ext)) {
|
| 257 |
+
$coverUrl = url('/assets/' . $slug . '/cover.' . $ext);
|
| 258 |
+
break;
|
| 259 |
+
}
|
| 260 |
+
}
|
| 261 |
+
if (!$coverUrl) {
|
| 262 |
+
$coverUrl = url('assets/images/book-placeholder.svg');
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
$result[] = [
|
| 266 |
+
'slug' => $slug,
|
| 267 |
+
'title' => $book['title'] ?? $slug,
|
| 268 |
+
'year' => $book['year'] ?? null,
|
| 269 |
+
'totalPages'=> $book['totalPages'] ?? 0,
|
| 270 |
+
'type' => $book['type'] ?? 'docx',
|
| 271 |
+
'coverUrl' => $coverUrl,
|
| 272 |
+
];
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
$this->json(['books' => $result]);
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
public function apiDetails() {
|
| 279 |
+
$slug = $_GET['slug'] ?? '';
|
| 280 |
+
$title = $_GET['title'] ?? '';
|
| 281 |
+
|
| 282 |
+
if (!$slug && !$title) {
|
| 283 |
+
http_response_code(400);
|
| 284 |
+
$this->json(['error' => 'Missing slug or title parameter']);
|
| 285 |
+
return;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
if (!$slug && $title) {
|
| 289 |
+
$books = PdfBook::getBooks();
|
| 290 |
+
foreach ($books as $book) {
|
| 291 |
+
if (($book['title'] ?? '') === $title) {
|
| 292 |
+
$slug = $book['slug'];
|
| 293 |
+
break;
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
if (!$slug) {
|
| 297 |
+
http_response_code(404);
|
| 298 |
+
$this->json(['error' => 'Book not found by title']);
|
| 299 |
+
return;
|
| 300 |
+
}
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
$info = PdfBook::getInfo($slug);
|
| 304 |
+
if (!$info) {
|
| 305 |
+
http_response_code(404);
|
| 306 |
+
$this->json(['error' => 'Book not found']);
|
| 307 |
+
return;
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
$coverDir = __DIR__ . '/../../public/assets/' . $slug;
|
| 311 |
+
$coverUrl = null;
|
| 312 |
+
foreach (['png', 'jpg', 'jpeg'] as $ext) {
|
| 313 |
+
if (file_exists($coverDir . '/cover.' . $ext)) {
|
| 314 |
+
$coverUrl = url('/assets/' . $slug . '/cover.' . $ext);
|
| 315 |
+
break;
|
| 316 |
+
}
|
| 317 |
+
}
|
| 318 |
+
if (!$coverUrl) {
|
| 319 |
+
$coverUrl = url('assets/images/book-placeholder.svg');
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
$firstPage = PdfBook::getPage($slug, 1);
|
| 323 |
+
$preview = null;
|
| 324 |
+
if ($firstPage && !empty($firstPage['text'])) {
|
| 325 |
+
$text = $firstPage['text'];
|
| 326 |
+
$lines = explode("\n", trim($text));
|
| 327 |
+
$previewLines = array_slice($lines, 0, 10);
|
| 328 |
+
$preview = implode("\n", $previewLines);
|
| 329 |
+
if (count($lines) > 10) $preview .= '...';
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
$this->json([
|
| 333 |
+
'book' => [
|
| 334 |
+
'slug' => $slug,
|
| 335 |
+
'title' => $info['title'] ?? $slug,
|
| 336 |
+
'year' => $info['year'] ?? null,
|
| 337 |
+
'totalPages' => $info['totalPages'] ?? 0,
|
| 338 |
+
'type' => $info['type'] ?? 'docx',
|
| 339 |
+
'coverUrl' => $coverUrl,
|
| 340 |
+
'preview' => $preview,
|
| 341 |
+
]
|
| 342 |
+
]);
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
public function apiPageNav() {
|
| 346 |
+
$slug = $_GET['book'] ?? '';
|
| 347 |
+
$pageNum = intval($_GET['n'] ?? 0);
|
| 348 |
+
$query = $_GET['q'] ?? '';
|
| 349 |
+
|
| 350 |
+
if (!$slug || $pageNum < 1) {
|
| 351 |
+
http_response_code(400);
|
| 352 |
+
$this->json(['error' => 'Missing or invalid book/n parameter']);
|
| 353 |
+
return;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
$info = PdfBook::getInfo($slug);
|
| 357 |
+
if (!$info) {
|
| 358 |
+
http_response_code(404);
|
| 359 |
+
$this->json(['error' => 'Book not found']);
|
| 360 |
+
return;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
$page = PdfBook::getPage($slug, $pageNum);
|
| 364 |
+
if (!$page) {
|
| 365 |
+
http_response_code(404);
|
| 366 |
+
$this->json(['error' => 'Page not found']);
|
| 367 |
+
return;
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
$totalPages = $info['totalPages'] ?? 0;
|
| 371 |
+
$prevPage = $pageNum > 1 ? $pageNum - 1 : null;
|
| 372 |
+
$nextPage = $pageNum < $totalPages ? $pageNum + 1 : null;
|
| 373 |
+
|
| 374 |
+
$highlightWords = [];
|
| 375 |
+
if ($query && !empty($page['words'])) {
|
| 376 |
+
$q = mb_strtolower(trim($query));
|
| 377 |
+
foreach ($page['words'] as $word) {
|
| 378 |
+
if (mb_strpos(mb_strtolower($word['w']), $q) !== false) {
|
| 379 |
+
$highlightWords[] = $word;
|
| 380 |
+
}
|
| 381 |
+
}
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
$this->json([
|
| 385 |
+
'book' => [
|
| 386 |
+
'slug' => $slug,
|
| 387 |
+
'title' => $info['title'] ?? $slug,
|
| 388 |
+
'totalPages' => $totalPages,
|
| 389 |
+
],
|
| 390 |
+
'page' => [
|
| 391 |
+
'number' => $page['page'],
|
| 392 |
+
'text' => $page['text'],
|
| 393 |
+
'words' => $page['words'] ?? [],
|
| 394 |
+
],
|
| 395 |
+
'highlightWords' => $highlightWords,
|
| 396 |
+
'prevPage' => $prevPage,
|
| 397 |
+
'nextPage' => $nextPage,
|
| 398 |
+
]);
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
public function apiFilter() {
|
| 402 |
+
$query = $_GET['q'] ?? '';
|
| 403 |
+
$year = $_GET['year'] ?? '';
|
| 404 |
+
$type = $_GET['type'] ?? '';
|
| 405 |
+
$page = max(1, intval($_GET['page'] ?? 1));
|
| 406 |
+
$perPage = max(1, min(100, intval($_GET['perPage'] ?? 50)));
|
| 407 |
+
|
| 408 |
+
$books = PdfBook::getBooks();
|
| 409 |
+
$result = [];
|
| 410 |
+
|
| 411 |
+
foreach ($books as $book) {
|
| 412 |
+
$slug = $book['slug'];
|
| 413 |
+
|
| 414 |
+
if ($year && ($book['year'] ?? '') != $year) continue;
|
| 415 |
+
if ($type && ($book['type'] ?? '') !== $type) continue;
|
| 416 |
+
if ($query) {
|
| 417 |
+
$title = $book['title'] ?? $slug;
|
| 418 |
+
if (mb_stripos($title, $query) === false) continue;
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
$coverDir = __DIR__ . '/../../public/assets/' . $slug;
|
| 422 |
+
$coverUrl = null;
|
| 423 |
+
foreach (['png', 'jpg', 'jpeg'] as $ext) {
|
| 424 |
+
if (file_exists($coverDir . '/cover.' . $ext)) {
|
| 425 |
+
$coverUrl = url('/assets/' . $slug . '/cover.' . $ext);
|
| 426 |
+
break;
|
| 427 |
+
}
|
| 428 |
+
}
|
| 429 |
+
if (!$coverUrl) {
|
| 430 |
+
$coverUrl = url('assets/images/book-placeholder.svg');
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
$result[] = [
|
| 434 |
+
'slug' => $slug,
|
| 435 |
+
'title' => $book['title'] ?? $slug,
|
| 436 |
+
'year' => $book['year'] ?? null,
|
| 437 |
+
'totalPages' => $book['totalPages'] ?? 0,
|
| 438 |
+
'type' => $book['type'] ?? 'docx',
|
| 439 |
+
'coverUrl' => $coverUrl,
|
| 440 |
+
];
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
$total = count($result);
|
| 444 |
+
$offset = ($page - 1) * $perPage;
|
| 445 |
+
$paged = array_slice($result, $offset, $perPage);
|
| 446 |
+
|
| 447 |
+
$this->json([
|
| 448 |
+
'books' => $paged,
|
| 449 |
+
'total' => $total,
|
| 450 |
+
'page' => $page,
|
| 451 |
+
'perPage' => $perPage,
|
| 452 |
+
'totalPages' => (int)ceil($total / $perPage),
|
| 453 |
+
]);
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
private function json($data) {
|
| 457 |
header('Content-Type: application/json; charset=utf-8');
|
| 458 |
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
routes/web.php
CHANGED
|
@@ -24,9 +24,13 @@ return [
|
|
| 24 |
'/search-books' => 'PdfBookController@index',
|
| 25 |
'/search-books/{slug}' => 'PdfBookController@viewer',
|
| 26 |
'/search-books/{slug}/page/{n}' => 'PdfBookController@show',
|
|
|
|
|
|
|
|
|
|
| 27 |
'/api/search-books/all' => 'PdfBookController@searchAll',
|
| 28 |
'/api/search-books/search' => 'PdfBookController@search',
|
| 29 |
-
'/api/search-books/page'
|
|
|
|
| 30 |
'/api/tts/synthesize' => 'TtsController@synthesize',
|
| 31 |
'/api/tts/check' => 'TtsController@check',
|
| 32 |
'/api/tts/play/{hash}' => 'TtsController@play',
|
|
|
|
| 24 |
'/search-books' => 'PdfBookController@index',
|
| 25 |
'/search-books/{slug}' => 'PdfBookController@viewer',
|
| 26 |
'/search-books/{slug}/page/{n}' => 'PdfBookController@show',
|
| 27 |
+
'/api/search-books/list' => 'PdfBookController@apiList',
|
| 28 |
+
'/api/search-books/details' => 'PdfBookController@apiDetails',
|
| 29 |
+
'/api/search-books/filter' => 'PdfBookController@apiFilter',
|
| 30 |
'/api/search-books/all' => 'PdfBookController@searchAll',
|
| 31 |
'/api/search-books/search' => 'PdfBookController@search',
|
| 32 |
+
'/api/search-books/page' => 'PdfBookController@page',
|
| 33 |
+
'/api/search-books/page-nav' => 'PdfBookController@apiPageNav',
|
| 34 |
'/api/tts/synthesize' => 'TtsController@synthesize',
|
| 35 |
'/api/tts/check' => 'TtsController@check',
|
| 36 |
'/api/tts/play/{hash}' => 'TtsController@play',
|