|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2018 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\v1\Wopi; |
|
13
|
|
|
|
|
14
|
|
|
use Balloon\App\Api\Controller; |
|
15
|
|
|
use Balloon\App\Office\Session; |
|
16
|
|
|
use Balloon\App\Office\Session\Member; |
|
17
|
|
|
use Balloon\Exception; |
|
18
|
|
|
use Balloon\Server; |
|
19
|
|
|
use Micro\Http\Response; |
|
20
|
|
|
use MongoDB\BSON\ObjectId; |
|
21
|
|
|
use Psr\Log\LoggerInterface; |
|
22
|
|
|
|
|
23
|
|
View Code Duplication |
class Document extends Controller |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Server. |
|
27
|
|
|
* |
|
28
|
|
|
* @var Server |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $server; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Logger. |
|
34
|
|
|
* |
|
35
|
|
|
* @var LoggerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $logger; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Server $server |
|
43
|
|
|
* @param LoggerInterface $logger |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(Server $server, LoggerInterface $logger) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->server = $server; |
|
48
|
|
|
$this->logger = $logger; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @api {get} /api/v1/office/wopi/document Get document sesssion information |
|
53
|
|
|
* @apiName get |
|
54
|
|
|
* @apiVersion 1.0.0 |
|
55
|
|
|
* @apiGroup App\Office |
|
56
|
|
|
* @apiPermission none |
|
57
|
|
|
* @apiDescription Get document session information including document owner, session user and document size |
|
58
|
|
|
* |
|
59
|
|
|
* @apiParam (GET Parameter) {string} id The document id |
|
60
|
|
|
* @apiParam (GET Parameter) {string} access_token An access token to access the document |
|
61
|
|
|
* |
|
62
|
|
|
* @apiExample (cURL) example: |
|
63
|
|
|
* curl -XGET "https://SERVER/api/v1/office/wopi/document/58a18a4ca271f962af6fdbc4?access_token=aae366363ee743412abb" |
|
64
|
|
|
* |
|
65
|
|
|
* @apiSuccessExample {json} Success-Response: |
|
66
|
|
|
* HTTP/1.1 200 OK |
|
67
|
|
|
* { |
|
68
|
|
|
* [***] |
|
69
|
|
|
* } |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $id |
|
72
|
|
|
* @param string $access_token |
|
73
|
|
|
* |
|
74
|
|
|
* @return Response |
|
75
|
|
|
*/ |
|
76
|
|
|
public function get(string $id, string $access_token): Response |
|
77
|
|
|
{ |
|
78
|
|
|
$session = Member::getByAccessToken($this->server, $this->logger, $this->parseId($id), $access_token); |
|
79
|
|
|
|
|
80
|
|
|
return (new Response())->setCode(200)->setBody($session->getAttributes(), true); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @api {post} /api/v1/office/wopi/document/contents Save document contents |
|
85
|
|
|
* @apiName postContents |
|
86
|
|
|
* @apiVersion 1.0.0 |
|
87
|
|
|
* @apiGroup App\Office |
|
88
|
|
|
* @apiPermission none |
|
89
|
|
|
* @apiDescription Save document contents |
|
90
|
|
|
* |
|
91
|
|
|
* @apiParam (GET Parameter) {string} id The document id |
|
92
|
|
|
* @apiParam (GET Parameter) {string} access_token An access token to access the document |
|
93
|
|
|
* |
|
94
|
|
|
* @apiExample (cURL) example: |
|
95
|
|
|
* curl -XPOST "https://SERVER/api/v1/office/wopi/document/58a18a4ca271f962af6fdbaa/contents?access_token=aae366363ee743412abb" |
|
96
|
|
|
* |
|
97
|
|
|
* @apiSuccessExample {json} Success-Response: |
|
98
|
|
|
* HTTP/1.1 200 OK |
|
99
|
|
|
* { |
|
100
|
|
|
* "status": 200, |
|
101
|
|
|
* "data": true |
|
102
|
|
|
* } |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $id |
|
105
|
|
|
* @param string $access_token |
|
106
|
|
|
* |
|
107
|
|
|
* @return Response |
|
108
|
|
|
*/ |
|
109
|
|
|
public function postContents(string $id, string $access_token): Response |
|
110
|
|
|
{ |
|
111
|
|
|
$session = Session::getByAccessToken($this->server, $this->parseId($id), $access_token); |
|
112
|
|
|
$node = $session->getDocument()->getNode(); |
|
113
|
|
|
ini_set('auto_detect_line_endings', '1'); |
|
114
|
|
|
$content = fopen('php://input', 'rb'); |
|
115
|
|
|
$result = $node->put($content, false); |
|
116
|
|
|
|
|
117
|
|
|
return (new Response())->setCode(200)->setBody($result); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @api {get} /api/v1/office/wopi/document/contents Get document contents |
|
122
|
|
|
* @apiName getContents |
|
123
|
|
|
* @apiVersion 1.0.0 |
|
124
|
|
|
* @apiGroup App\Office |
|
125
|
|
|
* @apiPermission none |
|
126
|
|
|
* @apiDescription Get document contents |
|
127
|
|
|
* |
|
128
|
|
|
* @apiParam (GET Parameter) {string} id The document id |
|
129
|
|
|
* @apiParam (GET Parameter) {string} access_token An access token to access the document |
|
130
|
|
|
* |
|
131
|
|
|
* @apiExample (cURL) Exampl: |
|
132
|
|
|
* curl -XGET "https://SERVER/api/v1/office/document/58a18a4ca271f962af6fdbaa/contents?access_token=aae366363ee743412abb" |
|
133
|
|
|
* |
|
134
|
|
|
* @apiSuccessExample {binary} Success-Response: |
|
135
|
|
|
* HTTP/1.1 200 OK |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $id |
|
138
|
|
|
* @param string $access_token |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getContents(string $id, string $access_token): void |
|
141
|
|
|
{ |
|
142
|
|
|
$session = Session::getByAccessToken($this->server, $this->parseId($id), $access_token); |
|
143
|
|
|
$stream = $session->getDocument()->get(); |
|
144
|
|
|
|
|
145
|
|
|
while (!feof($stream)) { |
|
146
|
|
|
echo fread($stream, 8192); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
exit(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get by access token. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $id |
|
156
|
|
|
* @param string $access_token |
|
|
|
|
|
|
157
|
|
|
* |
|
158
|
|
|
* @return Session |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function parseId(string $id): ObjectId |
|
161
|
|
|
{ |
|
162
|
|
|
try { |
|
163
|
|
|
return new ObjectId($id); |
|
164
|
|
|
} catch (\Exception $e) { |
|
165
|
|
|
throw new Exception\InvalidArgument('bad session id given'); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
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.