Skip to content

Commit 724efe7

Browse files
authored
fix #1776 テーマファイルAPI 画像を表示 (#1787)
1 parent b9dae41 commit 724efe7

File tree

6 files changed

+101
-28
lines changed

6 files changed

+101
-28
lines changed

plugins/bc-theme-file/src/Controller/Admin/ThemeFilesController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,11 @@ public function img(ThemeFilesAdminServiceInterface $service)
532532
{
533533
$this->disableAutoRender();
534534
$args = $this->parseArgs(func_get_args());
535-
return $this->getResponse()->withStringBody($service->getImg($args));
535+
536+
$imgDetail = $service->getImg($args);
537+
header("Content-Length: " . $imgDetail['size']);
538+
header("Content-type: image/" . $imgDetail['type']);
539+
return $this->getResponse()->withStringBody($imgDetail['img']);
536540
}
537541

538542
/**

plugins/bc-theme-file/src/Controller/Api/ThemeFilesController.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
use BaserCore\Annotation\UnitTest;
1717
use BaserCore\Controller\Api\BcApiController;
1818
use BaserCore\Error\BcFormFailedException;
19+
use BaserCore\View\Helper\BcBaserHelper;
1920
use BcThemeFile\Service\ThemeFilesServiceInterface;
2021

2122
/**
2223
* テーマファイルコントローラー
24+
* @property BcBaserHelper $BcBaser
2325
*/
2426
class ThemeFilesController extends BcApiController
2527
{
@@ -212,10 +214,33 @@ public function view(ThemeFilesServiceInterface $service)
212214
* [API] テーマファイル 画像を表示
213215
*
214216
* @param ThemeFilesServiceInterface $service
217+
* @checked
218+
* @noTodo
219+
* @unitTest
215220
*/
216221
public function img(ThemeFilesServiceInterface $service)
217222
{
218-
//todo テーマファイルAPI 画像を表示 #1776
223+
$this->request->allowMethod(['get']);
224+
225+
try {
226+
$data = $this->getRequest()->getQueryParams();
227+
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
228+
$imgDetail = $service->getImg($data);
229+
} catch (BcFormFailedException $e) {
230+
$this->setResponse($this->response->withStatus(400));
231+
$errors = $e->getForm()->getErrors();
232+
$message = __d('baser', '入力エラーです。内容を修正してください。' . $e->getMessage());
233+
} catch (\Throwable $e) {
234+
$this->setResponse($this->response->withStatus(400));
235+
$message = __d('baser', '処理中にエラーが発生しました。');
236+
}
237+
238+
$this->set([
239+
'img' => base64_encode($imgDetail['img']) ?? null,
240+
'message' => $message ?? null,
241+
'errors' => $errors ?? null
242+
]);
243+
$this->viewBuilder()->setOption('serialize', ['img', 'message', 'errors']);
219244
}
220245

221246
/**

plugins/bc-theme-file/src/Service/Admin/ThemeFilesAdminService.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,6 @@ public function getViewVarsForView(ThemeFile $themeFile, ThemeFileForm $themeFil
148148
];
149149
}
150150

151-
/**
152-
* テーマ内のイメージデータを取得する
153-
*
154-
* @param $args
155-
* @return false|string
156-
*/
157-
public function getImg($args)
158-
{
159-
$contents = ['jpg' => 'jpeg', 'gif' => 'gif', 'png' => 'png'];
160-
$pathinfo = pathinfo($args['fullpath']);
161-
162-
if (!BcThemeFileUtil::getTemplateTypeName($args['type']) || !isset($contents[$pathinfo['extension']]) || !file_exists($args['fullpath'])) {
163-
throw new NotFoundException();
164-
}
165-
166-
$file = new File($args['fullpath']);
167-
if (!$file->open('r')) {
168-
throw new NotFoundException();
169-
}
170-
171-
header("Content-Length: " . $file->size());
172-
header("Content-type: image/" . $contents[$pathinfo['extension']]);
173-
return $file->read();
174-
}
175-
176151
/**
177152
* テーマ内の画像のサムネイルイメージのデータを取得する
178153
*

plugins/bc-theme-file/src/Service/ThemeFilesService.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
use BaserCore\Utility\BcUtil;
2020
use BcThemeFile\Form\ThemeFileForm;
2121
use BcThemeFile\Model\Entity\ThemeFile;
22+
use BcThemeFile\Utility\BcThemeFileUtil;
2223
use Cake\Core\Plugin;
24+
use Cake\Filesystem\File;
2325
use Cake\Filesystem\Folder;
26+
use Cake\Http\Exception\NotFoundException;
2427

2528
/**
2629
* ThemeFilesService
@@ -228,4 +231,36 @@ public function copyToTheme(array $params)
228231
return false;
229232
}
230233
}
234+
235+
/**
236+
* テーマ内のイメージデータを取得する
237+
*
238+
* @param $args
239+
* @return array
240+
*
241+
* @checked
242+
* @noTodo
243+
* @unitTest
244+
*/
245+
public function getImg($args)
246+
{
247+
$contents = ['jpg' => 'jpeg', 'gif' => 'gif', 'png' => 'png'];
248+
$pathinfo = pathinfo($args['fullpath']);
249+
250+
if (!BcThemeFileUtil::getTemplateTypeName($args['type']) || !isset($contents[$pathinfo['extension']]) || !file_exists($args['fullpath'])) {
251+
throw new NotFoundException();
252+
}
253+
254+
$file = new File($args['fullpath']);
255+
if (!$file->open('r')) {
256+
throw new NotFoundException();
257+
}
258+
259+
return [
260+
'img' => $file->read(),
261+
'size' => $file->size(),
262+
'type' => $contents[$pathinfo['extension']]
263+
];
264+
}
265+
231266
}

