Completed
Push — master ( e26a82...5c3758 )
by Oscar
02:54
created

Index::file()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
cc 2
eloc 11
nc 2
nop 3
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
use Zend\Diactoros\Stream;
10
11
class Index
12
{
13
    public function __invoke(Request $request, Response $response, Admin $app)
14
    {
15
        $query = $request->getQueryParams();
16
17
        if (isset($query['thumb'])) {
18
            return $this->thumb($request, $response, $app);
19
        }
20
21
        if (isset($query['thumbs'])) {
22
            return $this->thumbs($request, $response, $app);
23
        }
24
25
        if (isset($query['file'])) {
26
            return $this->file($request, $response, $app);
27
        }
28
29
        return $app['templates']->render('pages/index');
30
    }
31
32
    private function file(Request $request, Response $response, Admin $app)
33
    {
34
        $query = $request->getQueryParams();
35
        $file = $app->getPath($query['file']);
36
37
        if (!is_file($file)) {
38
            return $response->withStatus(404);
39
        }
40
41
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
42
        $mime = finfo_file($finfo, $file);
43
        finfo_close($finfo);
44
45
        return $response
46
            ->withBody(new Stream($file, 'r'))
47
            ->withHeader('Content-Type', $mime);
48
    }
49
50
    private function thumb(Request $request, Response $response, Admin $app)
51
    {
52
        $query = $request->getQueryParams();
53
        $thumb = $app->getPath($query['thumb']);
54
55
        if (!is_file($thumb)) {
56
            return $response->withStatus(404);
57
        }
58
59
        $image = Image::fromFile($thumb);
60
        $image->resize(0, 200);
61
62
        $response->getBody()->write($image->getString());
63
64
        return $response->withHeader('Content-Type', $image->getMimeType());
65
    }
66
67
    private function thumbs(Request $request, Response $response, Admin $app)
68
    {
69
        $query = $request->getQueryParams();
70
        $thumbs = $app->getPath($query['thumbs']);
71
        $limit = empty($query['limit']) ? 100 : (int) $query['limit'];
72
        $offset = empty($query['offset']) ? 0 : (int) $query['offset'];
73
        $pattern = empty($query['pattern']) ? '/*' : $query['pattern'];
74
75
        $files = [];
76
        $baseLength = strlen($thumbs);
77
78
        if (is_dir($thumbs)) {
79
            foreach (glob($thumbs.$pattern, GLOB_NOSORT | GLOB_NOESCAPE | GLOB_BRACE) as $file) {
80
                if (is_file($file)) {
81
                    $files[] = substr($file, $baseLength);
82
                }
83
            }
84
        }
85
86
        $files = array_reverse($files);
87
        $files = array_splice($files, $offset, $limit);
88
89
        $response->getBody()->write(json_encode($files));
90
        return $response->withHeader('Content-Type', 'application/json');
91
    }
92
}
93