Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.

Commit 1f889e4

Browse files
committed
cleaned up the fix for #5
1 parent f043f9a commit 1f889e4

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

remarkable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ protected function cmdUpload($file, $parent, $uuid)
148148
$parent = $fs->mkdirP($parent);
149149
}
150150

151-
$name = basename($file);
152-
$this->api->uploadDocument(file_get_contents($file), $name, $parent);
151+
$name = basename($file, '.pdf');
152+
$this->api->uploadPDF(file_get_contents($file), $name, $parent);
153153
}
154154

155155
/**

src/RemarkableAPI.php

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace splitbrain\RemarkableAPI;
44

55

6-
use Psr\Http\Message\StreamInterface;
76
use Psr\Log\LoggerInterface;
87
use Psr\Log\NullLogger;
98
use Ramsey\Uuid\Uuid;
@@ -198,73 +197,92 @@ public function createFolder($name, $parentID = '')
198197
/**
199198
* Creates a new Document Item ready to be uploaded
200199
*
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
203203
*/
204-
protected function createUploadRequest($item)
204+
protected function createUploadRequest($id)
205205
{
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'];
208219
}
209220

210221
/**
211-
* Upload a new document
222+
* Upload a PDF File to the remarkable
212223
*
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
218227
*/
219-
public function uploadDocument($body, $name, $parentID = '')
228+
public function uploadPDF($pdfBody, $name, $parentID = '')
220229
{
221-
$stub = [
230+
$item = [
222231
'ID' => Uuid::uuid4()->toString(),
223232
'Parent' => $parentID,
224233
'VissibleName' => $name,
225234
'ModifiedClient' => (new \DateTime())->format('c'),
226235
'Type' => self::TYPE_DOCUMENT,
227236
'Version' => 1
228237
];
229-
$item = $this->createUploadRequest($stub);
230238

231-
# FIXME once this works it needs refactoring
232239
$zip = new Zip();
233240
$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([
237244
'extraMeatadata' => [],
238245
'fileType' => 'pdf',
239246
'lastOpenedPage' => 0,
240247
'lineHeight' => -1,
241248
'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
243250
'textScale' => 1,
244-
'transform' => [] #FIXME wtf is this?
251+
'transform' => [] # no idea how to fill this, but it seems optional
245252
], JSON_PRETTY_PRINT));
246-
$body = $zip->getArchive();
253+
$zipBody = $zip->getArchive();
247254

248-
if (!isset($item['BlobURLPut'])) {
249-
print_r($item);
250-
throw new \Exception('No put url');
251-
}
255+
$this->uploadDocument($item, $zipBody);
256+
}
252257

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']);
254270

255271
$this->logger->info('Uploading data');
256272
$this->client->request('PUT', $puturl, [
257-
'body' => $body
273+
'body' => $zipBody
258274
]);
259275

260-
$item = $this->updateMetaData($stub);
276+
$item = $this->updateMetaData($item);
261277

262278
return $item;
263279
}
264280

265281
/**
266282
* Download a document
267283
*
284+
* The document is an enriched zip file
285+
*
268286
* @param string $id
269287
* @return \Psr\Http\Message\ResponseInterface
270288
*/

0 commit comments

Comments
 (0)