Ejemplo n.º 1
0
 /**
  * @param \app\models\books\Model $model
  * @return bool
  */
 public function fill(\app\models\books\Model $model)
 {
     $oldFileNameHash = $model->preview_path_hash;
     if (!$this->cover) {
         $model->setAttributes(['preview_path' => null, 'preview_path_hash' => null], false);
         return true;
     }
     $fileName = $this->cover->baseName . '.' . $this->cover->extension;
     $fileNameHash = sha1($this->cover->baseName . time()) . '.' . $this->cover->extension;
     $this->cleanOldFile($oldFileNameHash, $fileNameHash, $model);
     if ($this->validate()) {
         $model->setAttributes(['preview_path' => $fileName, 'preview_path_hash' => $fileNameHash], false);
         $this->cover->saveAs(\Yii::getAlias('@covers') . DIRECTORY_SEPARATOR . $fileNameHash);
         return true;
     } else {
         return false;
     }
 }