/**
  * Creates a new Music model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Music();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->Id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Esempio n. 2
0
 public function actionAdd()
 {
     $user = User::thisUser();
     if (Yii::$app->request->isPost) {
         $music = new Music();
         if ($music->load(Yii::$app->request->post())) {
             $music->user_id = $user->id;
             if (Yii::$app->request->post('Music[artist]')) {
                 $music->artist = Yii::$app->request->post('Music[artist]');
             }
             if (Yii::$app->request->post('Music[title]')) {
                 $music->title = Yii::$app->request->post('Music[title]');
             }
             if ($music->save()) {
                 $music->musicFile = UploadedFile::getInstance($music, 'musicFile');
                 if ($music->musicFile instanceof UploadedFile && $music->validate('musicFile')) {
                     $dirName = 'user' . $user->id;
                     $path = '/music/' . $dirName . '/';
                     $fileName = 'music_' . $user->id . '_' . $music->id . '.' . pathinfo($music->musicFile->name, PATHINFO_EXTENSION);
                     if (!empty(Yii::$app->params['prefix'])) {
                         $fileName = Yii::$app->params['prefix'] . '_' . $fileName;
                     }
                     /** @var GetidComponent $audioInfoComponent */
                     $audioInfoComponent = Yii::$app->audioInfo;
                     $audioInfo = $audioInfoComponent->getInfo($music->musicFile->tempName);
                     $music->playtime = (double) $audioInfo['playtime_seconds'];
                     $audioArtist = !empty($audioInfo['id3v1']['artist']) ? $audioInfo['id3v1']['artist'] : '';
                     $audioTitle = !empty($audioInfo['id3v1']['title']) ? $audioInfo['id3v1']['title'] : '';
                     if (empty($music->artist)) {
                         $music->artist = $audioArtist;
                     }
                     if (empty($music->title)) {
                         $music->title = $audioTitle;
                     }
                     if (empty($music->title)) {
                         $music->title = $music->musicFile->name;
                     }
                     /** @var YandexDiskComponent $yandexDisk */
                     $yandexDisk = Yii::$app->yandexDisk;
                     // Тест папки $path на существование
                     $pathInfo = $yandexDisk->getProperty($path);
                     if (!$pathInfo) {
                         $pathInfoMusic = $yandexDisk->getProperty('/music/');
                         if (!$pathInfoMusic) {
                             $yandexDisk->createDirectory('/music/');
                         }
                         $yandexDisk->createDirectory($path);
                     }
                     $fileInfo = $yandexDisk->getProperty($path . $fileName);
                     if (!$fileInfo) {
                         // Такого файла еще нет
                         $uploadInfo = $yandexDisk->uploadFile($path, (array) $music->musicFile, $fileName);
                     }
                     $publishInfo = $yandexDisk->startPublishing($path . $fileName);
                     if (is_string($publishInfo) && !empty($publishInfo)) {
                         $music->url = $publishInfo;
                         $shortUrl = Yii::$app->google->getShortUrl('https://getfile.dokpub.com/yandex/get/' . $publishInfo);
                         if (!empty($shortUrl['id'])) {
                             $music->short_url = $shortUrl['id'];
                         }
                     }
                     $music->key = $yandexDisk->key;
                     $music->entity_key = $yandexDisk::THIS_ENTITY;
                     $music->save();
                 }
             }
         }
         $result = ['id' => $music->id, 'url' => Url::to(['music/sound', 'id' => $music->id])];
         return json_encode($result);
     }
     $this->redirect(Url::home());
 }