ActionGalleryList   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 20
dl 0
loc 41
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B galleryList() 0 32 8
1
<?php
2
3
namespace Apps\Controller\Api\Content;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Exception\NativeException;
7
use Ffcms\Core\Exception\NotFoundException;
8
use Ffcms\Core\Helper\FileSystem\Directory;
9
use Ffcms\Core\Helper\FileSystem\File;
10
use Ffcms\Core\Helper\FileSystem\Normalize;
11
use Ffcms\Core\Helper\Type\Any;
12
use Ffcms\Core\Helper\Type\Str;
13
use Ffcms\Core\Network\Request;
14
use Ffcms\Core\Network\Response;
15
16
/**
17
 * Trait ActionGalleryList
18
 * @package Apps\Controller\Api\Content
19
 * @property Request $request
20
 * @property Response $response
21
 * @method void setJsonHeader()
22
 */
23
trait ActionGalleryList
24
{
25
    /**
26
     * Show gallery images from upload directory
27
     * @param string $id
28
     * @return string
29
     * @throws NotFoundException
30
     * @throws NativeException
31
     */
32
    public function galleryList(string $id)
33
    {
34
        $this->setJsonHeader();
35
36
        // check if user have permission to access there
37
        if (!App::$User->isAuth() || !App::$User->identity()->role->can('global/file')) {
38
            throw new NativeException('Permission denied');
39
        }
40
41
        $thumbDir = Normalize::diskFullPath('/upload/gallery/' . $id . '/orig/');
42
        if (!Directory::exist($thumbDir)) {
43
            throw new NotFoundException('Nothing found');
44
        }
45
46
        $files = Directory::scan($thumbDir, null, true);
47
        if (!$files || !Any::isArray($files) || count($files) < 1) {
48
            throw new NotFoundException('Nothing found');
49
        }
50
51
        $output = [];
52
        foreach ($files as $file) {
53
            $fileExt = Str::lastIn($file, '.');
54
            $fileName = Str::sub($file, 0, -Str::length($fileExt));
55
            $output[] = [
56
                'thumbnailUrl' => '/upload/gallery/' . $id . '/thumb/' . $fileName . '.jpg',
57
                'url' => '/upload/gallery/' . $id . '/orig/' . $file,
58
                'name' => $file,
59
                'size' => File::size('/upload/gallery/' . $id . '/orig/' . $file)
60
            ];
61
        }
62
63
        return json_encode(['status' => 1, 'files' => $output]);
64
    }
65
}
66