From 88872926d13420e8b9752cc0087687bff63787cf Mon Sep 17 00:00:00 2001 From: Ben XO <75862+ben-xo@users.noreply.github.com> Date: Wed, 24 Aug 2022 09:53:37 +0100 Subject: [PATCH] Fix #61 - image file names were not subdir aware --- dir2cast.php | 19 ++++++++++++------- test/RSS_Item_getID3_Podcast_HelperTest.php | 2 +- ...ID3_Podcast_Helper_AUTO_SAVE_COVERTest.php | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/dir2cast.php b/dir2cast.php index 45cc1ef..7d94e35 100644 --- a/dir2cast.php +++ b/dir2cast.php @@ -590,14 +590,14 @@ public function setFilename($filename) protected function filenameToUrl($filename) { - return rtrim(self::$FILES_URL, '/') . '/' . str_replace('%2F', '/', rawurlencode($this->stripBasePath($filename))); + return rtrim(RSS_File_Item::$FILES_URL, '/') . '/' . str_replace('%2F', '/', rawurlencode($this->stripBasePath($filename))); } protected function stripBasePath($filename) { - if(strlen(self::$FILES_DIR) && strpos($filename, self::$FILES_DIR) === 0) + if(strlen(RSS_File_Item::$FILES_DIR) && strpos($filename, RSS_File_Item::$FILES_DIR) === 0) { - return ltrim(substr($filename, strlen(self::$FILES_DIR)), '/'); + return ltrim(substr($filename, strlen(RSS_File_Item::$FILES_DIR)), '/'); } return $filename; } @@ -685,12 +685,17 @@ public function getSubtitle() protected function getImageFilename($type) { - $image_file_name = basename($this->getFilename(), '.' . $this->getExtension()) . '.' . $type; - if(strpos($image_file_name, '/') === false) + $item_file_name = $this->getFilename(); + $ext_length = strlen($this->getExtension()); + if($ext_length == 0) { - return $image_file_name; + $image_file_name = rtrim($item_file_name, '.') . '.' . $type; } - return dirname($this->getFilename()) . '/' . $image_file_name; + else { + $item_file_name_length = strlen($this->getFilename()); + $image_file_name = rtrim(substr($item_file_name, 0, $item_file_name_length - $ext_length), '.') . '.' . $type; + } + return $image_file_name; } /** diff --git a/test/RSS_Item_getID3_Podcast_HelperTest.php b/test/RSS_Item_getID3_Podcast_HelperTest.php index fe0ccb4..c20e467 100644 --- a/test/RSS_Item_getID3_Podcast_HelperTest.php +++ b/test/RSS_Item_getID3_Podcast_HelperTest.php @@ -7,10 +7,10 @@ class RSS_Item_getID3_Podcast_HelperTest extends TestCase public static function setUpBeforeClass(): void { + prepare_testing_dir(); getID3_Podcast_Helper::$AUTO_SAVE_COVER_ART = false; RSS_File_Item::$FILES_URL = 'http://www.example.com/mp3/'; RSS_File_Item::$FILES_DIR = getcwd(); - prepare_testing_dir(); } public function newHelper() diff --git a/test/RSS_Item_getID3_Podcast_Helper_AUTO_SAVE_COVERTest.php b/test/RSS_Item_getID3_Podcast_Helper_AUTO_SAVE_COVERTest.php index fb862b4..2fa0fb8 100644 --- a/test/RSS_Item_getID3_Podcast_Helper_AUTO_SAVE_COVERTest.php +++ b/test/RSS_Item_getID3_Podcast_Helper_AUTO_SAVE_COVERTest.php @@ -10,6 +10,7 @@ public static function setUpBeforeClass(): void { RSS_Item_getID3_Podcast_HelperTest::setUpBeforeClass(); getID3_Podcast_Helper::$AUTO_SAVE_COVER_ART = true; + // Dir_Podcast::$RECURSIVE_DIRECTORY_ITERATOR = true; } public function test_mp4_tagged_cover() @@ -18,16 +19,23 @@ public function test_mp4_tagged_cover() $helper = new getID3_Podcast_Helper(); $mp->addHelper($helper); + mkdir('subdir'); copy('../fixtures/tagged_with_cover.mp4', './tagged_with_cover.mp4'); - $item = new Media_RSS_Item('tagged_with_cover.mp4'); + copy('../fixtures/tagged_with_cover.mp4', './subdir/tagged_with_cover2.mp4'); + $item = new Media_RSS_Item(RSS_File_Item::$FILES_DIR . '/tagged_with_cover.mp4'); + $item2 = new Media_RSS_Item(RSS_File_Item::$FILES_DIR . '/subdir/tagged_with_cover2.mp4'); $mp->addRssItem($item); + $mp->addRssItem($item2); $content = $mp->generate(); $this->assertEquals('http://www.example.com/mp3/tagged_with_cover.jpg', $item->getImage()); + $this->assertEquals('http://www.example.com/mp3/subdir/tagged_with_cover2.jpg', $item2->getImage()); $this->assertTrue(file_exists('tagged_with_cover.jpg')); + $this->assertTrue(file_exists('subdir/tagged_with_cover2.jpg')); $this->assertEquals(file_get_contents('tagged_with_cover.jpg'), file_get_contents('../fixtures/empty.jpg')); + $this->assertEquals(file_get_contents('subdir/tagged_with_cover2.jpg'), file_get_contents('../fixtures/empty.jpg')); } public function test_id3v2_artist_album_title_with_cover() @@ -36,16 +44,23 @@ public function test_id3v2_artist_album_title_with_cover() $helper = new getID3_Podcast_Helper(); $mp->addHelper($helper); + mkdir('another.subdir'); copy('../fixtures/id3v2_artist_album_title_cover.mp3', './id3v2_artist_album_title_cover.mp3'); + copy('../fixtures/id3v2_artist_album_title_cover.mp3', './another.subdir/id3v2_artist_album_title_cover.mp3'); $item = new Media_RSS_Item('id3v2_artist_album_title_cover.mp3'); + $item2 = new Media_RSS_Item('another.subdir/id3v2_artist_album_title_cover.mp3'); $mp->addRssItem($item); + $mp->addRssItem($item2); $content = $mp->generate(); $this->assertEquals('http://www.example.com/mp3/id3v2_artist_album_title_cover.jpg', $item->getImage()); + $this->assertEquals('http://www.example.com/mp3/another.subdir/id3v2_artist_album_title_cover.jpg', $item2->getImage()); $this->assertTrue(file_exists('id3v2_artist_album_title_cover.jpg')); + $this->assertTrue(file_exists('another.subdir/id3v2_artist_album_title_cover.jpg')); $this->assertEquals(file_get_contents('id3v2_artist_album_title_cover.jpg'), file_get_contents('../fixtures/empty.jpg')); + $this->assertEquals(file_get_contents('another.subdir/id3v2_artist_album_title_cover.jpg'), file_get_contents('../fixtures/empty.jpg')); } public static function tearDownAfterClass(): void