Passed
Push — develop ( 15bc6b...5ea31b )
by Nikolay
05:50 queued 12s
created

PostController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 30
dl 0
loc 40
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B callAction() 0 38 6
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, 6 2020
7
 */
8
9
namespace MikoPBX\PBXCoreREST\Controllers\Upload;
10
11
use MikoPBX\Core\System\Util;
12
use MikoPBX\PBXCoreREST\Controllers\BaseController;
13
use Phalcon\Di;
14
15
/**
16
 * /api/upload/{name}
17
 *   curl -F "[email protected]" http://127.0.0.1/pbxcore/api/upload/module -H 'Cookie: XDEBUG_SESSION=PHPSTORM'
18
 *   curl -X POST -d '{"id": "1531474060"}' http://127.0.0.1/pbxcore/api/upload/status; -H 'Cookie:
19
 *   XDEBUG_SESSION=PHPSTORM'
20
 */
21
class PostController extends BaseController
22
{
23
    public function callAction($actionName): void
24
    {
25
        $data           = [];
26
        $data['result'] = 'ERROR';
27
        $data['data']   = '';
28
29
        if ($this->request->hasFiles() > 0) {
30
            $data = [
31
                'resumableFilename'    => $this->request->getPost('resumableFilename'),
32
                'resumableIdentifier'  => $this->request->getPost('resumableIdentifier'),
33
                'resumableChunkNumber' => $this->request->getPost('resumableChunkNumber'),
34
                'resumableTotalSize'   => $this->request->getPost('resumableTotalSize'),
35
                'upload_id'            => time(),
36
            ];
37
            if (isset($data['resumableIdentifier'])){
38
                $data['upload_id'] = (int) filter_var($data['resumableIdentifier'], FILTER_SANITIZE_NUMBER_INT);
39
            }
40
            foreach ($this->request->getUploadedFiles() as $file) {
41
                    $data['files'][]= [
42
                        'file_path' => $file->getTempName(),
43
                        'file_size' => $file->getSize(),
44
                        'file_error'=> $file->getError(),
45
                        'file_name' => $file->getName(),
46
                        'file_type' => $file->getType()
47
                    ];
48
                if ($file->getError()) {
49
                    $data['data'] = 'error ' . $file->getError() . ' in file ' . $file->getTempName();
50
                    $this->sendError(400, $data['data']);
51
                    Util::sysLogMsg('UploadFile', 'error ' . $file->getError() . ' in file ' . $file->getTempName());
52
                    return;
53
                }
54
            }
55
            $actionName = 'uploadResumable';
56
        } elseif ($actionName === 'status') {
57
            $row_data = $this->request->getRawBody();
58
            $data     = json_decode($row_data, true);
59
        }
60
        $this->sendRequestToBackendWorker('upload', $actionName, $data);
61
    }
62
}