Skip to content

Commit

Permalink
【5.1】ファイルを選択し保存をするとエラーが発生する【テーマ設定】 (#3094) fix #3038
Browse files Browse the repository at this point in the history
Co-authored-by: Đỗ Văn Hùng <dovanhungk57@gmail.com>
  • Loading branch information
HungDV2022 and dovanhung committed Feb 9, 2024
1 parent fbe55fa commit 6cb1beb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
4 changes: 2 additions & 2 deletions plugins/baser-core/src/Model/Validation/BcValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ public static function fileExt($file, $exts)
}

// FILES形式のチェック
if (!empty($file['name'])) {
$ext = BcUtil::decodeContent($file['type'], $file['name']);
if (!is_string($file) && !empty($file->getClientMediaType())) {
$ext = BcUtil::decodeContent($file->getClientMediaType(), $file->getClientMediaType());
if (!in_array($ext, $exts)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Model\Validation\BcValidation;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;
use Laminas\Diactoros\UploadedFile;

/**
* Class BcValidationTest
Expand Down Expand Up @@ -253,26 +254,52 @@ public static function fileCheckDataProvider()
* @param boolean $expect
* @dataProvider fileExtDataProvider
*/
public function testFileExt($fileName, $fileType, $expect)
public function testFileExt($fileType, $expect)
{
$check = [
"name" => $fileName,
"type" => $fileType
];
$ext = "jpg,png";

$result = $this->BcValidation->fileExt($check, $ext);
$result = $this->BcValidation->fileExt($fileType, $ext);
$this->assertEquals($expect, $result);
}


public static function fileExtDataProvider()
{
return [
["test.jpg", "image/jpeg", true],
["test.png", "image/png", true],
["test.gif", "image/gif", false],
["test", "image/png", true],
[
new UploadedFile(
'test.jpg',
1,
UPLOAD_ERR_OK,
'test.jpg',
'image/jpeg'),
true
],
[
new UploadedFile(
'test.png',
1,
UPLOAD_ERR_OK,
'test.png',
'image/jpeg'),
true
],
[
new UploadedFile('test.gif',
1,
UPLOAD_ERR_OK,
'test.gif',
'image/jpeg'),
true
],
[
new UploadedFile('test',
1,
UPLOAD_ERR_OK,
'test.png',
'image/jpeg'),
true
]
];
}

Expand Down
17 changes: 6 additions & 11 deletions plugins/bc-theme-config/src/Service/ThemeConfigsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,16 @@ public function update(array $postData)
public function saveImage($entity)
{
$saveDir = WWW_ROOT . 'files' . DS . 'theme_configs' . DS;
if(!is_dir($saveDir)) {
if (!is_dir($saveDir)) {
$folder = new BcFolder($saveDir);
$folder->create();
}
$thumbSuffix = '_thumb';
$oldEntity = $this->ThemeConfigs->getKeyValue();

foreach(['logo', 'main_image_1', 'main_image_2', 'main_image_3', 'main_image_4', 'main_image_5'] as $image) {
if (!empty($entity->{$image}['tmp_name'])) {
foreach (['logo', 'main_image_1', 'main_image_2', 'main_image_3', 'main_image_4', 'main_image_5'] as $image) {
$imageEntity = $entity->{$image};
if (!is_null($imageEntity) && !empty($imageEntity->getClientFileName())) {
// 古い本体ファイルを削除
@unlink($saveDir . $oldEntity[$image]);

Expand All @@ -149,16 +150,10 @@ public function saveImage($entity)
@unlink($saveDir . $pathinfo['filename'] . $thumbSuffix . '.' . $pathinfo['extension']);

// 本体ファイルを保存
$ext = pathinfo($entity->{$image}['name'], PATHINFO_EXTENSION);
$ext = BcUtil::decodeContent($imageEntity->getClientMediaType(), $imageEntity->getClientFilename());
$fileName = $image . '.' . $ext;
$filePath = $saveDir . $fileName;
if(is_uploaded_file($entity->{$image}['tmp_name'])) {
move_uploaded_file($entity->{$image}['tmp_name'], $filePath);
} elseif(BcUtil::isTest()) {
copy($entity->{$image}['tmp_name'], $filePath);
} else {
continue;
}
$imageEntity->moveTo($filePath);

// サムネイルを保存
$imageresizer = new Imageresizer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Utility\BcContainerTrait;
use BaserCore\Utility\BcFile;
use BaserCore\Utility\BcFolder;
use BcThemeConfig\Model\Entity\ThemeConfig;
use BcThemeConfig\Service\ThemeConfigsService;
use BcThemeConfig\Service\ThemeConfigsServiceInterface;
use BcThemeConfig\Test\Scenario\ThemeConfigsScenario;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;
use Cake\Filesystem\File;
use Laminas\Diactoros\UploadedFile;

/**
* ThemeConfigsServiceTest
Expand Down Expand Up @@ -129,23 +131,22 @@ public function test_saveImage()
$this->ThemeConfigsService->get();

// アップロードファイルを準備
$logoPath = '/var/www/html/plugins/BcColumn/webroot/img/logo.png';
$this->setUploadFileToRequest('file', $logoPath);

$filePath = TMP . 'test_upload' . DS;
(new BcFolder($filePath))->create();
$testFile = $filePath . 'logo.png';
(new BcFile($testFile))->create();
$this->setUploadFileToRequest('file', $testFile);
// 実行
$rs = $this->ThemeConfigsService->saveImage(new ThemeConfig([
'logo' => [
'tmp_name' => $logoPath,
'error' => 0,
'name' => 'logo.png',
'type' => 'image/x-png',
'size' => 2962,
]
'logo' => new UploadedFile(
$testFile,
1000,
UPLOAD_ERR_OK,
'logo.png',
"image/png",
)
]));
// saveImage の内部で実行される move_uploaded_file() が、
// 実際にファイルをアップロードしないと失敗してしまうため、copy() で代替処理とする
$uploadedPath = WWW_ROOT . 'files' . DS . 'theme_configs' . DS. 'logo.png';
copy($logoPath, $uploadedPath);
$uploadedPath = WWW_ROOT . 'files' . DS . 'theme_configs' . DS . 'logo.png';

// 戻り値を確認
$this->assertEquals($rs['logo'], 'logo.png');
Expand Down

0 comments on commit 6cb1beb

Please sign in to comment.