Skip to content

Commit

Permalink
Added UserException
Browse files Browse the repository at this point in the history
  • Loading branch information
uldisn committed May 13, 2021
1 parent b3d898c commit 8e5023b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 44 deletions.
7 changes: 7 additions & 0 deletions components/DownloadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class DownloadAction extends Action

public const THE_REQUESTED_FILE_DOES_NOT_EXIST = 'The requested file does not exist.';

/**
* @throws \d3yii2\d3files\exceptions\D3FilesUserException
* @throws \yii\web\HttpException
* @throws \yii\web\ForbiddenHttpException
* @throws \ReflectionException
* @throws \yii\web\NotFoundHttpException
*/
public function run(int $id, string $model_name_id = ''): void
{

Expand Down
5 changes: 5 additions & 0 deletions components/DownloadShareAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
class DownloadShareAction extends Action
{
/**
* @throws \ReflectionException
* @throws \d3yii2\d3files\exceptions\D3FilesUserException
* @throws \yii\web\NotFoundHttpException
*/
public function run($id, $hash)
{

Expand Down
9 changes: 4 additions & 5 deletions components/EditNotesAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace d3yii2\d3files\components;

use d3yii2\d3files\exceptions\D3FilesUserException;
use d3yii2\d3files\models\D3files;
use d3yii2\d3files\models\D3filesModel;
use d3yii2\d3files\models\D3filesModelName;

use Exception;
use Yii;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use function in_array;


/**
* Class EditNotesAction
Expand All @@ -21,14 +21,13 @@ class EditNotesAction extends D3FilesAction
{
/**
* @param int $id
* @param string $model_name
* @return array
*/
public function run(int $id): array
{
try {
if (!$fileModel = D3files::findOne($id)) {
throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
throw new D3FilesUserException(Yii::t('d3files', 'The requested file does not exist.'));
}

//@FIXME - jāparedz iespēja pārbaudīt pieejas tiesības
Expand Down
16 changes: 8 additions & 8 deletions components/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace d3yii2\d3files\components;

use d3system\exceptions\D3Exception;
use d3yii2\d3files\exceptions\D3FilesUserException;
use ReflectionClass;
use ReflectionException;
use Yii;
Expand Down Expand Up @@ -31,7 +32,6 @@ class FileHandler
/**
* FileHandler constructor.
* @param $options
* @throws ForbiddenHttpException
* @throws ReflectionException
*/
public function __construct($options)
Expand Down Expand Up @@ -66,7 +66,7 @@ public function __construct($options)
* @throws ForbiddenHttpException
* @throws ReflectionException
*/
public function validateFileExtentsion()
public function validateFileExtentsion(): void
{
$fileTypes = self::getAllowedFileTypes($this->options);

Expand Down Expand Up @@ -111,14 +111,14 @@ protected static function getAllowedFileTypes(array $options = []): string
* copy posted file to upload directory
* @return bool
* @throws NotFoundHttpException
* @throws Exception
* @throws Exception|\ReflectionException
*/
public function upload(): bool
{
$this->validateFileExtentsion();

if (!isset($_FILES['upload_file'])) {
throw new InvalidArgumentException(Yii::t('d3files', 'upload_file is not set'));
throw new D3FilesUserException(Yii::t('d3files', 'upload_file is not set'));
}

$filePath = $this->getFilePath();
Expand All @@ -141,7 +141,7 @@ public function upload(): bool
: $_FILES['upload_file']['tmp_name'];

if (!move_uploaded_file($tmpName, $filePath)) {
throw new NotFoundHttpException(Yii::t('d3files', 'The uploaded file does not exist.'));
throw new D3FilesUserException(Yii::t('d3files', 'The uploaded file does not exist.'));
}

$this->uploadedFilePath = $filePath;
Expand Down Expand Up @@ -217,7 +217,7 @@ public function uploadYii2UloadFile(UploadedFile $uploadedFile): bool
$filePath = $this->getFilePath();
FileHelper::createDirectory(dirname($filePath));
if (!$uploadedFile->saveAs($filePath)) {
throw new NotFoundHttpException(Yii::t('d3files', 'The uploaded file does not exist.'));
throw new D3FilesUserException(Yii::t('d3files', 'The uploaded file does not exist.'));
}

return true;
Expand Down Expand Up @@ -275,14 +275,14 @@ public function remove(): bool
}

/**
* @throws NotFoundHttpException
* @throws \d3yii2\d3files\exceptions\D3FilesUserException
*/
public function download(): void
{
$file_path = $this->getFilePath();

if (!is_file($file_path)) {
throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
throw new D3FilesUserException(Yii::t('d3files', 'The requested file does not exist.'));
}

header('Content-Description: File Transfer');
Expand Down
2 changes: 1 addition & 1 deletion components/UploadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function run(int $id): array
$fileHandler->rename($model->id);
} else {
$fileHandler->remove();
throw new HttpException(500, Yii::t('d3files', 'Insert DB record failed'));
throw new \yii\db\Exception(Yii::t('d3files', 'Insert DB record failed'));
}

$renderParam = [
Expand Down
12 changes: 12 additions & 0 deletions exceptions/D3FilesUserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace d3yii2\d3files\exceptions;


use yii\base\Exception;

class D3FilesUserException extends Exception
{

}
43 changes: 20 additions & 23 deletions models/D3files.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
use d3system\exceptions\D3Exception;
use d3yii2\d3files\components\FileHandler;
use ReflectionException;
use RuntimeException;
use Yii;
use yii\base\Exception;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Exception;
use yii\db\Expression;
use yii\web\ForbiddenHttpException;
use yii\web\UploadedFile;
Expand Down Expand Up @@ -45,10 +44,13 @@ public static function tableName(): string
* @param int $modelId
* @param string $filePath
* @param string $fileTypes
* @param int $userId
* @throws \Exception
* @param int|null $userId
* @throws \ReflectionException
* @throws \d3system\exceptions\D3ActiveRecordException
* @throws \d3system\exceptions\D3Exception
* @throws \yii\base\Exception
*/
public static function saveFile($fileName, $modelName, $modelId, $filePath, $fileTypes, $userId = 0): void
public static function saveFile(string $fileName,string $modelName,int $modelId,string $filePath,string $fileTypes,?int $userId = 0): void
{
$fileHandler = new FileHandler(
[
Expand Down Expand Up @@ -83,12 +85,14 @@ public static function saveFile($fileName, $modelName, $modelId, $filePath, $fil
* @param int $modelId
* @param string $fileContent
* @param string $fileTypes
* @param int $userId
* @throws ForbiddenHttpException
* @throws Exception
* @throws ReflectionException
* @param int|null $userId
* @throws \ReflectionException
* @throws \d3system\exceptions\D3ActiveRecordException
* @throws \d3system\exceptions\D3Exception
* @throws \yii\base\Exception
* @throws \yii\db\Exception
*/
public static function saveContent($fileName, $modelName, $modelId, $fileContent, $fileTypes, $userId = 0): void
public static function saveContent(string $fileName,string $modelName,int $modelId,string $fileContent,string $fileTypes,?int $userId = 0): void
{
$fileHandler = new FileHandler(
[
Expand All @@ -114,7 +118,7 @@ public static function saveContent($fileName, $modelName, $modelId, $fileContent
}
} else {
$fileHandler->remove();
throw new RuntimeException(500, Yii::t('d3files', 'Insert DB record failed'));
throw new Exception(Yii::t('d3files', 'Insert DB record failed'));
}
}

Expand All @@ -125,7 +129,7 @@ public static function saveContent($fileName, $modelName, $modelId, $fileContent
* @param int $modelId
* @throws \Exception
*/
public static function saveYii2UploadFile(UploadedFile $uploadFile, $modelName, $modelId): void
public static function saveYii2UploadFile(UploadedFile $uploadFile, string $modelName,int $modelId): void
{
$fileHandler = new FileHandler(
[
Expand Down Expand Up @@ -157,6 +161,7 @@ public static function saveYii2UploadFile(UploadedFile $uploadFile, $modelName,
* @param string $modelName
* @param int $modelId
* @param int $filesModelId
* @throws \d3system\exceptions\D3ActiveRecordException
*/
private static function saveModelName(string $modelName, int $modelId, int $filesModelId): void
{
Expand Down Expand Up @@ -185,11 +190,9 @@ private static function saveModelName(string $modelName, int $modelId, int $file
* 'file_model_id' => '44' //d3files_model.id
* 'file_path => '/var/www/car/upload/car/111.jpg'
* ]
* @throws ForbiddenHttpException
* @throws \yii\db\Exception
* @throws ReflectionException
*/
public static function getRecordFilesList($modelName, $modelId): array
public static function getRecordFilesList(string $modelName, int $modelId): array
{
$filesList = self::fileListForWidget($modelName, $modelId);
foreach ($filesList as $k => $fileRow) {
Expand All @@ -212,7 +215,6 @@ public static function getRecordFilesList($modelName, $modelId): array
* @param $modelName
* @param $modelId
* @return array
* @throws \yii\db\Exception
*/
public static function fileListForWidget($modelName, $modelId): array
{
Expand Down Expand Up @@ -241,17 +243,15 @@ public static function fileListForWidget($modelName, $modelId): array

$connection = Yii::$app->getDb();
$command = $connection->createCommand($sSql, $parameters);
$raw = $command->getRawSql();
return $command->queryAll();
}

/**
* get file list for widget by model name id and model id
*
* @param $modelName
* @param $modelId
* @param int $modelNameId
* @param int $modelId
* @return array
* @throws \yii\db\Exception
*/
public static function fileListForWidgetByNameId(int $modelNameId, int $modelId): array
{
Expand Down Expand Up @@ -279,7 +279,6 @@ public static function fileListForWidgetByNameId(int $modelNameId, int $modelId)

$connection = Yii::$app->getDb();
$command = $connection->createCommand($sSql, $parameters);
$raw = $command->getRawSql();
return $command->queryAll();
}

Expand All @@ -301,7 +300,6 @@ public static function performReadValidation(string $model_name, int $model_id):
* @param string $modelClass
* @param array $modelIds
* @return array
* @throws \yii\db\Exception
*/

public static function forListBox(string $modelClass, array $modelIds): array
Expand All @@ -326,7 +324,6 @@ public static function forListBox(string $modelClass, array $modelIds): array
* @param string $modelClass
* @param array $ids
* @return array
* @throws \yii\db\Exception
*/
public static function getAllByModelRecordIds(string $modelClass, array $ids): array
{
Expand Down
13 changes: 6 additions & 7 deletions widgets/D3FilesPreviewWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use d3yii2\pdfobject\widgets\PDFObject;
use eaBlankonThema\assetbundles\AjaxAsset;
use Exception;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use Yii;
use d3yii2\d3files\components\D3Files;
use yii\web\View;
use function is_callable;

/**
* Class D3FilesPreviewWidget
Expand Down Expand Up @@ -107,7 +107,7 @@ public function init(): void
}

// Check for PDF and AJAX loaded attachments to assets and assign preview attributes
foreach ($this->fileList as $i => $file) {
foreach ($this->fileList as $file) {
$ext = D3Files::getFileExtension($file);
if ('pdf' === $ext) {
$hasPdf = true;
Expand Down Expand Up @@ -151,7 +151,7 @@ public function init(): void

// Ensure modal preview is enabled and the layout rendered once
if (self::VIEW_TYPE_MODAL === $this->viewType && !isset(Yii::$app->view->params['D3FilesModalRendered'])) {
if (\is_callable($this->dialogWidgetClass)) {
if (is_callable($this->dialogWidgetClass)) {
throw new D3Exception('Invalid Modal Dialog class: ' . $this->dialogWidgetClass);
}

Expand Down Expand Up @@ -184,6 +184,7 @@ public function init(): void

/**
* @return array
* @throws \yii\db\Exception
*/
public function initFilesList(): array
{
Expand All @@ -208,7 +209,6 @@ public function initFilesList(): array

/**
* @return string
* @throws InvalidConfigException
*/
public function getAssetsUrl(): string
{
Expand Down Expand Up @@ -279,18 +279,17 @@ public function getViewParams(): ?array
];

if (self::VIEW_MODAL_BUTTON === $this->view || self::VIEW_INLINE_BUTTON === $this->view) {
foreach ($this->viewExtension as $key => $extension) {
foreach ($this->viewExtension as $extension) {
if($params['file'] = D3Files::getFirstFileHavingExt($this->fileList, $extension)) {
break;
}
}
}

$params = array_merge(
return array_merge(
parent::getViewParams(),
$params
);
return $params;
}

/**
Expand Down

0 comments on commit 8e5023b

Please sign in to comment.