photos wip - almost there
CI / testsuite (mysql, 8.1, ) (push) Failing after 6m9s Details
CI / testsuite (mysql, 8.4, ) (push) Failing after 5m39s Details
CI / testsuite (sqlite, 8.1, prefer-lowest) (push) Failing after 6m8s Details
CI / Coding Standard & Static Analysis (push) Failing after 5m54s Details

This commit is contained in:
Brandon Shipley 2025-08-22 00:41:57 -07:00
parent 803e161a9a
commit 9636cc9091
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
3 changed files with 43 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -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,

View File

@ -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);
}
}