1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\API\ObjectStorage\S3; |
4
|
|
|
|
5
|
|
|
use App\Events\LibraryChanged; |
6
|
|
|
use App\Http\Requests\API\ObjectStorage\S3\PutSongRequest; |
7
|
|
|
use App\Http\Requests\API\ObjectStorage\S3\RemoveSongRequest; |
8
|
|
|
use App\Models\Album; |
9
|
|
|
use App\Models\Artist; |
10
|
|
|
use App\Models\Song; |
11
|
|
|
use App\Repositories\SongRepository; |
12
|
|
|
use App\Services\HelperService; |
13
|
|
|
use App\Services\MediaMetadataService; |
14
|
|
|
use Exception; |
15
|
|
|
use Illuminate\Http\JsonResponse; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @group AWS integration |
19
|
|
|
* |
20
|
|
|
* These routes are meant for Amazon Web Services (AWS) integration with Koel. For more information, visit |
21
|
|
|
* [koel-aws](https://github.com/koel/koel-aws). |
22
|
|
|
*/ |
23
|
|
|
class SongController extends Controller |
24
|
|
|
{ |
25
|
|
|
private $mediaMetadataService; |
26
|
|
|
private $songRepository; |
27
|
|
|
private $helperService; |
28
|
|
|
|
29
|
2 |
|
public function __construct( |
30
|
|
|
MediaMetadataService $mediaMetadataService, |
31
|
|
|
HelperService $helperService, |
32
|
|
|
SongRepository $songRepository |
33
|
|
|
) { |
34
|
2 |
|
$this->mediaMetadataService = $mediaMetadataService; |
35
|
2 |
|
$this->songRepository = $songRepository; |
36
|
2 |
|
$this->helperService = $helperService; |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Store a song. |
41
|
|
|
* |
42
|
|
|
* Create a new song or update an existing one with data sent from AWS. |
43
|
|
|
* |
44
|
|
|
* @return JsonResponse |
45
|
|
|
*/ |
46
|
1 |
|
public function put(PutSongRequest $request) |
47
|
|
|
{ |
48
|
1 |
|
$path = "s3://{$request->bucket}/{$request->key}"; |
49
|
|
|
|
50
|
1 |
|
$tags = $request->tags; |
51
|
1 |
|
$artist = Artist::get(array_get($tags, 'artist')); |
|
|
|
|
52
|
|
|
|
53
|
1 |
|
$compilation = (bool) trim(array_get($tags, 'albumartist')); |
|
|
|
|
54
|
1 |
|
$album = Album::get($artist, array_get($tags, 'album'), $compilation); |
|
|
|
|
55
|
|
|
|
56
|
1 |
|
if ($cover = array_get($tags, 'cover')) { |
57
|
|
|
$this->mediaMetadataService->writeAlbumCover($album, base64_decode($cover['data']), $cover['extension']); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$song = Song::updateOrCreate(['id' => $this->helperService->getFileHash($path)], [ |
61
|
1 |
|
'path' => $path, |
62
|
1 |
|
'album_id' => $album->id, |
63
|
1 |
|
'artist_id' => $artist->id, |
64
|
1 |
|
'title' => trim(array_get($tags, 'title', '')), |
65
|
1 |
|
'length' => array_get($tags, 'duration', 0) ?: 0, |
66
|
1 |
|
'track' => (int) array_get($tags, 'track'), |
67
|
1 |
|
'lyrics' => array_get($tags, 'lyrics', '') ?: '', |
68
|
1 |
|
'mtime' => time(), |
69
|
|
|
]); |
70
|
|
|
|
71
|
1 |
|
event(new LibraryChanged()); |
72
|
|
|
|
73
|
1 |
|
return response()->json($song); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Remove a song. |
78
|
|
|
* |
79
|
|
|
* Remove a song whose information matches with data sent from AWS. |
80
|
|
|
* |
81
|
|
|
* @throws Exception |
82
|
|
|
* |
83
|
|
|
* @return JsonResponse |
84
|
|
|
*/ |
85
|
1 |
|
public function remove(RemoveSongRequest $request) |
86
|
|
|
{ |
87
|
1 |
|
$song = $this->songRepository->getOneByPath("s3://{$request->bucket}/{$request->key}"); |
88
|
1 |
|
abort_unless((bool) $song, 404); |
89
|
|
|
|
90
|
1 |
|
$song->delete(); |
91
|
1 |
|
event(new LibraryChanged()); |
92
|
|
|
|
93
|
1 |
|
return response()->json(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|