Passed
Push — develop ( a56058...ddfce4 )
by Nikolay
04:39
created

DownloadNewFirmwareAction::main()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 39
rs 9.472
cc 3
nc 4
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\Files;
21
22
use MikoPBX\Core\System\Processes;
23
use MikoPBX\Core\System\Util;
24
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
25
use MikoPBX\PBXCoreREST\Workers\WorkerDownloader;
26
use Phalcon\Di;
27
28
/**
29
 *  Class DownloadNewFirmware
30
 *  Downloads new firmware from repository.
31
 *
32
 * @package MikoPBX\PBXCoreREST\Lib\Files
33
 */
34
class DownloadNewFirmwareAction extends \Phalcon\Di\Injectable
35
{
36
    /**
37
     * Downloads the firmware file from the provided URL.
38
     *
39
     * @param array $data The data array containing the following parameters:
40
     *   - Md5: The MD5 hash of the file.
41
     *   - size: The size of the file.
42
     *   - version: The version of the file.
43
     *   - Url: The download URL of the file.
44
     *
45
     * @return PBXApiResult An object containing the result of the API call.
46
     */
47
    public static function main(array $data): PBXApiResult
48
    {
49
        $di = Di::getDefault();
50
        if ($di !== null) {
51
            $uploadDir = $di->getConfig()->path('www.uploadDir');
52
        } else {
53
            $uploadDir = '/tmp';
54
        }
55
        $firmwareDirTmp = "{$uploadDir}/{$data['version']}";
56
57
        if (file_exists($firmwareDirTmp)) {
58
            $rmPath = Util::which('rm');
59
            Processes::mwExec("{$rmPath} -rf {$firmwareDirTmp}/* ");
60
        } else {
61
            Util::mwMkdir($firmwareDirTmp);
62
        }
63
64
        $download_settings = [
65
            'res_file' => "{$firmwareDirTmp}/update.img",
66
            'url'      => $data['url'],
67
            'size'     => $data['size'],
68
            'md5'      => $data['md5'],
69
        ];
70
71
        $workerDownloaderPath = Util::getFilePathByClassName(WorkerDownloader::class);
72
        file_put_contents(
73
            "{$firmwareDirTmp}/download_settings.json",
74
            json_encode($download_settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
75
        );
76
        $phpPath = Util::which('php');
77
        Processes::mwExecBg("{$phpPath} -f {$workerDownloaderPath} start {$firmwareDirTmp}/download_settings.json");
78
79
        $res                   = new PBXApiResult();
80
        $res->processor        = __METHOD__;
81
        $res->success          = true;
82
        $res->data['filename'] = $download_settings['res_file'];
83
        $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_IN_PROGRESS;
84
85
        return $res;
86
    }
87
}