Esempio n. 1
0
 /**
  * Add a song to the library
  *
  * @param artist_id  int: the related artist primary key
  * @param album_id   int: the related album primary key
  * @param genre_id   int: the related genre primary key
  * @param song_array array: the array of song info
  * @return          int: the song insert id
  * @see apps/client/lib/MediaScan.class.php for information about the song_array
  */
 public function addSong($artist_id, $album_id, $last_scan_id, $song_array)
 {
     if (isset($song_array['filename']) && !empty($song_array['filename']) && isset($song_array['mtime']) && !empty($song_array['mtime']) && $last_scan_id) {
         $song = new Song();
         $song->unique_id = sha1(uniqid('', true) . mt_rand(1, 99999999));
         $song->artist_id = (int) $artist_id;
         $song->album_id = (int) $album_id;
         $song->scan_id = (int) $last_scan_id;
         $song->name = $song_array['song_name'];
         $song->length = $song_array['song_length'];
         $song->accurate_length = (int) $song_array['accurate_length'];
         $song->filesize = (int) $song_array['filesize'];
         $song->bitrate = (int) $song_array['bitrate'];
         $song->yearpublished = (int) $song_array['yearpublished'];
         $song->tracknumber = (int) $song_array['tracknumber'];
         $song->label = $song_array['label'];
         $song->mtime = (int) $song_array['mtime'];
         $song->atime = (int) $song_array['atime'];
         $song->filename = $song_array['filename'];
         $song->save();
         $id = $song->getId();
         $song->free();
         unset($song, $song_array);
         return $id;
     }
     return false;
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Song();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Song'])) {
         $model->attributes = $_POST['Song'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
Esempio n. 3
0
 public function actionUpmusic()
 {
     $songModel = new Song();
     if (isset($_POST['Song'])) {
         $songModel->attributes = $_POST['Song'];
         $songModel->userid = Yii::app()->session['uid'];
         if ($songModel->validate()) {
             if (!empty($_FILES['songname']['tmp_name'])) {
                 $file = $_FILES['songname'];
                 if (!MusicTypeCheck($file['name'], $file['size'])) {
                     Yii::app()->user->setFlash('upstatus', 'Sorry, 音乐文件大小或格式错误 :(');
                     $this->redirect(array("Upmusic"));
                     die;
                 }
                 $type = "." . GetFileExtension($file['name']);
                 Yii::import('application.vendors.*');
                 require_once 'Qiniu/rs.php';
                 require_once 'Qiniu/io.php';
                 $bucket = Yii::app()->params['bucket'];
                 $accessKey = Yii::app()->params['accessKey'];
                 $secretKey = Yii::app()->params['secretKey'];
                 $newname = time() . rand(10000, 99999) . $type;
                 //先保存记录
                 $songModel->song = $newname;
                 if ($songModel->save()) {
                     /**
                      * 
                      */
                 } else {
                     Yii::app()->user->setFlash('upstatus', 'Sorry,系统错误,上传音乐失败 :(');
                 }
                 Qiniu_SetKeys($accessKey, $secretKey);
                 $putPolicy = new Qiniu_RS_PutPolicy($bucket);
                 $upToken = $putPolicy->Token(null);
                 list($ret, $err) = Qiniu_Put($upToken, $newname, file_get_contents($file['tmp_name']), null);
                 if ($err === null) {
                     //成功
                     /***
                      * 
                      */
                     $this->redirect(array('admin/Imusic'));
                 } else {
                     //失败
                     Yii::app()->user->setFlash('upstatus', 'Sorry,系统错误,上传音乐失败 :(');
                 }
             }
         }
     }
     $data = array('songModel' => $songModel);
     $this->render("upmusic", $data);
 }
Esempio n. 4
0
<?php

//ini_set("display_errors", 1);
require __DIR__ . '/autoload.php';
//create new Songs
$model = new Song();
$model->status = 2;
$model->rate = 66;
$model->save(3);
$model = new Song(['title' => 'I can teach you', 'artist' => 'bon jovi', 'status' => 1]);
$model->create();
Esempio n. 5
0
 public function save()
 {
     $input = file_get_contents("php://input");
     $array = array();
     parse_str($input, $array);
     // var_dump($array);
     $song = new Song();
     $song->name = $array['name'];
     $song->song = $array['song'];
     $song->tel = $array['tel'];
     $song->gender = $array['sex'];
     $song->school = $array['school'];
     $song->mid = 'fakeid' . time();
     if (Session::has('userInfo')) {
         $userInfo = Session::get('userInfo');
         $song->openid = $userInfo->openid;
         $song->avatar = $userInfo->headimgurl;
         $song->mid = $userInfo->openid . time();
     }
     $result = $song->save();
     // 保存点歌分享信息到session
     $share = new stdClass();
     $share->name = $song->name;
     $share->song = $song->song;
     $share->tel = $song->tel;
     Session::put('share', $share);
     if ($result) {
         return Response::json(array('msg' => 'success'));
     } else {
         return Response::json(array('msg' => 'failure'));
     }
 }
Esempio n. 6
0
 public function saveSong($id)
 {
     $validator = Validator::make(Input::all(), array('song_title' => 'required', 'video_code' => 'required'));
     if ($validator->fails()) {
         return Redirect::route('edit-albums')->withInput()->withErrors($validator)->with('song_modal', '#song_modal')->with('album-id', $id);
     } else {
         $album = Album::find($id);
         if ($album == null) {
             return Redirect::route('edit-albums')->with('fail', "That album doesn't exist.");
         }
         $song = new Song();
         $song->title = Input::get('song_title');
         $song->video = Input::get('video_code');
         $song->album_id = $id;
         if ($song->save()) {
             return Redirect::route('edit-albums')->with('success', 'The song was added.');
         } else {
             return Redirect::route('edit-albums')->with('fail', 'An error occured while saving the new song.');
         }
     }
 }
 /**
  * Handle song submission
  *
  * @return Response
  */
 public function upload()
 {
     $rules = array('song' => 'max:61440');
     $validation = Validator::make(Input::all(), $rules);
     if ($validation->fails()) {
         return Response::make($validation->errors->first(), 400);
     }
     $file = Input::file('song');
     if (count($file) > 0) {
         $extension = $file->getClientOriginalExtension();
         $directory = public_path() . '/uploads/' . sha1(time());
         $filename = sha1(time() . time()) . ".{$extension}";
         $name = $file->getClientOriginalName();
         $upload_success = $file->move($directory, $filename);
         if ($upload_success) {
             $song = new Song();
             $song->path = $directory;
             $song->original_name = $filename;
             $song->artist = Auth::user()->id;
             $song->title = $name;
             $song->save();
             Session::put('song', $song->id);
             // Generating waveform
             $transloadit = new Transloadit(array('key' => '8b6e8f905c0811e4b45af39f2111cd0b', 'secret' => '75b297187e5676cb0e4b6ac12b11ba550bb51082'));
             $response = $transloadit->createAssembly(array('files' => array($directory . "/" . $filename), 'wait' => true, 'params' => array('steps' => array('mp3' => array('robot' => '/audio/encode', 'preset' => "mp3", 'result' => true, 'ffmpeg' => array('ss' => "00:00:00.0", 't' => "00:00:30")), 'waveform' => array('robot' => "/audio/waveform", 'use' => ":original", 'width' => 295, 'height' => 55, 'background_color' => "ffffffff", 'outer_color' => "607BA9aa", 'center_color' => "607BA9aa", 'result' => true)), 'notify_url' => URL::to('/') . '/notify')));
             $assembly = $response->data['assembly_id'];
             $a = new Assembly();
             $a->assembly = $assembly;
             $a->song = $song->id;
             $a->save();
         }
         if ($upload_success) {
             return Response::json('success', 200);
         } else {
             return Response::json('error', 400);
         }
     } else {
         return Response::json('error', 400);
     }
 }
Esempio n. 8
0
 public function actionUpdate($id)
 {
     /* @var Artist $model */
     $model = $this->loadModel($id);
     // check access
     if (!Yii::app()->user->checkAccess('edit artist')) {
         throw new CHttpException(401);
     }
     // does this artists exists in our DB?
     if ($model === null) {
         Yii::log("Artist update requested with id {$id} but no such artist found!", CLogger::LEVEL_INFO, __METHOD__);
         throw new CHttpException(404, Yii::t("MusicModule.general", 'The requested page does not exist.'));
     }
     // enable adding songs by default (will be changed below if needed to)
     $enable_add_more_songs = true;
     if (isset($_POST['Artist'])) {
         // start optimistically
         $all_songs_valid = true;
         $songs_to_save = array();
         $success_saving_all = true;
         $model->attributes = $_POST['Artist'];
         if (isset($_POST['Song'])) {
             if (count($_POST['Song']) > Song::MAX_SONGS_PER_ARTIST) {
                 /*
                  * server side protection against attempt to submit more than MAX_SONGS_PER_ARTIST
                  * this should be accompanied with a client side (JS) protection.
                  * If its accompanied with client side protection then going into this code block means our system
                  * is being abused/"tested". No need to give a polite error message.
                  */
                 throw new CHttpException(500, Yii::t("MusicModule.forms", "The max amount of allowed songs is {max_songs_num}", array('{max_songs_num}' => Song::MAX_SONGS_PER_ARTIST)));
             }
             foreach ($_POST['Song'] as $index => $submitted_song) {
                 // We could have empty songs which means empty submitted forms in POST. Ignore those:
                 if ($submitted_song['title'] == '') {
                     // empty one - skip it, if you please.
                     continue;
                 }
                 // next, validate each submitted song instance
                 if ((int) $submitted_song['id'] > 0) {
                     /* Validate that the submitted song belong to this artist */
                     $song = Song::model()->findByPk($submitted_song['id']);
                     if ($song->artist->id != $model->id) {
                         Yii::log("Attempts to update Song with an id of {$song->id} but it belongs to an Artist with an id of {$song->model->id}" . " and not 'this' artist with id = {$model->id}", CLogger::LEVEL_ERROR, __METHOD__);
                         throw new CHttpException(500, "Error occurred");
                     }
                 } else {
                     // this submitted song object is a new model. instantiate one:
                     $song = new Song();
                 }
                 $song->attributes = $submitted_song;
                 if (!$song->validate()) {
                     // at least one invalid song:
                     $all_songs_valid = false;
                     // we do not 'break' here since we want validation on all song at the same shot
                 } else {
                     // put aside the valid song to be saved
                     $songs_to_save[] = $song;
                 }
             }
             // while we know that songs were submitted, determine if to show 'adding songs' or no.
             // a check whether the max songs per artist was exceeded was performed already above.
             if (count($_POST['Song']) == Song::MAX_SONGS_PER_ARTIST) {
                 $enable_add_more_songs = false;
             }
         }
         if ($all_songs_valid && $model->validate()) {
             /* all songs (if any) were valid and artist object is valid too. Save it all in one transaction. Save first the
              * artist as we need its id for its songs records
              */
             $trans = Yii::app()->db->beginTransaction();
             try {
                 // use aux variable for manipulating the image file.
                 $image = CUploadedFile::getInstance($model, 'icon_filename');
                 // check if a new image was submitted or not:
                 if ($image) {
                     /* the only thing that might have changed in the update is the extension name of the image (if you support more than 'only jpeg').
                      * therefore, if something was submitted, and since we already know the ID of the artist (this is an update scenario), we can
                      * determine the full updated icon_filename attribute of the model prior to its save() (unlike in create action - see there...).
                      */
                     $model->icon_filename = $model->getImageFsFilename($image);
                 }
                 $model->save(false);
                 // save the updated image, if any
                 if ($image) {
                     $image->saveAs($model->getImageFsFilename($image));
                 }
                 // save songs
                 foreach ($songs_to_save as $song) {
                     $song->save(false);
                 }
                 $trans->commit();
             } catch (Exception $e) {
                 // oops, saving artist or its songs failed. rollback, report us, and show the user an error.
                 $trans->rollback();
                 Yii::log("Error occurred while saving (update scenario) artist or its 'songs'. Rolling back... . Failure reason as reported in exception: " . $e->getMessage(), CLogger::LEVEL_ERROR, __METHOD__);
                 Yii::app()->user->setFlash('error', Yii::t("MusicModule.forms", "Error occurred"));
                 $success_saving_all = false;
             }
             if ($success_saving_all) {
                 $success_msg = count($songs_to_save) > 0 ? "Artist and song records have been updated" : "Artist record have been updated";
                 Yii::app()->user->setFlash('success', Yii::t("MusicModule.forms", $success_msg));
                 $this->redirect(array("view", "id" => $model->id));
             }
         }
     } else {
         // initial rendering of update form. prepare songs for printing.
         // we put it in the same variable as used for saving (that's the reason for the awkward variable naming).
         $songs_to_save = $model->songs;
     }
     $this->render('update', array('artist' => $model, 'songs' => isset($songs_to_save) ? $songs_to_save : array(new Song('insert')), 'enable_add_more_songs' => $enable_add_more_songs));
 }