* cors and other basic changes
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Response\JsonCorsResponse;
|
||||
use App\Service\GalleryService;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class GetGalleryImagesAction implements ServerMiddlewareInterface
|
||||
{
|
||||
@@ -20,6 +20,6 @@ class GetGalleryImagesAction implements ServerMiddlewareInterface
|
||||
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
|
||||
{
|
||||
$slug = $request->getAttribute('slug', false);
|
||||
return new JsonResponse($this->galleryService->getGallery($slug));
|
||||
return new JsonCorsResponse($this->galleryService->getGallery($slug));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +34,8 @@ class GetImageAction implements ServerMiddlewareInterface
|
||||
->withHeader('Content-Transfer-Encoding', 'Binary')
|
||||
->withHeader('Content-Description', 'File Transfer')
|
||||
->withHeader('Pragma', 'public')
|
||||
->withHeader('Expires', '0')
|
||||
->withHeader('Cache-Control', 'must-revalidate')
|
||||
// ->withHeader('Expires', '0')
|
||||
// ->withHeader('Cache-Control', 'must-revalidate')
|
||||
->withBody($stream)
|
||||
->withHeader('Content-Length', "{$stream->getSize()}");
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Response\JsonCorsResponse;
|
||||
use App\Service\GalleryService;
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class ListGalleriesAction implements ServerMiddlewareInterface
|
||||
{
|
||||
@@ -19,6 +19,7 @@ class ListGalleriesAction implements ServerMiddlewareInterface
|
||||
|
||||
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
|
||||
{
|
||||
return new JsonResponse($this->galleryService->listGalleries());
|
||||
// return new JsonCorsResponse($this->galleryService->listGalleries(true));
|
||||
return new JsonCorsResponse($this->galleryService->loadGalleryData(true));
|
||||
}
|
||||
}
|
||||
|
||||
34
src/App/Middleware/PreFlightMiddleware.php
Normal file
34
src/App/Middleware/PreFlightMiddleware.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Middleware;
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class PreFlightMiddleware
|
||||
{
|
||||
const ALLOW_HEADERS = [
|
||||
'DNT',
|
||||
'X-CustomHeader',
|
||||
'Keep-Alive',
|
||||
'User-Agent',
|
||||
'X-Requested-With',
|
||||
'If-Modified-Since',
|
||||
'Cache-Control',
|
||||
'Content-Type',
|
||||
'Authorization',
|
||||
];
|
||||
|
||||
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
|
||||
{
|
||||
$requestMethod = strtoupper($request->getMethod());
|
||||
if ($requestMethod == 'OPTIONS') {
|
||||
return $response
|
||||
->withHeader('Accept', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
|
||||
->withHeader('Access-Control-Allow-Origin', '*')
|
||||
->withHeader('Access-Control-Allow-Methods', 'OPTIONS,GET,POST,PUT,PATCH,DELETE')
|
||||
->withHeader('Access-Control-Allow-Headers', implode(",", self::ALLOW_HEADERS));
|
||||
}
|
||||
return $next($request, $response);
|
||||
}
|
||||
}
|
||||
32
src/App/Response/JsonCorsResponse.php
Normal file
32
src/App/Response/JsonCorsResponse.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Response;
|
||||
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class JsonCorsResponse extends JsonResponse
|
||||
{
|
||||
const ALLOW_HEADERS = [
|
||||
'DNT',
|
||||
'X-CustomHeader',
|
||||
'Keep-Alive',
|
||||
'User-Agent',
|
||||
'X-Requested-With',
|
||||
'If-Modified-Since',
|
||||
'Cache-Control',
|
||||
'Content-Type',
|
||||
'Authorization',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
$data,
|
||||
$status = 200,
|
||||
array $headers = [],
|
||||
$encodingOptions = self::DEFAULT_JSON_FLAGS
|
||||
) {
|
||||
$headers['Access-Control-Allow-Origin'] = '*';
|
||||
$headers['Access-Control-Allow-Methods'] = 'OPTIONS,GET,POST,PUT,PATCH,DELETE';
|
||||
$headers['Access-Control-Allow-Headers'] = implode(",", self::ALLOW_HEADERS);
|
||||
parent::__construct($data, $status, $headers, $encodingOptions);
|
||||
}
|
||||
}
|
||||
@@ -13,92 +13,177 @@ use Zend\Config\Reader\Yaml;
|
||||
class GalleryService
|
||||
{
|
||||
|
||||
const CONFIG_FILE = 'data/galleries/gallery.yaml';
|
||||
const IMAGE_BASEDIR = 'data/galleries/%s/';
|
||||
const THUMBNAIL_SIZE = 250;
|
||||
const CONFIG_FILE = 'data/galleries/config.yaml';
|
||||
const IMAGE_BASEDIR = 'data/galleries/%s/';
|
||||
const THUMBNAIL_SIZE = 250;
|
||||
|
||||
protected $contentTypeMap = [
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
];
|
||||
protected $config;
|
||||
protected $contentTypeMap = [
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
];
|
||||
protected $config;
|
||||
|
||||
public function listGalleries($includeHidden = false)
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
$slugs = [];
|
||||
foreach ($config['galleries'] as $id => $data) {
|
||||
if ($includeHidden || $data['public']) {
|
||||
$slugs[] = [
|
||||
'id' => $id,
|
||||
'label' => $data['name'],
|
||||
'new' => $data['new'],
|
||||
];
|
||||
}
|
||||
}
|
||||
return $slugs;
|
||||
}
|
||||
|
||||
public function getGallery(string $slug)
|
||||
{
|
||||
$galleryList = $this->listGalleries(true);
|
||||
$slugs = array_map(function($item) {
|
||||
return $item['id'];
|
||||
}, $galleryList);
|
||||
$config = $this->getConfig();
|
||||
$images = [];
|
||||
|
||||
if (in_array($slug, $slugs)) {
|
||||
$dir = $config['galleries'][$slug]['dir'];
|
||||
$images = array_map('basename', glob(sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir), GLOB_BRACE));
|
||||
}
|
||||
|
||||
return [
|
||||
'name' => $config['galleries'][$slug]['name'],
|
||||
'type' => $config['galleries'][$slug]['type'],
|
||||
'images' => $images,
|
||||
public function loadGalleryData($includeHidden = false)
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
$slugs = [];
|
||||
foreach ($config['galleries'] as $id => $data) {
|
||||
if ($includeHidden || $data['public']) {
|
||||
$slugs[] = [
|
||||
'slug' => $id,
|
||||
'name' => $data['name'],
|
||||
'coverImage' => isset($data['cover'])
|
||||
? $this->getCoverImage($data['cover'])
|
||||
: null,
|
||||
'date' => $this->getGalleryDate($data['dir']),
|
||||
'type' => $data['type'],
|
||||
'isNew' => $data['new'],
|
||||
'isPublic' => $data['public'],
|
||||
'images' => $this->loadGalleryImages($data['dir']),
|
||||
];
|
||||
}
|
||||
}
|
||||
return $slugs;
|
||||
}
|
||||
|
||||
private function getGalleryDate(string $dir): string
|
||||
{
|
||||
$timestamp = sprintf("@%s", filemtime(sprintf(self::IMAGE_BASEDIR, $dir)));
|
||||
$date = new \DateTime($timestamp);
|
||||
return $date->format("Y-m-d");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dir
|
||||
* @return array
|
||||
* @todo implement label for image in some way
|
||||
*/
|
||||
private function loadGalleryImages(string $dir)
|
||||
{
|
||||
$images = array_map('basename', glob(
|
||||
sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir),
|
||||
GLOB_BRACE
|
||||
));
|
||||
return array_map(function ($image) {
|
||||
return [
|
||||
'label' => '',
|
||||
'path' => $image,
|
||||
];
|
||||
}, $images);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $image
|
||||
* @return array
|
||||
*/
|
||||
private function getCoverImage(string $image)
|
||||
{
|
||||
return [
|
||||
'label' => '',
|
||||
'path' => $image,
|
||||
];
|
||||
}
|
||||
|
||||
public function listGalleries($includeHidden = false)
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
$slugs = [];
|
||||
foreach ($config['galleries'] as $id => $data) {
|
||||
if ($includeHidden || $data['public']) {
|
||||
$slugs[] = [
|
||||
'id' => $id,
|
||||
'label' => $data['name'],
|
||||
'new' => $data['new'],
|
||||
];
|
||||
}
|
||||
}
|
||||
return $slugs;
|
||||
}
|
||||
|
||||
public function getGallery(string $slug)
|
||||
{
|
||||
$galleryList = $this->listGalleries(true);
|
||||
$slugs = array_map(function ($item) {
|
||||
return $item['id'];
|
||||
}, $galleryList);
|
||||
$config = $this->getConfig();
|
||||
$images = [];
|
||||
|
||||
if (in_array($slug, $slugs)) {
|
||||
$dir = $config['galleries'][$slug]['dir'];
|
||||
$images = array_map('basename', glob(
|
||||
sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir),
|
||||
GLOB_BRACE
|
||||
));
|
||||
}
|
||||
|
||||
public function getImage(string $slug, string $image, $size = false): string
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
$galleryDir = sprintf(self::IMAGE_BASEDIR, $config['galleries'][$slug]['dir']);
|
||||
$imageFileName = $size
|
||||
? $this->getResizedImageName($galleryDir, $image, $size)
|
||||
: ( $galleryDir . $image );
|
||||
return [
|
||||
'name' => $config['galleries'][$slug]['name'],
|
||||
'type' => $config['galleries'][$slug]['type'],
|
||||
'images' => $images,
|
||||
];
|
||||
}
|
||||
|
||||
return $imageFileName;
|
||||
public function getImage(string $slug, string $image, $size = false): string
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
$galleryDir = sprintf(self::IMAGE_BASEDIR, $config['galleries'][$slug]['dir']);
|
||||
$imageFileName = $size
|
||||
? $this->getResizedImageName($galleryDir, $image, $size)
|
||||
: ($galleryDir . $image);
|
||||
|
||||
return $imageFileName;
|
||||
}
|
||||
|
||||
public function exportGallery(string $slug)
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
|
||||
if (!isset($config['galleries'][$slug])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function getResizedImageName($galleryDirectory, $imageName, $size): string
|
||||
{
|
||||
$numericSize = $size == 'thumb' ? self::THUMBNAIL_SIZE : $size;
|
||||
$dir = $config['galleries'][$slug]['dir'];
|
||||
$images = array_map('basename', glob(sprintf(self::IMAGE_BASEDIR . "*.{jpg,jpeg,png}", $dir), GLOB_BRACE));
|
||||
|
||||
$thumbPath = $galleryDirectory . "thumb_" . $numericSize;
|
||||
@mkdir($thumbPath);
|
||||
$thumbImageName = $thumbPath . "/" . $imageName;
|
||||
$zipName = tempnam("data/tmp", "export");
|
||||
$zipArchive = new \ZipArchive();
|
||||
$zipArchive->open($zipName, \ZipArchive::CREATE);
|
||||
foreach ($images as $image) {
|
||||
$zipArchive->addFile("{$dir}/{$image}");
|
||||
}
|
||||
$zipArchive->close();
|
||||
return $zipName;
|
||||
}
|
||||
|
||||
if (!file_exists($thumbImageName)) {
|
||||
$thumbSize = new Box($numericSize, $numericSize);
|
||||
$imagine = new Imagine();
|
||||
$image = $imagine->open($galleryDirectory . $imageName)
|
||||
->thumbnail($thumbSize, ImageInterface::THUMBNAIL_OUTBOUND);
|
||||
$image->effects()->sharpen();
|
||||
$image->save($thumbImageName);
|
||||
}
|
||||
protected function getResizedImageName($galleryDirectory, $imageName, $size): string
|
||||
{
|
||||
$numericSize = $size == 'thumb' ? self::THUMBNAIL_SIZE : $size;
|
||||
|
||||
return $thumbImageName;
|
||||
$thumbPath = $galleryDirectory . "thumb_" . $numericSize;
|
||||
@mkdir($thumbPath);
|
||||
$thumbImageName = $thumbPath . "/" . $imageName;
|
||||
|
||||
if (!file_exists($thumbImageName)) {
|
||||
$thumbSize = new Box($numericSize, $numericSize);
|
||||
$imagine = new Imagine();
|
||||
$image = $imagine->open($galleryDirectory . $imageName)
|
||||
->thumbnail($thumbSize, ImageInterface::THUMBNAIL_OUTBOUND);
|
||||
$image->effects()->sharpen();
|
||||
$image->save($thumbImageName);
|
||||
}
|
||||
|
||||
protected function getConfig()
|
||||
{
|
||||
if (null === $this->config) {
|
||||
$parser = new Parser();
|
||||
$configReader = new Yaml([$parser, 'parse']);
|
||||
$this->config = $configReader->fromFile(self::CONFIG_FILE);
|
||||
}
|
||||
return $this->config;
|
||||
return $thumbImageName;
|
||||
}
|
||||
|
||||
protected function getConfig()
|
||||
{
|
||||
if (null === $this->config) {
|
||||
$parser = new Parser();
|
||||
$configReader = new Yaml([$parser, 'parse']);
|
||||
$this->config = $configReader->fromFile(self::CONFIG_FILE);
|
||||
}
|
||||
return $this->config;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user