koel/app/Http/Controllers/API/ObjectStorage/S3/SongController.php

89 lines
2.6 KiB
PHP
Raw Normal View History

2016-06-13 09:04:42 +00:00
<?php
namespace App\Http\Controllers\API\ObjectStorage\S3;
use App\Events\LibraryChanged;
use App\Http\Requests\API\ObjectStorage\S3\PutSongRequest;
use App\Http\Requests\API\ObjectStorage\S3\RemoveSongRequest;
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
2018-08-29 06:15:11 +00:00
use App\Repositories\SongRepository;
use App\Services\HelperService;
2018-08-19 09:05:33 +00:00
use App\Services\MediaMetadataService;
2017-06-04 01:30:45 +00:00
use Exception;
use Illuminate\Http\JsonResponse;
2016-06-13 09:04:42 +00:00
class SongController extends Controller
{
2018-08-19 09:05:33 +00:00
private $mediaMetadataService;
2018-08-29 06:15:11 +00:00
private $songRepository;
/**
* @var HelperService
*/
private $helperService;
2018-08-19 09:05:33 +00:00
2018-08-29 06:15:11 +00:00
public function __construct(
MediaMetadataService $mediaMetadataService,
HelperService $helperService,
SongRepository $songRepository)
2018-08-19 09:05:33 +00:00
{
$this->mediaMetadataService = $mediaMetadataService;
2018-08-29 06:15:11 +00:00
$this->songRepository = $songRepository;
$this->helperService = $helperService;
2018-08-19 09:05:33 +00:00
}
2016-06-13 09:04:42 +00:00
/**
* Store a new song or update an existing one with data from AWS.
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2016-06-13 09:04:42 +00:00
*/
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')) {
2018-08-19 09:05:33 +00:00
$this->mediaMetadataService->writeAlbumCover($album, base64_decode($cover['data']), $cover['extension']);
2016-06-13 09:04:42 +00:00
}
2018-08-29 06:15:11 +00:00
$song = Song::updateOrCreate(['id' => $this->helperService->getFileHash($path)], [
2016-06-13 09:04:42 +00:00
'path' => $path,
'album_id' => $album->id,
'artist_id' => $artist->id,
2016-06-13 09:04:42 +00:00
'title' => trim(array_get($tags, 'title', '')),
'length' => array_get($tags, 'duration', 0) ?: 0,
2016-08-03 10:42:11 +00:00
'track' => (int) array_get($tags, 'track'),
'lyrics' => array_get($tags, 'lyrics', '') ?: '',
2016-06-13 09:04:42 +00:00
'mtime' => time(),
]);
2018-08-19 15:26:34 +00:00
event(new LibraryChanged());
2016-06-13 09:04:42 +00:00
return response()->json($song);
}
/**
* Remove a song whose info matches with data sent from AWS.
*
2017-06-04 01:30:45 +00:00
* @throws Exception
2016-08-16 15:12:35 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2016-06-13 09:04:42 +00:00
*/
public function remove(RemoveSongRequest $request)
{
2018-08-29 06:15:11 +00:00
$song = $this->songRepository->getOneByPath("s3://{$request->bucket}/{$request->key}");
2017-06-03 16:35:08 +00:00
abort_unless((bool) $song, 404);
2018-08-29 06:15:11 +00:00
2016-06-13 09:04:42 +00:00
$song->delete();
event(new LibraryChanged());
return response()->json();
}
}