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;
|
2020-09-13 22:04:07 +00:00
|
|
|
use Illuminate\Http\Response;
|
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;
|
|
|
|
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,
|
2018-12-09 21:24:43 +00:00
|
|
|
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
|
|
|
public function put(PutSongRequest $request)
|
|
|
|
{
|
|
|
|
$path = "s3://{$request->bucket}/{$request->key}";
|
|
|
|
|
|
|
|
$tags = $request->tags;
|
2020-11-14 16:57:25 +00:00
|
|
|
$artist = Artist::getOrCreate(array_get($tags, 'artist'));
|
2016-06-13 09:04:42 +00:00
|
|
|
|
|
|
|
$compilation = (bool) trim(array_get($tags, 'albumartist'));
|
2020-11-14 16:57:25 +00:00
|
|
|
$album = Album::getOrCreate($artist, array_get($tags, 'album'), $compilation);
|
2020-12-22 20:11:22 +00:00
|
|
|
$cover = array_get($tags, 'cover');
|
2016-06-13 09:04:42 +00:00
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
if ($cover) {
|
|
|
|
$this->mediaMetadataService->writeAlbumCover(
|
|
|
|
$album,
|
|
|
|
base64_decode($cover['data'], true),
|
|
|
|
$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,
|
2017-04-29 03:49:14 +00:00
|
|
|
'artist_id' => $artist->id,
|
2016-06-13 09:04:42 +00:00
|
|
|
'title' => trim(array_get($tags, 'title', '')),
|
2018-06-16 09:29:41 +00:00
|
|
|
'length' => array_get($tags, 'duration', 0) ?: 0,
|
2016-08-03 10:42:11 +00:00
|
|
|
'track' => (int) array_get($tags, 'track'),
|
2018-06-16 09:29:41 +00:00
|
|
|
'lyrics' => array_get($tags, 'lyrics', '') ?: '',
|
2016-06-13 09:04:42 +00:00
|
|
|
'mtime' => time(),
|
|
|
|
]);
|
2018-08-19 15:26:34 +00:00
|
|
|
|
2018-06-16 09:29:41 +00:00
|
|
|
event(new LibraryChanged());
|
2016-06-13 09:04:42 +00:00
|
|
|
|
|
|
|
return response()->json($song);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove(RemoveSongRequest $request)
|
|
|
|
{
|
2018-08-29 06:15:11 +00:00
|
|
|
$song = $this->songRepository->getOneByPath("s3://{$request->bucket}/{$request->key}");
|
2020-09-13 22:04:07 +00:00
|
|
|
abort_unless((bool) $song, Response::HTTP_NOT_FOUND);
|
2018-08-29 06:15:11 +00:00
|
|
|
|
2016-06-13 09:04:42 +00:00
|
|
|
$song->delete();
|
|
|
|
event(new LibraryChanged());
|
|
|
|
|
2020-09-13 22:04:07 +00:00
|
|
|
return response()->json(null, Response::HTTP_NO_CONTENT);
|
2016-06-13 09:04:42 +00:00
|
|
|
}
|
|
|
|
}
|