示例#1
0
 /**
  * Store a new song or update an existing one with data from AWS.
  *
  * @param PutSongRequest $request
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function put(PutSongRequest $request)
 {
     $path = "s3://{$request->bucket}/{$request->key}";
     $tags = $request->tags;
     $artist = Artist::get(array_get($tags, 'artist'));
     $compilation = (bool) trim(array_get($tags, 'albumartist'));
     $album = Album::get($artist, array_get($tags, 'album'), $compilation);
     if ($cover = array_get($tags, 'cover')) {
         $album->writeCoverFile(base64_decode($cover['data']), $cover['extension']);
     }
     $song = Song::updateOrCreate(['id' => Media::getHash($path)], ['path' => $path, 'album_id' => $album->id, 'contributing_artist_id' => $compilation ? $artist->id : null, 'title' => trim(array_get($tags, 'title', '')), 'length' => array_get($tags, 'duration', 0), 'track' => intval(array_get($tags, 'track')), 'lyrics' => array_get($tags, 'lyrics', ''), 'mtime' => time()]);
     return response()->json($song);
 }
示例#2
0
文件: Media.php 项目: Holdlen2DH/koel
 /**
  * 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;
 }
示例#3
0
文件: File.php 项目: phanan/koel
 /**
  * Sync the song with all available media info against the database.
  *
  * @param array $tags  The (selective) tags to sync (if the song exists)
  * @param bool  $force Whether to force syncing, even if the file is unchanged
  *
  * @return bool|Song A Song object on success,
  *                   true if file exists but is unmodified,
  *                   or false on an error.
  */
 public function sync($tags, $force = false)
 {
     // If the file is not new or changed and we're not forcing update, don't do anything.
     if (!$this->isNewOrChanged() && !$force) {
         return true;
     }
     // If the file is invalid, don't do anything.
     if (!($info = $this->getInfo())) {
         return false;
     }
     // Fixes #366. If the file is new, we use all tags by simply setting $force to false.
     if ($this->isNew()) {
         $force = false;
     }
     $artist = null;
     if ($this->isChanged() || $force) {
         // This is a changed file, or the user is forcing updates.
         // In such a case, the user must have specified a list of tags to sync.
         // A sample command could be: ./artisan koel:sync --force --tags=artist,album,lyrics
         // We cater for these tags by removing those not specified.
         // There's a special case with 'album' though.
         // If 'compilation' tag is specified, 'album' must be counted in as well.
         // But if 'album' isn't specified, we don't want to update normal albums.
         // This variable is to keep track of this state.
         $changeCompilationAlbumOnly = false;
         if (in_array('compilation', $tags, true) && !in_array('album', $tags, true)) {
             $tags[] = 'album';
             $changeCompilationAlbumOnly = true;
         }
         $info = array_intersect_key($info, array_flip($tags));
         // If the "artist" tag is specified, use it.
         // Otherwise, re-use the existing model value.
         $artist = isset($info['artist']) ? Artist::get($info['artist']) : $this->song->album->artist;
         $isCompilation = (bool) array_get($info, 'compilation');
         // If the "album" tag is specified, use it.
         // Otherwise, re-use the existing model value.
         if (isset($info['album'])) {
             $album = $changeCompilationAlbumOnly ? $this->song->album : Album::get($artist, $info['album'], $isCompilation);
         } else {
             $album = $this->song->album;
         }
     } else {
         // The file is newly added.
         $isCompilation = (bool) array_get($info, 'compilation');
         $artist = Artist::get($info['artist']);
         $album = Album::get($artist, $info['album'], $isCompilation);
     }
     if (!$album->has_cover) {
         // If the album has no cover, we try to get the cover image from existing tag data
         if (!empty($info['cover'])) {
             try {
                 $album->generateCover($info['cover']);
             } catch (Exception $e) {
                 Log::error($e);
             }
         } elseif ($cover = $this->getCoverFileUnderSameDirectory()) {
             $album->copyCoverFile($cover);
         }
     }
     $info['album_id'] = $album->id;
     // If the song is part of a compilation, make sure we properly set its
     // artist and contributing artist attributes.
     if ($isCompilation) {
         $info['contributing_artist_id'] = $artist->id;
     }
     // Remove these values from the info array, so that we can just use the array as model's input data.
     array_forget($info, ['artist', 'albumartist', 'album', 'cover', 'compilation']);
     return Song::updateOrCreate(['id' => $this->hash], $info);
 }
