From e6c31a1500118899b1afb53795ae50565d858af3 Mon Sep 17 00:00:00 2001
From: Peter Deltchev <peter@deltchev.com>
Date: Sun, 20 Dec 2015 07:07:36 -0800
Subject: [PATCH] Updated URL generation to use Laravel 5's helpers.

---
 app/Album.php                             |  7 ++--
 app/Genre.php                             |  3 +-
 app/Http/Controllers/AuthController.php   |  5 ++-
 app/Http/Controllers/ImagesController.php |  3 +-
 app/Http/Controllers/UsersController.php  | 44 -----------------------
 app/Http/routes.php                       |  2 --
 app/Image.php                             |  5 ++-
 app/Playlist.php                          |  7 ++--
 app/Providers/AppServiceProvider.php      | 10 ------
 app/Track.php                             | 11 +++---
 app/TrackFile.php                         |  3 +-
 app/User.php                              | 13 +------
 12 files changed, 19 insertions(+), 94 deletions(-)
 delete mode 100644 app/Http/Controllers/UsersController.php

diff --git a/app/Album.php b/app/Album.php
index f1c8c11d..abe43872 100644
--- a/app/Album.php
+++ b/app/Album.php
@@ -28,7 +28,6 @@ use Illuminate\Foundation\Bus\DispatchesJobs;
 use Auth;
 use Cache;
 use Poniverse\Ponyfm\Traits\TrackCollection;
-use URL;
 use Poniverse\Ponyfm\Traits\SlugTrait;
 use Venturecraft\Revisionable\RevisionableTrait;
 
@@ -131,7 +130,7 @@ class Album extends Model
         $data['description'] = $album->description;
         $data['is_downloadable'] = $is_downloadable;
         $data['share'] = [
-            'url' => URL::to('/a' . $album->id),
+            'url' => action('AlbumsController@getShortlink', ['id' => $album->id]),
             'tumblrUrl' => 'http://www.tumblr.com/share/link?url=' . urlencode($album->url) . '&name=' . urlencode($album->title) . '&description=' . urlencode($album->description),
             'twitterUrl' => 'https://platform.twitter.com/widgets/tweet_button.html?text=' . $album->title . ' by ' . $album->user->display_name . ' on Pony.fm'
         ];
@@ -198,12 +197,12 @@ class Album extends Model
 
     public function getUrlAttribute()
     {
-        return URL::to('albums/' . $this->id . '-' . $this->slug);
+        return action('AlbumsController@getShow', ['id' => $this->id, 'slug' => $this->slug]);
     }
 
     public function getDownloadUrl($format)
     {
-        return URL::to('a' . $this->id . '/dl.' . Track::$Formats[$format]['extension']);
+        return action('AlbumsController@getDownload', ['id' => $this->id, 'extension' => Track::$Formats[$format]['extension']]);
     }
 
     public function getFilesize($format)
diff --git a/app/Genre.php b/app/Genre.php
index f44d5c71..529352b3 100644
--- a/app/Genre.php
+++ b/app/Genre.php
@@ -25,7 +25,6 @@ use Illuminate\Database\Eloquent\Relations\Relation;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Poniverse\Ponyfm\Traits\SlugTrait;
 use Illuminate\Database\Eloquent\Model;
-use URL;
 use Venturecraft\Revisionable\RevisionableTrait;
 
 class Genre extends Model
@@ -75,6 +74,6 @@ class Genre extends Model
      * @return string relative, Angular-friendly URL to this genre
      */
     public function getUrlAttribute() {
-        return URL::route('tracks.discover', ['filter' => "genres-{$this->id}"], false);
+        return route('tracks.discover', ['filter' => "genres-{$this->id}"], false);
     }
 }
diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php
index 5d8b7c32..53bf1bb3 100644
--- a/app/Http/Controllers/AuthController.php
+++ b/app/Http/Controllers/AuthController.php
@@ -27,7 +27,6 @@ use DB;
 use Input;
 use Poniverse;
 use Redirect;
