|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Folk\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
7
|
|
|
use Folk\Admin; |
|
8
|
|
|
use Imagecow\Image; |
|
9
|
|
|
|
|
10
|
|
|
class Index |
|
11
|
|
|
{ |
|
12
|
|
|
public function __invoke(Request $request, Response $response, Admin $app) |
|
13
|
|
|
{ |
|
14
|
|
|
$query = $request->getQueryParams(); |
|
15
|
|
|
|
|
16
|
|
|
if (isset($query['thumb'])) { |
|
17
|
|
|
return $this->thumb($request, $response, $app); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
if (isset($query['thumbs'])) { |
|
21
|
|
|
return $this->thumbs($request, $response, $app); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
return $app['templates']->render('pages/index'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
private function thumb(Request $request, Response $response, Admin $app) |
|
28
|
|
|
{ |
|
29
|
|
|
$query = $request->getQueryParams(); |
|
30
|
|
|
$thumb = $app->getPath($query['thumb']); |
|
31
|
|
|
|
|
32
|
|
|
if (!is_file($thumb)) { |
|
33
|
|
|
return $response->withStatus(404); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$image = Image::fromFile($thumb); |
|
37
|
|
|
$image->resize(0, 200); |
|
38
|
|
|
|
|
39
|
|
|
$response->getBody()->write($image->getString()); |
|
40
|
|
|
|
|
41
|
|
|
return $response->withHeader('Content-Type', $image->getMimeType()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function thumbs(Request $request, Response $response, Admin $app) |
|
45
|
|
|
{ |
|
46
|
|
|
$query = $request->getQueryParams(); |
|
47
|
|
|
$thumbs = $app->getPath($query['thumbs']); |
|
48
|
|
|
$limit = empty($query['limit']) ? 100 : (int) $query['limit']; |
|
49
|
|
|
$offset = empty($query['offset']) ? 0 : (int) $query['offset']; |
|
50
|
|
|
$pattern = empty($query['pattern']) ? '/*' : $query['pattern']; |
|
51
|
|
|
|
|
52
|
|
|
$files = []; |
|
53
|
|
|
$baseLength = strlen($thumbs); |
|
54
|
|
|
|
|
55
|
|
|
if (is_dir($thumbs)) { |
|
56
|
|
|
foreach (glob($thumbs.$pattern, GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE) as $file) { |
|
57
|
|
|
if (is_file($file)) { |
|
58
|
|
|
$files[] = substr($file, $baseLength); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$files = array_reverse($files); |
|
64
|
|
|
$files = array_splice($files, $offset, $limit); |
|
65
|
|
|
|
|
66
|
|
|
$response->getBody()->write(json_encode($files)); |
|
67
|
|
|
return $response->withHeader('Content-Type', 'application/json'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|