|
3 | 3 | namespace splitbrain\RemarkableAPI; |
4 | 4 |
|
5 | 5 |
|
6 | | -use Psr\Http\Message\StreamInterface; |
7 | 6 | use Psr\Log\LoggerInterface; |
8 | 7 | use Psr\Log\NullLogger; |
9 | 8 | use Ramsey\Uuid\Uuid; |
@@ -198,73 +197,92 @@ public function createFolder($name, $parentID = '') |
198 | 197 | /** |
199 | 198 | * Creates a new Document Item ready to be uploaded |
200 | 199 | * |
201 | | - * @param array $item the item to create |
202 | | - * @return array info about the new item |
| 200 | + * @param string $id the new document ID |
| 201 | + * @return string the upload URL |
| 202 | + * @throws \Exception |
203 | 203 | */ |
204 | | - protected function createUploadRequest($item) |
| 204 | + protected function createUploadRequest($id) |
205 | 205 | { |
206 | | - $this->logger->info('Creating upload item'); |
207 | | - return $this->storageRequest('PUT', 'upload/request', $item); |
| 206 | + $this->logger->info('Creating upload request'); |
| 207 | + $stub = [ |
| 208 | + 'ID' => $id, |
| 209 | + 'Type' => self::TYPE_DOCUMENT, |
| 210 | + 'Version' => 1 |
| 211 | + ]; |
| 212 | + $item = $this->storageRequest('PUT', 'upload/request', $stub); |
| 213 | + |
| 214 | + if (!isset($item['BlobURLPut'])) { |
| 215 | + throw new \Exception('No BlobURLPut in upload request response'); |
| 216 | + } |
| 217 | + |
| 218 | + return $item['BlobURLPut']; |
208 | 219 | } |
209 | 220 |
|
210 | 221 | /** |
211 | | - * Upload a new document |
| 222 | + * Upload a PDF File to the remarkable |
212 | 223 | * |
213 | | - * @param string|resource|StreamInterface $body The file contents to upload |
214 | | - * @param $name |
215 | | - * @param string $parentID |
216 | | - * @return array the newly created (minimal) item information |
217 | | - * @throws \Exception |
| 224 | + * @param string $pdfBody The PDF contents |
| 225 | + * @param string $name Name to display |
| 226 | + * @param string $parentID Folder where the PDF should be stored |
218 | 227 | */ |
219 | | - public function uploadDocument($body, $name, $parentID = '') |
| 228 | + public function uploadPDF($pdfBody, $name, $parentID = '') |
220 | 229 | { |
221 | | - $stub = [ |
| 230 | + $item = [ |
222 | 231 | 'ID' => Uuid::uuid4()->toString(), |
223 | 232 | 'Parent' => $parentID, |
224 | 233 | 'VissibleName' => $name, |
225 | 234 | 'ModifiedClient' => (new \DateTime())->format('c'), |
226 | 235 | 'Type' => self::TYPE_DOCUMENT, |
227 | 236 | 'Version' => 1 |
228 | 237 | ]; |
229 | | - $item = $this->createUploadRequest($stub); |
230 | 238 |
|
231 | | - # FIXME once this works it needs refactoring |
232 | 239 | $zip = new Zip(); |
233 | 240 | $zip->create(); |
234 | | - $zip->addData($stub['ID'] . '.pdf', (string)$body); |
235 | | - $zip->addData($stub['ID'] . '.pagedata', ''); |
236 | | - $zip->addData($stub['ID'] . '.content', json_encode([ |
| 241 | + $zip->addData($item['ID'] . '.pdf', $pdfBody); |
| 242 | + $zip->addData($item['ID'] . '.pagedata', ''); |
| 243 | + $zip->addData($item['ID'] . '.content', json_encode([ |
237 | 244 | 'extraMeatadata' => [], |
238 | 245 | 'fileType' => 'pdf', |
239 | 246 | 'lastOpenedPage' => 0, |
240 | 247 | 'lineHeight' => -1, |
241 | 248 | 'margins' => 100, |
242 | | -# 'pageCount' => 1, #FIXME how to find out |
| 249 | + #'pageCount' => 1, # we don't know this, but it seems the reMarkable can count |
243 | 250 | 'textScale' => 1, |
244 | | - 'transform' => [] #FIXME wtf is this? |
| 251 | + 'transform' => [] # no idea how to fill this, but it seems optional |
245 | 252 | ], JSON_PRETTY_PRINT)); |
246 | | - $body = $zip->getArchive(); |
| 253 | + $zipBody = $zip->getArchive(); |
247 | 254 |
|
248 | | - if (!isset($item['BlobURLPut'])) { |
249 | | - print_r($item); |
250 | | - throw new \Exception('No put url'); |
251 | | - } |
| 255 | + $this->uploadDocument($item, $zipBody); |
| 256 | + } |
252 | 257 |
|
253 | | - $puturl = $item['BlobURLPut']; |
| 258 | + /** |
| 259 | + * Upload a new document |
| 260 | + * |
| 261 | + * The document has to be an enriched zip file |
| 262 | + * |
| 263 | + * @param array $item The new item to be created |
| 264 | + * @param string $zipBody The zip compressed data to upload |
| 265 | + * @return array the newly created (minimal) item information |
| 266 | + */ |
| 267 | + public function uploadDocument($item, $zipBody) |
| 268 | + { |
| 269 | + $puturl = $this->createUploadRequest($item['ID']); |
254 | 270 |
|
255 | 271 | $this->logger->info('Uploading data'); |
256 | 272 | $this->client->request('PUT', $puturl, [ |
257 | | - 'body' => $body |
| 273 | + 'body' => $zipBody |
258 | 274 | ]); |
259 | 275 |
|
260 | | - $item = $this->updateMetaData($stub); |
| 276 | + $item = $this->updateMetaData($item); |
261 | 277 |
|
262 | 278 | return $item; |
263 | 279 | } |
264 | 280 |
|
265 | 281 | /** |
266 | 282 | * Download a document |
267 | 283 | * |
| 284 | + * The document is an enriched zip file |
| 285 | + * |
268 | 286 | * @param string $id |
269 | 287 | * @return \Psr\Http\Message\ResponseInterface |
270 | 288 | */ |
|
0 commit comments