public static function get($name)
{
// Remove the BOM from UTF-8/16/32, as it will mess up the database constraints.
if ($encoding = Util::detectUTFEncoding($name)) {
$name = mb_convert_encoding($name, 'UTF-8', $encoding);
}
$name = trim($name) ?: self::UNKNOWN_NAME;
return self::firstOrCreate(compact('name'), compact('name'));
}
/** * Sync a song with all available media info against the database. * * @param SplFileInfo $file The SplFileInfo instance of the file. * * @return bool|Song A Song object on success, * true if file existing but unmodified, * or false on error. */ public function syncFile(SplFileInfo $file) { if (!($info = $this->getInfo($file))) { return false; } if (!$this->isNewOrChanged($file)) { return true; } $artist = Artist::get($info['artist']); $album = Album::get($artist, $info['album']); if ($info['cover'] && !$album->has_cover) { try { $album->generateCover($info['cover']); } catch (Exception $e) { Log::error($e); } } $info['album_id'] = $album->id; unset($info['artist']); unset($info['album']); unset($info['cover']); $song = Song::updateOrCreate(['id' => $this->getHash($file->getPathname())], $info); $song->save(); return $song; }