From 9636cc9091b9666af7c91fd06e0088695132cf9c Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Fri, 22 Aug 2025 00:41:57 -0700 Subject: [PATCH] photos wip - almost there --- src/Controller/ProductPhotosController.php | 22 +++++++++++++---- tests/Fixture/ProductPhotosFixture.php | 2 +- .../ProductPhotosControllerTest.php | 24 +++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/Controller/ProductPhotosController.php b/src/Controller/ProductPhotosController.php index bcc65d4..7ff0f34 100644 --- a/src/Controller/ProductPhotosController.php +++ b/src/Controller/ProductPhotosController.php @@ -71,7 +71,7 @@ class ProductPhotosController extends AppController $postData['id'] = $uuid; $baseDir = Configure::readOrFail('CakeProducts.photos.directory'); $product = $productPhotosTable->Products->get($this->request->getData('product_id')); - $path = $baseDir . $product->id; + $path = $product->id; if ($this->request->getData('product_sku_id')) { $productSku = $productPhotosTable->ProductSkus->get($this->request->getData('product_sku_id')); $path .= DS . 'skus' . DS . $productSku->id; @@ -81,12 +81,13 @@ class ProductPhotosController extends AppController */ $photoObject = $this->request->getData('photo'); - if (!file_exists($path)) { - if (!mkdir($path, 0777, true)) { + $fullPath = $baseDir . $path; + if (!file_exists($fullPath)) { + if (!mkdir($fullPath, 0777, true)) { throw new ForbiddenException('Failed to create the required folders. Please check the folder permissions and try again.'); } } - $destination = $path . DS . $uuid; + $destination = $fullPath . DS . $uuid; // Existing files with the same name will be replaced. $photoObject->moveTo($destination); @@ -157,4 +158,17 @@ class ProductPhotosController extends AppController return $this->redirect(['action' => 'index']); } + + /** + * @param $id + * @return Response + */ + public function image($id = null) + { + $productPhoto = $this->getTable()->get($id); + + $fullPath = Configure::readOrFail('CakeProducts.photos.directory') . $productPhoto->photo_dir . DS . $productPhoto->photo_filename; + + return $this->response->withFile($fullPath); + } } diff --git a/tests/Fixture/ProductPhotosFixture.php b/tests/Fixture/ProductPhotosFixture.php index 66564bd..3888f6a 100644 --- a/tests/Fixture/ProductPhotosFixture.php +++ b/tests/Fixture/ProductPhotosFixture.php @@ -22,7 +22,7 @@ class ProductPhotosFixture extends TestFixture 'id' => '2c386086-f4c5-4093-bea5-ee9c29479f58', 'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', 'product_sku_id' => null, - 'photo_dir' => '/products/cfc98a9a-29b2-44c8-b587-8156adc05317/', + 'photo_dir' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', 'photo_filename' => '2c386086-f4c5-4093-bea5-ee9c29479f58.png', 'primary_photo' => 1, 'photo_position' => 100, diff --git a/tests/TestCase/Controller/ProductPhotosControllerTest.php b/tests/TestCase/Controller/ProductPhotosControllerTest.php index dd852e0..1dc0d33 100644 --- a/tests/TestCase/Controller/ProductPhotosControllerTest.php +++ b/tests/TestCase/Controller/ProductPhotosControllerTest.php @@ -347,4 +347,28 @@ class ProductPhotosControllerTest extends BaseControllerTest $cntAfter = $this->ProductPhotos->find()->count(); $this->assertEquals($cntBefore - 1, $cntAfter); } + + /** + * Test image method + * + * Tests the image action with a logged in user + * + * @return void + * @throws Exception + * + * @uses ProductPhotosController::image + */ + public function testImageGetLoggedIn(): void + { + $id = '2c386086-f4c5-4093-bea5-ee9c29479f58'; + // $this->loginUserByRole('admin'); + $url = [ + 'plugin' => 'CakeProducts', + 'controller' => 'ProductPhotos', + 'action' => 'image', + $id, + ]; + $this->get($url); + $this->assertResponseCode(200); + } }