Passed
Push — develop ( 368e3d...115c7b )
by Nikolay
12:16
created

DownloadLogFileAction::main()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 24
rs 9.6
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\PBXCoreREST\Lib\SysLogs;
21
22
use MikoPBX\Core\System\Processes;
23
use MikoPBX\Core\System\System;
24
use MikoPBX\Core\System\Util;
25
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
26
use Phalcon\Di;
27
28
/**
29
 * Prepares a downloadable link for a log file with the provided name.
30
 *
31
 * @package MikoPBX\PBXCoreREST\Lib\SysLogs
32
 */
33
class DownloadLogFileAction extends \Phalcon\Di\Injectable
34
{
35
    /**
36
     * Prepares a downloadable link for a log file with the provided name.
37
     *
38
     * @param string $filename The name of the log file.
39
     *
40
     * @return PBXApiResult An object containing the result of the API call.
41
     *
42
     */
43
    public static function main(string $filename): PBXApiResult
44
    {
45
        $res            = new PBXApiResult();
46
        $res->processor = __METHOD__;
47
        $filename       = System::getLogDir() . '/' . $filename;
48
        if ( ! file_exists($filename)) {
49
            $res->success    = false;
50
            $res->messages[] = 'File does not exist ' . $filename;
51
        } else {
52
            $uid          = Util::generateRandomString(36);
53
            $di           = Di::getDefault();
54
            $downloadLink = $di->getShared('config')->path('www.downloadCacheDir');
55
            $result_dir   = "{$downloadLink}/{$uid}";
56
            Util::mwMkdir($result_dir);
57
            $link_name = basename($filename);
58
            $lnPath    = Util::which('ln');
59
            $chownPath = Util::which('chown');
60
            Processes::mwExec("{$lnPath} -s {$filename} {$result_dir}/{$link_name}");
61
            Processes::mwExec("{$chownPath} www:www {$result_dir}/{$link_name}");
62
            $res->success          = true;
63
            $res->data['filename'] = "{$uid}/{$link_name}";
64
        }
65
66
        return $res;
67
    }
68
}