Beispiel #1
0
 public function actionAdd()
 {
     $user = User::thisUser();
     if (Yii::$app->request->isPost) {
         $img = new Img();
         $img->user_id = $user->id;
         if ($img->save()) {
             $img->imgFile = UploadedFile::getInstance($img, 'imgFile');
             if ($img->imgFile instanceof UploadedFile && $img->validate('imgFile')) {
                 $prefix = Yii::$app->params['prefix'];
                 if (empty($prefix)) {
                     $prefix = "no_prefix";
                 }
                 $dirName = 'user' . $user->id;
                 $path = '/' . $prefix . '/img/' . $dirName . '/';
                 $fileName = 'img_' . $user->id . '_' . $img->id . '.' . pathinfo($img->imgFile->name, PATHINFO_EXTENSION);
                 if (!empty($prefix)) {
                     $fileName = $prefix . '_' . $fileName;
                 }
                 /** @var YandexDiskComponent $yandexDisk */
                 $yandexDisk = Yii::$app->yandexDisk;
                 $yandexDisk->setClientInfoImgDefault();
                 // Тест папки $path на существование
                 $pathInfo = $yandexDisk->getProperty($path);
                 if (!$pathInfo) {
                     $pathInfoMusic = $yandexDisk->getProperty('/' . $prefix . '/');
                     if (!$pathInfoMusic) {
                         $yandexDisk->createDirectory('/' . $prefix . '/');
                     }
                     $pathInfoMusic = $yandexDisk->getProperty('/' . $prefix . '/img/');
                     if (!$pathInfoMusic) {
                         $yandexDisk->createDirectory('/' . $prefix . '/img/');
                     }
                     $yandexDisk->createDirectory($path);
                 }
                 $fileInfo = $yandexDisk->getProperty($path . $fileName);
                 if (!$fileInfo) {
                     // Такого файла еще нет
                     $uploadInfo = $yandexDisk->uploadFile($path, (array) $img->imgFile, $fileName);
                 }
                 $publishInfo = $yandexDisk->startPublishing($path . $fileName);
                 if (is_string($publishInfo) && !empty($publishInfo)) {
                     $img->url = $publishInfo;
                     $shortUrl = Yii::$app->google->getShortUrl('https://getfile.dokpub.com/yandex/get/' . $publishInfo);
                     if (!empty($shortUrl['id'])) {
                         $img->short_url = $shortUrl['id'];
                     }
                 }
                 $img->key = $yandexDisk->key;
                 $img->entity_key = $yandexDisk::THIS_ENTITY;
                 $img->save();
             }
         }
         $result = ['id' => $img->id, 'short_url' => $img->short_url];
         return json_encode($result);
     }
     $this->redirect(Url::home());
 }
Beispiel #2
0
 public static function getImgs($data)
 {
     $query = Img::find()->limit(!empty($data['limit']) ? $data['limit'] : 40)->orderBy('id DESC');
     if (!empty($data['userId'])) {
         $query->andWhere('user_id = :user_id', [':user_id' => $data['userId']]);
     }
     return $query->all();
 }
Beispiel #3
0
 /**
  * @return Img[]
  */
 public function getImgsSort()
 {
     $query = Img::find()->innerJoin(EntityLink::tableName(), Img::tableName() . '.id = `' . EntityLink::tableName() . '`.entity_2_id')->andWhere(['entity_1' => self::THIS_ENTITY, 'entity_2' => Img::THIS_ENTITY, 'entity_1_id' => $this->id])->orderBy(['sort' => SORT_ASC]);
     return $query->all();
 }
Beispiel #4
0
 public function getUserImgs()
 {
     return Img::getImgs(['userId' => $this->id]);
 }