Passed
Push — master ( e99d7d...a203c5 )
by Petr
02:39
created

Uploader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
3
namespace kalanis\UploadPerPartes\examples\controller;
4
5
6
use kalanis\UploadPerPartes;
7
8
9
/**
10
 * Class Uploader
11
 * @package kalanis\UploadPerPartes\examples\controller
12
 * What can be displayed on page for content upload
13
 * This one uses Redis as driver storage; just for fun
14
 */
15
class Uploader extends \Nette\Application\UI\Presenter //  extends \yourFavouriteFrameworkControllerClass
16
{
17
    const ENCODING_UPLOAD_PATH = '/path-to-temp';
18
19
    public function ajaxUploadPartesInit()
20
    {
21
        $lib = new UploadPerPartes\examples\Uploader(); // here temp path and init everytime with it
22
        $this->sendResponse($lib->init(
23
            static::ENCODING_UPLOAD_PATH,
24
            $this->getHttpRequest()->getPost()->__get('fileName'),
25
            $this->getHttpRequest()->getPost()->__get('fileSize')
26
        ));
27
    }
28
29
    public function ajaxUploadPartesCheck()
30
    {
31
        $lib = new UploadPerPartes\examples\Uploader();
32
        $this->sendResponse($lib->check(
33
            $this->getHttpRequest()->getPost()->__get('sharedKey'),
34
            (int)$this->getHttpRequest()->getPost()->__get('segment'))
35
        );
36
    }
37
38
    public function ajaxUploadPartesPart()
39
    {
40
        $lib = new UploadPerPartes\examples\Uploader();
41
        $this->sendResponse($lib->upload(
42
            $this->getHttpRequest()->getPost()->__get('sharedKey'),
43
            base64_decode($this->getHttpRequest()->getPost()->__get('content'))
44
        ));
45
    }
46
47
    public function ajaxUploadPartesTruncate()
48
    {
49
        $lib = new UploadPerPartes\examples\Uploader();
50
        $this->sendResponse($lib->truncateFrom(
51
            $this->getHttpRequest()->getPost()->__get('sharedKey'),
52
            $this->getHttpRequest()->getPost()->__get('segment')
53
        ));
54
    }
55
56
    public function ajaxUploadPartesCancel()
57
    {
58
        $lib = new UploadPerPartes\examples\Uploader();
59
        $this->sendResponse($lib->cancel(
60
            $this->getHttpRequest()->getPost()->__get('sharedKey')
61
        ));
62
    }
63
64
    public function ajaxUploadPartesDone()
65
    {
66
        try {
67
            $lib = new UploadPerPartes\examples\Uploader();
68
            $result = $lib->done($this->getHttpRequest()->getPost()->__get('sharedKey'));
69
70
            // check uploaded content and move it on drive
71
            $libMove = new Lib\Content\FileSave($this->getUser());
72
            $uploadedContent = $libMove->checkAndMove(
73
                $result->getTemporaryLocation(), // temp name
74
                $result->getFileName() // final name
75
            );
76
            $this->getSession()->getSection('uploader')->__set('uploadedContent', $uploadedContent->getPath()); // pass file between pages
77
            $this->sendResponse($result);
78
            // and user shall got Thanks
79
80
        } catch (Lib\Content\UploadException $ex) {
81
            $this->sendResponse(UploadPerPartes\Response\DoneResponse::initError(
82
                $this->getHttpRequest()->getPost()->__get('sharedKey'),
83
                UploadPerPartes\InfoFormat\Data::init(),
84
                $ex
85
            ));
86
        }
87
    }
88
}
89