11
11
use Threema \MsgApi \Connection ;
12
12
use Threema \MsgApi \Messages \FileMessage ;
13
13
use Threema \MsgApi \Messages \ImageMessage ;
14
+ use Threema \MsgApi \Messages \ThreemaMessage ;
14
15
use Threema \MsgApi \Tools \CryptTool ;
15
16
use Threema \Core \Exception ;
16
17
use Threema \MsgApi \Tools \FileAnalysisTool ;
@@ -178,6 +179,7 @@ public final function sendFileMessage($threemaId, $filePath, $thumbnailPath = nu
178
179
return $ this ->connection ->sendE2E ($ threemaId , $ nonce , $ fileMessage );
179
180
}
180
181
182
+
181
183
/**
182
184
* Encrypt a message and download the files of the message to the $outputFolder
183
185
*
@@ -186,10 +188,19 @@ public final function sendFileMessage($threemaId, $filePath, $thumbnailPath = nu
186
188
* @param string $box box as binary string
187
189
* @param string $nonce nonce as binary string
188
190
* @param string|null $outputFolder folder for storing the files
189
- * @throws \Threema\Core\Exception
191
+ * @param \Closure $downloadMessage
190
192
* @return ReceiveMessageResult
193
+ * @throws Exception
194
+ * @throws \Threema\MsgApi\Exceptions\BadMessageException
195
+ * @throws \Threema\MsgApi\Exceptions\DecryptionFailedException
196
+ * @throws \Threema\MsgApi\Exceptions\UnsupportedMessageTypeException
191
197
*/
192
- public final function receiveMessage ($ threemaId , $ messageId , $ box , $ nonce , $ outputFolder = null ) {
198
+ public final function receiveMessage ($ threemaId ,
199
+ $ messageId ,
200
+ $ box ,
201
+ $ nonce ,
202
+ $ outputFolder = null ,
203
+ \Closure $ downloadMessage = null ) {
193
204
194
205
if ($ outputFolder == null || strlen ($ outputFolder ) == 0 ) {
195
206
$ outputFolder = '. ' ;
@@ -216,51 +227,48 @@ public final function receiveMessage($threemaId, $messageId, $box, $nonce, $outp
216
227
$ receiveResult = new ReceiveMessageResult ($ messageId , $ message );
217
228
218
229
if ($ message instanceof ImageMessage) {
219
- $ result = $ this ->connection ->downloadFile ($ message ->getBlobId ());
220
- if (null === $ result || false === $ result ->isSuccess ()) {
221
- throw new Exception ('could not download the image with blob id ' .$ message ->getBlobId ());
222
- }
223
-
224
- $ image = $ this ->cryptTool ->decryptImage (
225
- $ result ->getData (),
226
- hex2bin ($ receiverPublicKey ->getPublicKey ()),
227
- $ this ->privateKey ,
228
- $ message ->getNonce ()
229
- );
230
+ $ result = $ this ->downloadFile ($ message , $ message ->getBlobId (), $ downloadMessage );
231
+ if (null !== $ result && true === $ result ->isSuccess ()) {
232
+ $ image = $ this ->cryptTool ->decryptImage (
233
+ $ result ->getData (),
234
+ hex2bin ($ receiverPublicKey ->getPublicKey ()),
235
+ $ this ->privateKey ,
236
+ $ message ->getNonce ()
237
+ );
238
+
239
+ if (null === $ image ) {
240
+ throw new Exception ('decryption of image failed ' );
241
+ }
242
+ //save file
243
+ $ filePath = $ outputFolder . '/ ' . $ messageId . '.jpg ' ;
244
+ $ f = fopen ($ filePath , 'w+ ' );
245
+ fwrite ($ f , $ image );
246
+ fclose ($ f );
230
247
231
- if (null === $ image ) {
232
- throw new Exception ('decryption of image failed ' );
248
+ $ receiveResult ->addFile ('image ' , $ filePath );
233
249
}
234
- //save file
235
- $ filePath = $ outputFolder .'/ ' .$ messageId .'.jpg ' ;
236
- $ f = fopen ($ filePath , 'w+ ' );
237
- fwrite ($ f , $ image );
238
- fclose ($ f );
239
-
240
- $ receiveResult ->addFile ('image ' , $ filePath );
241
250
}
242
251
else if ($ message instanceof FileMessage) {
243
- $ result = $ this ->connection ->downloadFile ($ message ->getBlobId ());
244
- if (null === $ result || false === $ result ->isSuccess ()) {
245
- throw new Exception ('could not download the file with blob id ' .$ message ->getBlobId ());
246
- }
252
+ $ result = $ this ->downloadFile ($ message , $ message ->getBlobId (), $ downloadMessage );
247
253
248
- $ file = $ this ->cryptTool ->decryptFile (
249
- $ result ->getData (),
250
- hex2bin ($ message ->getEncryptionKey ()));
254
+ if (null !== $ result && true === $ result ->isSuccess ()) {
255
+ $ file = $ this ->cryptTool ->decryptFile (
256
+ $ result ->getData (),
257
+ hex2bin ($ message ->getEncryptionKey ()));
251
258
252
- if (null === $ file ) {
253
- throw new Exception ('file decryption failed ' );
254
- }
259
+ if (null === $ file ) {
260
+ throw new Exception ('file decryption failed ' );
261
+ }
255
262
256
- //save file
257
- $ filePath = $ outputFolder. '/ ' . $ messageId. '- ' . $ message ->getFilename ();
258
- file_put_contents ($ filePath , $ file );
263
+ //save file
264
+ $ filePath = $ outputFolder . '/ ' . $ messageId . '- ' . $ message ->getFilename ();
265
+ file_put_contents ($ filePath , $ file );
259
266
260
- $ receiveResult ->addFile ('file ' , $ filePath );
267
+ $ receiveResult ->addFile ('file ' , $ filePath );
268
+ }
261
269
262
270
if (null !== $ message ->getThumbnailBlobId () && strlen ($ message ->getThumbnailBlobId ()) > 0 ) {
263
- $ result = $ this ->connection -> downloadFile ($ message ->getThumbnailBlobId ());
271
+ $ result = $ this ->downloadFile ($ message, $ message ->getThumbnailBlobId (), $ downloadMessage );
264
272
if (null !== $ result && true === $ result ->isSuccess ()) {
265
273
$ file = $ this ->cryptTool ->decryptFileThumbnail (
266
274
$ result ->getData (),
@@ -285,9 +293,9 @@ public final function receiveMessage($threemaId, $messageId, $box, $nonce, $outp
285
293
* Fetch a public key and check the capability of the threemaId
286
294
*
287
295
* @param string $threemaId
288
- * @param callable $capabilityCheck
296
+ * @param \Closure $capabilityCheck
289
297
* @return string Public key as binary
290
- * @throws \Threema\Core\ Exception
298
+ * @throws Exception
291
299
*/
292
300
private final function fetchPublicKeyAndCheckCapability ($ threemaId , \Closure $ capabilityCheck = null ) {
293
301
//fetch the public key
@@ -307,4 +315,25 @@ private final function fetchPublicKeyAndCheckCapability($threemaId, \Closure $ca
307
315
308
316
return hex2bin ($ receiverPublicKey ->getPublicKey ());
309
317
}
318
+
319
+ /**
320
+ * @param ThreemaMessage $message
321
+ * @param string $blobId blob id as hex
322
+ * @param \Closure|null $downloadMessage
323
+ * @return null|\Threema\MsgApi\Commands\Results\DownloadFileResult
324
+ * @throws Exception
325
+ */
326
+ private final function downloadFile (ThreemaMessage $ message , $ blobId , \Closure $ downloadMessage = null ) {
327
+ if (null === $ downloadMessage
328
+ || true === $ downloadMessage ->__invoke ($ message , $ blobId )) {
329
+ //make a download
330
+ $ result = $ this ->connection ->downloadFile ($ blobId );
331
+ if (null === $ result || false === $ result ->isSuccess ()) {
332
+ throw new Exception ('could not download the file with blob id ' .$ blobId );
333
+ }
334
+
335
+ return $ result ;
336
+ }
337
+ return null ;
338
+ }
310
339
}
0 commit comments