Author: caefer
Date: 2010-05-18 17:50:45 +0200 (Tue, 18 May 2010)
New Revision: 29503

Modified:
   plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml
   
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceRemoteAbstract.class.php
   
plugins/sfImageTransformExtraPlugin/trunk/test/unit/lib/source/sfImageSourceHTTPTest.php
Log:

 * moved default http route upwards to avoid a conflict with the default file 
route
 * implemented seekable stream_read() for remote sources
 * added tests to ensure seeking/telling is working as expected



Modified: plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml
===================================================================
--- plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml        
2010-05-18 14:49:35 UTC (rev 29502)
+++ plugins/sfImageTransformExtraPlugin/trunk/config/routing.yml        
2010-05-18 15:50:45 UTC (rev 29503)
@@ -13,18 +13,6 @@
     image_source: Doctrine
     segment_separators: [ '/', '.', '-' ]
 
-sf_image_file:
-  class: sfImageTransformRoute
-  url:   /thumbnails/:format/:filepath.:sf_format
-  param: { module: sfImageTransformator, action: index }
-  requirements:
-    format:    '[\w_-]+'
-    filepath:  '[\w/.]+'
-    sf_format: 'gif|png|jpg'
-    sf_method: [ get ]
-  options:
-    image_source: File
-
 sf_image_http:
   class: sfImageTransformRoute
   url:   /thumbnails/site/:format/:filepath.:sf_format
@@ -39,6 +27,18 @@
   options:
     image_source: HTTP
 
+sf_image_file:
+  class: sfImageTransformRoute
+  url:   /thumbnails/:format/:filepath.:sf_format
+  param: { module: sfImageTransformator, action: index }
+  requirements:
+    format:    '[\w_-]+'
+    filepath:  '[\w/.]+'
+    sf_format: 'gif|png|jpg'
+    sf_method: [ get ]
+  options:
+    image_source: File
+
 sf_image_mock:
   class: sfImageTransformRoute
   url:   /thumbnails/:format.:sf_format

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceRemoteAbstract.class.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceRemoteAbstract.class.php
  2010-05-18 14:49:35 UTC (rev 29502)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/lib/source/sfImageSourceRemoteAbstract.class.php
  2010-05-18 15:50:45 UTC (rev 29503)
@@ -23,9 +23,78 @@
  * @package    sfImageTransformExtraPlugin
  * @subpackage source
  * @author     Christian Schaefer <[email protected]>
+ * @author     Jan Schumann <[email protected]>
  */
 abstract class sfImageSourceRemoteAbstract extends sfImageSourceLocalAbstract 
implements sfImageSourceInterface
 {
+  /**
+   * @var int $offset current file pointer position
+   */
+  protected $offset = 0;
+
+  /**
+   * @var string $content Binary contents of the remote file. Unfortunately 
this is necessary as remote streams are not seekable but getimagesize() tries 
to do that..
+   */
+  private $content = null;
+
+  protected function getSize($filename)
+  {
+    $headers = get_headers($filename, 1);
+    return $headers['Content-Length'];
+  }
+
+  /**
+   * Tests for end-of-file on a file pointer
+   *
+   * @return bool
+   */
+  public function stream_eof()
+  {
+    return $this->offset >= $this->getSize($this->filename);
+  }
+
+  /**
+   * Read from stream
+   *
+   * @param int $count
+   * @return string
+   */
+  final public function stream_read($count)
+  {
+    if(is_null($this->content))
+    {
+      $this->content = stream_get_contents($this->resource);
+    }
+
+    $chunk = substr($this->content, $this->offset, $count);
+    $this->offset += strlen($chunk);
+    return $chunk;
+  }
+
+  /**
+   * Seeks to specific location in a stream
+   *
+   * @param int  $offset
+   * @param int  $whence
+   * @return bool
+   */
+  public function stream_seek($offset, $whence = SEEK_SET)
+  {
+    switch($whence)
+    {
+      case SEEK_SET:
+        $this->offset = $offset;
+        break;
+      case SEEK_CUR:
+        $this->offset += $offset;
+        break;
+      case SEEK_END:
+        $this->offset = $this->getSize($this->filename) + $offset;
+        break;
+    }
+    return true;
+  }
+
   /** 
    * Retrieve information about a file resource
    *
@@ -40,6 +109,16 @@
     return array();
   }
 
+  /** 
+   * Retrieve the current position of a stream
+   * 
+   * @return int 
+   */ 
+  public function stream_tell()
+  {
+    return $this->offset;
+  }
+
   /**
    * Retrieve information about a file
    *
@@ -55,7 +134,8 @@
   public function url_stat($path , $flags)
   {
     return array(
-      'mode' => 0555
+      'mode' => 0555,
+      'size' => $this->getSize($this->translatePathToFilename($path))
     );
   }
 }

Modified: 
plugins/sfImageTransformExtraPlugin/trunk/test/unit/lib/source/sfImageSourceHTTPTest.php
===================================================================
--- 
plugins/sfImageTransformExtraPlugin/trunk/test/unit/lib/source/sfImageSourceHTTPTest.php
    2010-05-18 14:49:35 UTC (rev 29502)
+++ 
plugins/sfImageTransformExtraPlugin/trunk/test/unit/lib/source/sfImageSourceHTTPTest.php
    2010-05-18 15:50:45 UTC (rev 29503)
@@ -73,6 +73,22 @@
     fclose($fh);
   }
 
+  public function testStream_seek()
+  {
+    $fh = fopen($this->testSourceUri, 'r');
+    $this->assertEquals(0, fseek($fh, 1));
+    $this->assertEquals(1, ftell($fh));
+    fseek($fh, 1, SEEK_CUR);
+    $this->assertEquals(2, ftell($fh));
+    fseek($fh, 1, SEEK_SET);
+    $this->assertEquals(1, ftell($fh));
+    fseek($fh, -1, SEEK_END);
+    $this->assertEquals(filesize($this->testSourceUri)-1, ftell($fh));
+    rewind($fh);
+    $this->assertEquals(0, ftell($fh));
+    fclose($fh);
+  }
+
   public function testStream_stat()
   {
     $fh = fopen($this->testSourceUri, 'r');

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to