plugins/bc-theme-file/tests/TestCase/Controller/Api/ThemeFilesControllerTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,21 @@ public function test_view()
247247
*/
248248
public function test_img()
249249
{
250-
$this->markTestIncomplete('このテストは未実装です。');
250+
//POSTデータを生成
251+
$data = [
252+
'theme' => 'BcFront',
253+
'type' => 'img',
254+
'path' => 'logo.png',
255+
'token' => $this->accessToken
256+
];
257+
$query = http_build_query($data);
258+
//APIをコール
259+
$this->get('/baser/api/bc-theme-file/theme_files/img.json?' . $query);
260+
//レスポンスコードを確認
261+
$this->assertResponseSuccess();
262+
//戻る値を確認
263+
$result = json_decode((string)$this->_response->getBody());
264+
$this->assertNotNull(base64_decode($result->img));
251265
}
252266

253267
/**

plugins/bc-theme-file/tests/TestCase/Service/ThemeFilesServiceTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,24 @@
1212
namespace BcThemeFile\Test\TestCase\Service;
1313

1414
use BaserCore\TestSuite\BcTestCase;
15+
use BcThemeFile\Service\ThemeFilesService;
1516

1617
/**
1718
* ThemeFilesServiceTest
1819
*/
1920
class ThemeFilesServiceTest extends BcTestCase
2021
{
2122

23+
public $ThemeFileService = null;
24+
2225
/**
2326
* set up
2427
*/
2528
public function setUp(): void
2629
{
30+
$this->setFixtureTruncate();
2731
parent::setUp();
32+
$this->ThemeFileService = new ThemeFilesService();
2833
}
2934

3035
/**
@@ -35,4 +40,19 @@ public function tearDown(): void
3540
parent::tearDown();
3641
}
3742

43+
/**
44+
* test getImg
45+
*/
46+
public function test_getImg()
47+
{
48+
$data = [
49+
'theme' => 'BcFront',
50+
'type' => 'img',
51+
'path' => 'logo.png',
52+
'fullpath' => '/var/www/html/plugins/bc-front/webroot/img/logo.png'
53+
];
54+
55+
$img = $this->ThemeFileService->getImg($data);
56+
$this->assertNotNull($img);
57+
}
3858
}

0 commit comments

Comments
 (0)