diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 14:16:07 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 14:16:07 +0300 |
| commit | 1bdf80397c19362c019cdd231e8813e251764370 (patch) | |
| tree | 126c1e0ffcc5664ac2b5883bec64007070411a9b /classes/Handler/DownloadHandler.php | |
| parent | 1917ab77550613654620b6f5d4ad8570704246de (diff) | |
| download | the-epube-1bdf80397c19362c019cdd231e8813e251764370.tar.gz the-epube-1bdf80397c19362c019cdd231e8813e251764370.tar.xz | |
switch to PSR-4 router with namespaced handlers
Diffstat (limited to 'classes/Handler/DownloadHandler.php')
| -rw-r--r-- | classes/Handler/DownloadHandler.php | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/classes/Handler/DownloadHandler.php b/classes/Handler/DownloadHandler.php new file mode 100644 index 0000000..db8663a --- /dev/null +++ b/classes/Handler/DownloadHandler.php @@ -0,0 +1,35 @@ +<?php + +namespace Handler; + +use Config; +use Request; +use RequestHandlerInterface; +use Response; +use PDO; + +class DownloadHandler implements RequestHandlerInterface +{ + public function handle(Request $request): Response + { + $bookid = (int) $request->getAttribute('id'); + $caldb = new PDO('sqlite:' . Config::get(Config::CALIBRE_DB)); + $sth = $caldb->prepare("SELECT path, name, format FROM data LEFT JOIN books ON (data.book = books.id) WHERE data.id = ?"); + $sth->execute([$bookid]); + + while ($row = $sth->fetch()) { + $filename = Config::get(Config::BOOKS_DIR) . "/" . $row["path"] . "/" . $row["name"] . "." . strtolower($row["format"]); + + if (file_exists($filename)) { + return (new Response()) + ->withHeader("Content-type", mime_content_type($filename)) + ->withHeader("Content-Disposition", 'attachment; filename="' . basename($filename) . '"') + ->withBody(readfile($filename) ?: ''); + } + } + + return (new Response()) + ->withStatus(404) + ->withBody("File not found."); + } +} |
