rugk / threema-msgapi-sdk-php

Gateway MsgApi SDK - PHP
https://rugk.github.io/threema-msgapi-sdk-php/
Other
12 stars 5 forks source link

fetch files (after callback) => high php memory requirements #30

Closed medienverbinder closed 8 years ago

medienverbinder commented 8 years ago

Using the "E2Ehelper.php" class (threema-msgapi-sdk-php/source/Threema/MsgApi/Helpers/E2EHelper.php) I receive image and file messages via callback URL.

Sending an exemplary image (about 1 MB) through "image dialog" on my smartphone to the gateway account can be received untainted by the helper class E2EHelper.php. However, the same image needs a larger php memory allocation via the "file dialog", and often leads to the following annoying error.

PHP Fatal error:  Allowed memory size of 268435456 bytes exhausted (tried to allocate 72 bytes) in "composer/rugk/threema-msgapi-sdk-php/source/Salt/FieldElement.php" on line 66

Has anyone else problems with the reception of the messages and possibly propose a solution?

( but I do not mean an increase in the memory ... ;-) )

Best Regards

rugk commented 8 years ago

Do you probably have some small wrapper code for receiving file messages so that it can be reproduced easily?

medienverbinder commented 8 years ago

I use the official version of the Threema SDK. The structure is the same for all supported message types:

...

$e2eHelper = new E2EHelper(hex2bin($threema_privatekey), $threema_connector);

try {
    $message = $e2eHelper->receiveMessage(
      $threema_id_sender,
      $threema_message_id,
      hex2bin($threema_box),
      hex2bin($threema_nonce),
      $threema_private_folder_uri
    );

    return $message;
}
catch (Exception $exception) {
    watchdog('threema:', 'Error: %errmsg', array('%errmsg' => $exception), WATCHDOG_ERROR);
    return false;
}

...

...

if($message->isSuccess()) {

  $threema_message = $message->getThreemaMessage();

}

...

The handling of the message is processed by "public final function receiveMessage" within the "E2EHelper" class. (https://github.com/rugk/threema-msgapi-sdk-php/blob/official/source/Threema/MsgApi/Helpers/E2EHelper.php)

My initial php configuration worked with 128 MB memory size receiving an exemplary image (about 1 MB) through "image dialog".

Same image needs about > 300 MB php memory size receiving through the "file dialog".

rugk commented 8 years ago

Mind that Threema clients usually compress images before sending, but this obviously should not make the image larger. Did you already contacted Threema about this issue?

medienverbinder commented 8 years ago

I have sent a message to the Threema support. Here are the results when processing an image of approximately 1 MB:

(message instanceof ImageMessage) 225 11/Jan 21:50 notice threema Peak of memory usage: 115.75 MB.

(message instanceof FileMessage) 233 11/Jan 21:51 notice threema Peak of memory usage: 291.50 MB.

medienverbinder commented 8 years ago

With the libsodium library installed the PHP memory requirements are much lower.

Sending an exemplary image (about 1 MB) through "image dialog":

 308  16/Jan 12:33  notice    threema  Peak of memory usage: 14.25 MB.

Sending an exemplary image (about 1 MB) through "file dialog":

 317  16/Jan 12:33  notice    threema  Peak of memory usage: 16.25 MB.

For me this is now O.K.

rugk commented 8 years ago

Yeah, the php implementation is quite resource-intensive and buggy. Personally I would not mind if it would be removed completely.