Completed
Push — master ( cc01fd...c570ee )
by Oscar
04:59
created

Index::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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