Test Setup Failed
Push — develop ( 09a6a8...5231a1 )
by Nikolay
10:01 queued 11s
created

WorkerMergeUploadedFile   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 35
c 2
b 0
f 0
dl 0
loc 69
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeFilesInDirectory() 0 24 3
A start() 0 31 3
1
<?php
2
/**
3
 * Copyright © MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Alexey Portnov, 7 2020
7
 */
8
9
namespace MikoPBX\PBXCoreREST\Workers;
10
11
require_once 'Globals.php';
12
13
use MikoPBX\Core\Workers\WorkerBase;
14
use MikoPBX\Core\System\Util;
15
16
17
class WorkerMergeUploadedFile extends WorkerBase
18
{
19
    public function start($argv): void
20
    {
21
        $settings_file = trim($argv[1]);
22
        if ( ! file_exists($settings_file)) {
23
            Util::sysLogMsg("WorkerMergeUploadedFile", 'File with settings not found');
24
25
            return;
26
        }
27
        $settings = json_decode(file_get_contents($settings_file), true);
28
        $progress_file = $settings['tempDir'] . '/merging_progress';
29
        $this->mergeFilesInDirectory(
30
            $settings['tempDir'],
31
            $settings['resumableFilename'],
32
            $settings['resumableTotalChunks'],
33
            $settings['fullUploadedFileName'],
34
            $progress_file
35
        );
36
37
        // Check filesize is equal uploaded size
38
        $resultFileSize = filesize($settings['fullUploadedFileName']);
39
        if ((int)$settings['resumableTotalSize'] === $resultFileSize){
40
            file_put_contents($progress_file, '100');
41
        } else {
42
            Util::sysLogMsg('UploadFile', "File {$settings['fullUploadedFileName']} size {$resultFileSize} does not equal {$settings['resumableTotalSize']}");
43
        }
44
45
        // Delete uploaded file after 10 minutes
46
        Util::mwExecBg(
47
            '/sbin/shell_functions.sh killprocesses ' . $settings['tempDir'] . ' -TERM 0;rm -rf ' . $settings['tempDir'],
48
            '/dev/null',
49
            600
50
        );
51
    }
52
53
    /**
54
     * Glues uploaded parts of file to en one with fileName
55
     *
56
     * @param string $tempDir
57
     * @param string $fileName
58
     * @param int    $total_files
59
     * @param string $result_file
60
     * @param string $progress_file
61
     */
62
    private function mergeFilesInDirectory(
63
        string $tempDir,
64
        string $fileName,
65
        int $total_files,
66
        string $result_file,
67
        string $progress_file
68
    ): void {
69
        file_put_contents($progress_file, '0');
70
        // Restore original file from chunks
71
        if (($fp = fopen($result_file, 'w')) !== false) {
72
            for ($i = 1; $i <= $total_files; $i++) {
73
                $tmp_file = $tempDir . '/' . $fileName . '.part' . $i;
74
                fwrite($fp, file_get_contents($tmp_file));
75
                unlink($tmp_file);
76
                $currentProgress = round($i / $total_files * 100)-1; //Up to 99%
77
                file_put_contents($progress_file, $currentProgress, 2);
78
            }
79
            fclose($fp);
80
        } else {
81
            Util::sysLogMsg('UploadFile', 'cannot create the destination file - ' . $result_file);
82
83
            return;
84
        }
85
        Util::sysLogMsg('UploadFile', 'destination file - ' . $result_file);
86
    }
87
}
88
89
// Start worker process
90
$workerClassname = WorkerMergeUploadedFile::class;
91
if (isset($argv) && count($argv) > 1) {
92
    cli_set_process_title($workerClassname);
93
    try {
94
        $worker = new $workerClassname();
95
        $worker->start($argv);
96
    } catch (\Error $e) {
97
        global $errorLogger;
98
        $errorLogger->captureException($e);
99
        Util::sysLogMsg("{$workerClassname}_EXCEPTION", $e->getMessage());
100
    }
101
}
102