示例#4
0
文件: File.php 项目: everdaniel/koel
 /**
  * Sync the song with all available media info against the database.
  *
  * @param array $tags  The (selective) tags to sync (if the song exists)
  * @param bool  $force Whether to force syncing, even if the file is unchaged
  *
  * @return bool|Song A Song object on success,
  *                   true if file exists but is unmodified,
  *                   or false on an error.
  */
 public function sync($tags, $force = false)
 {
     // If the file is not new or changed and we're not forcing update, don't do anything.
     if (!$this->isNewOrChanged() && !$force) {
         return true;
     }
     // If the file is invalid, don't do anything.
     if (!($info = $this->getInfo())) {
         return false;
     }
     if ($this->isChanged() || $force) {
         // This is a changed file, or the user is forcing updates.
         // We cater for the tags by removing those not specified.
         $info = array_intersect_key($info, array_flip($tags));
         $artist = isset($info['artist']) ? Artist::get($info['artist']) : $this->song->album->artist;
         $album = isset($info['album']) ? Album::get($artist, $info['album']) : $this->song->album;
     } else {
         $album = Album::get(Artist::get($info['artist']), $info['album']);
     }
     if (!empty($info['cover']) && !$album->has_cover) {
         try {
             $album->generateCover($info['cover']);
         } catch (Exception $e) {
             Log::error($e);
         }
     }
     $info['album_id'] = $album->id;
     // Remove these values from the info array, so that we can just use the array as model's input data.
     array_forget($info, ['artist', 'album', 'cover']);
     $song = Song::updateOrCreate(['id' => $this->hash], $info);
     $song->save();
     return $song;
 }
示例#5
0
文件: File.php 项目: carriercomm/koel
 /**
  * Sync the song with all available media info against the database.
  *
  * @param array $tags  The (selective) tags to sync (if the song exists)
  * @param bool  $force Whether to force syncing, even if the file is unchaged
  *
  * @return bool|Song A Song object on success,
  *                   true if file exists but is unmodified,
  *                   or false on an error.
  */
 public function sync($tags, $force = false)
 {
     // If the file is not new or changed and we're not forcing update, don't do anything.
     if (!$this->isNewOrChanged() && !$force) {
         return true;
     }
     // If the file is invalid, don't do anything.
     if (!($info = $this->getInfo())) {
         return false;
     }
     $isCompilation = false;
     $artist = null;
     if ($this->isChanged() || $force) {
         // This is a changed file, or the user is forcing updates.
         // In such a case, the user must have specified a list of tags to sync.
         // A sample command could be: ./artisan koel:sync --force --tags=artist,album,lyrics
         // We cater for these tags by removing those not specified.
         if (in_array('part_of_a_compilation', $tags) && !in_array('album', $tags)) {
             // If 'part_of_a_compilation' tag is specified, 'album' must be counted in as well.
             $tags[] = 'album';
         }
         $info = array_intersect_key($info, array_flip($tags));
         // If the "artist" tag is specified, use it.
         // Otherwise, re-use the existing model value.
         $artist = isset($info['artist']) ? Artist::get($info['artist']) : $this->song->album->artist;
         $isCompilation = (bool) array_get($info, 'part_of_a_compilation');
         // If the "album" tag is specified, use it.
         // Otherwise, re-use the existing model value.
         $album = isset($info['album']) ? Album::get($artist, $info['album'], $isCompilation) : $this->song->album;
     } else {
         // The file is newly added.
         $isCompilation = (bool) array_get($info, 'part_of_a_compilation');
         $artist = Artist::get($info['artist']);
         $album = Album::get($artist, $info['album'], $isCompilation);
     }
     if (!empty($info['cover']) && !$album->has_cover) {
         try {
             $album->generateCover($info['cover']);
         } catch (Exception $e) {
             Log::error($e);
         }
     }
     $info['album_id'] = $album->id;
     // If the song is part of a compilation, make sure we properly set its
     // artist and contributing artist attributes.
     if ($isCompilation) {
         $info['contributing_artist_id'] = $artist->id;
     }
     // Remove these values from the info array, so that we can just use the array as model's input data.
     array_forget($info, ['artist', 'album', 'cover', 'part_of_a_compilation']);
     $song = Song::updateOrCreate(['id' => $this->hash], $info);
     $song->save();
     return $song;
 }