/**
  * Create a new cached image.
  *
  */
 public function __construct($source = null, $args = null)
 {
     parent::__construct(array(), false);
     $this->ID = -1;
     $this->Filename = CloudAssets::config()->missing_image;
     $this->source = $source;
     if (empty($args)) {
         $this->dimensions = '100x100';
     } else {
         switch ($args[0]) {
             case 'SetWidth':
             case 'SetHeight':
                 $this->dimensions = $args[1] . 'x' . $args[1];
                 break;
             case 'SetSize':
             case 'SetRatioSize':
             case 'ResizedImage':
             case 'CroppedImage':
             case 'PaddedImage':
                 $this->dimensions = $args[1] . 'x' . $args[2];
                 break;
             default:
                 $this->dimensions = '100x100';
         }
     }
 }
 public function setUp()
 {
     parent::setUp();
     Config::inst()->update('CloudAssets', 'disabled', false);
     Config::inst()->update('CloudAssets', 'uploads_disabled', false);
     Config::inst()->update('Director', 'alternate_protocol', 'http');
     if (!file_exists(ASSETS_PATH)) {
         mkdir(ASSETS_PATH);
     }
     /* Create a test folders for each of the fixture references */
     $folderIDs = $this->allFixtureIDs('Folder');
     foreach ($folderIDs as $folderID) {
         $folder = DataObject::get_by_id('Folder', $folderID);
         if (!file_exists(BASE_PATH . "/{$folder->Filename}")) {
             mkdir(BASE_PATH . "/{$folder->Filename}");
         }
     }
     /* Create a test files for each of the fixture references */
     $fileIDs = $this->allFixtureIDs('File');
     foreach ($fileIDs as $fileID) {
         if ($fileID == 'png') {
             continue;
         }
         $file = DataObject::get_by_id('File', $fileID);
         $fh = fopen(BASE_PATH . "/{$file->Filename}", "w");
         fwrite($fh, str_repeat('x', 1000));
         fclose($fh);
     }
     // Conditional fixture creation in case the 'cms' module is installed
     if (class_exists('ErrorPage')) {
         $page = new ErrorPage(array('Title' => 'Page not Found', 'ErrorCode' => 404));
         $page->write();
         $page->publish('Stage', 'Live');
     }
     $src = dirname(__FILE__) . '/test-png32.png';
     $dest = ASSETS_PATH . '/FileTest-folder1/test-png32.png';
     $f = copy($src, $dest);
     if (!$f) {
         die('unable to copy $src to $dest');
     }
     CloudAssets::inst()->clearBucketCache();
 }
 function setUp()
 {
     $this->bucket = CloudAssets::inst()->map('assets/Uploads/CloudAssetsS3Test.txt');
     parent::setUpOnce();
 }
 /**
  * If the file is present in the database and the cloud but not
  * locally, create a placeholder for it. This can happen in a lot
  * of cases such as load balanced servers and local development.
  */
 public function createLocalIfNeeded()
 {
     if ($this->owner->CloudStatus === 'Live') {
         $bucket = $this->getCloudBucket();
         if ($bucket && $bucket->isLocalCopyEnabled()) {
             if (!file_exists($this->owner->getFullPath()) || $this->containsPlaceholder()) {
                 try {
                     $this->downloadFromCloud();
                 } catch (Exception $e) {
                     // I'm not sure what the correct behaviour is here
                     // Pretty sure it'd be better to have a broken image
                     // link than a 500 error though.
                     CloudAssets::inst()->getLogger()->error("CloudAssets: Failed bucket download: " . $e->getMessage() . " for " . $this->owner->getFullPath());
                 }
             }
         } else {
             if (!file_exists($this->owner->getFullPath())) {
                 $this->convertToPlaceholder();
             }
         }
     }
 }
 /**
  * Return an image object representing the image in the given format.
  * This image will be generated using generateFormattedImage().
  * The generated image is cached, to flush the cache append ?flush=1 to your URL.
  *
  * Just pass the correct number of parameters expected by the working function
  *
  * @param string $format The name of the format.
  * @return CloudImageCached|null
  */
 public function getFormattedImage($format)
 {
     $args = func_get_args();
     $logger = CloudAssets::inst()->getLogger();
     if ($this->ID && $this->Filename && Director::fileExists($this->Filename)) {
         $cacheFile = call_user_func_array(array($this, "cacheFilename"), $args);
         $cachePath = Director::baseFolder() . "/" . $cacheFile;
         $stored = CloudImageCachedStore::get()->filter('Filename', $cacheFile)->first();
         if ($stored && !$stored->exists()) {
             $stored = null;
         }
         // If ?flush is present, always wipe existing data and start clean
         if (isset($_GET['flush'])) {
             // There was a bug here caused by the fact that GDBackend tries
             // to read the size off the cached image, which would be a placeholder
             // in certain cases.
             // I'm not 100% sure what the correct behaviour is here. For now
             // we'll destroy the existing image, causing it to be re-uploaded
             // every time. That seems safer if a little bit wasteful.
             $logger->debug("CloudAssets: deleting cached image because of flush: {$cachePath}");
             if (file_exists($cachePath)) {
                 unlink($cachePath);
             }
             // delete the existing meta if it existed
             if ($stored) {
                 $stored->delete();
                 $stored = null;
             }
         }
         // start building out the record
         $cached = new CloudImageCached($cacheFile);
         $cached->Title = $this->Title;
         $cached->ParentID = $this->ParentID;
         // Is there a meta record for this formatted file?
         if ($stored) {
             // Has it been successfully uploaded to the cloud?
             // If so, we can just send this puppy on
             // If not, is there a local file that's present and correct?
             // If not, we need to wipe the meta and anything local and regenerate
             if ($stored->CloudStatus !== 'Live' && $cached->isLocalMissing()) {
                 $stored->delete();
                 $stored = null;
                 if (file_exists($cachePath)) {
                     unlink($cachePath);
                 }
             } else {
                 $cached->setStoreRecord($stored);
             }
         }
         // If there is no meta record (or an invalid one), is there a local file or placeholder?
         if (!$stored) {
             // if the local exists as a placeholder, we need to check if the cloud version is valid
             if (file_exists($cachePath) && $cached->containsPlaceholder()) {
                 try {
                     $cached->downloadFromCloud();
                 } catch (Exception $e) {
                     // We want to fail silently here if there is any trouble
                     // because we can always regenerate the thumbnail
                     if (CloudAssets::config()->missing_image) {
                         return new CloudImageMissing($this, $args);
                     }
                 }
             }
             // If we don't have a valid local version at this point...
             if ($cached->isLocalMissing()) {
                 // delete whatever might have been there
                 if (file_exists($cachePath)) {
                     unlink($cachePath);
                 }
                 $logger->debug("CloudAssets: generating formatted image at {$cachePath}");
                 // Regenerate the formatted image
                 if ($this->CloudStatus === 'Live' && $this->isLocalMissing()) {
                     try {
                         $this->downloadFromCloud();
                     } catch (Exception $e) {
                         if (CloudAssets::config()->missing_image) {
                             return new CloudImageMissing($this, $args);
                         }
                     }
                     call_user_func_array(array($this, "generateFormattedImage"), $args);
                     $this->convertToPlaceholder();
                 } else {
                     call_user_func_array(array($this, "generateFormattedImage"), $args);
                 }
             }
             // If we now have a valid image, generate a stored meta record for it
             if (file_exists($cachePath)) {
                 $stored = new CloudImageCachedStore();
                 $stored->Filename = $cacheFile;
                 $stored->SourceID = $this->ID;
                 $stored->write();
                 // all the other fields will get set when the cloud status is updated
                 $cached->setStoreRecord($stored);
             }
         }
         // upload to cloud if needed
         $cached->updateCloudStatus();
         return $cached;
     }
 }