-use URL;
 
 class AuthController extends Controller
 {
@@ -36,7 +35,7 @@ class AuthController extends Controller
     public function __construct()
     {
         $this->poniverse = new Poniverse(Config::get('poniverse.client_id'), Config::get('poniverse.secret'));
-        $this->poniverse->setRedirectUri(URL::to('/auth/oauth'));
+        $this->poniverse->setRedirectUri(action('AuthController@getOAuth'));
     }
 
     public function getLogin()
@@ -62,7 +61,7 @@ class AuthController extends Controller
             'authorization_code',
             [
                 'code' => Input::query('code'),
-                'redirect_uri' => URL::to('/auth/oauth')
+                'redirect_uri' => action('AuthController@getOAuth')
             ]);
 
         if ($code['code'] != 200) {
diff --git a/app/Http/Controllers/ImagesController.php b/app/Http/Controllers/ImagesController.php
index 384e0b52..f6cbc515 100644
--- a/app/Http/Controllers/ImagesController.php
+++ b/app/Http/Controllers/ImagesController.php
@@ -25,7 +25,6 @@ use Config;
 use Illuminate\Support\Facades\App;
 use Illuminate\Support\Facades\Redirect;
 use Response;
-use URL;
 
 class ImagesController extends Controller
 {
@@ -46,7 +45,7 @@ class ImagesController extends Controller
         $filename = $image->getFile($coverType['id']);
 
         if (!is_file($filename)) {
-            $redirect = URL::to('/images/icons/profile_' . Image::$ImageTypes[$coverType['id']]['name'] . '.png');
+            $redirect = url('/images/icons/profile_' . Image::$ImageTypes[$coverType['id']]['name'] . '.png');
 
             return Redirect::to($redirect);
         }
diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php
deleted file mode 100644
index 0484641a..00000000
--- a/app/Http/Controllers/UsersController.php
+++ /dev/null
@@ -1,44 +0,0 @@
-<?php
-
-/**
- * Pony.fm - A community for pony fan music.
- * Copyright (C) 2015 Peter Deltchev
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-namespace Poniverse\Ponyfm\Http\Controllers;
-
-use Poniverse\Ponyfm\User;
-use File;
-use Illuminate\Support\Facades\App;
-
-class UsersController extends Controller
-{
-    public function getAvatar($id, $type)
-    {
-        $coverType = Cover::getCoverFromName($type);
-
-        if ($coverType == null) {
-            App::abort(404);
-        }
-
-        $user = User::find($id);
-        if (!$user) {
-            App::abort(404);
-        }
-
-        return File::inline($user->getAvatarFile($coverType['id']), 'image/png', 'cover.png');
-    }
-}
diff --git a/app/Http/routes.php b/app/Http/routes.php
index afa12b27..1dc8b931 100644
--- a/app/Http/routes.php
+++ b/app/Http/routes.php
@@ -62,8 +62,6 @@ Route::get('/mlpforums-advertising-program', function() { return View::make('pag
 
 Route::get('i{id}/{type}.png', 'ImagesController@getImage')->where('id', '\d+');
 
-Route::get('u{id}/avatar_{type}.png', 'UsersController@getAvatar')->where('id', '\d+');
-
 Route::get('playlist/{id}-{slug}', 'PlaylistsController@getPlaylist');
 Route::get('p{id}', 'PlaylistsController@getShortlink')->where('id', '\d+');
 Route::get('p{id}/dl.{extension}', 'PlaylistsController@getDownload' );
diff --git a/app/Image.php b/app/Image.php
index bc7829d9..c888ff79 100644
--- a/app/Image.php
+++ b/app/Image.php
@@ -22,8 +22,7 @@ namespace Poniverse\Ponyfm;
 
 use External;
 use Illuminate\Database\Eloquent\Model;
-use Illuminate\Support\Facades\Config;
-use Illuminate\Support\Facades\URL;
+use Config;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 
 class Image extends Model
@@ -101,7 +100,7 @@ class Image extends Model
     {
         $type = self::$ImageTypes[$type];
 
-        return URL::to('i' . $this->id . '/' . $type['name'] . '.png');
+        return action('ImagesController@getImage', ['id' => $this->id, 'type' => $type['name']]);
     }
 
     public function getFile($type = self::NORMAL)
diff --git a/app/Playlist.php b/app/Playlist.php
index 042249e2..e9deaf61 100644
--- a/app/Playlist.php
+++ b/app/Playlist.php
@@ -27,7 +27,6 @@ use Illuminate\Foundation\Bus\DispatchesJobs;
 use Auth;
 use Cache;
 use Poniverse\Ponyfm\Traits\TrackCollection;
-use URL;
 use Poniverse\Ponyfm\Traits\SlugTrait;
 use Venturecraft\Revisionable\RevisionableTrait;
 
@@ -88,7 +87,7 @@ class Playlist extends Model
         $data['comments'] = $comments;
         $data['formats'] = $formats;
         $data['share'] = [
-            'url' => URL::to('/p' . $playlist->id),
+            'url' => action('PlaylistsController@getShortlink', ['id' => $playlist->id]),
             'tumblrUrl' => 'http://www.tumblr.com/share/link?url=' . urlencode($playlist->url) . '&name=' . urlencode($playlist->title) . '&description=' . urlencode($playlist->description),
             'twitterUrl' => 'https://platform.twitter.com/widgets/tweet_button.html?text=' . $playlist->title . ' by ' . $playlist->user->display_name . ' on Pony.fm'
         ];
@@ -202,12 +201,12 @@ class Playlist extends Model
 
     public function getUrlAttribute()
     {
-        return URL::to('/playlist/' . $this->id . '-' . $this->slug);
+        return action('PlaylistsController@getPlaylist', ['id' => $this->id, 'slug' => $this->slug]);
     }
 
     public function getDownloadUrl($format)
     {
-        return URL::to('p' . $this->id . '/dl.' . Track::$Formats[$format]['extension']);
+        return action('PlaylistsController@getDownload', ['id' => $this->id]);
     }
 
     public function getFilesize($format)
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index b0e2ff1e..dfaf53db 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -20,11 +20,7 @@
 
 namespace Poniverse\Ponyfm\Providers;
 
-use App;
-use Auth;
-use Illuminate\Auth\Guard;
 use Illuminate\Support\ServiceProvider;
-// use PFMAuth;
 use PfmValidator;
 use Validator;
 
@@ -37,12 +33,6 @@ class AppServiceProvider extends ServiceProvider
      */
     public function boot()
     {
-/*
-        Auth::extend('pfm', function() {
-            return new Guard(new PFMAuth(), App::make('session.store'));
-        });
-*/
-
         Validator::resolver(function($translator, $data, $rules, $messages)
         {
             return new PfmValidator($translator, $data, $rules, $messages);
diff --git a/app/Track.php b/app/Track.php
index 2882db92..d5b005b6 100644
--- a/app/Track.php
+++ b/app/Track.php
@@ -33,7 +33,6 @@ use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\SoftDeletes;
 use Illuminate\Support\Str;
 use Log;
-use URL;
 use Venturecraft\Revisionable\RevisionableTrait;
 
 class Track extends Model
@@ -257,8 +256,8 @@ class Track extends Model
         }
 
         $returnValue['share'] = [
-            'url' => URL::to('/t' . $track->id),
-            'html' => '<iframe src="' . URL::to('t' . $track->id . '/embed') . '" width="100%" height="150" allowTransparency="true" frameborder="0" seamless allowfullscreen></iframe>',
+            'url' => action('TracksController@getShortlink', ['id' => $track->id]),
+            'html' => '<iframe src="' . action('TracksController@getEmbed', ['id' => $track->id]) . '" width="100%" height="150" allowTransparency="true" frameborder="0" seamless allowfullscreen></iframe>',
             'bbcode' => '[url=' . $track->url . '][img]' . $track->getCoverUrl() . '[/img][/url]',
             'twitterUrl' => 'https://platform.twitter.com/widgets/tweet_button.html?text=' . $track->title . ' by ' . $track->user->display_name . ' on Pony.fm'
         ];
@@ -477,7 +476,7 @@ class Track extends Model
 
     public function getUrlAttribute()
     {
-        return URL::to('/tracks/' . $this->id . '-' . $this->slug);
+        return action('TracksController@getTrack', ['id' => $this->id, 'slug' => $this->slug]);
     }
 
     public function getDownloadDirectoryAttribute()
@@ -537,7 +536,7 @@ class Track extends Model
 
     public function getStreamUrl($format = 'MP3')
     {
-        return URL::to('/t' . $this->id . '/stream.' . self::$Formats[$format]['extension']);
+        return action('TracksController@getStream', ['id' => $this->id, 'extension' => self::$Formats[$format]['extension']]);
     }
 
     public function getDirectory()
@@ -605,7 +604,7 @@ class Track extends Model
 
         $format = self::$Formats[$format];
 
-        return URL::to('/t' . $this->id . '/dl.' . $format['extension']);
+        return action('TracksController@getDownload', ['id' => $this->id, 'extension' => $format['extension']]);
     }
 
 
diff --git a/app/TrackFile.php b/app/TrackFile.php
index 22e9d779..a705c998 100644
--- a/app/TrackFile.php
+++ b/app/TrackFile.php
@@ -25,7 +25,6 @@ use Helpers;
 use Illuminate\Database\Eloquent\Model;
 use App;
 use File;
-use URL;
 
 class TrackFile extends Model
 {
@@ -88,7 +87,7 @@ class TrackFile extends Model
 
     public function getUrlAttribute()
     {
-        return URL::to('/t' . $this->track_id . '/dl.' . $this->extension);
+        return action('TracksController@getDownload', ['id' => $this->track_id, 'extension' => $this->extension]);
     }
 
     public function getSizeAttribute()
diff --git a/app/User.php b/app/User.php
index 6062154e..071d7cce 100644
--- a/app/User.php
+++ b/app/User.php
@@ -85,7 +85,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
 
     public function getUrlAttribute()
     {
-        return URL::to('/' . $this->slug);
+        return action('ArtistsController@getProfile', $this->slug);
     }
 
     public function getMessageUrlAttribute()
@@ -133,17 +133,6 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
         return Gravatar::getUrl($email, Image::$ImageTypes[$type]['width']);
     }
 
-    public function getAvatarFile($type = Image::NORMAL)
-    {
-        if ($this->uses_gravatar) {
-            throw new Exception('Cannot get avatar file if this user is configured to use Gravatar!');
-        }
-
-        $imageType = Image::$ImageTypes[$type];
-
-        return URL::to('t' . $this->id . '/cover_' . $imageType['name'] . '.png?' . $this->cover_id);
-    }
-
     /**
      * Get the token value for the "remember me" session.
      *