From 8b0e556544c9c9b450c14dc636fb4590458b0ad9 Mon Sep 17 00:00:00 2001 From: sajad666 Date: Wed, 29 Jul 2015 18:11:21 +0430 Subject: [PATCH 1/4] mp3 class this is a mp3 class for calculator with handle for audio files --- .gitignore | 1 + app/config/app.php | 1 + app/controllers/AdminController.php | 2 ++ app/controllers/AnswerController.php | 15 ++++++++------- .../Mp3Controller.php => lib/Mp3.php} | 10 +++++++--- app/routes.php | 6 ++++++ composer.json | 3 ++- 7 files changed, 27 insertions(+), 11 deletions(-) rename app/{controllers/Mp3Controller.php => lib/Mp3.php} (99%) diff --git a/.gitignore b/.gitignore index 126be62..23172b0 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,4 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk vendor/ +public/1.mp3 diff --git a/app/config/app.php b/app/config/app.php index 1561900..29ee92e 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -191,6 +191,7 @@ 'URL' => 'Illuminate\Support\Facades\URL', 'Validator' => 'Illuminate\Support\Facades\Validator', 'View' => 'Illuminate\Support\Facades\View', + 'MP3' => 'app\lib\MP3' ), diff --git a/app/controllers/AdminController.php b/app/controllers/AdminController.php index 716f127..4337089 100644 --- a/app/controllers/AdminController.php +++ b/app/controllers/AdminController.php @@ -61,7 +61,9 @@ public function store() } $answer->save(); return Redirect::route('answer.admin..index'); + + } return Redirect::back()->withErrors($validate)->withInput(); diff --git a/app/controllers/AnswerController.php b/app/controllers/AnswerController.php index 1eebe77..5af635e 100644 --- a/app/controllers/AnswerController.php +++ b/app/controllers/AnswerController.php @@ -38,7 +38,12 @@ public function create() */ public function store() { - $validate = Validator::make(Input::all(), Answer::$rules); + $file = Input::get('audio'); + // $mp3file = new MP3(file); + // $duration = $mp3file->getDuration(); + return "$file"; + + /*$validate = Validator::make(Input::all(), Answer::$rules); if ($validate->passes()) { @@ -57,17 +62,13 @@ public function store() } $answer->save(); return Redirect::action('AnswerController@index'); - $response = array( - 'status' => 'success', - 'msg' => 'Setting created successfully', - ); - return Response::json( $response ); + } return Redirect::back()->withErrors($validate)->withInput(); - +*/ } diff --git a/app/controllers/Mp3Controller.php b/app/lib/Mp3.php similarity index 99% rename from app/controllers/Mp3Controller.php rename to app/lib/Mp3.php index 3707f46..6f3f75b 100644 --- a/app/controllers/Mp3Controller.php +++ b/app/lib/Mp3.php @@ -1,6 +1,10 @@ getDuration(); +return "$duration2"; +}); + diff --git a/composer.json b/composer.json index 3e1a52d..c68d079 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "app/models", "app/database/migrations", "app/database/seeds", - "app/tests/TestCase.php" + "app/tests/TestCase.php", + "app/lib" ] }, "scripts": { From bd7a486fd0055959c7ca24c11b1d36c161a57e2c Mon Sep 17 00:00:00 2001 From: Rahman Mousavian Date: Thu, 30 Jul 2015 11:56:43 +0800 Subject: [PATCH 2/4] Ignoring all media files --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 23172b0..f2a3ed3 100644 --- a/.gitignore +++ b/.gitignore @@ -49,4 +49,8 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk vendor/ -public/1.mp3 + +#Media Files +*.mp3 +*.ogg +*.mp4 \ No newline at end of file From 1733732b55e8530c63d7fa277b379414c295cacd Mon Sep 17 00:00:00 2001 From: Rahman Mousavian Date: Thu, 30 Jul 2015 11:57:33 +0800 Subject: [PATCH 3/4] validate MP3 file duration --- app/controllers/AnswerController.php | 67 +++++++++++++++++----------- app/models/Answer.php | 2 +- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/app/controllers/AnswerController.php b/app/controllers/AnswerController.php index 5af635e..a0ed728 100644 --- a/app/controllers/AnswerController.php +++ b/app/controllers/AnswerController.php @@ -38,38 +38,51 @@ public function create() */ public function store() { - $file = Input::get('audio'); - // $mp3file = new MP3(file); - // $duration = $mp3file->getDuration(); - return "$file"; - - /*$validate = Validator::make(Input::all(), Answer::$rules); - if ($validate->passes()) - { - - $audio = Input::file('audio'); - $name = time() . "-" . $audio->getClientOriginalName(); - $avatar = $audio->move("./answers/",$name); - - $answer= new answer; - $answer->title=Input::get('title'); - $answer->info=Input::get('info'); - $answer->audio=$name; - if (Auth::check()){ - $answer->user_id=Auth::id(); - }else{ - $answer->user_id=0; - } - $answer->save(); - return Redirect::action('AnswerController@index'); + $validate = Validator::make(Input::all(), Answer::$rules); + + if ($validate->passes()){ + + //get file from input + $audio = Input::file('audio'); + + //get file's temporary path in server + $file_temporary_path = $audio->getPathname(); + + //create MP3 Object + $audio_file = new MP3( $file_temporary_path ); + + $duration = $audio_file->getDuration(); + + #Do same thing in 1 line: + #$duration = with(new MP3($audio->getPathname()))->getDuration(); + + //check if audio is less than/equal to 120 Seconds, then save it! + if ($duration <= 120){ //seconds + + $name = time() . '-' . $audio->getClientOriginalName(); + + //Move file from temporary folder to PUBLIC folder. + //PUBLIC folder because we want user have access to this file later. + $avatar = $audio->move( public_path() . '/answers/', $name); + + $answer= new Answer; + $answer->title=Input::get('title'); + $answer->info=Input::get('info'); + $answer->audio = $name; + if (Auth::check()){ + $answer->user_id=Auth::id(); + }else{ + $answer->user_id=0; + } + + $answer->save(); + } + return Redirect::action('AnswerController@index'); } return Redirect::back()->withErrors($validate)->withInput(); - -*/ - } /** diff --git a/app/models/Answer.php b/app/models/Answer.php index 4e49e8f..497bfb0 100644 --- a/app/models/Answer.php +++ b/app/models/Answer.php @@ -5,7 +5,7 @@ class Answer extends \Eloquent { // Add your validation rules here public static $rules = [ 'title' => 'required|between:3,60|unique:answers,title', - 'audio' => 'required|mimes:mpga,aac,ogg|max:2000', + 'audio' => 'required|mimes:mpga,aac,ogg,mp3', 'info' => 'required|max:100' ]; From da47f38fa5f710e744a1fad5c3b4e7876f792719 Mon Sep 17 00:00:00 2001 From: Sajad Ayooby Date: Thu, 30 Jul 2015 11:20:44 +0430 Subject: [PATCH 4/4] Remove unnecessary extention MP3 in rules won't work in laravel so should be remove. --- app/models/Answer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/Answer.php b/app/models/Answer.php index 497bfb0..dd2150f 100644 --- a/app/models/Answer.php +++ b/app/models/Answer.php @@ -5,7 +5,7 @@ class Answer extends \Eloquent { // Add your validation rules here public static $rules = [ 'title' => 'required|between:3,60|unique:answers,title', - 'audio' => 'required|mimes:mpga,aac,ogg,mp3', + 'audio' => 'required|mimes:mpga,aac,ogg', 'info' => 'required|max:100' ]; @@ -17,4 +17,4 @@ public function Categorie(){ return $this->belongsTo('Categorie'); } -} \ No newline at end of file +}