Skip to content

Commit

Permalink
Priority on cover filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
leizh committed Mar 18, 2014
1 parent 517bbec commit 65445ca
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions db/albummapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,33 @@ public function getAlbumsWithoutCover(){
}

public function findAlbumCover($albumId, $parentFolderId){
$sql = 'UPDATE `*PREFIX*music_albums`
SET `cover_file_id` = (
SELECT `fileid`
$coverNames = array("front", "cover");
$imagesSql = 'SELECT `fileid`, `name`
FROM `*PREFIX*filecache`
JOIN `*PREFIX*mimetypes` ON `*PREFIX*mimetypes`.`id` = `*PREFIX*filecache`.`mimetype`
WHERE `parent` = ? AND `*PREFIX*mimetypes`.`mimetype` LIKE \'image%\' LIMIT 1
) WHERE `id` = ?';
$params = array($parentFolderId, $albumId);
WHERE `parent` = ? AND `*PREFIX*mimetypes`.`mimetype` LIKE \'image%\'';
$params = array($parentFolderId);
$result = $this->execute($imagesSql, $params);
$images = $result->fetchAll();
$imageId = null;
if (count($images)) {
usort($images, function ($imageA, $imageB) use ($coverNames) {
$nameA = strtolower($imageA['name']);
$nameB = strtolower($imageB['name']);
$indexA = PHP_INT_MAX;
$indexB = PHP_INT_MAX;
foreach ($coverNames as $i => $coverName) {
if ($indexA == PHP_INT_MAX && strpos($nameA, $coverName) === 0) $indexA = $i;
if ($indexB == PHP_INT_MAX && strpos($nameB, $coverName) === 0) $indexB = $i;
if ($indexA != PHP_INT_MAX && $indexB != PHP_INT_MAX) break;
}
return $indexA > $indexB;
});
$imageId = $images[0]['fileid'];
};
$sql = 'UPDATE `*PREFIX*music_albums`
SET `cover_file_id` = ? WHERE `id` = ?';
$params = array($imageId, $albumId);
$this->execute($sql, $params);
}

Expand Down

0 comments on commit 65445ca

Please sign in to comment.