From 515031d8540e40ae7bf44e340167b218a4855773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Wed, 19 Mar 2014 20:10:21 +0100 Subject: [PATCH] Extend the URL rewriting url to the OPDS stream. fix #141 --- data.php | 39 +++++++++++++++++++++++---------------- test/bookTest.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 16 deletions(-) diff --git a/data.php b/data.php index 3600fe0..3d8da77 100644 --- a/data.php +++ b/data.php @@ -103,32 +103,39 @@ class Data extends Base { } public function getDataLink ($rel, $title = NULL) { + global $config; + + if ($rel == Link::OPDS_ACQUISITION_TYPE && $config['cops_use_url_rewriting'] == "1") { + return $this->getHtmlLinkWithRewriting($title); + } + return self::getLink ($this->book, $this->extension, $this->getMimeType (), $rel, $this->getFilename (), $this->id, $title); } + public function getHtmlLink () { + return $this->getDataLink(Link::OPDS_ACQUISITION_TYPE)->href; + } + public function getLocalPath () { return $this->book->path . "/" . $this->getFilename (); } - public function getHtmlLink () { + public function getHtmlLinkWithRewriting ($title = NULL) { global $config; - if ($config['cops_use_url_rewriting'] == "1") - { - $database = ""; - if (!is_null (GetUrlParam (DB))) $database = GetUrlParam (DB) . "/"; - if ($config['cops_provide_kepub'] == "1" && - $this->isEpubValidOnKobo () && - preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) { - return "download/" . $this->id . "/" . $database . urlencode ($this->getUpdatedFilenameKepub ()); - } else { - return "download/" . $this->id . "/" . $database . urlencode ($this->getFilename ()); - } - } - else - { - return self::getLink ($this->book, $this->extension, $this->getMimeType (), NULL, $this->getFilename (), $this->id, NULL)->href; + $database = ""; + if (!is_null (GetUrlParam (DB))) $database = GetUrlParam (DB) . "/"; + + $href = "download/" . $this->id . "/" . $database; + + if ($config['cops_provide_kepub'] == "1" && + $this->isEpubValidOnKobo () && + preg_match("/Kobo/", $_SERVER['HTTP_USER_AGENT'])) { + $href .= urlencode ($this->getUpdatedFilenameKepub ()); + } else { + $href .= urlencode ($this->getFilename ()); } + return new Link ($href, $this->getMimeType (), Link::OPDS_ACQUISITION_TYPE, $title); } public static function getDataByBook ($book) { diff --git a/test/bookTest.php b/test/bookTest.php index 14daf27..441fbd4 100644 --- a/test/bookTest.php +++ b/test/bookTest.php @@ -195,6 +195,40 @@ class BookTest extends PHPUnit_Framework_TestCase $this->assertEquals ("Strand Magazine", $book->getPublisher()->name); } + public function testBookGetLinkArrayWithUrlRewriting () + { + global $config; + + $book = Book::getBookById(2); + $config['cops_use_url_rewriting'] = "1"; + + $linkArray = $book->getLinkArray (); + foreach ($linkArray as $link) { + if ($link->rel == Link::OPDS_ACQUISITION_TYPE && $link->title == "EPUB" ) { + $this->assertEquals ("download/1/The+Return+of+Sherlock+Holmes+-+Arthur+Conan+Doyle.epub", $link->href); + return; + } + } + $this->fail (); + } + + public function testBookGetLinkArrayWithoutUrlRewriting () + { + global $config; + + $book = Book::getBookById(2); + $config['cops_use_url_rewriting'] = "0"; + + $linkArray = $book->getLinkArray (); + foreach ($linkArray as $link) { + if ($link->rel == Link::OPDS_ACQUISITION_TYPE && $link->title == "EPUB" ) { + $this->assertEquals ("fetch.php?data=1&type=epub&id=2", $link->href); + return; + } + } + $this->fail (); + } + public function testGetThumbnailNotNeeded () { $book = Book::getBookById(2);