public function syncFile($file)
 {
     $extension = pathinfo($file, PATHINFO_EXTENSION);
     if ('php' === $extension) {
         return;
     }
     // local hash
     $md5Local = md5_file($file);
     // remote hash
     $relativePath = $this->getRelativePath($file);
     try {
         $obj = $this->container->get_object($relativePath);
         if ($obj->getETag() != $md5Local) {
             $this->log('~ ' . $relativePath);
             $obj->content_type = $this->getMimeTypeFromExtension($extension);
             $obj->load_from_filename($file);
         }
     } catch (NoSuchObjectException $e) {
         $this->log('+ ' . $relativePath);
         $obj = $this->container->create_object($relativePath);
         $obj->content_type = $this->getMimeTypeFromExtension($extension);
         $obj->load_from_filename($file);
     }
 }
Ejemplo n.º 2
0
 /**
  * Test CDN connection
  *
  * @param string $error
  * @return boolean
  */
 function test(&$error)
 {
     if (!parent::test($error)) {
         return false;
     }
     if (!$this->_init($error) || !$this->_init_container($error)) {
         return false;
     }
     $string = 'test_rscf_' . md5(time());
     try {
         $object = $this->_container->create_object($string);
         $object->content_type = 'text/plain';
         $object->write($string, strlen($string));
     } catch (Exception $exception) {
         $error = sprintf('Unable to write object (%s).', $exception->getMessage());
         return false;
     }
     try {
         $object = $this->_container->get_object($string);
         $data = $object->read();
     } catch (Exception $exception) {
         $error = sprintf('Unable to read object (%s).', $exception->getMessage());
         try {
             $this->_container->delete_object($string);
         } catch (Exception $exception) {
         }
         return false;
     }
     if ($data != $string) {
         $error = 'Objects are not equal.';
         try {
             $this->_container->delete_object($string);
         } catch (Exception $exception) {
         }
         return false;
     }
     try {
         $this->_container->delete_object($string);
     } catch (Exception $exception) {
         $error = sprintf('Unable to delete object (%s).', $exception->getMessage());
         return false;
     }
     return true;
 }
 public function commit($redirect = true)
 {
     // save to cloud
     $object_name = $this->getCloudName();
     $this->object = $this->container->create_object($object_name);
     $this->object->load_from_filename($this->getDestination());
     // clean up temp file
     unlink($this->getDestination());
     // check if crop exists
     $imageCrop = sfImagePoolCropTable::getInstance()->findCrop($this->image, $this->resizer_options['width'], $this->resizer_options['height'], !$this->resizer_options['scale'], self::CROP_IDENTIFIER);
     if (!$imageCrop) {
         // create image crop
         $imageCrop = new sfImagePoolCrop();
     }
     // add/ update details
     $imageCrop->Image = $this->image;
     $imageCrop->width = $this->resizer_options['width'];
     $imageCrop->height = $this->resizer_options['height'];
     $imageCrop->location = self::CROP_IDENTIFIER;
     $imageCrop->is_crop = !$this->resizer_options['scale'];
     // controller redirect 301 to cdn
     // If we are on a secure page we want to use the ssl option to avoid security warnings
     $ssl = sfContext::getInstance()->getRequest()->isSecure();
     $off_site_index = $ssl ? 'off_site_ssl_uri' : 'off_site_uri';
     $url = $this->options[$off_site_index] . DIRECTORY_SEPARATOR . $object_name;
     // There's a chance that save() will fail because the crop already exists
     // in the database (race condition). So, if it does fail, let's try and grab it. If it
     // failed and it doesn't exist, then re-throw the exception
     try {
         $imageCrop->save();
     } catch (Doctrine_Connection_Exception $e) {
         if ($e->getPortableCode() != Doctrine_Core::ERR_ALREADY_EXISTS) {
             throw $e;
         }
     }
     if ($redirect) {
         sfContext::getInstance()->getController()->redirect($url, 0, 301);
     } else {
         return $url;
     }
 }
Ejemplo n.º 4
0
 /**
  * ensure a subcontainer file exists and return it's object
  * @param \CF_Container $container
  * @return \CF_Object
  */
 private function getSubContainerFile($container)
 {
     try {
         return $container->get_object(self::SUBCONTAINER_FILE);
     } catch (\NoSuchObjectException $e) {
         return $container->create_object(self::SUBCONTAINER_FILE);
     }
 }
 /**
  * @param \CF_Container $container
  * @param \CF_Object $object
  */
 function it_should_create_object_when_write($container, $object)
 {
     $object->write('some content')->shouldBeCalled()->willReturn(true);
     $container->get_object('filename')->shouldBeCalled()->willReturn(false);
     $container->create_object('filename')->shouldBeCalled()->willReturn($object);
     $this->write('filename', 'some content')->shouldReturn(12);
 }
Ejemplo n.º 6
0
 /**
  * Create a new remote storage Object
  *
  * Return a new Object instance.  If the remote storage Object exists,
  * the instance's attributes are populated.
  *
  * Example:
  * <code>
  * # ... authentication code excluded (see previous examples) ...
  * #
  * $conn = new CF_Connection($auth);
  *
  * $public_container = $conn->get_container("public");
  *
  * # This creates a local instance of a storage object but only creates
  * # it in the storage system when the object's write() method is called.
  * #
  * $pic = $public_container->create_object("baby.jpg");
  * </code>
  *
  * @param string $obj_name name of storage Object
  * @return obj CF_Object instance
  */
 function create_object($obj_name = NULL)
 {
     $this->initContainer();
     return parent::create_object($obj_name);
 }