Ejemplo n.º 1
0
 public function beforeSave($insert)
 {
     $this->_init();
     $this->created_at = time();
     $this->publish = 1;
     $baseName = $baseExt = $fileToUpload = $movie = "";
     // $fileToUpload = $snapshot = $movie = "";
     $base_name = [];
     $snapshotNeeded = false;
     $result = $result2 = $this->preview = null;
     if ($this->fileName != "") {
         if (preg_match('/([\\w\\d_]*\\.mp4$)/', $this->fileName)) {
             $base_name = explode(".", $this->fileName);
             $baseName = $base_name[0];
             $baseExt = $base_name[1];
             $snapshotNeeded = true;
         } else {
             $baseName = $this->fileName;
         }
     }
     if (file_exists("{$this->tmpDir}/{$baseName}.mp4")) {
         $fileToUpload = "{$this->tmpDir}/{$baseName}.mp4";
         $movie = $this->newFileName . '.mp4';
     }
     /*else {
           $fileToUpload = "{$this->tmpDir}/$baseName.webm";
           $movie = $this->newFileName . '.webm';
       }
       */
     if (file_exists("{$this->tmpDir}/{$baseName}.png")) {
         $imgToUpload = "{$this->tmpDir}/{$baseName}.png";
         $snapshot = $this->newFileName . '.png';
     } elseif (file_exists("{$this->tmpDir}/{$baseName}.jpg")) {
         $imgToUpload = "{$this->tmpDir}/{$baseName}.jpg";
         $snapshot = $this->newFileName . '.jpg';
     } else {
         if ($snapshotNeeded) {
             sleep(5);
             echo shell_exec(Yii::$app->params['ffmpeg']['path'] . '/' . 'ffmpeg -ss 00:00:02 -i "' . $this->tmpDir . '/' . $baseName . '.' . $baseExt . '" -f image2 -vframes 1 "' . $this->tmpDir . '/' . $baseName . '.png"');
         }
         $imgToUpload = "{$this->tmpDir}/{$baseName}.png";
         $snapshot = $this->newFileName . '.png';
     }
     if ($this->api) {
         // TODO
     } else {
         $this->user_id = Yii::$app->user->id;
         if ($movie != "") {
             // Сохраниение видео на s3
             $s3 = new S3Client(Yii::$app->params['s3']);
             $result = $s3->putObject(['Bucket' => 'razzd1', 'Key' => $movie, 'Body' => fopen($fileToUpload, 'r'), 'ACL' => 'public-read']);
             if (!$result) {
                 Yii::$app->getSession()->setFlash('error', 'There was an error uploading the file to cloud storage.');
                 return;
             } else {
                 $this->url = $result->search('ObjectURL');
             }
             // Сохраниение превьюшки на s3
             if (file_exists($imgToUpload)) {
                 $result2 = $s3->putObject(['Bucket' => 'razzd1', 'Key' => $snapshot, 'Body' => fopen($imgToUpload, 'r'), 'ACL' => 'public-read']);
             } else {
             }
             if ($result2) {
                 $this->preview = $result2->search('ObjectURL');
             }
         }
     }
     if (!$insert) {
         // Respond to existing one
         if (!$this->pureUpdate) {
             $this->responder_uid = $this->user_id;
             if ($movie != "") {
                 $this->responder_stream = $this->url;
                 $this->responder_stream_preview = $this->preview;
             } else {
                 $this->responder_stream = $this->fileName;
             }
             $this->hash = null;
         }
     } else {
         // Create new one
         $this->uid = $this->user_id;
         if ($movie != "") {
             $this->stream = $this->url;
             $this->stream_preview = $this->preview;
         } else {
             // Ziggeo
             $this->stream = $this->fileName;
         }
         // SomeOne через аккаунт сайта - отправка майла
         if ($this->type == 1 && !$this->fb_friend) {
             $this->hash = md5(time() . $this->email);
             $this->sendMail = true;
             $user = \frontend\models\User::findOne(['email' => $this->email]);
             if ($user) {
                 $this->responder_uid = $user->id;
             }
         }
         /*
         if( $this->fileName != "" ){
             $this->stream_preview = \common\helpers\Ziggeo::getPreview($this->fileName);
         }
         */
         // SomeOne через социальную сеть
         if ($this->type == 1 && $this->fb_friend) {
             $this->hash = md5(time() . $this->fb_friend);
             $user_id = \frontend\models\User::getUidByClientId($this->fb_friend);
             if ($user_id) {
                 // Шлём razzd внешнему пользователю, который уже есть в базе
                 $this->responder_uid = $user_id;
             } else {
                 // Razzd незарегистрированному внешнему пользователю
                 $this->facebook_id = $this->fb_friend;
             }
         }
     }
     if (file_exists("{$this->tmpDir}/{$baseName}.png")) {
         unlink("{$this->tmpDir}/{$baseName}.png");
     }
     if (file_exists("{$this->tmpDir}/{$baseName}.jpg")) {
         unlink("{$this->tmpDir}/{$baseName}.jpg");
     }
     if (file_exists("{$this->tmpDir}/{$baseName}.wav")) {
         unlink("{$this->tmpDir}/{$baseName}.wav");
     }
     if (file_exists("{$this->tmpDir}/{$baseName}.mp3")) {
         unlink("{$this->tmpDir}/{$baseName}.mp3");
     }
     if (file_exists("{$this->tmpDir}/{$baseName}.mp4")) {
         unlink("{$this->tmpDir}/{$baseName}.mp4");
     }
     if (file_exists("{$this->tmpDir}/{$baseName}.webm")) {
         unlink("{$this->tmpDir}/{$baseName}.webm");
     }
     return parent::beforeSave($insert);
 }