1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2017 Facebook, Inc. |
4
|
|
|
* |
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
7
|
|
|
* form for use in connection with the web services and APIs provided by |
8
|
|
|
* Facebook. |
9
|
|
|
* |
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
namespace Facebook\FileUpload; |
24
|
|
|
|
25
|
|
|
use Facebook\Authentication\AccessToken; |
26
|
|
|
use Facebook\Exception\ResponseException; |
27
|
|
|
use Facebook\Exception\ResumableUploadException; |
28
|
|
|
use Facebook\Exception\SDKException; |
29
|
|
|
use Facebook\Application; |
30
|
|
|
use Facebook\Client; |
31
|
|
|
use Facebook\Request; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @package Facebook |
35
|
|
|
*/ |
36
|
|
|
class ResumableUploader |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var Application |
40
|
|
|
*/ |
41
|
|
|
protected $app; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $accessToken; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Client the Facebook client service |
50
|
|
|
*/ |
51
|
|
|
protected $client; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string graph version to use for this request |
55
|
|
|
*/ |
56
|
|
|
protected $graphVersion; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Application $app |
60
|
|
|
* @param Client $client |
61
|
|
|
* @param null|AccessToken|string $accessToken |
62
|
|
|
* @param string $graphVersion |
63
|
|
|
*/ |
64
|
|
|
public function __construct(Application $app, Client $client, $accessToken, $graphVersion) |
65
|
|
|
{ |
66
|
|
|
$this->app = $app; |
67
|
|
|
$this->client = $client; |
68
|
|
|
$this->accessToken = $accessToken; |
69
|
|
|
$this->graphVersion = $graphVersion; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Upload by chunks - start phase. |
74
|
|
|
* |
75
|
|
|
* @param string $endpoint |
76
|
|
|
* @param File $file |
77
|
|
|
* |
78
|
|
|
* @throws SDKException |
79
|
|
|
* |
80
|
|
|
* @return TransferChunk |
81
|
|
|
*/ |
82
|
|
|
public function start($endpoint, File $file) |
83
|
|
|
{ |
84
|
|
|
$params = [ |
85
|
|
|
'upload_phase' => 'start', |
86
|
|
|
'file_size' => $file->getSize(), |
87
|
|
|
]; |
88
|
|
|
$response = $this->sendUploadRequest($endpoint, $params); |
89
|
|
|
|
90
|
|
|
return new TransferChunk($file, $response['upload_session_id'], $response['video_id'], $response['start_offset'], $response['end_offset']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Upload by chunks - transfer phase. |
95
|
|
|
* |
96
|
|
|
* @param string $endpoint |
97
|
|
|
* @param TransferChunk $chunk |
98
|
|
|
* @param bool $allowToThrow |
99
|
|
|
* |
100
|
|
|
* @throws ResponseException |
101
|
|
|
* |
102
|
|
|
* @return TransferChunk |
103
|
|
|
*/ |
104
|
|
|
public function transfer($endpoint, TransferChunk $chunk, $allowToThrow = false) |
105
|
|
|
{ |
106
|
|
|
$params = [ |
107
|
|
|
'upload_phase' => 'transfer', |
108
|
|
|
'upload_session_id' => $chunk->getUploadSessionId(), |
109
|
|
|
'start_offset' => $chunk->getStartOffset(), |
110
|
|
|
'video_file_chunk' => $chunk->getPartialFile(), |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
try { |
114
|
|
|
$response = $this->sendUploadRequest($endpoint, $params); |
115
|
|
|
} catch (ResponseException $e) { |
116
|
|
|
$preException = $e->getPrevious(); |
117
|
|
|
if ($allowToThrow || !$preException instanceof ResumableUploadException) { |
118
|
|
|
throw $e; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Return the same chunk entity so it can be retried. |
122
|
|
|
return $chunk; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return new TransferChunk($chunk->getFile(), $chunk->getUploadSessionId(), $chunk->getVideoId(), $response['start_offset'], $response['end_offset']); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Upload by chunks - finish phase. |
130
|
|
|
* |
131
|
|
|
* @param string $endpoint |
132
|
|
|
* @param string $uploadSessionId |
133
|
|
|
* @param array $metadata the metadata associated with the file |
134
|
|
|
* |
135
|
|
|
* @throws SDKException |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function finish($endpoint, $uploadSessionId, $metadata = []) |
140
|
|
|
{ |
141
|
|
|
$params = array_merge($metadata, [ |
142
|
|
|
'upload_phase' => 'finish', |
143
|
|
|
'upload_session_id' => $uploadSessionId, |
144
|
|
|
]); |
145
|
|
|
$response = $this->sendUploadRequest($endpoint, $params); |
146
|
|
|
|
147
|
|
|
return $response['success']; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Helper to make a FacebookRequest and send it. |
152
|
|
|
* |
153
|
|
|
* @param string $endpoint the endpoint to POST to |
154
|
|
|
* @param array $params the params to send with the request |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
private function sendUploadRequest($endpoint, $params = []) |
159
|
|
|
{ |
160
|
|
|
$request = new Request($this->app, $this->accessToken, 'POST', $endpoint, $params, null, $this->graphVersion); |
161
|
|
|
|
162
|
|
|
return $this->client->sendRequest($request)->getDecodedBody(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|