Backend for songs.zachdecook.com
Playlist: add m3u files
Zach DeCook 2024-06-06
parent cde3d35 · commit 1eb554e
-rw-r--r--laravel/app/Http/Controllers/PlaylistController.php5
-rw-r--r--laravel/app/Playlist.php10
-rw-r--r--laravel/resources/views/playlist.blade.php1
-rw-r--r--laravel/routes/web.php1
4 files changed, 17 insertions, 0 deletions
diff --git a/laravel/app/Http/Controllers/PlaylistController.php b/laravel/app/Http/Controllers/PlaylistController.php
index c055601..96b2dad 100644
--- a/laravel/app/Http/Controllers/PlaylistController.php
+++ b/laravel/app/Http/Controllers/PlaylistController.php
@@ -27,4 +27,9 @@ class PlaylistController extends Controller
// Do people want to see the favorites playlist when they've added to it, or stay on the song?
return redirect()->route('playlist.show', [ 'playlist' => $playlistName]);
}
+ public function m3u($playlistName)
+ {
+ $pl = Playlist::where('name', $playlistName)->firstOrFail();
+ return response($pl->m3u, 200)->header('Content-Type', 'audio/x-mpegurl');
+ }
}
diff --git a/laravel/app/Playlist.php b/laravel/app/Playlist.php
index 7284b0e..f56e1be 100644
--- a/laravel/app/Playlist.php
+++ b/laravel/app/Playlist.php
@@ -11,4 +11,14 @@ class Playlist extends Model
{
return $this->belongsToMany('App\Song');
}
+ public function getM3uAttribute()
+ {
+ $output = "#EXTM3U\n";
+ foreach($this->songs as $song) {
+ if ($song->audio){
+ $output .= config('app.url') . $song->audio . "\n";
+ }
+ }
+ return $output;
+ }
}
diff --git a/laravel/resources/views/playlist.blade.php b/laravel/resources/views/playlist.blade.php
index 23f192b..8817cd4 100644
--- a/laravel/resources/views/playlist.blade.php
+++ b/laravel/resources/views/playlist.blade.php
@@ -4,6 +4,7 @@
@section('content')
<h2>Playlist "<i>{{$playlist->name}}</i>"</h2>
+ <a href="/playlist/{{$playlist->name}}.m3u">Download audio playlist file</a>
<ul>
@foreach($playlist->songs as $song)
<li><a href='{{ route('song.show', ['song' => $song, 'playlist' => $playlist]) }}'>{{$song->name}}</a>
diff --git a/laravel/routes/web.php b/laravel/routes/web.php
index c783313..3618216 100644
--- a/laravel/routes/web.php
+++ b/laravel/routes/web.php
@@ -15,6 +15,7 @@ Route::get('/', function () {
return view('welcome');
})->name('song.list');
Route::post('/new/song', 'SongController@post')->name('song.postnew')/*->middleware('can:create,App\Song')*/;
+Route::get('/playlist/{playlist}.m3u', 'PlaylistController@m3u')->name('playlist.m3u');
Route::get('/playlist/{playlist}', 'PlaylistController@show')->name('playlist.show');
Route::post('/playlist/{playlist}/{song}', 'PlaylistController@post')->name('playlist.post');
Route::get('/s/{song}', 'SongController@show')->name('song.show');