1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\API\ObjectStorage\GCS; |
4
|
|
|
|
5
|
|
|
use App\Events\LibraryChanged; |
6
|
|
|
use App\Http\Requests\API\ObjectStorage\GCS\PutSongRequest; |
7
|
|
|
use App\Http\Requests\API\ObjectStorage\GCS\RemoveSongRequest; |
8
|
|
|
use App\Models\Album; |
9
|
|
|
use App\Models\Artist; |
10
|
|
|
use App\Models\Song; |
11
|
|
|
use Exception; |
12
|
|
|
use Illuminate\Http\JsonResponse; |
13
|
|
|
use Media; |
14
|
|
|
|
15
|
|
View Code Duplication |
class SongController extends Controller |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Store a new song or update an existing one with data from AWS. |
19
|
|
|
* |
20
|
|
|
* @param PutSongRequest $request |
21
|
|
|
* |
22
|
|
|
* @return JsonResponse |
23
|
|
|
*/ |
24
|
|
|
public function put(PutSongRequest $request) |
25
|
|
|
{ |
26
|
|
|
$path = "gs://{$request->bucket}/{$request->key}"; |
27
|
|
|
|
28
|
|
|
$tags = $request->tags; |
29
|
|
|
$artist = Artist::get(array_get($tags, 'artist')); |
30
|
|
|
|
31
|
|
|
$compilation = (bool) trim(array_get($tags, 'albumartist')); |
32
|
|
|
$album = Album::get($artist, array_get($tags, 'album'), $compilation); |
33
|
|
|
|
34
|
|
|
if ($cover = array_get($tags, 'cover')) { |
35
|
|
|
$album->writeCoverFile(base64_decode($cover['data']), $cover['extension']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$song = Song::updateOrCreate(['id' => Media::getHash($path)], [ |
39
|
|
|
'path' => $path, |
40
|
|
|
'album_id' => $album->id, |
41
|
|
|
'artist_id' => $artist->id, |
42
|
|
|
'title' => trim(array_get($tags, 'title', '')), |
43
|
|
|
'length' => array_get($tags, 'duration', 0), |
44
|
|
|
'track' => (int) array_get($tags, 'track'), |
45
|
|
|
'lyrics' => array_get($tags, 'lyrics', ''), |
46
|
|
|
'mtime' => time(), |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
return response()->json($song); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Remove a song whose info matches with data sent from AWS. |
54
|
|
|
* |
55
|
|
|
* @param RemoveSongRequest $request |
56
|
|
|
* |
57
|
|
|
* @throws Exception |
58
|
|
|
* |
59
|
|
|
* * @return JsonResponse |
60
|
|
|
*/ |
61
|
|
|
public function remove(RemoveSongRequest $request) |
62
|
|
|
{ |
63
|
|
|
$song = Song::byPath("gs://{$request->bucket}/{$request->key}"); |
64
|
|
|
abort_unless((bool) $song, 404); |
65
|
|
|
$song->delete(); |
66
|
|
|
event(new LibraryChanged()); |
67
|
|
|
return response()->json(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.