Skip to content

Commit

Permalink
Fix ben-xo#61 - image file names were not subdir aware
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-xo committed Aug 24, 2022
1 parent b43ff05 commit 8887292
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
19 changes: 12 additions & 7 deletions dir2cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/RSS_Item_getID3_Podcast_HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 16 additions & 1 deletion test/RSS_Item_getID3_Podcast_Helper_AUTO_SAVE_COVERTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 8887292

Please sign in to comment.