/** * @return array * @throws \HttpException */ public function run() { $result = []; $uploadedFiles = UploadedFile::getInstancesByName($this->fileparam); foreach ($uploadedFiles as $uploadedFile) { /* @var \yii\web\UploadedFile $uploadedFile */ $output = []; $output['id'] = -1; $output[$this->responseUrlParam] = ""; $output['thumbnailUrl'] = ""; $output['deleteUrl'] = ""; $output['updateUrl'] = ""; $output['path'] = ""; if ($uploadedFile->error === UPLOAD_ERR_OK) { $validationModel = DynamicModel::validateData(['file' => $uploadedFile], $this->validationRules); if (!$validationModel->hasErrors()) { $attachment = new Attachment(); if ($this->storageLocation == static::STORAGE_LOCATION_TEMPPATH) { $attachment->setStorageDirectory($attachment->getTempDirectory()); $attachment->uploadFromPost($uploadedFile); } else { if ($this->storageLocation == static::STORAGE_LOCATION_USERPATH_DATABASE) { $attachment->uploadFromPost($uploadedFile); $attachment->save(); } } $output = array_merge($output, $attachment->toArray()); if ($attachment->primaryKey) { $output["id"] = $attachment->primaryKey; } $output["path"] = $attachment->getPath(); $output[$this->responseUrlParam] = $attachment->getUrl(); $output["thumbnailUrl"] = $attachment->getUrl(); $output["deleteUrl"] = Url::to(array_merge($this->deleteUrl, ['path' => $output["path"], 'id' => $output['id']])); $output["updateUrl"] = Url::to(array_merge($this->updateUrl, ['path' => $output["path"], 'id' => $output['id']])); } else { $output['error'] = true; $output['errors'] = $validationModel->errors; } } else { $output['error'] = true; $output['errors'] = $this->resolveErrorMessage($uploadedFile->error); } $result["files"][] = $output; } $result = $this->multiple ? $result : array_shift($result); return $result; }
/** * * @param \hass\attachment\models\Attachment $attachment */ protected function formartAttachment($attachment) { if ($attachment instanceof \hass\attachment\models\Attachment) { $result = $attachment->toArray(); $result['id'] = $attachment->primaryKey; $result['path'] = $attachment->getPath(); $result['url'] = $attachment->getUrl(); $result['thumbnailUrl'] = $result['url']; $result['deleteUrl'] = Url::to(array_merge($this->deleteUrl, ["path" => $result['path'], 'id' => $result['id']])); $result['deleteType'] = "DELETE"; return $result; } else { if (is_string($attachment) && !empty($attachment)) { return ["url" => $attachment, "path" => $attachment]; } else { if (is_array($attachment)) { return $attachment; } } } return null; }