Passed
Push — master ( 81f334...c86e8c )
by Mihail
03:35
created

ActionGalleryList::galleryList()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 33
Code Lines 20

Duplication

Lines 3
Ratio 9.09 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 5
nop 1
dl 3
loc 33
rs 5.3846
c 0
b 0
f 0
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 View Code Duplication
        if (!App::$User->isAuth() || !App::$User->identity()->role->can('global/file')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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