dosamigos\fileupload\actions\AbstractUploadAction::run PHP Méthode

run() public méthode

public run ( ) : array
Résultat array
    public function run()
    {
        $ownerId = Yii::$app->request->get($this->ownerIdParamName);
        if (!$ownerId) {
            throw new NotFoundHttpException(Yii::t('fileupload', 'Page not found'));
        }
        /** @var ActiveRecord $model */
        $model = new $this->className();
        $model->{$this->fileAttributeName} = UploadedFile::getInstance($model, $this->uploadAttributeName);
        if (isset($model->{$this->fileAttributeName}) && $model->validate($this->{$this->fileAttributeName})) {
            Yii::$app->response->getHeaders()->set('Vary', 'Accept');
            Yii::$app->response->format = Response::FORMAT_JSON;
            $response = [];
            $filePath = null;
            $transaction = ActiveRecord::getDb()->beginTransaction();
            try {
                $this->upload($model);
                $deleteRoute = $this->deleteUrlRoute + [$this->deleteIdParamName => $model->id];
                $response['files'][] = ['name' => $model->{$this->fileAttributeName}->name, 'type' => $model->{$this->fileAttributeName}->type, 'size' => $model->{$this->fileAttributeName}->size, 'url' => $model->{$this->urlAttributeName}, 'thumbnailUrl' => $model->{$this->thumbnailUrlAttributeName}, 'deleteUrl' => Url::to($deleteRoute), 'deleteType' => 'POST'];
                $this->link($ownerId, $model->id);
                $transaction->commit();
            } catch (Exception $e) {
                $transaction->rollBack();
                $response[] = ['error' => Yii::t('fileupload', 'Unable to save picture: `{errorMessage}`', ['errorMessage' => $e->getMessage()])];
                @unlink($model->{$this->fileAttributeName}->tempName);
            }
        } else {
            if ($model->hasErrors([$this->fileAttributeName])) {
                $response[] = ['errors' => $this->getModelErrors($model)];
            } else {
                throw new HttpException(500, Yii::t('fileupload', 'Could not upload file.'));
            }
        }
        return $response;
    }