plugins/libkolab

Aleksander Machniak machniak at kolabsys.com
Thu Jun 27 20:23:02 CEST 2013


 plugins/libkolab/lib/kolab_format_file.php    |   18 ++++++++++++---
 plugins/libkolab/lib/kolab_storage_folder.php |   30 +++++++++++++++++++++-----
 2 files changed, 39 insertions(+), 9 deletions(-)

New commits:
commit 9a7f2dd61bccb7eaa6ec88d8ebaac3aa0b3f2f88
Author: Aleksander Machniak <machniak at kolabsys.com>
Date:   Thu Jun 27 20:22:01 2013 +0200

    Support passing object attachments also as file handle

diff --git a/plugins/libkolab/lib/kolab_format_file.php b/plugins/libkolab/lib/kolab_format_file.php
index a71a794..f5b153b 100644
--- a/plugins/libkolab/lib/kolab_format_file.php
+++ b/plugins/libkolab/lib/kolab_format_file.php
@@ -66,12 +66,22 @@ class kolab_format_file extends kolab_format
 
             // make sure size is set, so object saved in cache contains this info
             if (!isset($attach_attr['size'])) {
-                if (isset($attach_attr['content'])) {
-                    $object['_attachments'][$cid]['size'] = strlen($attach_attr['content']);
+                $size = 0;
+
+                if (!empty($attach_attr['content'])) {
+                    if (is_resource($attach_attr['content'])) {
+                        $stat = fstat($attach_attr['content']);
+                        $size = $stat ? $stat['size'] : 0;
+                    }
+                    else {
+                        $size = strlen($attach_attr['content']);
+                    }
                 }
                 else if (isset($attach_attr['path'])) {
-                    $object['_attachments'][$cid]['size'] = @filesize($attach_attr['path']);
+                    $size = @filesize($attach_attr['path']);
                 }
+
+                $object['_attachments'][$cid]['size'] = $size;
             }
         }
 
@@ -81,7 +91,7 @@ class kolab_format_file extends kolab_format
     }
 
     /**
-     *
+     * Check if object's data validity
      */
     public function is_valid()
     {
diff --git a/plugins/libkolab/lib/kolab_storage_folder.php b/plugins/libkolab/lib/kolab_storage_folder.php
index 23b0629..088302c 100644
--- a/plugins/libkolab/lib/kolab_storage_folder.php
+++ b/plugins/libkolab/lib/kolab_storage_folder.php
@@ -648,7 +648,16 @@ class kolab_storage_folder
                 // make sure size is set, so object saved in cache contains this info
                 if (!isset($attachment['size'])) {
                     if (!empty($attachment['content'])) {
-                        $attachment['size'] = strlen($attachment['content']);
+                        if (is_resource($attachment['content'])) {
+                            // this need to be a seekable resource, otherwise
+                            // fstat() failes and we're unable to determine size
+                            // here nor in rcube_imap_generic before IMAP APPEND
+                            $stat = fstat($attachment['content']);
+                            $attachment['size'] = $stat ? $stat['size'] : 0;
+                        }
+                        else {
+                            $attachment['size'] = strlen($attachment['content']);
+                        }
                     }
                     else if (!empty($attachment['path'])) {
                         $attachment['size'] = filesize($attachment['path']);
@@ -970,14 +979,25 @@ class kolab_storage_folder
             $headers = array('Content-ID' => Mail_mimePart::encodeHeader('Content-ID', '<' . $key . '>', RCUBE_CHARSET, 'quoted-printable'));
             $name = !empty($att['name']) ? $att['name'] : $key;
 
+            // To store binary files we can use faster method
+            // without writting full message content to a temporary file but
+            // directly to IMAP, see rcube_imap_generic::append().
+            // I.e. use file handles where possible
             if (!empty($att['content'])) {
-                $mime->addAttachment($att['content'], $att['mimetype'], $name, false, $encoding, 'attachment', '', '', '', null, null, '', RCUBE_CHARSET, $headers);
+                if (is_resource($att['content']) && $is_file && $binary) {
+                    $files[] = $att['content'];
+                    $mime->addAttachment($marker, $att['mimetype'], $name, false, $encoding, 'attachment', '', '', '', null, null, '', RCUBE_CHARSET, $headers);
+                }
+                else {
+                    if (is_resource($att['content'])) {
+                        @rewind($att['content']);
+                        $att['content'] = stream_get_contents($att['content']);
+                    }
+                    $mime->addAttachment($att['content'], $att['mimetype'], $name, false, $encoding, 'attachment', '', '', '', null, null, '', RCUBE_CHARSET, $headers);
+                }
                 $part_id++;
             }
             else if (!empty($att['path'])) {
-                // To store binary files we can use faster method
-                // without writting full message content to a temporary file but
-                // directly to IMAP, see rcube_imap_generic::append().
                 if ($is_file && $binary) {
                     $files[] = fopen($att['path'], 'r');
                     $mime->addAttachment($marker, $att['mimetype'], $name, false, $encoding, 'attachment', '', '', '', null, null, '', RCUBE_CHARSET, $headers);





More information about the commits mailing list