photos wip - almost there
This commit is contained in:
parent
803e161a9a
commit
9636cc9091
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue