Completed
Push — master ( 287393...7ff50d )
by Raffael
18:27 queued 14:12
created

app/Balloon.App.Office/Api/v2/Wopi/Document.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Office\Api\v2\Wopi;
13
14
use Balloon\App\Api\Controller;
15
use Balloon\App\Office\Session;
16
use Balloon\App\Office\Session\Member;
17
use Balloon\Server;
18
use Micro\Http\Response;
19
use MongoDB\BSON\ObjectId;
20
use Psr\Log\LoggerInterface;
21
22 View Code Duplication
class Document extends Controller
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    /**
25
     * Server.
26
     *
27
     * @var Server
28
     */
29
    protected $server;
30
31
    /**
32
     * Logger.
33
     *
34
     * @var LoggerInterface
35
     */
36
    protected $logger;
37
38
    /**
39
     * Constructor.
40
     */
41
    public function __construct(Server $server, LoggerInterface $logger)
42
    {
43
        $this->server = $server;
44
        $this->logger = $logger;
45
    }
46
47
    /**
48
     * @api {get} /api/v2/office/wopi/document Get document sesssion information
49
     * @apiName get
50
     * @apiVersion 2.0.0
51
     * @apiGroup App\Office
52
     * @apiPermission none
53
     * @apiDescription Get document session information including document owner, session user and document size
54
     *
55
     * @apiParam (GET Parameter) {string} id The document id
56
     * @apiParam (GET Parameter) {string} access_token An access token to access the document
57
     *
58
     * @apiExample (cURL) example:
59
     * curl -XGET "https://SERVER/api/v2/office/wopi/document/58a18a4ca271f962af6fdbc4?access_token=aae366363ee743412abb"
60
     *
61
     * @apiSuccessExample {json} Success-Response:
62
     * HTTP/1.1 200 OK
63
     * {
64
     *      [***]
65
     * }
66
     */
67
    public function get(ObjectId $id, string $access_token): Response
68
    {
69
        $session = Member::getByAccessToken($this->server, $this->logger, $id, $access_token);
70
71
        return (new Response())->setCode(200)->setBody($session->getAttributes(), true);
72
    }
73
74
    /**
75
     * @api {post} /api/v2/office/wopi/document/contents Save document contents
76
     * @apiName postContents
77
     * @apiVersion 2.0.0
78
     * @apiGroup App\Office
79
     * @apiPermission none
80
     * @apiDescription Save document contents
81
     *
82
     * @apiParam (GET Parameter) {string} id The document id
83
     * @apiParam (GET Parameter) {string} access_token An access token to access the document
84
     *
85
     * @apiExample (cURL) example:
86
     * curl -XPOST "https://SERVER/api/v2/office/wopi/document/58a18a4ca271f962af6fdbaa/contents?access_token=aae366363ee743412abb"
87
     *
88
     * @apiSuccessExample {json} Success-Response:
89
     * HTTP/1.1 200 OK
90
     * {
91
     *      "status": 200,
92
     *      "data": true
93
     * }
94
     */
95
    public function postContents(ObjectId $id, string $access_token): Response
96
    {
97
        $session = Session::getByAccessToken($this->server, $id, $access_token);
98
        $node = $session->getDocument()->getNode();
99
        ini_set('auto_detect_line_endings', '1');
100
        $content = fopen('php://input', 'rb');
101
        $result = $node->put($content, false);
102
103
        return (new Response())->setCode(200)->setBody($result);
104
    }
105
106
    /**
107
     * @api {get} /api/v2/office/wopi/document/contents Get document contents
108
     * @apiName getContents
109
     * @apiVersion 2.0.0
110
     * @apiGroup App\Office
111
     * @apiPermission none
112
     * @apiDescription Get document contents
113
     *
114
     * @apiParam (GET Parameter) {string} id The document id
115
     * @apiParam (GET Parameter) {string} access_token An access token to access the document
116
     *
117
     * @apiExample (cURL) Exampl:
118
     * curl -XGET "https://SERVER/api/v2/office/document/58a18a4ca271f962af6fdbaa/contents?access_token=aae366363ee743412abb"
119
     *
120
     * @apiSuccessExample {binary} Success-Response:
121
     * HTTP/1.1 200 OK
122
     */
123
    public function getContents(ObjectId $id, string $access_token): void
124
    {
125
        $session = Session::getByAccessToken($this->server, $id, $access_token);
126
        $stream = $session->getDocument()->get();
127
128
        while (!feof($stream)) {
129
            echo fread($stream, 8192);
130
        }
131
132
        exit();
133
    }
134
}
135