fix: throw validation error for filename

This commit is contained in:
b1ek 2024-08-21 17:58:35 +10:00
parent 976e463f5c
commit 572dae98ab
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 8 additions and 2 deletions

View File

@ -2,8 +2,10 @@
namespace app\models; namespace app\models;
use Anper\Iuliia\Iuliia;
use Yii; use Yii;
use yii\helpers\FileHelper; use yii\helpers\FileHelper;
use yii\web\BadRequestHttpException;
use yii\web\UploadedFile; use yii\web\UploadedFile;
/** /**
@ -37,9 +39,13 @@ class Image extends \yii\db\ActiveRecord
copy($file->tempName, env('UPLOADS_PATH') . '/' . $hash); copy($file->tempName, env('UPLOADS_PATH') . '/' . $hash);
if (!preg_match('/^[a-z0-9-_]+(|\.[\w\d]+)$/i', Iuliia::translate($file->name, Iuliia::WIKIPEDIA))) {
throw new BadRequestHttpException('Filename must be a-Z0-9_-!');
}
$new = new Image([ $new = new Image([
'sha256' => $hash, 'sha256' => $hash,
'original_name' => $file->name, 'original_name' => Iuliia::translate($file->name, Iuliia::WIKIPEDIA),
'mime' => FileHelper::getMimeType($file->tempName) 'mime' => FileHelper::getMimeType($file->tempName)
]); ]);
$new->save(true); $new->save(true);
@ -54,7 +60,7 @@ class Image extends \yii\db\ActiveRecord
{ {
return [ return [
[['sha256', 'original_name'], 'required'], [['sha256', 'original_name'], 'required'],
[['sha256', 'original_name'], 'string', 'max' => 255], [['original_name'], 'match', '/^[a-z0-9-_]+(|\.[\w\d]+)$/i'],
]; ];
} }